changeset 64:702a4939312d

New mechanism for layers to trigger repaints (beginning).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 26 Aug 2010 06:29:30 +0400
parents 2d74abee0c93
children fd1bcb67bc32
files src/net/kryshen/indyvon/core.clj
diffstat 1 files changed, 47 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/core.clj	Thu Aug 26 03:45:52 2010 +0400
+++ b/src/net/kryshen/indyvon/core.clj	Thu Aug 26 06:29:30 2010 +0400
@@ -102,6 +102,47 @@
             :center (/ (:height size) 2)
             :bottom (:height size)))))))
 
+(defn- assoc-cons [m key val]
+  (->> (get m key) (cons val) (assoc m key)))
+
+(defn- assoc-in-cons [m keys val]
+  (->> (get-in m keys) (cons val) (assoc-in m keys)))
+
+;;
+;; Observers
+;;
+
+(def observers (atom nil))
+
+(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)
+  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- change-group-id
+  [old-id new-id]
+  (swap! observers change-group-id* old-id new-id))
+
+(defn update
+  "Notify observers."
+  [target & args]
+  (doseq [f (reduce #(concat %1 (get %2 target)) nil (vals @observers))]
+    (apply f target args)))
+
 ;;
 ;; Rendering
 ;;
@@ -242,6 +283,12 @@
   ([layer x y width height]
      (with-bounds* x y width height render! layer)))
 
+;; TODO: объект-сцена вместо context-draw!
+;; Сцена устанавливает контекст и рисует слой. Сцена - слой?
+;; Сцена идентифицирует группу обозревателя слоев.
+;; Обновления любого слоя, изображенного в сцене, вызывает обновление сцены.
+;; Обозреватель сцены вызывает repaint().
+
 (defn context-draw!
   "Sets up layer context, draws layer and commits event dispatcher."
   ([layer graphics event-dispatcher update-fn width height]
@@ -309,9 +356,6 @@
                    (inverse-relative-transform)
                    (get-thread-bindings)))
 
-(defn- assoc-cons [m key val]
-  (assoc m key (cons val (get m key))))
-
 (defn- add-node [tree node]
   (assoc-cons tree (:parent node) node))