view src/indyvon/core.clj @ 10:9af27ccccfac

Separated namespaces.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sun, 13 Jun 2010 04:57:14 +0400
parents 160e9ec945a2
children ea6fc44f19c8
line source
1 ;;
2 ;; Copyright (C) 2010 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
7 (ns indyvon.core)
9 (defprotocol Layer
10 (render! [this context graphics])
11 (size [this context])
12 (anchor [this context]))
14 (defrecord LayerContext [layer parent x y width height update-fn dispatcher])
16 (defn default-context []
17 (LayerContext. nil nil 0 0 0 0 nil nil))
19 (defmacro reify-layer [& fns]
20 (let [method-map {'size [['_ '_] [0 0]]
21 'anchor [['_ '_] [0 0]]}
22 method-map (loop [fns fns
23 mm method-map]
24 (if-let [form (first fns)]
25 (recur (next fns)
26 (conj mm [(first form) (next form)]))
27 mm))
28 methods (for [m method-map]
29 (cons (first m) (second m)))]
30 `(reify Layer ~@methods)))
32 (defn- make-graphics [g x y w h clip]
33 (if clip
34 (.create g x y w h)
35 (doto (.create g)
36 (.translate x y))))
38 (defn render-layer!
39 "Render layer in a new graphics context."
40 ([context layer graphics]
41 (render-layer! context layer graphics
42 0 0 (:width context) (:height context)))
43 ([context layer graphics x y]
44 (render-layer! context layer graphics x y true))
45 ([context layer graphics x y clip]
46 (let [s (size layer context)]
47 (render-layer! context layer graphics
48 x y (s 0) (s 1) clip)))
49 ([context layer graphics x y w h]
50 (render-layer! context layer graphics
51 x y w h true))
52 ([context layer graphics x y w h clip]
53 (let [graphics (make-graphics graphics x y w h clip)]
54 (try
55 (render! layer
56 (assoc context
57 :layer layer
58 :parent context
59 :x (+ (:x context) x)
60 :y (+ (:y context) y)
61 :width w
62 :height h)
63 graphics)
64 (finally
65 (.dispose graphics))))))