view src/net/kryshen/indyvon/demo.clj @ 117:a50a304e58d8

Empty layer.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 02 Mar 2012 05:21:45 +0400
parents fd8fb8a3ff5a
children 173616375eb5
line source
1 ;;
2 ;; Copyright 2010, 2011 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
6 ;; Indyvon is free software: you can redistribute it and/or modify it
7 ;; under the terms of the GNU Lesser General Public License version 3
8 ;; only, as published by the Free Software Foundation.
9 ;;
10 ;; Indyvon is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with Indyvon. If not, see
17 ;; <http://www.gnu.org/licenses/>.
18 ;;
20 (ns net.kryshen.indyvon.demo
21 "Indyvon demo and experiments."
22 (:gen-class)
23 (:use
24 (net.kryshen.indyvon core layers component))
25 (:import
26 (java.awt Color)
27 (javax.swing JFrame)))
29 (defn draw-button!
30 "Draws button immediately (but uses callback for button action
31 unlike IMGUI)."
32 [id content callback & args]
33 (with-handlers id
34 (let [shadow-offset 2
35 padding 4
36 border-width 1
37 offset (if (picked? id) (/ shadow-offset 2) 0)
38 ^Color color (:alt-back-color *theme*)
39 color (if (hovered? id) (.brighter color) color)
40 width (- *width* shadow-offset)
41 height (- *height* shadow-offset)]
42 (with-color (:shadow-color *theme*)
43 (.fillRect *graphics* shadow-offset shadow-offset width height))
44 (with-color color
45 (.fillRect *graphics* offset offset width height))
46 (draw! (border content border-width padding)
47 offset offset width height))
48 ;; Event handlers
49 (:mouse-entered _ (repaint))
50 (:mouse-exited _ (repaint))
51 (:mouse-pressed _ (repaint))
52 (:mouse-released _ (repaint))
53 (:mouse-clicked _ (apply callback args))))
55 (defn combine-colors [^Color color1 ^Color color2 c]
56 "Returns color between color1 and color2. When c (0 <= c <= 1.0) is
57 closer to 0 the returned сolor is closer to color1."
58 (case c
59 0.0 color1
60 1.0 color2
61 (let [rgb1 (.getRGBComponents color1 nil)
62 rgb2 (.getRGBComponents color2 nil)
63 rgb (float-array (map #(+ (* (- 1 c) %1) (* c %2)) rgb1 rgb2))]
64 (Color. (aget rgb 0) (aget rgb 1) (aget rgb 2) (aget rgb 3)))))
66 (defn animate
67 "Changes atom value according to specified range, speed, and current
68 frame interval. Invokes repaint if change happens."
69 [atom from to speed]
70 (let [prev @atom
71 state (cond
72 (zero? speed) :stop
73 (= prev from) (if (pos? speed) :start :stop)
74 (= prev to) (if (neg? speed) :start :stop)
75 :default :continue)]
76 (if (= state :stop)
77 prev
78 (let [interval (if (= state :start) 1 *interval*)
79 step (* speed interval 1E-9)
80 val (swap! atom #(-> % (+ step) (max from) (min to)))]
81 (repaint)
82 val))))
84 (defn animated-button
85 "Create animated button layer."
86 [content callback & args]
87 (let [padding 4
88 border-width 1
89 shadow-offset 2
90 face (border content padding border-width)
91 highlight (atom 0)
92 animation-speed (atom 0)]
93 (interval-layer
94 (reify
95 Layer
96 (render! [button]
97 (with-handlers button
98 (let [hovered (hovered? button)
99 offset (if (picked? button) (/ shadow-offset 2) 0)
100 color (combine-colors
101 (:alt-back-color *theme*) Color/WHITE
102 (animate highlight 0.0 1.0 @animation-speed))
103 width (- *width* shadow-offset)
104 height (- *height* shadow-offset)]
105 (with-color (:shadow-color *theme*)
106 (.fillRect *graphics*
107 shadow-offset shadow-offset
108 width height))
109 (with-color color
110 (.fillRect *graphics* offset offset width height))
111 (draw! (border content border-width padding)
112 offset offset width height))
113 ;; Event handlers
114 (:mouse-entered _
115 (reset! animation-speed 4)
116 (repaint))
117 (:mouse-exited _
118 (reset! animation-speed -2)
119 (repaint))
120 (:mouse-pressed _ (repaint))
121 (:mouse-released _ (repaint))
122 (:mouse-clicked _ (apply callback args))))
123 (geometry [button]
124 (let [face-geom (geometry face)]
125 (->Size (+ (width face-geom) shadow-offset)
126 (+ (height face-geom) shadow-offset))))))))
128 (def button1 (animated-button (label "Animated button 1")
129 println "Animated button 1 clicked"))
131 (def button2 (animated-button (label "Animated button 2")
132 println "Animated button 2 clicked"))
134 (def test-layer1
135 (reify
136 Layer
137 (render! [layer]
138 (with-handlers layer
139 (with-color (if (hovered? layer) Color/ORANGE Color/RED)
140 (.fillRect *graphics* 0 0 *width* *height*))
141 (:mouse-entered e
142 (repaint)
143 (println e))
144 (:mouse-exited e
145 (repaint)
146 (println e))
147 (:mouse-moved e
148 (println e))))
149 (geometry [layer]
150 (->Size 30 20))))
152 (def test-layer1b (border test-layer1 2 3))
154 (def test-layer2
155 (reify
156 Layer
157 (render! [layer]
158 (doto *graphics*
159 (.setColor Color/YELLOW)
160 (.fillRect 0 0 *width* *height*))
161 (with-rotate 0.5 0 0
162 (draw! test-layer1b 30 25))
163 (draw! test-layer1 55 5))
164 (geometry [layer]
165 (->Size 70 65))))
167 (def test-layer2m (miniature test-layer2 30 30))
169 (def test-layer3 (border (label "Sample\ntext" :right :bottom)))
171 (def root
172 (reify
173 Layer
174 (render! [layer]
175 ;;(repaint)
176 (doto *graphics*
177 (.drawLine 0 0 *width* *height*)
178 (.drawLine *width* 0 0 *height*)
179 ;; Random color to see when repaint happens.
180 (.setColor (rand-nth [Color/BLACK Color/BLUE Color/RED]))
181 (.fillOval 5 5 20 20))
182 (draw! test-layer2 30 20)
183 (draw! test-layer2m 120 50)
184 (draw! test-layer3 100 100 80 50)
185 (draw! button1 50 160)
186 (with-rotate (/ Math/PI 6) 250 200
187 (draw! button1 210 140))
188 (draw! button2 100 200)
189 (with-bounds 180 240 140 30
190 (draw-button! :button
191 (label "Immediate button" :center :center)
192 #(println "Button clicked!"))))
193 (geometry [layer]
194 (->Size 400 300))))
196 ;; Main viewport
197 (def vp (viewport root))
199 ;; Miniature (rendered asynchronously)
200 (def vp-miniature (-> vp (viewport-miniature 100 75) border shadow))
202 ;; Main scene
203 (def scene
204 (fps-layer
205 (decorate-layer vp [_]
206 (draw! vp)
207 (draw-aligned!
208 (label (str "Drag mouse to pan," \newline
209 "use mouse wheel to zoom."))
210 :left :bottom 5 (- *height* 5))
211 (draw! vp-miniature (- *width* 105) 5))))
213 (defn show-frame [layer]
214 (doto (make-jframe "Test" layer)
215 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
216 (.setVisible true)))
218 (defn -main []
219 (show-frame scene))
221 (comment
222 (show-frame (viewport-miniature vp 200 150)))