Mercurial > hg > indyvon
changeset 146:dc437b4ceeea
Refactored align-x and align-y macros.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Thu, 25 Apr 2013 02:41:50 +0400 |
parents | d5e3e3936f34 |
children | 7c982d2ee9f3 |
files | src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/layers.clj src/net/kryshen/indyvon/viewport.clj |
diffstat | 3 files changed, 33 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/core.clj Thu Apr 25 02:40:24 2013 +0400 +++ b/src/net/kryshen/indyvon/core.clj Thu Apr 25 02:41:50 2013 +0400 @@ -1,5 +1,5 @@ ;; -;; Copyright 2010, 2011, 2012 Mikhail Kryshen <mikhail@kryshen.net> +;; Copyright 2010, 2011, 2012, 2013 Mikhail Kryshen <mikhail@kryshen.net> ;; ;; This file is part of Indyvon. ;; @@ -112,6 +112,25 @@ vertical alignment and height, v-align could be :top, :center or :bottom.")) +(defn- emit-align-xy [align size first center last] + `(case ~align + ~first 0 + ~center (/ ~size 2) + ~last ~size)) + +;; Define as macro to avoid unnecessary calculation of width or height. +(defmacro align-x + ([align inner outer] + `(align-x ~align (- ~outer ~inner))) + ([align width] + (emit-align-xy align width :left :center :right))) + +(defmacro align-y + ([align inner outer] + `(align-y ~align (- ~outer ~inner))) + ([align height] + (emit-align-xy align height :top :center :bottom))) + (defrecord Size [width height] Geometry (width [_] width) @@ -119,15 +138,9 @@ (height [_] height) (height [_ _] height) (anchor-x [_ h-align width] - (case h-align - :left 0 - :center (/ width 2) - :right width)) + (align-x h-align width)) (anchor-y [_ v-align height] - (case v-align - :top 0 - :center (/ height 2) - :bottom height))) + (align-y v-align height))) (defrecord FixedGeometry [ax ay width height] Geometry
--- a/src/net/kryshen/indyvon/layers.clj Thu Apr 25 02:40:24 2013 +0400 +++ b/src/net/kryshen/indyvon/layers.clj Thu Apr 25 02:41:50 2013 +0400 @@ -29,24 +29,6 @@ (java.awt.font FontRenderContext TextLayout) java.util.concurrent.TimeUnit (com.google.common.cache Cache CacheBuilder CacheLoader))) - -;; Define as macro to avoid unnecessary calculation of inner and outer -;; sizes in the first case. -;; -;; TODO: replace inner and outer with size; (- outer inner) could be -;; passed instead where needed. Move to core. -;; -(defmacro align-xy [inner outer align first center last] - `(case ~align - ~first 0 - ~center (/ (- ~outer ~inner) 2) - ~last (- ~outer ~inner))) - -(defmacro align-x [inner outer align] - `(align-xy ~inner ~outer ~align :left :center :right)) - -(defmacro align-y [inner outer align] - `(align-xy ~inner ~outer ~align :top :center :bottom)) (defmacro decorate-layer "Decorate Layer replacing render! implementation." @@ -217,12 +199,12 @@ h *height* font (.getFont *graphics*) layouts (layout-text lines font (font-context)) - y (align-y (text-height layouts) h v-align)] + y (align-y v-align (text-height layouts) h)] (loop [layouts layouts, y y] (when-first [^TextLayout layout layouts] (let [ascent (.getAscent layout) lh (+ ascent (.getDescent layout) (.getLeading layout)) - x (align-x (.getAdvance layout) w h-align)] + x (align-x h-align (.getAdvance layout) w)] (.draw layout *graphics* x (+ y ascent)) (recur (next layouts) (+ y lh))))))) (geometry [layer]
--- a/src/net/kryshen/indyvon/viewport.clj Thu Apr 25 02:40:24 2013 +0400 +++ b/src/net/kryshen/indyvon/viewport.clj Thu Apr 25 02:41:50 2013 +0400 @@ -1,5 +1,5 @@ ;; -;; Copyright 2010, 2011, 2012 Mikhail Kryshen <mikhail@kryshen.net> +;; Copyright 2010, 2011, 2012, 2013 Mikhail Kryshen <mikhail@kryshen.net> ;; ;; This file is part of Indyvon. ;; @@ -60,8 +60,8 @@ ch (height content-geom) ax (anchor-x content-geom h-align cw) ay (anchor-y content-geom v-align ch) - ax1 (align-x (:last-width state) w h-align) - ay1 (align-y (:last-height state) h v-align) + ax1 (align-x h-align (:last-width state) w) + ay1 (align-y v-align (:last-height state) h) ax2 (- (:last-anchor-x state) ax) ay2 (- (:last-anchor-y state) ay) transform (:transform state) @@ -136,8 +136,8 @@ (let [^AffineTransform tr (:transform state) sx (if relative? s (/ s (.getScaleX tr))) sy (if relative? s (/ s (.getScaleY tr))) - x (or x (align-x 0 (:last-width state) (:h-align vp))) - y (or y (align-y 0 (:last-height state) (:v-align vp))) + x (or x (align-x (:h-align vp) (:last-width state))) + y (or y (align-y (:v-align vp) (:last-height state))) x (- x (* x sx)) y (- y (* y sy)) scaled (doto (AffineTransform/getTranslateInstance x y) @@ -188,8 +188,8 @@ s (scaling cw ch mw mh)] (.scale *graphics* s s) (draw! content - (align-x cw (/ mw s) :center) - (align-y ch (/ mh s) :center) + (align-x :center cw (/ mw s)) + (align-y :center ch (/ mh s)) cw ch))) (geometry [_] (->Size mw mh))) @@ -205,8 +205,8 @@ s (scaling (width geom) (height geom) m-width m-height) vp-state @(:state viewport) {:keys [transform last-width last-height]} @(:state viewport) - ox (align-x (width geom) (/ m-width s) :center) - oy (align-y (height geom) (/ m-height s) :center) + ox (align-x :center (width geom) (/ m-width s)) + oy (align-y :center (height geom) (/ m-height s)) inverse (.createInverse ^AffineTransform transform) transform (doto (AffineTransform.) (.scale s s)