Mercurial > hg > indyvon
changeset 156:dc13cacf3a43
API changes (themed, hinted, add-handlers); formatting.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Wed, 16 Apr 2014 00:10:48 +0400 |
parents | c3782e84486f |
children | 4fea68ec12f4 |
files | src/indyvon/core.clj src/indyvon/demo.clj src/indyvon/viewport.clj src/indyvon/views.clj |
diffstat | 4 files changed, 58 insertions(+), 61 deletions(-) [+] |
line diff
1.1 --- a/src/indyvon/core.clj Tue Apr 15 23:45:14 2014 +0400 1.2 +++ b/src/indyvon/core.clj Wed Apr 16 00:10:48 2014 +0400 1.3 @@ -105,8 +105,8 @@ 1.4 (height [geom] [geom width]) 1.5 (anchor-x [geom h-align width] 1.6 "Returns the x coordinate of the anchor point for the specified 1.7 - horizontal alignment and width, h-align could be :left, :center 1.8 - or :right.") 1.9 + horizontal alignment and width, h-align could be :left, :center 1.10 + or :right.") 1.11 (anchor-y [geom v-align height] 1.12 "Returns the y coordinate of the anchor point for the specified 1.13 vertical alignment and height, v-align could be :top, :center 1.14 @@ -218,16 +218,16 @@ 1.15 "Listen for events on the specified AWT Component.") 1.16 (create-dispatcher [this handle handlers] 1.17 "Returns new event dispatcher associated with the specified event 1.18 - handlers (an event-id -> handler-fn map). Handle is used to 1.19 - match the contexts between commits.") 1.20 + handlers (an event-id -> handler-fn map). Handle is used to match 1.21 + the contexts between commits.") 1.22 (commit [this] 1.23 "Apply the registered handlers for event processing.") 1.24 (handle-picked? [this handle] 1.25 "Returns true if the specified handle received the :mouse-pressed 1.26 - event and have not yet received :moused-released.") 1.27 + event and have not yet received :moused-released.") 1.28 (handle-hovered? [this handle] 1.29 "Returns true if the specified handle received the :mouse-entered 1.30 - event and have not yet received :mouse-exited.")) 1.31 + event and have not yet received :mouse-exited.")) 1.32 1.33 (defn- assoc-cons [m key val] 1.34 (->> (get m key) (cons val) (assoc m key))) 1.35 @@ -366,8 +366,8 @@ 1.36 ;; Use faster clipping calculation provided by Graphics2D. 1.37 (defn- clip 1.38 "Intersect clipping area with the specified Shape in current 1.39 - transform coordinates. Returns new clip in the AWT component 1.40 - coordinates (Shape or nil if empty)." 1.41 + transform coordinates. Returns new clip in the AWT component 1.42 + coordinates (Shape or nil if empty)." 1.43 [^Shape shape] 1.44 (let [^Graphics2D clip-g (.create *graphics*)] 1.45 (try 1.46 @@ -383,7 +383,7 @@ 1.47 1.48 (defn- ^Graphics2D apply-theme 1.49 "Set graphics' color and font to match theme. 1.50 - Modifies and returns the first argument." 1.51 + Modifies and returns the first argument." 1.52 ([] 1.53 (apply-theme *graphics* *theme*)) 1.54 ([^Graphics2D graphics theme] 1.55 @@ -438,6 +438,10 @@ 1.56 `(binding [*theme* (merge *theme* ~theme)] 1.57 ~@body)) 1.58 1.59 +(defn with-theme* [theme f & args] 1.60 + (with-theme theme 1.61 + (apply f args))) 1.62 + 1.63 (defmacro with-color [color-or-key & body] 1.64 `(let [color# ~color-or-key 1.65 color# (get *theme* color# color#)
2.1 --- a/src/indyvon/demo.clj Tue Apr 15 23:45:14 2014 +0400 2.2 +++ b/src/indyvon/demo.clj Wed Apr 16 00:10:48 2014 +0400 2.3 @@ -46,11 +46,11 @@ 2.4 (draw! (border border-width padding content) 2.5 offset offset width height)) 2.6 ;; Event handlers 2.7 - (:mouse-entered _ (repaint)) 2.8 - (:mouse-exited _ (repaint)) 2.9 - (:mouse-pressed _ (repaint)) 2.10 - (:mouse-released _ (repaint)) 2.11 - (:mouse-clicked _ (apply callback args)))) 2.12 + [:mouse-entered _ (repaint)] 2.13 + [:mouse-exited _ (repaint)] 2.14 + [:mouse-pressed _ (repaint)] 2.15 + [:mouse-released _ (repaint)] 2.16 + [:mouse-clicked _ (apply callback args)])) 2.17 2.18 (defn combine-colors 2.19 "Returns color between color1 and color2. When c (0 <= c <= 1.0) is 2.20 @@ -112,15 +112,15 @@ 2.21 (draw! (border border-width padding content) 2.22 offset offset width height)) 2.23 ;; Event handlers 2.24 - (:mouse-entered _ 2.25 - (reset! animation-speed 4) 2.26 - (repaint)) 2.27 - (:mouse-exited _ 2.28 - (reset! animation-speed -2) 2.29 - (repaint)) 2.30 - (:mouse-pressed _ (repaint)) 2.31 - (:mouse-released _ (repaint)) 2.32 - (:mouse-clicked _ (apply callback args)))) 2.33 + [:mouse-entered _ 2.34 + (reset! animation-speed 4) 2.35 + (repaint)] 2.36 + [:mouse-exited _ 2.37 + (reset! animation-speed -2) 2.38 + (repaint)] 2.39 + [:mouse-pressed _ (repaint)] 2.40 + [:mouse-released _ (repaint)] 2.41 + [:mouse-clicked _ (apply callback args)])) 2.42 (geometry [button] 2.43 (let [face-geom (geometry face)] 2.44 (->Size (+ (width face-geom) shadow-offset) 2.45 @@ -139,14 +139,14 @@ 2.46 (with-handlers view 2.47 (with-color (if (hovered? view) Color/ORANGE Color/RED) 2.48 (.fillRect *graphics* 0 0 *width* *height*)) 2.49 - (:mouse-entered e 2.50 + [:mouse-entered e 2.51 (repaint) 2.52 - (println e)) 2.53 - (:mouse-exited e 2.54 + (println e)] 2.55 + [:mouse-exited e 2.56 (repaint) 2.57 - (println e)) 2.58 - (:mouse-moved e 2.59 - (println e)))) 2.60 + (println e)] 2.61 + [:mouse-moved e 2.62 + (println e)])) 2.63 (geometry [view] 2.64 (->Size 30 20)))) 2.65
3.1 --- a/src/indyvon/viewport.clj Tue Apr 15 23:45:14 2014 +0400 3.2 +++ b/src/indyvon/viewport.clj Wed Apr 16 00:10:48 2014 +0400 3.3 @@ -92,14 +92,14 @@ 3.4 *viewport-transform* transform] 3.5 (with-transform transform 3.6 (draw! content 0 0 (width geom) (height geom) false)))) 3.7 - (:mouse-pressed e 3.8 + [:mouse-pressed e 3.9 (swap! state assoc 3.10 :fix-x (:x-on-screen e) 3.11 :fix-y (:y-on-screen e)) 3.12 - (set-cursor! (Cursor. Cursor/MOVE_CURSOR))) 3.13 - (:mouse-released e 3.14 - (set-cursor! (Cursor. Cursor/DEFAULT_CURSOR))) 3.15 - (:mouse-dragged e 3.16 + (set-cursor! (Cursor. Cursor/MOVE_CURSOR))] 3.17 + [:mouse-released e 3.18 + (set-cursor! (Cursor. Cursor/DEFAULT_CURSOR))] 3.19 + [:mouse-dragged e 3.20 (swap! state 3.21 (fn [s] 3.22 (assoc s 3.23 @@ -109,12 +109,12 @@ 3.24 (- (:y-on-screen e) (:fix-y s))) 3.25 :fix-x (:x-on-screen e) 3.26 :fix-y (:y-on-screen e)))) 3.27 - (update view)) 3.28 - (:mouse-wheel e 3.29 + (update view)] 3.30 + [:mouse-wheel e 3.31 (scale-viewport! 3.32 view 3.33 (Math/pow *viewport-scaling-step* (:wheel-rotation e)) 3.34 - true (:x e) (:y e))))) 3.35 + true (:x e) (:y e))])) 3.36 (geometry [_] 3.37 (geometry content))) 3.38 3.39 @@ -231,8 +231,8 @@ 3.40 (.fillRect *graphics* 0 0 last-width last-height))) 3.41 (with-handlers l 3.42 (draw! miniature) 3.43 - (:mouse-pressed e (move-vp! (:x e) (:y e))) 3.44 - (:mouse-dragged e (move-vp! (:x e) (:y e)))) 3.45 + [:mouse-pressed e (move-vp! (:x e) (:y e))] 3.46 + [:mouse-dragged e (move-vp! (:x e) (:y e))]) 3.47 (with-transform transform 3.48 (with-color :border-color 3.49 (.drawRect *graphics* 0 0 last-width last-height)))))))
4.1 --- a/src/indyvon/views.clj Tue Apr 15 23:45:14 2014 +0400 4.2 +++ b/src/indyvon/views.clj Wed Apr 16 00:10:48 2014 +0400 4.3 @@ -47,9 +47,6 @@ 4.4 4.5 (def empty-view (->Empty)) 4.6 4.7 -;; TODO: change argument order for decorators, content should be the 4.8 -;; last. 4.9 - 4.10 (defn padding 4.11 "Adds padding to the content view." 4.12 ([distance content] 4.13 @@ -264,7 +261,7 @@ 4.14 ;; View context decorators 4.15 ;; 4.16 4.17 -(defmacro handler [view & handlers] 4.18 +(defmacro add-handlers [view & handlers] 4.19 "Adds event handling to the view." 4.20 `(let [view# ~view] 4.21 (decorate-view view# [t#] 4.22 @@ -272,25 +269,21 @@ 4.23 (render! view#) 4.24 ~@handlers)))) 4.25 4.26 -(defn themed [view & map-or-keyvals] 4.27 - (let [theme (if (== (count map-or-keyvals) 1) 4.28 - (first map-or-keyvals) 4.29 - (apply array-map map-or-keyvals))] 4.30 - (reify 4.31 - View 4.32 - (render! [_] 4.33 - (with-theme theme 4.34 - (render! view))) 4.35 - (geometry [_] 4.36 - (with-theme theme 4.37 - (geometry view)))))) 4.38 +(defn themed [theme view] 4.39 + (reify 4.40 + View 4.41 + (render! [_] 4.42 + (with-theme* theme render! view)) 4.43 + (geometry [_] 4.44 + (with-theme* theme geometry view)))) 4.45 4.46 -(defn hinted [view & map-or-keyvals] 4.47 - (let [hints (if (== (count map-or-keyvals) 1) 4.48 - (first map-or-keyvals) 4.49 - (apply array-map map-or-keyvals))] 4.50 - (decorate-view view [_] 4.51 - (with-hints* hints render! view)))) 4.52 +(defn hinted [hints view] 4.53 + (reify 4.54 + View 4.55 + (render! [_] 4.56 + (with-hints* hints render! view)) 4.57 + (geometry [_] 4.58 + (with-hints* hints geometry view)))) 4.59 4.60 ;; 4.61 ;; Measuring time