/
Main.elm
307 lines (269 loc) · 10.5 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
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
, previousOptions : Maybe 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
| ToggleDimUnknown
| SetRandomSeed Random.Seed
init : () -> ( Model, Cmd Msg )
init _ =
let
options =
{ image = Image.nyan
, windowSize = { width = 2, height = 2 }
, waveSize = { width = 20, height = 20 }
}
in
( { options = options
, previousOptions = Nothing
, 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 )
ToggleDimUnknown ->
( { model | dimUnknown = not model.dimUnknown }, Cmd.none )
SetRandomSeed seed ->
( { model | seed = seed }, 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 "Source Image" ]
-- , [ ( "Waves", Image.waves )
-- , ( "Bars", Image.bars )
-- , ( "Recurse", Image.recurse )
-- , ( "Nyan Cat", Image.nyan )
-- , ( "Composition II", Image.mondrianCompositionIIinRedBlueAndYellow )
-- ]
-- |> List.map
-- (\( name, image ) ->
-- [ { width = 1, height = 1 }
-- , { width = 2, height = 2 }
-- , { width = 3, height = 3 }
-- ]
-- |> List.map
-- (\dimensions ->
-- Html.button [ Events.onClick (Load image dimensions) ] [ Html.text (name ++ "(" ++ String.fromInt dimensions.height ++ "x" ++ String.fromInt dimensions.width ++ ")") ]
-- )
-- |> Html.p []
-- )
-- |> Html.div []
, Image.view [] model.options.image
, Html.details []
[ Html.summary [] [ Html.text "Windows" ]
-- , model.windows
-- |> Grid.view
-- (\window ->
-- Html.div
-- [ css
-- [ Css.border3 (Css.px 1) Css.solid (Css.hex "000")
-- , Css.display Css.inlineBlock
-- , Css.margin (Css.px 5)
-- ]
-- ]
-- [ Image.view [] window ]
-- )
]
, 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.p []
-- [ Html.button [ Events.onClick (Reset { width = 2, height = 2 }) ] [ Html.text "Reset (2x2)" ]
-- , Html.button [ Events.onClick (Reset { width = 5, height = 5 }) ] [ Html.text "Reset (5x5)" ]
-- , Html.button [ Events.onClick (Reset { width = 10, height = 10 }) ] [ Html.text "Reset (10x10)" ]
-- , Html.button [ Events.onClick (Reset { width = 20, height = 20 }) ] [ Html.text "Reset (20x20)" ]
-- , Html.button [ Events.onClick (Reset { width = 50, height = 50 }) ] [ Html.text "Reset (50x50)" ]
-- ]
, Html.p [] [ Html.button [ Events.onClick ToggleDimUnknown ] [ Html.text "Toggle Dimming Uknown Cells (debug)" ] ]
, 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 contents =
Html.h1
[ css
[ Css.fontSize (Css.rem 2)
, Css.lineHeight (Css.rem 2.5)
, Css.marginBottom (Css.rem 0.5)
, Css.fontWeight Css.bold
]
]
contents
h2 : List (Html msg) -> Html msg
h2 contents =
Html.h2
[ css
[ Css.fontSize (Css.rem 1.25)
, Css.lineHeight (Css.rem 1.5)
]
]
contents