changeset 90:dd7b8dbb20bc

Viewport miniature: preserve aspect ratio; draw visible area highlight synchronously.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 28 Nov 2010 03:46:59 +0300
parents 54f6e6d196c3
children f87363889015
files src/net/kryshen/indyvon/async.clj src/net/kryshen/indyvon/component.clj src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/layers.clj
diffstat 4 files changed, 46 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/async.clj	Mon Nov 22 16:02:27 2010 +0300
+++ b/src/net/kryshen/indyvon/async.clj	Sun Nov 28 03:46:59 2010 +0300
@@ -23,7 +23,7 @@
    net.kryshen.indyvon.core)
   (:import
    (net.kryshen.indyvon.core Size Location)
-   (java.awt Image)
+   (java.awt Image AlphaComposite)
    (java.awt.image BufferedImage)
    (java.util.concurrent ThreadFactory ThreadPoolExecutor
                          ThreadPoolExecutor$DiscardOldestPolicy
@@ -111,13 +111,18 @@
 (defn- draw-offscreen [async-layer]
   ;;(Thread/sleep 1000)
   (with-buffer async-layer :back [b]
-    (draw-root! (:content async-layer)
-                (.getGraphics ^Image (:image b))
-                (:width async-layer)
-                (:height async-layer)
-                ;; TODO: use operational event dispatcher.
-                dummy-event-dispatcher))
-  (update async-layer))
+    (let [g (.getGraphics ^Image (:image b))]
+      ;; Clear the buffer.
+      (.setComposite g AlphaComposite/Clear)
+      (.fillRect g 0 0 (:width async-layer) (:height async-layer))
+      (.setComposite g AlphaComposite/Src)
+      (draw-root! (:content async-layer)
+                  g
+                  (:width async-layer)
+                  (:height async-layer)
+                  ;; TODO: use operational event dispatcher.
+                  dummy-event-dispatcher))
+    (update async-layer)))
 
 (defn- draw-offscreen-async [async-layer]
   (.execute ^ThreadPoolExecutor (:executor async-layer)
--- a/src/net/kryshen/indyvon/component.clj	Mon Nov 22 16:02:27 2010 +0300
+++ b/src/net/kryshen/indyvon/component.clj	Sun Nov 28 03:46:59 2010 +0300
@@ -38,6 +38,8 @@
            (proxy [JPanel] []
              (paintComponent [g]
                (let [size (.getSize ^Component this)]
+                 (.setColor g (:back-color *theme*))
+                 (.fillRect g 0 0 (.width size) (.height size))
                  (draw-root! layer g (.width size) (.height size)
                              event-dispatcher this)))
              (getPreferredSize []
--- a/src/net/kryshen/indyvon/core.clj	Mon Nov 22 16:02:27 2010 +0300
+++ b/src/net/kryshen/indyvon/core.clj	Sun Nov 28 03:46:59 2010 +0300
@@ -369,8 +369,6 @@
        ;;                    RenderingHints/KEY_ANTIALIASING
        ;;                    RenderingHints/VALUE_ANTIALIAS_ON)
        (apply-theme)
-       (with-color (:back-color *theme*)
-         (.fillRect graphics 0 0 width height))
        (let [tmp-watcher (Object.)]
          ;; Keep current context observers until the rendering is
          ;; complete. Some observers may be invoked twice if they
--- a/src/net/kryshen/indyvon/layers.clj	Mon Nov 22 16:02:27 2010 +0300
+++ b/src/net/kryshen/indyvon/layers.clj	Sun Nov 28 03:46:59 2010 +0300
@@ -204,6 +204,11 @@
 
 (def *miniature-thread-priority* 2)
 
+(defn- scaling
+  [width height max-width max-height]
+  (min (/ max-width width)
+       (/ max-height height)))
+  
 (defn miniature
   "Creates layer that asynchronously renders view of the content
   scaled to the specified size."
@@ -213,12 +218,17 @@
     Layer
     (render! [this]
       (let [size (layer-size content)
-            sx (/ width (:width size))
-            sy (/ height (:height size))]
-        (.scale *graphics* sx sy)
-        (draw! content 0 0 (:width size) (:height size))))
+            s (scaling (:width size) (:height size) width height)]
+        (.scale *graphics* s s)
+        (draw! content
+               (align-x (:width size) (/ width s) :center)
+               (align-y (:height size) (/ height s) :center)
+               (:width size) (:height size))))
     (layer-size [this]
       (Size. width height)))
+      ;; (let [size (layer-size content)
+      ;;       s (scaling (:width size) (:height size) width height)]  
+      ;; (Size. (* (:width size) s) (* (:height size) s)))))
    width height *miniature-thread-priority*))
 
 (defrecord Viewport [content h-align v-align
@@ -278,18 +288,23 @@
 (defn viewport-miniature
   "Creates miniature view of the viewport's contents."
   [viewport width height]
-  (miniature
-   (decorate-layer (:content viewport) [_]
-     (repaint-on-update viewport)
-     (let [[x y w h] (viewport-visible-bounds viewport)]
-       (with-color :alt-back-color
-         (.fillRect *graphics* 0 0 *width* *height*))
-       (with-color :back-color
-         (.fillRect *graphics* x y w h))
-       (draw! (:content viewport))
-       (with-color :border-color
-         (.drawRect *graphics* x y w h))))
-   width height))
+  (let [miniature (miniature (:content viewport) width height)]
+    (decorate-layer miniature [_]
+      ;;(repaint-on-update viewport)
+      (let [size (layer-size (:content viewport))
+            s (scaling (:width size) (:height size) width height)
+            [x y w h] (viewport-visible-bounds viewport)
+            x (* x s)
+            y (* y s)
+            w (* w s)
+            h (* h s)]
+        (with-color :alt-back-color
+          (.fillRect *graphics* 0 0 *width* *height*))
+        (with-color :back-color
+          (.fillRect *graphics* x y w h))
+        (draw! miniature)
+        (with-color :border-color
+          (.drawRect *graphics* x y w h))))))
 
 ;;
 ;; Layer context decorators.