changeset 71:59e1810c0278

Moved fps code from demo to layers. Moved frame creation code from demo to component.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 29 Aug 2010 20:38:22 +0400
parents b2f6c78413d3
children 76f4126aba84
files src/net/kryshen/indyvon/component.clj src/net/kryshen/indyvon/demo.clj src/net/kryshen/indyvon/layers.clj
diffstat 3 files changed, 75 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/component.clj	Sun Aug 29 18:33:41 2010 +0400
+++ b/src/net/kryshen/indyvon/component.clj	Sun Aug 29 20:38:22 2010 +0400
@@ -34,3 +34,8 @@
        (add-observer panel layer (fn [_] (.repaint panel)))
        (listen! event-dispatcher panel)
        panel)))
+
+(defn make-jframe [title layer]
+  (doto (JFrame. title)
+    (.. (getContentPane) (add (make-jpanel layer)))
+    (.pack)))
--- a/src/net/kryshen/indyvon/demo.clj	Sun Aug 29 18:33:41 2010 +0400
+++ b/src/net/kryshen/indyvon/demo.clj	Sun Aug 29 20:38:22 2010 +0400
@@ -9,90 +9,69 @@
   (:use
    (net.kryshen.indyvon core layers component))
   (:import
-   (net.kryshen.indyvon.core Size Bounds)
+   (net.kryshen.indyvon.core Size)
    (java.awt Color)
    (javax.swing JFrame)))
 
 (def layer1
-     (reify
-      Layer
-      (render! [layer]
-         (with-handlers layer
-           (doto *graphics*
-             (.setColor (rand-nth [Color/RED Color/ORANGE]))
-             (.fillRect 0 0 *width* *height*))
-           (:mouse-entered e (println e))
-           (:mouse-exited e (println e))
-           (:mouse-moved e (println e))))
-      (layer-size [layer] (Size. 30 20))))
+  (reify
+   Layer
+   (render! [layer]
+      (with-handlers layer
+        (doto *graphics*
+          (.setColor (rand-nth [Color/RED Color/ORANGE]))
+          (.fillRect 0 0 *width* *height*))
+        (:mouse-entered e (println e))
+        (:mouse-exited e (println e))
+        (:mouse-moved e (println e))))
+   (layer-size [layer]
+      (Size. 30 20))))
 
 (def layer1b (border layer1 2 3))
 
-;;(def layer1c (async-layer layer1 20 30))
-
 (def layer2
-     (reify
-      Layer
-      (render! [layer]
-         (doto *graphics*
-           (.setColor Color/YELLOW)
-           (.fillRect 0 0 *width* *height*))
-         (with-rotate 0.5 0 0
-           (draw! layer1b 30 25))
-         (draw! layer1 55 5))
-      (layer-size [layer] (Size. 70 65))))
-
-(def layer2m
-     (miniature layer2 30 30))
-
-(def layer3
-     (border (text-layer "Sample\ntext" :right :center)))
+  (reify
+   Layer
+   (render! [layer]
+      (doto *graphics*
+        (.setColor Color/YELLOW)
+        (.fillRect 0 0 *width* *height*))
+      (with-rotate 0.5 0 0
+        (draw! layer1b 30 25))
+      (draw! layer1 55 5))
+   (layer-size [layer]
+      (Size. 70 65))))
 
-(defn fps-layer [fps]
-  (border (text-layer (format "%.1f" fps) :right :bottom) 0 5))
+(def layer2m (miniature layer2 30 30))
 
-(def fps
-     (let [update-interval 0.1
-           frames (ref 0)
-           last (ref 0)
-           fl (ref (fps-layer 0.0))]
-       (reify
-        Layer
-        (render! [layer]
-           (draw! @fl)
-           (dosync
-            (alter frames + 1)
-            (let [time (System/currentTimeMillis)
-                  elapsed (/ (- time @last) 1000.0)]
-              (when (> elapsed update-interval)
-                (ref-set fl (fps-layer (/ @frames elapsed)))
-                (ref-set frames 0)
-                (ref-set last time)))))
-        (layer-size [layer] (layer-size @fl)))))
+(def layer3 (border (text-layer "Sample\ntext" :right :center)))
 
 (def layer
-     (reify
-      Layer
-      (render! [layer]
-         ;;(repaint)
-         (doto *graphics*
-           (.setColor (rand-nth [Color/BLACK Color/BLUE Color/RED]))
-           (.drawLine 0 0 *width* *height*))
-         (draw! layer2 15 20)
-         (draw! layer2m 120 50)
-         (draw! layer3 100 100 80 50)
-         (draw! fps))
-      (layer-size [layer] (Size. 400 300))))
+  (reify
+   Layer
+   (render! [layer]
+      ;;(repaint)
+      (doto *graphics*
+        ;; Random color to see when repaint happens.
+        (.setColor (rand-nth [Color/BLACK Color/BLUE Color/RED]))
+        (.drawLine 0 0 *width* *height*)
+        (.drawLine *width* 0 0 *height*))
+      (draw! layer2 15 20)
+      (draw! layer2m 120 50)
+      (draw! layer3 100 100 80 50))
+   (layer-size [layer]
+      (Size. 400 300))))
 
-(def root (viewport layer))
+(def vp (viewport layer))
+
+(def root (fps-layer vp))
 
 (defn show-frame [layer]
-  (doto (JFrame. "Test")
+  (doto (make-jframe "Test" layer)
     (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
-    (.. (getContentPane) (add (make-jpanel layer)))
-    (.pack)
     (.setVisible true)))
 
 (defn -main []
+  (println "Try to drag the viewport.")
   (show-frame root)
-  (show-frame (viewport-miniature root 80 60)))
+  (show-frame (fps-layer (viewport-miniature vp 80 60))))
--- a/src/net/kryshen/indyvon/layers.clj	Sun Aug 29 18:33:41 2010 +0400
+++ b/src/net/kryshen/indyvon/layers.clj	Sun Aug 29 20:38:22 2010 +0400
@@ -171,7 +171,7 @@
               width (if (pos? width) width 1)
               height (if (pos? height) height 1)]
           (Size. width height))))))
-    
+
 (defn miniature
   "Creates layer that asynchronously renders view of the content
   scaled to the specified size."
@@ -254,6 +254,29 @@
           (.drawRect *graphics* x y w h))))
    width height))
 
+(defn fps-layer
+  "Creates layer that draws content and displays
+   the frames per seconds rate." 
+  [content]
+  (let [update-interval 0.1
+        frames (ref 0)
+        prev-time (ref 0)
+        display (ref nil)]
+    (decorate-layer content [_]
+       (draw! content)
+       (draw!
+        (dosync
+         (alter frames + 1)
+         (let [time (System/nanoTime)
+               elapsed (/ (- time @prev-time) 1E9)
+               fps (/ @frames elapsed)]
+           (when (or (> elapsed update-interval) (not @display))
+             (ref-set display
+              (border (text-layer (format "%.1f" fps) :right :bottom) 0 5))
+             (ref-set frames 0)
+             (ref-set prev-time time)))
+         @display)))))
+
 ;;
 ;; Layer context decorators.
 ;;