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 diff
     1.1 --- a/src/net/kryshen/indyvon/core.clj	Thu Apr 25 02:40:24 2013 +0400
     1.2 +++ b/src/net/kryshen/indyvon/core.clj	Thu Apr 25 02:41:50 2013 +0400
     1.3 @@ -1,5 +1,5 @@
     1.4  ;;
     1.5 -;; Copyright 2010, 2011, 2012 Mikhail Kryshen <mikhail@kryshen.net>
     1.6 +;; Copyright 2010, 2011, 2012, 2013 Mikhail Kryshen <mikhail@kryshen.net>
     1.7  ;;
     1.8  ;; This file is part of Indyvon.
     1.9  ;;
    1.10 @@ -112,6 +112,25 @@
    1.11      vertical alignment and height, v-align could be :top, :center
    1.12      or :bottom."))
    1.13  
    1.14 +(defn- emit-align-xy [align size first center last]
    1.15 +  `(case ~align
    1.16 +         ~first 0
    1.17 +         ~center (/ ~size 2)
    1.18 +         ~last ~size))
    1.19 +
    1.20 +;; Define as macro to avoid unnecessary calculation of width or height.
    1.21 +(defmacro align-x
    1.22 +  ([align inner outer]
    1.23 +     `(align-x ~align (- ~outer ~inner)))
    1.24 +  ([align width]
    1.25 +     (emit-align-xy align width :left :center :right)))
    1.26 +
    1.27 +(defmacro align-y
    1.28 +  ([align inner outer]
    1.29 +     `(align-y ~align (- ~outer ~inner)))
    1.30 +  ([align height]
    1.31 +     (emit-align-xy align height :top :center :bottom)))
    1.32 +
    1.33  (defrecord Size [width height]
    1.34    Geometry
    1.35    (width  [_] width)
    1.36 @@ -119,15 +138,9 @@
    1.37    (height [_] height)
    1.38    (height [_ _] height)
    1.39    (anchor-x [_ h-align width]
    1.40 -    (case h-align
    1.41 -      :left 0
    1.42 -      :center (/ width 2)
    1.43 -      :right width))
    1.44 +    (align-x h-align width))
    1.45    (anchor-y [_ v-align height]
    1.46 -    (case v-align
    1.47 -      :top 0
    1.48 -      :center (/ height 2)
    1.49 -      :bottom height)))
    1.50 +    (align-y v-align height)))
    1.51  
    1.52  (defrecord FixedGeometry [ax ay width height]
    1.53    Geometry
     2.1 --- a/src/net/kryshen/indyvon/layers.clj	Thu Apr 25 02:40:24 2013 +0400
     2.2 +++ b/src/net/kryshen/indyvon/layers.clj	Thu Apr 25 02:41:50 2013 +0400
     2.3 @@ -29,24 +29,6 @@
     2.4     (java.awt.font FontRenderContext TextLayout)
     2.5     java.util.concurrent.TimeUnit
     2.6     (com.google.common.cache Cache CacheBuilder CacheLoader)))
     2.7 -  
     2.8 -;; Define as macro to avoid unnecessary calculation of inner and outer
     2.9 -;; sizes in the first case.
    2.10 -;; 
    2.11 -;; TODO: replace inner and outer with size; (- outer inner) could be
    2.12 -;; passed instead where needed. Move to core.
    2.13 -;;
    2.14 -(defmacro align-xy [inner outer align first center last]
    2.15 -  `(case ~align
    2.16 -         ~first 0
    2.17 -         ~center (/ (- ~outer ~inner) 2)
    2.18 -         ~last (- ~outer ~inner)))
    2.19 -
    2.20 -(defmacro align-x [inner outer align]
    2.21 -  `(align-xy ~inner ~outer ~align :left :center :right))
    2.22 -
    2.23 -(defmacro align-y [inner outer align]
    2.24 -  `(align-xy ~inner ~outer ~align :top :center :bottom))
    2.25  
    2.26  (defmacro decorate-layer
    2.27    "Decorate Layer replacing render! implementation."
    2.28 @@ -217,12 +199,12 @@
    2.29                  h *height*
    2.30                  font (.getFont *graphics*)
    2.31                  layouts (layout-text lines font (font-context))
    2.32 -                y (align-y (text-height layouts) h v-align)]
    2.33 +                y (align-y v-align (text-height layouts) h)]
    2.34              (loop [layouts layouts, y y]
    2.35                (when-first [^TextLayout layout layouts]
    2.36                  (let [ascent (.getAscent layout)
    2.37                        lh (+ ascent (.getDescent layout) (.getLeading layout))
    2.38 -                      x (align-x (.getAdvance layout) w h-align)]
    2.39 +                      x (align-x h-align (.getAdvance layout) w)]
    2.40                    (.draw layout *graphics* x (+ y ascent))
    2.41                    (recur (next layouts) (+ y lh)))))))
    2.42          (geometry [layer]
     3.1 --- a/src/net/kryshen/indyvon/viewport.clj	Thu Apr 25 02:40:24 2013 +0400
     3.2 +++ b/src/net/kryshen/indyvon/viewport.clj	Thu Apr 25 02:41:50 2013 +0400
     3.3 @@ -1,5 +1,5 @@
     3.4  ;;
     3.5 -;; Copyright 2010, 2011, 2012 Mikhail Kryshen <mikhail@kryshen.net>
     3.6 +;; Copyright 2010, 2011, 2012, 2013 Mikhail Kryshen <mikhail@kryshen.net>
     3.7  ;;
     3.8  ;; This file is part of Indyvon.
     3.9  ;;
    3.10 @@ -60,8 +60,8 @@
    3.11          ch (height content-geom)
    3.12          ax (anchor-x content-geom h-align cw)
    3.13          ay (anchor-y content-geom v-align ch)
    3.14 -        ax1 (align-x (:last-width state) w h-align)
    3.15 -        ay1 (align-y (:last-height state) h v-align)
    3.16 +        ax1 (align-x h-align (:last-width state) w)
    3.17 +        ay1 (align-y v-align (:last-height state) h)
    3.18          ax2 (- (:last-anchor-x state) ax)
    3.19          ay2 (- (:last-anchor-y state) ay)
    3.20          transform (:transform state)
    3.21 @@ -136,8 +136,8 @@
    3.22    (let [^AffineTransform tr (:transform state)
    3.23          sx (if relative? s (/ s (.getScaleX tr)))
    3.24          sy (if relative? s (/ s (.getScaleY tr)))
    3.25 -        x (or x (align-x 0 (:last-width state) (:h-align vp)))
    3.26 -        y (or y (align-y 0 (:last-height state) (:v-align vp)))
    3.27 +        x (or x (align-x (:h-align vp) (:last-width state)))
    3.28 +        y (or y (align-y (:v-align vp) (:last-height state)))
    3.29          x (- x (* x sx))
    3.30          y (- y (* y sy))
    3.31          scaled (doto (AffineTransform/getTranslateInstance x y)
    3.32 @@ -188,8 +188,8 @@
    3.33              s (scaling cw ch mw mh)]
    3.34          (.scale *graphics* s s)
    3.35          (draw! content
    3.36 -               (align-x cw (/ mw s) :center)
    3.37 -               (align-y ch (/ mh s) :center)
    3.38 +               (align-x :center cw (/ mw s))
    3.39 +               (align-y :center ch (/ mh s))
    3.40                 cw ch)))
    3.41      (geometry [_]
    3.42        (->Size mw mh)))
    3.43 @@ -205,8 +205,8 @@
    3.44              s (scaling (width geom) (height geom) m-width m-height)
    3.45              vp-state @(:state viewport)
    3.46              {:keys [transform last-width last-height]} @(:state viewport)
    3.47 -            ox (align-x (width geom) (/ m-width s) :center)
    3.48 -            oy (align-y (height geom) (/ m-height s) :center)
    3.49 +            ox (align-x :center (width geom) (/ m-width s))
    3.50 +            oy (align-y :center (height geom) (/ m-height s))
    3.51              inverse (.createInverse ^AffineTransform transform)
    3.52              transform (doto (AffineTransform.)
    3.53                          (.scale s s)