/
membrane_ui.clj
332 lines (290 loc) · 9.41 KB
/
membrane_ui.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
(ns ui.membrane-ui
(:require
[membrane.skia.paragraph :as para]
[membrane.ui :as ui]
[membrane.skia :as skia]
[genek.entities :as e]
[membrane.basic-components :as basic]
[membrane.component :refer
[defui defeffect make-app]]))
; someday
; - learn about focus
; next
; X I’ll have ability to nav through all the frames
; - prev/next
; - last frame
; - handle inc/dec :last-frame
; X and a way to animate all the frames.
; - show all stages [:start :waiting-for-movers1 ...]
; - highlight which stage we're in (RED)
; Adrian, to run:
; go into ns notebooks/s02-recursive-search.clj, and load the namespace -- it will
; create the state, which is about 500 frames
; then in this namespace, run
;(def w (skia/run #'dev-view))
(def *sim-state genek.sim2/*state)
; :frame: either frame number or :last-frame
(defonce *app-state (atom nil))
(defn init-state!
[]
(reset! *app-state {:frame :last-frame}))
(comment
(init-state!)
0)
(defn next-frame!
[curr-page total-pages]
; cases:
; handle overflow: more than total-pages
; handle :last-frame: leave unchanged
(cond
(= :last-frame curr-page) nil
(= (dec total-pages) curr-page) nil
:else (swap! *app-state update-in [:frame] inc)))
(defn prev-frame!
[curr-page total-pages]
; cases:
; handle underflow: if 0, leave at zero
; handle :last-frame: (dec total-pages)
(println :prev-frame! :curr-page curr-page :total-pages total-pages)
(case curr-page
0 nil
:last-frame (swap! *app-state assoc-in [:frame] (dec total-pages))
(swap! *app-state update-in [:frame] dec)))
(defn last-frame!
[]
(swap! *app-state assoc-in [:frame] :last-frame))
(defn first-frame!
[]
(swap! *app-state assoc-in [:frame] 0))
(defn animate-all-frames!
" start from zero, and then on a timer, advance frames to the end
run in a future; pass first frame, recurse to end (total-pages)
input: framenum"
[framenum total-pages]
(first-frame!)
(loop [framenum framenum
total-page total-pages]
; termination case
(println :animate-all-frames!/entering :framenum framenum :total-pages total-pages)
(if (< framenum total-pages)
(do
(Thread/sleep 50)
(next-frame! framenum total-pages)
; see if we can force repaint
(if-let [w (resolve `w)]
((:membrane.skia/repaint @w)))
(recur (inc framenum) total-pages)))))
(defn selector
[curr-page total-pages]
(ui/on
::next-frame (fn []
(println ::next-frame)
(next-frame! curr-page total-pages)
nil)
::prev-frame (fn []
(println ::prev-frame)
(prev-frame! curr-page total-pages)
nil)
::last-frame (fn []
(println ::last-frame)
(last-frame!)
nil)
::first-frame (fn []
(println ::first-frame)
(first-frame!)
nil)
::animate-all-frames (fn []
(println ::animate-all-frames)
; run in future
(future
(animate-all-frames! 0 total-pages))
nil)
:key-press (fn [k]
(println :selector :key-press k :type (type k))
(case k
"j" [[::next-frame]]
"k" [[::prev-frame]]
"$" [[::last-frame]]
;["^" "0"] [[::first-frame]]
"^" [[::first-frame]]
"0" [[::first-frame]]
"A" [[::animate-all-frames]]
nil))
(ui/horizontal-layout
(ui/button "<<"
(fn []
[[::prev-frame]]))
(ui/label (format "curr-page: %s, total-pages %s"
(str curr-page) (str total-pages)))
(ui/button ">>"
(fn []
[[::next-frame]]))
(ui/button "Animate!"
(fn []
[[::animate-all-frames]])))))
(defn turn
[state]
(ui/vertical-layout
(ui/horizontal-layout
(ui/label (format "Turn: %d"
(-> state :turn))))
(ui/spacer 25)))
(def states-text { :initial "start"
:waiting-for-movers1 "wait"
:removing-furniture "🛋 moving"
;:removing-furniture "moving"
:waiting-for-painters "wait"
:painting "painting"
:waiting-for-movers2 "wait"
:restoring-furniture "moving"
:finished "done!"})
(comment
(get states-text :initial)
0)
(defn room-state
" show all room states, and highlight the current one
[:initial :waiting-for-movers1 :removing-furniture :waiting-for-painters :painting :waiting-for-movers2 :restoring-furniture :finished])"
[room]
(apply
ui/horizontal-layout
(for [st e/room-states]
(ui/vertical-layout
(ui/spacer 0 10)
(if (not= st (:state room))
;(ui/label (str st))
;(para/paragraph "🛋")
;(ui/label "🛋️"
; (ui/font "Apple Color Emoji" nil)
(ui/horizontal-layout
;(para/paragraph "🛋")
(para/paragraph (str (get states-text st) " ")))
#_(ui/label (str (get states-text st) " ")
(ui/font "Apple Color Emoji" nil))
(ui/with-color [1 0 0]
;(ui/label (str st))
;(ui/label (str (get states-text st) " "))
(para/paragraph
{:text (str (get states-text st) " ")
:style #:text-style {:color [1 0 0]}})))))))
(defn time-remaining-bar
" just time remaining as string of chars "
[n]
(ui/label
(apply str (repeat n "*"))))
(defn workers-present
" show any movers/painters in room"
[r movers painters]
(let [rid (:id r)
workers (->> (concat movers painters)
(filter #(= rid (:at-room %)))
vec)
msg (->> workers
(mapv (fn [x]
(format "%s %d" (:role x) (:id x)))))]
(ui/horizontal-layout
;(ui/label (str workers))
(ui/with-color [0 0 1]
(if-not (empty? msg)
(ui/label msg)
(ui/label " "))))))
(defn room
[r movers painters]
(ui/vertical-layout
(ui/horizontal-layout
(ui/label (format "Room %d:"
(-> r :id)))
(room-state r))
(ui/horizontal-layout
(ui/label (format " :moving1-time-remaining: %d"
(-> r :moving1-time-remaining)))
(time-remaining-bar (-> r :moving1-time-remaining)))
(ui/horizontal-layout
(ui/label (format " :painting-time-remaining: %d"
(-> r :painting-time-remaining)))
(time-remaining-bar (-> r :painting-time-remaining)))
(ui/horizontal-layout
(ui/label (format " :moving2-time-remaining: %d"
(-> r :moving2-time-remaining)))
(time-remaining-bar (-> r :moving2-time-remaining)))
(ui/vertical-layout
#_(ui/spacer 70 20)
(ui/horizontal-layout
(ui/spacer 70 10)
(workers-present r movers painters)))
(ui/spacer 10)))
(defn rooms
" main view: will show all details of room, as well as any movers/painters present "
[state]
(let [{:keys [rooms movers painters]} state]
(apply
ui/vertical-layout
(for [r rooms]
(let [relem (room r movers painters)
bounds (ui/bounds relem)]
[(ui/with-style :membrane.ui/style-stroke
(ui/rectangle (first bounds) (- (second bounds) 2)))
relem])))))
(defn movers
[state]
(apply
ui/vertical-layout
(interpose (ui/spacer 10)
(for [r (-> state :movers)]
(let [roomnum (-> r :at-room)]
(ui/label (format "Mover %d -- In Room: %s"
(-> r :id)
(if roomnum
(str (-> r :at-room))
"---"))))))))
(defn painters
[state]
(apply
ui/vertical-layout
(interpose (ui/spacer 10)
(for [r (-> state :painters)]
(let [roomnum (-> r :at-room)]
(ui/label (format "Painter %d -- In Room: %s"
(-> r :id)
(if roomnum
(str (-> r :at-room))
"---"))))))))
(defn get-frame
" get frame, and prevent overflow "
[n frames]
(let [maxn (count frames)]
;(println :get-frame :n n :maxn maxn)
(if (>= n maxn)
(last frames)
(nth frames n))))
(comment
(get-frame 100 @*sim-state)
0)
(defn render-view
[sim-state *app-state]
(let [framenum (-> @*app-state :frame)
state (case framenum
:last-frame (last sim-state)
(get-frame framenum sim-state))]
(ui/vertical-layout
; curr-page total-pages
(selector (-> @*app-state :frame) (count sim-state))
(turn state)
(rooms state)
(movers state)
(painters state))))
(defn dev-view
" helper: put anything you're working in here in dev
(for prod app, it'll just be another view, composing all your components "
[]
(let [states @*sim-state]
;(selector (-> @*app-state :frame) (count states))
(render-view @*sim-state *app-state)))
(comment
(skia/run #'dev-view)
(def w (skia/run #'dev-view))
((:membrane.skia/repaint w))
@*app-state
0)
(comment
genek.sim2/*state
(skia/run (fn [] (ui/label "hi"))))