generated from Phlox-GL/phlox-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.cirru
436 lines (435 loc) · 18.2 KB
/
compact.cirru
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.4.10)
:modules $ [] |memof/ |lilac/ |respo.calcit/ |respo-ui.calcit/ |phlox/
:entries $ {}
:files $ {}
|app.comp.container $ {}
:defs $ {}
|comp-container $ quote
defn comp-container (store)
let
cursor $ []
states $ :states store
grid $ :grid store
drop-position $ either (:drop-position store) ([] 0 0)
drop-pick $ either (:drop-pick store) ([] 0 0)
dropping $ get-in shapes-variations drop-pick
dropping-cells $ if
some? $ :drop-pick store
-> dropping
map $ fn (x) (complex/add drop-position x)
.to-set
#{}
; println $ :failed? store
container
{} $ :position ([] 0 0)
rect $ {}
:position $ [] -300 -300
:fill $ hslx 0 0 96
:size $ [] 600 600
container ({}) & $ -> (:grid store)
map-indexed $ fn (row-idx row)
-> row $ map-indexed
fn (col-idx cell)
let
p $ [] row-idx col-idx
rect $ {}
:position $ []
* (- col-idx middle-idx) 12
* (- row-idx middle-idx) 12
:size $ [] 12 12
:fill $ cond
.includes? dropping-cells p
hslx 200 10 50
(some? cell)
case-default (:kind cell) (hslx 10 80 10)
:preset $ :color cell
:collapsing $ hslx 30 90 70
true $ hslx 200 10 100
concat-all
comp-button $ {} (:text "\"Reset")
:position $ [] 340 -40
:color $ hslx 0 0 100
:fill $ hslx 0 0 60
:on-pointertap $ fn (e d!) (d! :reset nil)
comp-button $ {} (:text "\"Fullscreen")
:position $ [] 340 80
:color $ hslx 0 0 100
:fill $ hslx 0 0 60
:on-pointertap $ fn (e d!) (js/document.body.requestFullscreen)
if (:failed? store)
text $ {}
:position $ [] 340 0
:text "\"Failed"
:style $ {} (:font-size 24) (:fill "\"red") (:font-family "\"Josefin Sans")
|concat-all $ quote
defn concat-all (xs) (&list:concat & xs)
|middle-idx $ quote
def middle-idx $ * 0.5 (dec grid-size)
:ns $ quote
ns app.comp.container $ :require
phlox.core :refer $ g hslx rect circle text container graphics create-list >>
phlox.comp.button :refer $ comp-button
phlox.comp.drag-point :refer $ comp-drag-point
"\"shortid" :as shortid
respo-ui.core :as ui
memof.alias :refer $ memof-call
app.config :refer $ grid-size
phlox.complex :as complex
app.schema :refer $ shapes-variations
|app.config $ {}
:defs $ {}
|dev? $ quote
def dev? $ = "\"dev" (get-env "\"mode" "\"release")
|grid-size $ quote
def grid-size $ js/parseInt
either (get-env "\"size") "\"31"
|site $ quote
def site $ {} (:dev-ui "\"http://localhost:8100/main.css") (:release-ui "\"http://cdn.tiye.me/favored-fonts/main.css") (:cdn-url "\"http://cdn.tiye.me/phlox/") (:title "\"Phlox") (:icon "\"http://cdn.tiye.me/logo/quamolit.png") (:storage-key "\"phlox")
|tick-interval $ quote
def tick-interval $ js/parseInt
either (get-env "\"interval") "\"300"
:ns $ quote (ns app.config)
|app.main $ {}
:defs $ {}
|*dispatch-fn $ quote (defatom *dispatch-fn dispatch!)
|*store $ quote (defatom *store schema/store)
|dispatch! $ quote
defn dispatch! (op op-data)
when
and dev? $ not= op :states
println "\"dispatch!" op op-data
let
op-id $ shortid/generate
op-time $ js/Date.now
reset! *store $ updater @*store op op-data op-id op-time
|main! $ quote
defn main! () (; js/console.log PIXI)
if dev? $ load-console-formatter!
-> (new FontFaceObserver/default "\"Josefin Sans") (.!load)
.!then $ fn (event) (render-app!)
add-watch *store :change $ fn (store prev) (render-app!)
js/window.addEventListener "\"resize" $ fn (e) (render-app!)
js/setInterval
fn () $ if
not (:paused? @*store) (:failed? @*store)
@*dispatch-fn :tick nil
, config/tick-interval
js/window.addEventListener "\"keydown" $ fn (event)
case-default (.-key event)
do
println "\"Event:" $ .-key event
, nil
"\"ArrowUp" $ @*dispatch-fn :up nil
"\"ArrowDown" $ if (.-shiftKey event) (@*dispatch-fn :down-most nil) (@*dispatch-fn :down nil)
"\"ArrowLeft" $ if (.-shiftKey event) (@*dispatch-fn :left-most nil) (@*dispatch-fn :left nil)
"\"ArrowRight" $ if (.-shiftKey event) (@*dispatch-fn :right-most nil) (@*dispatch-fn :right nil)
"\" " $ @*dispatch-fn :down-most nil
"\"Enter" $ @*dispatch-fn :toggle-pause nil
println "\"App Started"
|reload! $ quote
defn reload! () $ if (nil? build-errors)
do (println "\"Code updated.") (clear-phlox-caches!) (reset! *dispatch-fn dispatch!) (remove-watch *store :change)
add-watch *store :change $ fn (store prev) (render-app!)
render-app!
hud! "\"ok~" "\"Ok"
hud! "\"error" build-errors
|render-app! $ quote
defn render-app! (? arg)
render! (comp-container @*store) @*dispatch-fn $ or arg ({})
:ns $ quote
ns app.main $ :require ("\"pixi.js" :as PIXI)
phlox.core :refer $ render! clear-phlox-caches!
app.comp.container :refer $ comp-container
app.schema :as schema
app.config :refer $ dev?
app.config :as config
"\"shortid" :as shortid
app.updater :refer $ updater
"\"fontfaceobserver-es" :as FontFaceObserver
"\"./calcit.build-errors" :default build-errors
"\"bottom-tip" :default hud!
|app.schema $ {}
:defs $ {}
|detection-pattern-5 $ quote
def detection-pattern-5 $ [] ([] p1 p1 p1 p1 p1) ([] p1 p0 p0 p0 p1) ([] p1 p0 p1 p0 p1) ([] p1 p0 p0 p0 p1) ([] p1 p1 p1 p1 p1)
|detection-pattern-7 $ quote
def detection-pattern-7 $ [] ([] p1 p1 p1 p1 p1 p1 p1) ([] p1 p0 p0 p0 p0 p0 p1) ([] p1 p0 p1 p1 p1 p0 p1) ([] p1 p0 p1 p1 p1 p0 p1) ([] p1 p0 p1 p1 p1 p0 p1) ([] p1 p0 p0 p0 p0 p0 p1) ([] p1 p1 p1 p1 p1 p1 p1)
|gen-qrcode-grid $ quote
defn gen-qrcode-grid (size)
map (range size)
fn (row-idx)
map (range size)
fn (col-idx)
let
r-row-idx $ - size row-idx 1
r-col-idx $ - size col-idx 1
dx $ - col-idx (- size 9)
dy $ - row-idx (- size 9)
cond
and (< row-idx 7) (< col-idx 7)
get-in detection-pattern-7 $ [] row-idx col-idx
(and (<= row-idx 7) (= col-idx 7))
, p0
(and (<= col-idx 7) (= row-idx 7))
, p0
(and (< row-idx 7) (< r-col-idx 7))
get-in detection-pattern-7 $ [] row-idx r-col-idx
(and (<= row-idx 7) (= r-col-idx 7))
, p0
(and (<= r-col-idx 7) (= row-idx 7))
, p0
(and (< r-row-idx 7) (< col-idx 7))
get-in detection-pattern-7 $ [] r-row-idx col-idx
(and (<= r-row-idx 7) (= col-idx 7))
, p0
(and (<= col-idx 7) (= r-row-idx 7))
, p0
(and (>= dx 0) (< dx 5) (>= dy 0) (< dy 5))
get-in detection-pattern-5 $ [] dy dx
true nil
|p0 $ quote
def p0 $ {} (:kind :preset) (:color 0xffffff)
|p1 $ quote
def p1 $ {} (:kind :preset) (:color 0x000000)
|shapes-variations $ quote
def shapes-variations $ []
[]
[][] (0 -1) (0 0) (0 1) (0 2)
[][] (-1 0) (0 0) (1 0) (2 0)
[]
[][] (-1 0) (0 -1) (0 0) (0 1)
[][] (-1 0) (0 -1) (0 0) (1 0)
[][] (0 -1) (0 0) (0 1) (1 0)
[][] (-1 0) (0 0) (0 1) (1 0)
[] $ [][] (-1 0) (-1 1) (0 0) (0 1)
[]
[][] (-1 -1) (0 -1) (0 0) (0 1)
[][] (-1 0) (-1 1) (0 0) (1 0)
[][] (1 1) (0 -1) (0 0) (0 1)
[][] (-1 0) (1 -1) (0 0) (1 0)
[]
[][] (-1 1) (0 -1) (0 0) (0 1)
[][] (1 1) (-1 0) (0 0) (1 0)
[][] (1 -1) (0 -1) (0 0) (0 1)
[][] (-1 -1) (-1 0) (0 0) (1 0)
[]
[][] (-1 0) (-1 1) (0 -1) (0 0)
[][] (-1 0) (0 0) (0 1) (1 1)
[]
[][] (-1 -1) (-1 0) (0 0) (0 1)
[][] (-1 1) (0 1) (0 0) (1 0)
|store $ quote
def store $ {}
:states $ {}
:cursor $ []
:grid $ gen-qrcode-grid grid-size
:failed? false
:paused? false
:score 0
:drop-pick nil
:drop-position nil
:ns $ quote
ns app.schema $ :require
app.config :refer $ grid-size
|app.updater $ {}
:defs $ {}
|change-shape $ quote
defn change-shape (store)
let
drop-pick $ :drop-pick store
grid $ :grid store
pos $ :drop-position store
shape-variants $ nth shapes-variations (first drop-pick)
prev $ last drop-pick
next-pick $ [] (first drop-pick)
if
< (inc prev) (count shape-variants)
inc prev
, 0
if (valid-put? next-pick pos grid) (assoc store :drop-pick next-pick) store
|collapse-column $ quote
defn collapse-column (acc col collapsed)
if (empty? col)
if (> collapsed 0)
concat (repeat nil collapsed) acc
, acc
let
cursor $ last col
cond
nil? cursor
recur (prepend acc nil) (butlast col) collapsed
(= :collapsing (:kind cursor))
recur acc (butlast col) (inc collapsed)
(= :preset (:kind cursor))
if (> collapsed 0)
recur
concat ([] cursor) (repeat nil collapsed) acc
butlast col
, 0
recur (prepend acc cursor) (butlast col) collapsed
true $ recur (prepend acc cursor) (butlast col) collapsed
|collapse-grid $ quote
defn collapse-grid (grid)
if
any? grid $ fn (row)
any? row $ fn (cell)
= :collapsing $ :kind cell
-> grid (flip-grid)
map $ fn (col)
collapse-column ([]) col 0
flip-grid
, grid
|contains-in? $ quote
defn contains-in? (xs path)
if (empty? path) true $ let
p0 $ first path
cond
list? xs
if (number? p0)
if (contains? xs p0)
recur (nth xs p0) (rest path)
, false
, false
(map? xs)
if (contains? xs p0)
recur (&map:get xs p0) (rest path)
, false
true false
|detect-collapse $ quote
defn detect-collapse (grid)
apply-args
[]
, grid 0
fn (acc xs collapsed-size)
cond
empty? xs
if (= 0 collapsed-size) acc $ &list:concat
repeat (repeat nil grid-size) collapsed-size
, acc
(any? (last xs) (fn (x) (and (map? x) (= (&map:get x :kind) :preset))))
let
next-acc $ if (= 0 collapsed-size) acc
&list:concat
repeat (repeat nil grid-size) collapsed-size
, acc
recur
prepend next-acc $ last xs
butlast xs
, 0
(every? (last xs) (fn (x) (and (map? x) (= (&map:get x :kind) :filled))))
recur acc (butlast xs) (inc collapsed-size)
true $ recur
prepend acc $ last xs
butlast xs
, collapsed-size
|drop-shape $ quote
defn drop-shape (store)
let
next-pos $ complex/add (:drop-position store) ([] 1 0)
if
valid-put? (:drop-pick store) next-pos $ :grid store
-> store (assoc :drop-position next-pos) (update :grid collapse-grid)
let
pick $ :drop-pick store
pos $ :drop-position store
grid $ :grid store
real-cells $ -> (get-in shapes-variations pick)
map $ fn (cell) (complex/add cell pos)
new-grid $ mark-collapsing
foldl (.to-list real-cells) grid $ fn (acc pos)
assoc-in acc pos $ {} (:kind :filled)
-> store
assoc :drop-position $ [] 1
* 0.5 $ dec grid-size
assoc :drop-pick $ let
a $ rand-int (count shapes-variations)
b $ rand-int
count $ nth shapes-variations a
[] a b
assoc :grid new-grid
|flip-grid $ quote
defn flip-grid (grid)
let
size $ count grid
map (range size)
fn (row-idx)
map (range size)
fn (col-idx)
get-in grid $ [] col-idx row-idx
|mark-collapsing $ quote
defn mark-collapsing (grid)
map grid $ fn (row)
if (every? row some?)
map row $ fn (cell)
if
= :filled $ :kind cell
{} $ :kind :collapsing
, cell
, row
|move-shape $ quote
defn move-shape (store step)
let
next-pos $ complex/add (:drop-position store) step
if
valid-put? (:drop-pick store) next-pos $ :grid store
assoc store :drop-position next-pos
, store
|quick-move $ quote
defn quick-move (store step)
apply-args
[] 0 0
fn (at)
let
pos $ complex/add (:drop-position store) at
next-pos $ complex/add pos step
if
valid-put? (:drop-pick store) next-pos $ :grid store
recur $ complex/add at step
assoc store :drop-position pos
|updater $ quote
defn updater (store op op-data op-id op-time)
case-default op
do (println "\"unknown op" op op-data) store
:states $ update-states store op-data
:hydrate-storage op-data
:tick $ cond
or (:failed? store) (:paused? store)
, store
(nil? (:drop-pick store))
-> store
assoc :drop-position $ [] 1
* 0.5 $ dec grid-size
assoc :drop-pick $ let
a $ rand-int (count shapes-variations)
b $ rand-int
count $ nth shapes-variations a
[] a b
(not (valid-put? (:drop-pick store) (:drop-position store) (:grid store)))
assoc store :failed? true
true $ drop-shape store
:up $ change-shape store
:left $ move-shape store ([] 0 -1)
:right $ move-shape store ([] 0 1)
:down $ move-shape store ([] 1 0)
:reset schema/store
:down-most $ quick-move store ([] 1 0)
:left-most $ quick-move store ([] 0 -1)
:right-most $ quick-move store ([] 0 1)
:toggle-pause $ update store :paused? not
|valid-put? $ quote
defn valid-put? (pick pos grid)
let
real-cells $ -> (get-in shapes-variations pick)
map $ fn (cell) (complex/add cell pos)
every? real-cells $ fn (p)
and (contains-in? grid p)
nil? $ get-in grid p
:ns $ quote
ns app.updater $ :require
phlox.cursor :refer $ [] update-states
app.schema :refer $ shapes-variations
app.schema :as schema
app.config :refer $ grid-size
phlox.complex :as complex
"\"@calcit/std" :refer $ rand rand-int