Mercurial > hg > indyvon
changeset 66:a1999c1f7289
Changes in the observe-repaint mechanism.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Fri, 27 Aug 2010 19:47:28 +0400 |
parents | fd1bcb67bc32 |
children | a19cf5007d14 |
files | src/net/kryshen/indyvon/component.clj src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/demo.clj src/net/kryshen/indyvon/layers.clj |
diffstat | 4 files changed, 42 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/component.clj Fri Aug 27 01:24:31 2010 +0400 +++ b/src/net/kryshen/indyvon/component.clj Fri Aug 27 19:47:28 2010 +0400 @@ -31,6 +31,6 @@ (let [s (root-size layer (font-context this) this)] (Dimension. (:width s) (:height s)))))] (.setBackground panel (:back-color *theme*)) - (add-observer layer (fn [_] (.repaint panel))) + (add-observer panel layer (fn [_] (.repaint panel))) (listen! event-dispatcher panel) panel)))
--- a/src/net/kryshen/indyvon/core.clj Fri Aug 27 01:24:31 2010 +0400 +++ b/src/net/kryshen/indyvon/core.clj Fri Aug 27 19:47:28 2010 +0400 @@ -113,28 +113,29 @@ (def observers (atom nil)) +;; TODO: groups should be weakly referenced. +;; Need persistent analog to java.util.WeakHashMap. (defn add-observer - ([target f] - (add-observer target f :default)) - ([target f group-id] - (swap! observers assoc-in-cons [group-id target] f) - nil)) - -(defn remove-group - "Remove group of observers." - [group-id] - (swap! observers dissoc group-id) + "Add observer fn for the target to the specified group." + [group target f] + (swap! observers assoc-in-cons [group target] f) nil) -(defn- change-group-id* - [observers old-id new-id] - (let [group (get observers old-id)] - (assoc (dissoc observers old-id) - new-id group))) +(defn remove-observer-group + "Remove group of observers." + [group] + (swap! observers dissoc group) + nil) -(defn- change-group-id - [old-id new-id] - (swap! observers change-group-id* old-id new-id)) +;; (defn- replace-observer-group* +;; [observers old-id new-id] +;; (let [group (get observers old-id)] +;; (assoc (dissoc observers old-id) +;; new-id group))) + +;; (defn- replace-observer-group +;; [old-id new-id] +;; (swap! observers replace-observer-group* old-id new-id)) (defn update "Notify observers." @@ -152,13 +153,14 @@ removed after the next frame rendering is complete." [target f] (let [root *root*] - (add-observer target f root))) + (add-observer root target f))) (defn repaint-on-update "Trigger repaint of the current scene when the target updates." [target] (let [root *root*] - (add-observer target (fn [_] (update root)) root))) + (if (not= root target) + (add-observer root target (fn [_] (update root)))))) ;; ;; Rendering @@ -326,15 +328,14 @@ *width* width *height* height *clip* (Rectangle2D$Double. 0 0 width height)] - (let [tmp-group (Object.)] - (apply-theme) - (.clearRect graphics 0 0 width height) - (change-group-id layer tmp-group) - (try - (render! layer) - (finally - (remove-group tmp-group) - (commit event-dispatcher))))))) + (apply-theme) + (with-color (:back-color *theme*) + (.fillRect graphics 0 0 width height)) + (remove-observer-group layer) + (try + (render! layer) + (finally + (commit event-dispatcher)))))) (defn root-size ([layer font-context]
--- a/src/net/kryshen/indyvon/demo.clj Fri Aug 27 01:24:31 2010 +0400 +++ b/src/net/kryshen/indyvon/demo.clj Fri Aug 27 19:47:28 2010 +0400 @@ -13,8 +13,6 @@ (java.awt Color) (javax.swing JFrame))) -(def frame (JFrame. "Test")) - (def layer1 (reify Layer @@ -86,11 +84,17 @@ (draw! fps)) (layer-size [layer] (Size. 400 300)))) -(defn -main [] - (doto frame +(def root (viewport layer)) + +(defn show-frame [layer] + (doto (JFrame. "Test") (.addWindowListener (proxy [java.awt.event.WindowAdapter] [] (windowClosing [event] (.dispose frame)))) - (.. (getContentPane) (add (make-jpanel (viewport layer)))) + (.. (getContentPane) (add (make-jpanel layer))) (.pack) (.setVisible true))) + +(defn -main [] + (show-frame root) + (show-frame (miniature root 80 60)))
--- a/src/net/kryshen/indyvon/layers.clj Fri Aug 27 01:24:31 2010 +0400 +++ b/src/net/kryshen/indyvon/layers.clj Fri Aug 27 19:47:28 2010 +0400 @@ -271,6 +271,7 @@ (reify Layer (render! [layer] + (repaint-on-update layer) (with-handlers layer (let [anchor (anchor content h-align v-align)] (dosync @@ -294,7 +295,7 @@ (alter y + (- @fix-y (:y-on-screen e))) (ref-set fix-x (:x-on-screen e)) (ref-set fix-y (:y-on-screen e))) - (repaint)))) + (update layer)))) (layer-size [layer] (layer-size content)))))) ;;