-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.elm
More file actions
141 lines (108 loc) · 2.52 KB
/
Main.elm
File metadata and controls
141 lines (108 loc) · 2.52 KB
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
module Main exposing (main)
import Dict exposing (Dict)
import List.Extra as List
import Svg exposing (Svg)
import Svg.Attributes exposing (..)
puzzle =
{ draftMode = True
, piecesX = 12
, piecesY = 6
, pixelsPerCell = 50
}
params =
{ width = puzzle.piecesX * puzzle.pixelsPerCell
, height = puzzle.piecesY * puzzle.pixelsPerCell
}
type alias Point =
{ x : Int
, y : Int
}
main : Svg msg
main =
let
grid =
rectangularGrid puzzle.piecesX puzzle.piecesY
markers =
Dict.values grid
|> List.map drawMarker
in
canvas params.width
params.height
markers
rectangularGrid : Int -> Int -> Dict ( Int, Int ) Point
rectangularGrid nx ny =
let
indicesX =
List.range 0 nx
indicesY =
List.range 0 ny
indices =
List.lift2 Tuple.pair indicesX indicesY
in
indices
|> List.map
(\( ix, iy ) ->
( ( ix, iy )
, { x = ix * puzzle.pixelsPerCell
, y = iy * puzzle.pixelsPerCell
}
)
)
|> Dict.fromList
-- DRAWING FUNCTIONS
drawMarker : Point -> Svg msg
drawMarker { x, y } =
Svg.circle
[ cx <| String.fromInt x
, cy <| String.fromInt y
, r "2"
, stroke "#666"
, fillOpacity "0"
]
[]
-- CANVAS HELPER
canvas : Int -> Int -> List (Svg msg) -> Svg msg
canvas w h children =
let
hStr =
String.fromInt h
wStr =
String.fromInt w
tileSize =
10
xnumtiles =
w // tileSize
ynumtiles =
h // tileSize
tiles =
List.lift2 (tile tileSize)
(List.range 0 <| xnumtiles - 1)
(List.range 0 <| ynumtiles - 1)
in
Svg.svg
[ width wStr
, height hStr
, viewBox <| "0 0 " ++ wStr ++ " " ++ hStr
]
(if puzzle.draftMode then
Svg.g [] tiles :: children
else
children
)
tile : Int -> Int -> Int -> Svg msg
tile size xc yc =
let
col =
if modBy 2 (xc + yc) == 0 then
"#eee"
else
"#fff"
in
Svg.rect
[ x (String.fromInt (xc * size))
, y (String.fromInt (yc * size))
, width (String.fromInt size)
, height (String.fromInt size)
, fill col
]
[]