/
Main.elm
457 lines (406 loc) · 15.8 KB
/
Main.elm
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
module Main exposing (..)
import Adjacency
import Array
import Browser
import Color.Transparent as Color
import Css
import Css.Reset as Reset
import Dict exposing (Dict)
import Grid exposing (Grid)
import Heap
import Html as RootHtml
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attributes exposing (css, style)
import Html.Styled.Events as Events
import Image exposing (Image)
import Murmur3
import Process
import Random
import Set exposing (Set)
import Task
import Wave exposing (Wave)
type alias Model =
{ -- OPTIONS
options : Options
, draftOptions : Options
-- DISPLAY OPTIONS
, dimUnknown : Bool
-- INTERNAL STATE
, running : Bool
, wave : Wave Color.Color Int
, seed : Random.Seed
}
type alias Options =
{ image : Image
, windowSize : { width : Int, height : Int }
, waveSize : { width : Int, height : Int }
}
type Msg
= Start
| Stop
| Step
| KeepRunning
| Reset
| SetDimUnknown Bool
| SetRandomSeed Random.Seed
| SetWaveSizeWidth String
| SetWaveSizeHeight String
| SetWindowSize { width : Int, height : Int }
| SetImage Image
| ResetDraftOptions
| ApplyOptions
init : () -> ( Model, Cmd Msg )
init _ =
let
options =
{ image = Image.nyan
, windowSize = { width = 2, height = 2 }
, waveSize = { width = 20, height = 20 }
}
in
( { options = options
, draftOptions = options
, dimUnknown = True
, running = False
, wave =
Wave.load
{ windowSize = options.windowSize
, waveSize = options.waveSize
, hash = imageHash
}
options.image
, seed = Random.initialSeed 0
}
, Random.generate SetRandomSeed Random.independentSeed
)
imageHash : Image -> Int
imageHash =
Grid.toArrays
>> Array.foldr
(\row rowString ->
Array.foldr (\value string -> Color.toRGBAString value ++ string)
rowString
row
)
""
>> Murmur3.hashString 0
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Step ->
let
( newWave, newSeed ) =
Wave.step model.seed model.wave
in
( { model | wave = newWave, seed = newSeed }
, Cmd.none
)
Start ->
update KeepRunning { model | running = True }
Stop ->
( { model | running = False }, Cmd.none )
KeepRunning ->
if model.running then
model
|> update Step
|> Tuple.mapSecond
(\cmd ->
Cmd.batch
[ Task.perform (\_ -> KeepRunning) (Process.sleep 0)
, cmd
]
)
else
( model, Cmd.none )
Reset ->
( { model
| wave =
Wave.load
{ windowSize = model.options.windowSize
, waveSize = model.options.waveSize
, hash = imageHash
}
model.options.image
}
, Cmd.none
)
SetDimUnknown dimUnknown ->
( { model | dimUnknown = dimUnknown }, Cmd.none )
SetRandomSeed seed ->
( { model | seed = seed }, Cmd.none )
SetWaveSizeWidth input ->
case String.toInt input of
Just width ->
let
oldDraft =
model.draftOptions
oldWaveSize =
model.draftOptions.waveSize
in
( { model | draftOptions = { oldDraft | waveSize = { oldWaveSize | width = width } } }, Cmd.none )
Nothing ->
( model, Cmd.none )
SetWaveSizeHeight input ->
case String.toInt input of
Just height ->
let
oldDraft =
model.draftOptions
oldWaveSize =
model.draftOptions.waveSize
in
( { model | draftOptions = { oldDraft | waveSize = { oldWaveSize | height = height } } }, Cmd.none )
Nothing ->
( model, Cmd.none )
SetWindowSize windowSize ->
let
oldDraft =
model.draftOptions
in
( { model | draftOptions = { oldDraft | windowSize = windowSize } }, Cmd.none )
SetImage image ->
let
oldDraft =
model.draftOptions
in
( { model | draftOptions = { oldDraft | image = image } }, Cmd.none )
ResetDraftOptions ->
( { model | draftOptions = model.options }
, Cmd.none
)
ApplyOptions ->
( { model
| options = model.draftOptions
, running = False
, wave =
Wave.load
{ windowSize = model.draftOptions.windowSize
, waveSize = model.draftOptions.waveSize
, hash = imageHash
}
model.draftOptions.image
}
, Cmd.none
)
main : Program () Model Msg
main =
Browser.element
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
view : Model -> RootHtml.Html Msg
view model =
Html.toUnstyled <|
Html.div
[ css
[ Css.fontFamily Css.sansSerif
, Css.margin2 (Css.rem 1) Css.auto
, Css.padding2 (Css.rem 1.5) (Css.rem 2)
, Css.width (Css.pct 80)
, Css.backgroundColor (Css.hex "F0F8FF")
]
]
[ Reset.meyerV2
, Reset.borderBoxV201408
, h1 [ Html.text "Wave Function Collapse" ]
, h2 [ Html.text "Options" ]
, Html.form
[ css [ Css.displayFlex ]
, Events.onSubmit ApplyOptions
]
[ fieldset
[ legend [ Html.text "Wave" ]
, Html.text "Wave Size"
, Html.div []
[ Html.input
[ Attributes.type_ "number"
, Attributes.min "1"
, Attributes.value (String.fromInt model.draftOptions.waveSize.width)
, Events.onInput SetWaveSizeWidth
]
[]
, Html.text "×"
, Html.input
[ Attributes.type_ "number"
, Attributes.min "1"
, Attributes.value (String.fromInt model.draftOptions.waveSize.height)
, Events.onInput SetWaveSizeHeight
]
[]
]
, Html.text "Window Size"
, [ ( { width = 1, height = 1 }, "absurd" )
, ( { width = 2, height = 2 }, "default" )
, ( { width = 3, height = 3 }, "expensive" )
]
|> List.map
(\( size, comment ) ->
Html.label []
[ Html.input
[ Attributes.type_ "radio"
, Attributes.attribute "radiogroup" "windowsize"
, Attributes.checked (model.draftOptions.windowSize == size)
, Events.onCheck (\_ -> SetWindowSize size)
]
[]
, Html.text (String.fromInt size.width ++ "×" ++ String.fromInt size.height ++ " (" ++ comment ++ ")")
]
)
|> Html.div []
, Html.text "Image"
, [ ( Image.bars, "Bars" )
, ( Image.mondrianCompositionIIinRedBlueAndYellow, "Composition II" )
, ( Image.nyan, "Nyan Cat" )
, ( Image.recurse, "RC Logo" )
, ( Image.waves, "Waves" )
]
|> List.map
(\( image, name ) ->
Html.label []
[ Html.input
[ Attributes.type_ "radio"
, Attributes.attribute "radiogroup" "image"
, Attributes.checked (model.draftOptions.image == image)
, Events.onCheck (\_ -> SetImage image)
]
[]
, Html.text name
]
)
|> Html.div []
, if model.draftOptions /= model.options then
Html.div []
[ Html.button
[ Attributes.type_ "button" -- submit is default
, Events.onClick ResetDraftOptions
]
[ Html.text "Reset Options" ]
, Html.button
[ Attributes.type_ "submit"
, Events.onClick ApplyOptions
]
[ Html.text "Apply" ]
]
else
Html.text ""
]
, fieldset
[ legend [ Html.text "View" ]
, Html.label []
[ Html.input
[ Attributes.type_ "checkbox"
, Attributes.checked model.dimUnknown
, Events.onCheck SetDimUnknown
]
[]
, Html.text "Toggle Dimming Unknown Cells"
]
]
]
, h2 [ Html.text "Wave" ]
, Html.p []
[ if model.running then
Html.button [ Events.onClick Stop ] [ Html.text "Stop" ]
else
Html.button [ Events.onClick Start ] [ Html.text "Start" ]
, Html.button [ Events.onClick Step ] [ Html.text "Step" ]
, Html.button [ Events.onClick Reset ] [ Html.text "Reset" ]
]
, Html.div [ css [ Css.displayFlex ] ]
[ Image.view [] model.options.image
, Wave.view
(\windows cell ->
case cell of
Wave.Collapsed tile ->
case Dict.get tile windows |> Maybe.andThen Grid.topLeft of
Just color ->
Image.viewColor [] color
Nothing ->
Html.text "invalid index"
Wave.Open indexes ->
let
{ reds, blues, greens, opacities } =
indexes
|> Set.toList
|> List.filterMap (\i -> Dict.get i windows)
|> List.filterMap Grid.topLeft
|> List.foldl
(\color soFar ->
let
rgba =
Color.toRGBA color
in
{ reds = rgba.red :: soFar.reds
, blues = rgba.blue :: soFar.blues
, greens = rgba.green :: soFar.greens
, opacities = Color.opacityToFloat rgba.alpha :: soFar.opacities
}
)
{ reds = [], greens = [], blues = [], opacities = [] }
average items =
List.sum items / toFloat (List.length items)
in
if Set.isEmpty indexes then
Html.td
[ css
[ Css.lineHeight (Css.px 10)
, Css.fontSize (Css.px 10)
, Css.textAlign Css.center
]
, Attributes.width 10
, Attributes.height 10
]
[ Html.text "×" ]
else
Image.viewColor
[ Attributes.attribute "data-count" (String.fromInt (Set.size indexes))
, style "opacity"
(if model.dimUnknown then
"0.5"
else
"1"
)
]
(Color.fromRGBA
{ red = average reds
, green = average greens
, blue = average blues
, alpha = Color.customOpacity (average opacities)
}
)
)
model.wave
]
]
h1 : List (Html msg) -> Html msg
h1 =
Html.h1
[ css
[ Css.fontSize (Css.rem 2)
, Css.lineHeight (Css.rem 2.5)
, Css.marginBottom (Css.rem 0.5)
, Css.fontWeight Css.bold
]
]
h2 : List (Html msg) -> Html msg
h2 =
Html.h2
[ css
[ Css.fontSize (Css.rem 1.25)
, Css.lineHeight (Css.rem 1.5)
]
]
fieldset : List (Html msg) -> Html msg
fieldset =
Html.fieldset
[ css
[ Css.border3 (Css.px 1) Css.solid (Css.hex "000")
, Css.padding (Css.px 10)
, Css.margin (Css.px 5)
]
]
legend : List (Html msg) -> Html msg
legend =
Html.legend [ css [ Css.padding2 (Css.px 3) (Css.px 6) ] ]