-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceView.elm
286 lines (222 loc) · 7.11 KB
/
PieceView.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
module PieceView exposing (..)
import Html exposing (Html)
import Svg exposing (Svg, svg, rect, path, Attribute, ellipse, g)
import Svg.Attributes exposing (..)
import Svg.Events exposing (onClick)
import Msg exposing (Msg(..))
import Model exposing (Piece(..), Rack, Shape(..), Colour(..), Pattern(..), TurnState(..))
rackWidth =
250
rackHeight =
600
rackWidthString =
toString rackWidth
rackHeightString =
toString rackHeight
renderRack : TurnState -> Maybe Piece -> Rack -> Html Msg
renderRack turnState selected rack =
svg
[ width rackWidthString
, height rackHeightString
, viewBox ("0 0 " ++ rackWidthString ++ " " ++ rackHeightString)
]
<| [ Svg.defs []
[ Svg.linearGradient [ id "RedGradient", x1 "0", x2 "1", y1 "0", y2 "1" ]
[ Svg.stop [ offset "12.5%", stopColor (colourToString Red) ] []
, Svg.stop [ offset "87.5%", stopColor (colourToString Red), stopOpacity "0" ] []
]
, Svg.linearGradient [ id "GreenGradient", x1 "0", x2 "1", y1 "0", y2 "1" ]
[ Svg.stop [ offset "12.5%", stopColor (colourToString Green) ] []
, Svg.stop [ offset "87.5%", stopColor (colourToString Green), stopOpacity "0" ] []
]
, Svg.linearGradient [ id "BlueGradient", x1 "0", x2 "1", y1 "0", y2 "1" ]
[ Svg.stop [ offset "12.5%", stopColor (colourToString Blue) ] []
, Svg.stop [ offset "87.5%", stopColor (colourToString Blue), stopOpacity "0" ] []
]
]
, Svg.rect
[ x "0"
, y "0"
, width rackWidthString
, height rackHeightString
, stroke "black"
, strokeWidth "2"
, fillOpacity "0"
]
[]
]
++ renderPieces turnState selected rack
renderPieces : TurnState -> Maybe Piece -> Rack -> List (Svg Msg)
renderPieces turnState selected rack =
let
isSelected =
case selected of
Just selectedPiece ->
(==) selectedPiece
Nothing ->
always False
in
Model.piecePossibilities
|> List.indexedMap
(\index piece ->
renderPieceInRack turnState
(indexToPosition index)
(isSelected piece)
(Model.isInRack piece rack)
piece
)
nullSvg =
Svg.text ""
renderPieceInRack : TurnState -> ( Float, Float ) -> Bool -> Bool -> Piece -> Svg Msg
renderPieceInRack turnState (( xPos, yPos ) as point) isSelected isPresent piece =
if isPresent then
let
extraAttributes =
if turnState == SelectPiece then
[ onClick (Select piece) ]
else
[]
renderedPiece =
renderPiece extraAttributes point piece
in
if isSelected then
g []
[ renderedPiece
, selectedIndicator point
]
else
renderedPiece
else
nullSvg
renderPiece : List (Svg.Attribute Msg) -> ( Float, Float ) -> Piece -> Svg Msg
renderPiece extraAttributes ( xPos, yPos ) (Piece shape colour pattern) =
let
pieceAtrributes =
case pattern of
Full ->
[ fill (colourToString colour) ]
StrokeOnly ->
[ fillOpacity "0.0"
, stroke (colourToString colour)
, strokeWidth "4"
]
Gradient ->
[ fill (colourToGradientString colour) ]
in
case shape of
Square ->
rect
(extraAttributes
++ pieceAtrributes
++ [ x (toString xPos)
, y (toString yPos)
, width pieceWidthString
, height pieceHeightString
]
)
[]
Circle ->
ellipse
(extraAttributes
++ pieceAtrributes
++ [ cx (toString (xPos + halfPieceWidth))
, cy (toString (yPos + halfPieceHeight))
, rx halfPieceWidthString
, ry halfPieceHeightString
]
)
[]
Triangle ->
Svg.path
(extraAttributes
++ pieceAtrributes
++ [ d
("M "
++ toString (xPos + halfPieceWidth)
++ " "
++ toString yPos
++ " l "
++ (toString (-pieceWidth / 2))
++ " "
++ pieceHeightString
++ " l "
++ pieceWidthString
++ " 0 Z"
)
]
)
[]
colourToString colour =
case colour of
Red ->
"#FF4136"
Green ->
"#2ECC40"
Blue ->
"#0074D9"
colourToGradientString colour =
case colour of
Red ->
"url(#RedGradient)"
Green ->
"url(#GreenGradient)"
Blue ->
"url(#BlueGradient)"
selectedIndicator ( xPos, yPos ) =
rect
[ fillOpacity "0.0"
, x (toString (xPos - 1.5))
, y (toString (yPos - 1.5))
, width (toString (pieceWidth + 3))
, height (toString (pieceHeight + 3))
, stroke "#FFDC00"
, strokeWidth "2"
]
[]
left : Float
left =
pieceWidth - spacing
center : Float
center =
2 * pieceWidth
right : Float
right =
3 * pieceWidth + spacing
spacing : Float
spacing =
15
pieceWidth : Float
pieceWidth =
50
pieceWidthString =
toString pieceWidth
pieceHeight : Float
pieceHeight =
50
pieceHeightString =
toString pieceHeight
halfPieceWidth : Float
halfPieceWidth =
pieceWidth / 2
halfPieceWidthString =
toString halfPieceWidth
halfPieceHeight : Float
halfPieceHeight =
pieceHeight / 2
halfPieceHeightString =
toString halfPieceHeight
indexToPosition : Int -> ( Float, Float )
indexToPosition index =
let
x =
case index % 3 of
0 ->
left
1 ->
center
_ ->
right
y =
toFloat (index // 3) * (pieceHeight + spacing) + spacing
in
( x, y )