view src/net/kryshen/indyvon/layers.clj @ 54:1d2dfe5026a8

Support transformations.
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 19 Aug 2010 20:20:21 +0400
parents ca728127d605
children 6adbc03a52cb
line source
1 ;;
2 ;; Copyright (C) 2010 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
7 (ns net.kryshen.indyvon.layers
8 "Implementations of Layer protocol."
9 (:use
10 net.kryshen.indyvon.core)
11 (:import
12 (net.kryshen.indyvon.core Size Location)
13 (java.lang.ref SoftReference)
14 (java.awt Font Cursor Image Toolkit)
15 (java.awt.image ImageObserver)
16 (java.awt.font FontRenderContext TextLayout)))
18 ;; Define as macro to avoid unnecessary calculation of inner and outer
19 ;; sizes in the first case.
20 (defmacro align-xy [inner outer align first center last]
21 `(case ~align
22 ~first 0
23 ~center (/ (- ~outer ~inner) 2)
24 ~last (- ~outer ~inner)))
26 (defmacro align-x [inner outer align]
27 `(align-xy ~inner ~outer ~align :left :center :right))
29 (defmacro align-y [inner outer align]
30 `(align-xy ~inner ~outer ~align :top :center :bottom))
32 (defmacro decorate-layer
33 "Decorate Layer and Anchored replacing render! implementation."
34 [layer & render-tail]
35 `(reify
36 Layer
37 (render! ~@render-tail)
38 (layer-size [t#] (layer-size ~layer))
39 Anchored
40 (anchor [t# xa# ya#] (anchor ~layer xa# ya#))))
42 (defn padding
43 "Decorates layer adding padding."
44 ([content pad]
45 (padding content pad pad pad pad))
46 ([content top left bottom right]
47 (if (== 0 top left bottom right)
48 content
49 (reify
50 Layer
51 (render! [l]
52 (draw! content
53 left top
54 (- (:width *bounds*) left right)
55 (- (:height *bounds*) top bottom)))
56 (layer-size [l]
57 (let [s (layer-size content)]
58 (Size. (+ (:width s) left right)
59 (+ (:height s) top bottom))))))))
61 (defn border
62 "Decorate layer with a border."
63 ([content]
64 (border content 1))
65 ([content width]
66 (border content width 0))
67 ([content width gap]
68 (let [layer (padding content (+ width gap))]
69 (decorate-layer layer [_]
70 (let [w (:width *bounds*)
71 h (:height *bounds*)]
72 (with-color (:border-color *theme*)
73 (doseq [i (range 0 width)]
74 (.drawRect *graphics* i i (- w 1 i i) (- h 1 i i))))
75 (render! layer))))))
77 (defn panel
78 "Opaque layer using theme's alt-back-color."
79 ([content]
80 (panel content 0))
81 ([content gap]
82 (let [layer (padding content gap)]
83 (decorate-layer layer [_]
84 (with-color (:alt-back-color *theme*)
85 (.fillRect *graphics* 0 0
86 (:width *bounds*) (:height *bounds*)))
87 (render! layer)))))
89 (defn- re-split [^java.util.regex.Pattern re s]
90 (seq (.split re s)))
92 (def text-layout-cache (atom {}))
94 (defn- get-text-layout
95 [^String line ^Font font ^FontRenderContext font-context]
96 (let [key [line font font-context]]
97 (or (if-let [^SoftReference softref (@text-layout-cache key)]
98 (.get softref)
99 (do (swap! text-layout-cache dissoc key)
100 false))
101 (let [layout (TextLayout. line font font-context)]
102 ;;(println "text-layout-cache miss" line)
103 (swap! text-layout-cache assoc key (SoftReference. layout))
104 layout))))
106 (defn- layout-text
107 [lines ^Font font ^FontRenderContext font-context]
108 (map #(get-text-layout % font font-context) lines))
109 ;;(map #(TextLayout. ^String % font font-context) lines))
111 (defn- text-width [layouts]
112 (reduce (fn [w ^TextLayout tl] (max w (.getAdvance tl))) 0 layouts))
114 (defn- text-height [layouts]
115 (reduce (fn [w ^TextLayout tl] (+ w (.getAscent tl)
116 (.getDescent tl)
117 (.getLeading tl)))
118 0 layouts))
120 (defn text-layer
121 "Creates a layer to display multiline text."
122 ([text]
123 (text-layer text :left :top))
124 ([text h-align v-align]
125 (let [lines (re-split #"\r\n|\n|\r|\u0085|\u2028|\u2029" text)]
126 (reify Layer
127 (render! [layer]
128 (let [w (:width *bounds*)
129 h (:height *bounds*)
130 font (.getFont *graphics*)
131 layouts (layout-text lines font *font-context*)
132 y (align-y (text-height layouts) h v-align)]
133 (loop [layouts layouts, y y]
134 (when-first [^TextLayout layout layouts]
135 (let [ascent (.getAscent layout)
136 lh (+ ascent (.getDescent layout) (.getLeading layout))
137 x (align-x (.getAdvance layout) w h-align)]
138 (.draw layout *graphics* x (+ y ascent))
139 (recur (next layouts) (+ y lh)))))))
140 (layer-size [layer]
141 (let [layouts (layout-text lines (:font *theme*) *font-context*)
142 width (text-width layouts)
143 height (text-height layouts)]
144 (Size. width height)))))))
146 (defn- ^ImageObserver image-observer [update-fn]
147 (reify
148 ImageObserver
149 (imageUpdate [this img infoflags x y width height]
150 (update-fn)
151 (zero? (bit-and infoflags
152 (bit-or ImageObserver/ALLBITS
153 ImageObserver/ABORT))))))
155 (defn image-layer
156 [image-or-uri]
157 (let [^Image image (if (isa? image-or-uri Image)
158 image-or-uri
159 (.getImage (Toolkit/getDefaultToolkit)
160 ^java.net.URL image-or-uri))]
161 (.prepareImage (Toolkit/getDefaultToolkit) image -1 -1 nil)
162 (reify
163 Layer
164 (render! [layer]
165 (.drawImage *graphics* image 0 0 (image-observer *update*)))
166 (layer-size [layer]
167 (let [observer (image-observer *update*)
168 width (.getWidth image observer)
169 height (.getHeight image observer)
170 width (if (pos? width) width 1)
171 height (if (pos? height) height 1)]
172 (Size. width height))))))
174 (defn viewport
175 "Creates scrollable viewport layer."
176 ([content] (viewport content :left :top))
177 ([content h-align v-align]
178 (let [x (ref 0)
179 y (ref 0)
180 fix-x (ref 0)
181 fix-y (ref 0)
182 last-width (ref 0)
183 last-height (ref 0)]
184 (reify
185 Layer
186 (render! [layer]
187 (with-handlers layer
188 (let [anchor (anchor content h-align v-align)
189 width (:width *bounds*)
190 height (:height *bounds*)]
191 (dosync
192 (alter x + (align-x width @last-width h-align))
193 (alter y + (align-y height @last-height v-align))
194 (ref-set last-width width)
195 (ref-set last-height height))
196 (draw! content (- 0 @x (:x anchor)) (- 0 @y (:y anchor))))
197 (:mouse-pressed e
198 (dosync
199 (ref-set fix-x (:x-on-screen e))
200 (ref-set fix-y (:y-on-screen e)))
201 (->> Cursor/MOVE_CURSOR Cursor. (.setCursor *target*)))
202 (:mouse-released e
203 (->> Cursor/DEFAULT_CURSOR Cursor. (.setCursor *target*)))
204 (:mouse-dragged e
205 (dosync
206 (alter x + (- @fix-x (:x-on-screen e)))
207 (alter y + (- @fix-y (:y-on-screen e)))
208 (ref-set fix-x (:x-on-screen e))
209 (ref-set fix-y (:y-on-screen e)))
210 (*update*))))
211 (layer-size [layer] (layer-size content))))))
213 ;;
214 ;; Layer context decorators.
215 ;;
217 (defmacro handler [layer & handlers]
218 "Decorate layer to handle events."
219 `(let [layer# ~layer]
220 (decorate-layer layer# [t#]
221 (with-handlers t#
222 (render! layer#)
223 ~@handlers))))
225 (defn theme [layer & map-or-keyvals]
226 (let [theme (if (== (count map-or-keyvals) 1)
227 map-or-keyvals
228 (apply array-map map-or-keyvals))]
229 (reify
230 Layer
231 (render! [t]
232 (with-theme theme
233 (render! layer)))
234 (layer-size [t]
235 (with-theme theme
236 (layer-size layer)))
237 Anchored
238 (anchor [t xa ya]
239 (with-theme theme
240 (anchor layer xa ya))))))