forked from cljfx/cljfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe12_interactive_development.clj
257 lines (219 loc) · 9.41 KB
/
e12_interactive_development.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
;; This file is supposed to be explored from the REPL, evaluating forms one
;; by one from top to bottom.
(ns e12-interactive-development
(:require [cljfx.api :as fx]))
;; I want to build an interactive chart that shows how bouncing object falls
;; on the ground. I want to be able to edit gravity and friction to see how
;; it affects object's behavior, so I will put it into state:
(def *state
(atom {:gravity 10
:friction 0.4}))
;; I want to have map event handlers extensible during runtime to avoid full app
;; restarts. One way is using vars instead of functions to get that kind of
;; behavior, but I'll go with another way: multi-methods.
(defmulti event-handler :event/type)
;; Now we'll create our app with dummy root view
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :h-box
:children [{:fx/type :label
:text (str "g = " gravity ", f = " friction)}]}}})
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc (fn [state]
{:fx/type root-view
:state state}))
:opts {:fx.opt/map-event-handler event-handler}))
(fx/mount-renderer *state renderer)
;; At this point, really tiny window appears that displays current gravity and
;; friction. We want to have an ability to change these values, so let's create
;; some slider views for them:
(defn slider-view [{:keys [min max value]}]
{:fx/type :slider
:min min
:max max
:value value})
;; Now we will update our root view to display these sliders:
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :h-box
:children [{:fx/type slider-view
:min 0
:max 100
:value gravity}
{:fx/type slider-view
:min 0
:max 1
:value friction}]}}})
;; Now we updated our root function, but window didn't change. It happens
;; because cljfx has no way to know if definition of some component functions is
;; changed. But we can ask renderer to refresh itself by calling it without any
;; arguments:
(renderer)
;; Now small label got replaced with 2 sliders. Problem is, there are no labels
;; on them, so users can't really see what these sliders mean, so let's fix it:
(defn slider-view [{:keys [min max value label]}]
{:fx/type :v-box
:children [{:fx/type :label
:text label}
{:fx/type :slider
:min min
:max max
:value value
:major-tick-unit max
:show-tick-labels true}]})
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :h-box
:spacing 10
:children [{:fx/type slider-view
:min 0
:max 100
:value gravity
:label "Gravity"}
{:fx/type slider-view
:min 0
:max 1
:label "Friction"
:value friction}]}}})
(renderer)
;; Great, time to add a chart that uses gravity and friction, but first let's
;; try to display something dummy to make sure it works
(defn chart-view [{:keys [gravity friction]}]
{:fx/type :line-chart
:x-axis {:fx/type :number-axis
:label "Time"}
:y-axis {:fx/type :number-axis
:label "Y"}
:data [{:fx/type :xy-chart-series
:name "Position by time"
:data (for [t (range 100)]
{:fx/type :xy-chart-data
:x-value t
:y-value t})}]})
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :v-box
:spacing 20
:children [{:fx/type chart-view
:gravity gravity
:friction friction}
{:fx/type :h-box
:spacing 10
:alignment :center
:children [{:fx/type slider-view
:min 0
:max 100
:value gravity
:label "Gravity"}
{:fx/type slider-view
:min 0
:max 1
:label "Friction"
:value friction}]}]}}})
(renderer)
;; Now chart is added to a window. Everything looks fine, time to do some
;; simulation:
(defn simulate-step [{:keys [velocity y]} gravity friction]
(let [new-velocity (* (- velocity gravity) (- 1 friction))
new-y (+ y new-velocity)]
(if (neg? new-y)
{:velocity (- new-velocity) :y 0}
{:velocity new-velocity :y new-y})))
(defn chart-view [{:keys [gravity friction]}]
{:fx/type :line-chart
:x-axis {:fx/type :number-axis
:label "Time"}
:y-axis {:fx/type :number-axis
:label "Y"}
:data [{:fx/type :xy-chart-series
:name "Position by time"
:data (->> {:velocity 0 :y 100}
(iterate #(simulate-step % gravity friction))
(take 100)
(map-indexed (fn [index {:keys [y]}]
{:fx/type :xy-chart-data
:x-value index
:y-value y})))}]})
(renderer)
;; Okay, there are some results showing, but there is no bouncing, probably
;; gravity and friction have some weird values. It's time to make it all alive!
;; What remains is propagating changes in slider values to our model, so let's
;; add it
(defmethod event-handler ::set-friction [e]
(swap! *state assoc :friction (:fx/event e)))
(defmethod event-handler ::set-gravity [e]
(swap! *state assoc :gravity (:fx/event e)))
(defn slider-view [{:keys [min max value label event]}] ;; add event as arg
{:fx/type :v-box
:children [{:fx/type :label
:text label}
{:fx/type :slider
:min min
:max max
:value value
:on-value-changed {:event/type event} ;; fire it on value
:major-tick-unit max
:show-tick-labels true}]})
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :v-box
:spacing 20
:children [{:fx/type chart-view
:gravity gravity
:friction friction}
{:fx/type :h-box
:spacing 10
:alignment :center
:children [{:fx/type slider-view
:min 0
:max 100
:value gravity
:label "Gravity"
:event ::set-gravity} ;; provide events
{:fx/type slider-view
:min 0
:max 1
:label "Friction"
:value friction
:event ::set-friction}]}]}}})
(renderer)
;; Nice, now playing with sliders makes chart data change! And all this is
;; done in runtime! Looks like the problem was that gravity is too high, so as a
;; final touch let's make it lower (both current value and slider range)
(swap! *state assoc :gravity 1)
(defn root-view [{{:keys [gravity friction]} :state}]
{:fx/type :stage
:showing true
:scene {:fx/type :scene
:root {:fx/type :v-box
:spacing 20
:children [{:fx/type chart-view
:gravity gravity
:friction friction}
{:fx/type :h-box
:spacing 10
:alignment :center
:children [{:fx/type slider-view
:min 0
:max 5 ;; 100 -> 5
:value gravity
:label "Gravity"
:event ::set-gravity}
{:fx/type slider-view
:min 0
:max 1
:label "Friction"
:value friction
:event ::set-friction}]}]}}})
(renderer)