changeset 2:0f2a98f71a9c

Beginnings of event dispatching code.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 07 Jun 2010 21:13:05 +0400
parents a93cfcf60f27
children 6bc931b1b755
files src/indyvon/core.clj
diffstat 1 files changed, 54 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/core.clj	Mon Jun 07 04:42:52 2010 +0400
+++ b/src/indyvon/core.clj	Mon Jun 07 21:13:05 2010 +0400
@@ -1,10 +1,11 @@
 (ns indyvon.core
-  (:import (java.awt Dimension Point Component Graphics2D)))
+  (:import (java.awt Dimension Point Component Graphics2D AWTEvent)))
 
-(def ^{:private true} *graphics*)
 (def ^{:private true} *rx* 0)
 (def ^{:private true} *ry* 0)
 
+(def *graphics*)
+
 (def *width*)
 (def *height*)
 
@@ -17,15 +18,20 @@
   (size [this])
   (anchor [this]))
 
-;; (defmacro make-layer [& fns]
-;;   (let [fn-map
-;;         (apply hash-map
-;;                (reduce #(conj %1 (first %2) (next %2)) []
-;;                        fns))]
-;;     ;; TODO
-;;     fn-map))
+(defmacro reify-layer [& fns]
+  (let [method-map {'size [['_] [0 0]]
+                    'anchor [['_] [0 0]]}
+        method-map (loop [fns fns
+                          mm method-map]
+                     (if-let [form (first fns)]
+                       (recur (next fns)
+                              (conj mm [(first form) (next form)]))
+                       mm))
+        methods (for [m method-map]
+                  (cons (first m) (second m)))]
+    `(reify Layer ~@methods)))
 
-(defn- translate [g x y w h clip]
+(defn- make-graphics [g x y w h clip]
   (if clip
     (.create g x y w h)
     (doto (.create g)
@@ -43,13 +49,46 @@
   ([layer x y w h]
      (render-layer! layer x y w h true))
   ([layer x y w h clip]
-     (binding [*graphics* (translate *graphics* x y w h clip)
+     (binding [*graphics* (make-graphics *graphics* x y w h clip)
                *rx* (+ *rx* x)
                *ry* (+ *ry* y)
                *width* w
                *height* h]
        (render! layer))))
 
+;;
+;; Event handling
+;;
+
+(defprotocol EventDispatcher
+  (register [this layer])
+  (commit [this])
+  (dispatch [this event]))
+
+(defrecord LayerContext [layer rx ry width height update-fn])
+
+(defn make-event-dispatcher []
+  (let [contexts-r (ref [])
+        contexts-d (ref [])]
+    (reify
+     EventDispatcher
+     (register [this layer]
+      (dosync
+       (alter contexts-r conj
+              (LayerContext. layer *rx* *ry*
+                             *width* *height*
+                             *update-fn*))))
+     (commit [this]
+      (dosync (ref-set contexts-d @contexts-r)
+              (ref-set contexts-r [])))
+     (dispatch [this event]
+               ;; TODO
+               ))))
+
+;;
+;; Connection to AWT.
+;;
+
 (defn- make-update-fn [component]
   (fn [] (.repaint component)))
 
@@ -89,11 +128,10 @@
   (do 
     (def frame (java.awt.Frame. "Test"))
     (def layer
-         (reify Layer
-                (render! [this]
-                         (.fillRect *graphics* 10 10 40 40))
-                (size [this] [100 100])
-                (anchor [this] [0 0])))
+         (reify-layer
+          (render! [this]
+                   (.fillRect *graphics* 10 10 40 40))
+          (size [this] [100 100])))
     (doto frame
       (.addWindowListener
        (proxy [java.awt.event.WindowAdapter] []