changeset 133:2c2e4c639311

Refactored viewport scaling code into separate function.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Apr 2012 18:49:06 +0400
parents 77a3a609b2fe
children 16643a84b9e4
files src/net/kryshen/indyvon/layers.clj
diffstat 1 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/layers.clj	Tue Apr 24 18:47:31 2012 +0400
+++ b/src/net/kryshen/indyvon/layers.clj	Tue Apr 24 18:49:06 2012 +0400
@@ -130,7 +130,9 @@
   ([content]
      (panel content 0))
   ([content gap]
-     (let [layer (padding content gap)]
+     (panel content gap gap gap gap))
+  ([content top left bottom right]
+     (let [layer (padding content top left bottom right)]
        (decorate-layer layer [_]
          (with-color :alt-back-color
            (.fill *graphics* (Rectangle2D$Double. 0.0 0.0 *width* *height*)))
@@ -304,6 +306,8 @@
 (def ^:dynamic *viewport-min-scale* 1E-6)
 (def ^:dynamic *viewport-max-scale* 1E6)
 
+(declare scale-viewport)
+
 (defrecord Viewport [content h-align v-align
                      ;; State (refs)
                      transform
@@ -352,21 +356,10 @@
         (ref-set fix-y (:y-on-screen e)))
        (update layer))
       (:mouse-wheel e
-       (dosync
-        (let [s (Math/pow *viewport-scaling-step* (:wheel-rotation e))
-              x (- (:x e) (* (:x e) s))
-              y (- (:y e) (* (:y e) s))
-              scaled (doto (AffineTransform/getTranslateInstance x y)
-                       (.scale s s)
-                       (.concatenate @transform))
-              sx (.getScaleX scaled)
-              sy (.getScaleY scaled)]
-          (if (<= *viewport-min-scale*
-                  (min sx sy)
-                  (max sx sy)
-                  *viewport-max-scale*)
-            (ref-set transform scaled))))
-       (update layer))))
+       (scale-viewport
+        layer
+        (Math/pow *viewport-scaling-step* (:wheel-rotation e))
+        true (:x e) (:y e)))))
   (geometry [_]
     (geometry content)))
 
@@ -381,6 +374,32 @@
                 (ref 0) (ref 0)          ; last-width last-height
                 (ref 0) (ref 0))))       ; last-anchor-x last-anchor-y
 
+(defn scale-viewport
+  ([viewport s]
+     (scale-viewport viewport s true))
+  ([viewport s relative?]
+     (scale-viewport viewport s relative? nil nil))
+  ([viewport s relative? x y]
+     (dosync
+      (let [^AffineTransform tr @(:transform viewport)
+            sx (if relative? s (/ s (.getScaleX tr)))
+            sy (if relative? s (/ s (.getScaleY tr)))
+            x (or x (align-x 0 @(:last-width viewport) (:h-align viewport)))
+            y (or y (align-y 0 @(:last-height viewport) (:v-align viewport)))
+            x (- x (* x sx))
+            y (- y (* y sy))
+            scaled (doto (AffineTransform/getTranslateInstance x y)
+                     (.scale sx sy)
+                     (.concatenate tr))
+            sx (.getScaleX scaled)
+            sy (.getScaleY scaled)]
+        (if (<= *viewport-min-scale*
+                (min sx sy)
+                (max sx sy)
+                *viewport-max-scale*)
+          (ref-set (:transform viewport) scaled))))
+     (update viewport)))
+
 (defn reset-viewport [viewport]
   (dosync
    (ref-set (:last-width viewport) 0)