changeset 13:c6009a144727

Layer decorator macro.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 14 Jun 2010 06:26:07 +0400
parents cd8a378414d1
children 0a2fafca72d8
files src/indyvon/core.clj
diffstat 1 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/indyvon/core.clj	Mon Jun 14 05:04:03 2010 +0400
+++ b/src/indyvon/core.clj	Mon Jun 14 06:26:07 2010 +0400
@@ -16,18 +16,24 @@
 (defn default-context []
   (LayerContext. nil nil 0 0 0 0 nil nil))
 
-(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- spec-map
+  ([specs]
+     (spec-map {} specs))
+  ([mm specs]
+     (if-let [form (first specs)]
+       (recur (conj mm [(first form) (next form)])
+              (next specs))
+       mm)))
+
+(defn- merge-specs [s1 s2]
+  (for [spec (spec-map (spec-map s1) s2)]
+    (cons (first spec) (second spec))))
+     
+(defmacro reify-layer [& specs]
+  `(reify Layer ~@(merge-specs
+                   '((size [_ _] [0 0])
+                     (anchor [_ _] [0 0]))
+                   specs)))
 
 (defn- make-graphics [g x y w h clip]
   (if clip
@@ -64,3 +70,10 @@
          (finally
           (.dispose graphics))))))
 
+(defmacro decorate-layer [layer & specs]
+  `(let [layer# ~layer]
+     (reify Layer ~@(merge-specs
+                     `((~'render! [l# c# g#] (draw! c# layer# g#))
+                       (~'size [l# c#] (size layer# c#))
+                       (~'anchor [l# c#] (anchor layer# c#)))
+                     specs))))