Mercurial > hg > indyvon
changeset 168:5b80af180da0
Function to apply AffineTransform to a View.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Sun, 30 Nov 2014 18:45:12 +0300 |
parents | a51fcaf00264 |
children | 8471a45645ad |
files | src/indyvon/core.clj src/indyvon/views.clj |
diffstat | 2 files changed, 40 insertions(+), 6 deletions(-) [+] |
line diff
1.1 --- a/src/indyvon/core.clj Sat Nov 29 19:44:53 2014 +0300 1.2 +++ b/src/indyvon/core.clj Sun Nov 30 18:45:12 2014 +0300 1.3 @@ -550,10 +550,11 @@ 1.4 (with-hints hints 1.5 (apply f args))) 1.6 1.7 -;; TODO: constructor for AffineTransform. 1.8 -;; (transform :scale 0.3 0.5 1.9 -;; :translate 5 10 1.10 -;; :rotate (/ Math/PI 2)) 1.11 +;; TODO: Composable transformations: 1.12 +;; (with-transform (-> (scale 0.3 0.5) 1.13 +;; (rotate-deg 30) 1.14 +;; (translate 5 10)) 1.15 +;; ...) 1.16 1.17 (defmacro with-transform [transform & body] 1.18 `(let [g# *graphics* 1.19 @@ -564,6 +565,10 @@ 1.20 (finally 1.21 (.setTransform g# old-t#))))) 1.22 1.23 +(defn with-transform* [transform f & args] 1.24 + (with-transform transform 1.25 + (apply f args))) 1.26 + 1.27 (defmacro with-rotate [theta ax ay & body] 1.28 `(let [transform# (AffineTransform/getRotateInstance ~theta ~ax ~ay)] 1.29 (with-transform transform# ~@body)))
2.1 --- a/src/indyvon/views.clj Sat Nov 29 19:44:53 2014 +0300 2.2 +++ b/src/indyvon/views.clj Sun Nov 30 18:45:12 2014 +0300 2.3 @@ -24,8 +24,8 @@ 2.4 (:import 2.5 (java.awt Font Image Toolkit) 2.6 java.awt.image.ImageObserver 2.7 - (java.awt.geom Area AffineTransform Rectangle2D$Double Point2D 2.8 - Point2D$Double) 2.9 + (java.awt.geom Area AffineTransform Rectangle2D Rectangle2D$Double 2.10 + Point2D Point2D$Double) 2.11 (java.awt.font FontRenderContext TextLayout) 2.12 java.util.concurrent.TimeUnit 2.13 (com.google.common.cache Cache CacheBuilder CacheLoader))) 2.14 @@ -344,6 +344,35 @@ 2.15 (with-hints* hints geometry view) 2.16 (geometry view))))) 2.17 2.18 +(defn- transform-bounds 2.19 + ^Rectangle2D [^AffineTransform tr ^double w ^double h] 2.20 + (.getBounds2D 2.21 + (.createTransformedShape tr (Rectangle2D$Double. 0 0 w h)))) 2.22 + 2.23 +(defn transform [tr view] 2.24 + (reify View 2.25 + (render! [v] 2.26 + (let [g (geometry view) 2.27 + w (double (width g)) 2.28 + h (double (height g)) 2.29 + ^Rectangle2D bounds (transform-bounds tr w h) 2.30 + g *graphics*] 2.31 + (.translate g (- (.getX bounds)) (- (.getY bounds))) 2.32 + (.transform g tr) 2.33 + ;; TODO: scale w and h to fit *width* and *height*. 2.34 + (draw! view 0 0 w h))) 2.35 + (geometry [_] 2.36 + (let [g (geometry view) 2.37 + w (double (width g)) 2.38 + h (double (height g)) 2.39 + ^Rectangle2D bounds (transform-bounds tr w h)] 2.40 + (->Size (.getWidth bounds) (.getHeight bounds)))))) 2.41 + 2.42 +(defn rotate [^double degrees view] 2.43 + (transform 2.44 + (AffineTransform/getRotateInstance (Math/toRadians degrees)) 2.45 + view)) 2.46 + 2.47 ;; 2.48 ;; Measuring time 2.49 ;;