This repository was archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.elm
275 lines (213 loc) · 5.06 KB
/
Piece.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
module Piece
exposing
( Model
, Msg(..)
, init
, initWithInfo
, update
, view
)
-- import Easing exposing (ease, easeOutQuint, float)
-- import Effects exposing (Effects)
import Html exposing (Html)
import Svg exposing (..)
import Svg.Attributes
exposing
( alignmentBaseline
, fill
, fontSize
, height
, points
, stroke
, strokeWidth
, textAnchor
, version
, viewBox
, width
, x
, y
)
import Color
import AnimationFrame
import Matrix exposing (Location)
import Time exposing (Time, second, millisecond)
import Style
import Style.Properties exposing (..)
import Debug exposing (log)
-- MODEL
type alias Pixels =
Float
type alias PieceNumber =
Int
type Role
= Head
| Middle
| Tail
| Unassigned
type alias Model =
{ role : Role
, location : Location
, pieceNumber : PieceNumber
, sideSize : Pixels
, svgStyle : Style.Animation
}
init : ( Model, Cmd Msg )
init =
( { role = Unassigned
, location = ( 1, 1 )
, pieceNumber = 1
, sideSize = 44
, svgStyle =
Style.init []
}
, Cmd.none
)
initWithInfo : PieceNumber -> Pixels -> Location -> ( Model, Cmd Msg )
initWithInfo pieceNumber sideSize location =
let
m =
{ role = Unassigned
, location = location
, pieceNumber = pieceNumber
, sideSize = sideSize
, svgStyle = Style.init []
}
model =
{ m | svgStyle = (setSvgStyle m) }
in
( model
, Cmd.none
)
subscriptions : Model -> Sub Msg
subscriptions model =
let
dummy =
log "subscriptions" model.location
in
AnimationFrame.times Animate
-- UPDATE
type Msg
= Show
| Animate Time
| Move Location
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Show ->
( model, Cmd.none )
Animate time ->
( { model
| svgStyle = Style.tick time model.svgStyle
}
, Cmd.none
)
Move location ->
let
newModel =
moveLoc location model
newModel2 =
{ newModel
| svgStyle =
Style.animate
|> Style.to (getSvgValues newModel)
|> Style.on (setSvgStyle model)
}
in
( newModel2, Cmd.none )
moveLoc : Location -> Model -> Model
moveLoc delta model =
let
( dx, dy ) =
delta
( x, y ) =
model.location
newLocation =
( x + dx, y + dy )
in
{ model | location = newLocation }
getSvgValues : Model -> List (Property Float a)
getSvgValues model =
let
( xloc, yloc ) =
model.location
pixelsX =
model.sideSize * (toFloat xloc)
pixelsY =
model.sideSize * (toFloat yloc)
in
[ X pixelsX
, Y pixelsY
]
setSvgStyle : Model -> Style.Animation
setSvgStyle model =
Style.init (getSvgValues model)
-- VIEW
edgeThickness : number
edgeThickness =
3
darkBrown : String
darkBrown =
"saddlebrown"
renderPiece : Model -> Html Msg
renderPiece model =
let
sideSize =
model.sideSize
( locX, locY ) =
model.location
edgeRatio =
(edgeThickness * sideSize) / 100
plusIndent =
toString edgeRatio
minusIndent =
toString (sideSize - edgeRatio)
whole =
toString sideSize
half =
toString (sideSize / 2.0)
narrow =
toString (sideSize / 10.0)
textDownMore =
toString (sideSize / 1.8)
polyPoints =
half
++ " "
++ plusIndent
++ ", "
++ minusIndent
++ " "
++ half
++ ", "
++ half
++ " "
++ minusIndent
++ ", "
++ plusIndent
++ " "
++ half
polys =
polygon
[ fill "white"
, points polyPoints
, stroke "indianred"
, strokeWidth (toString edgeRatio)
]
[]
myText =
text'
[ x half
, y textDownMore
, fill "black"
, fontSize "20"
, alignmentBaseline "middle"
, textAnchor "middle"
]
[ text (toString model.pieceNumber) ]
in
Svg.svg (Style.renderAttr model.svgStyle)
[ polys
, myText
]
view : Model -> Html Msg
view model =
renderPiece model