-
Notifications
You must be signed in to change notification settings - Fork 5
/
map_example.elm
191 lines (137 loc) · 3.6 KB
/
map_example.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
module MapExample exposing (main)
import Browser
import Dict
import Hexagons.Hex exposing (..)
import Hexagons.Layout exposing (..)
import Hexagons.Map exposing (..)
import Html exposing (Html)
import Json.Decode as Json
import String
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Svg.Events exposing (..)
import Svg.Lazy exposing (lazy, lazy2, lazy3)
import Task
main : Program () Model Msg
main =
Browser.sandbox
{ init = init
, view = view
, update = update
}
-- MODEL
-- The full application state of our todo app.
type alias Model =
{ map : Map
, greenCells : List Hash
}
emptyModel : Model
emptyModel =
{ map = rectangularPointyTopMap 10 10
, greenCells = []
}
init : Model
init =
emptyModel
-- UPDATE
{-| Users of our app can trigger messages by clicking and typing. These
messages are fed into the `update` function as they occur, letting us react
to them.
-}
type Msg
= NoOp
| SetGreen Hash
-- How we update our Model on a given Msg?
update : Msg -> Model -> Model
update msg model =
case msg of
NoOp ->
model
SetGreen cell ->
{ model | greenCells = model.greenCells ++ [ cell ] }
-- VIEW
cellWidth =
20.0
cellHeight =
20.0
svgWidth =
500
svgHeight =
500
layout =
{ orientation = Hexagons.Layout.orientationLayoutPointy
, size = ( 20.0, 20.0 )
, origin = ( 0.0, 0.0 )
}
viewBoxStringCoords : String
viewBoxStringCoords =
String.fromFloat (-cellWidth + cellWidth * 0.1)
++ " "
++ String.fromFloat -(cellHeight + 0)
++ " "
++ String.fromInt svgWidth
++ " "
++ String.fromInt svgHeight
view : Model -> Html Msg
view model =
svg
[ version "1.1"
, x "0"
, y "0"
, Svg.Attributes.height (String.fromInt svgHeight)
, Svg.Attributes.width (String.fromInt svgWidth)
, viewBox viewBoxStringCoords
]
[ lazy hexGrid model
]
hexGrid : Model -> Html Msg
hexGrid model =
let
toSvg : Hash -> String -> Svg Msg
toSvg hexLocation cornersCoords =
g
[]
(toPolygon hexLocation cornersCoords)
toPolygon : Hash -> String -> List (Svg Msg)
toPolygon hexLocation cornersCoords =
[ polygon
[ Svg.Attributes.style "cursor: pointer"
, stroke "#ffff00"
, strokeWidth "1px"
, fill <|
if List.member hexLocation model.greenCells then
"#179f83"
else
"#777777"
, points cornersCoords
, Svg.Events.onClick <|
SetGreen hexLocation
]
[]
]
in
g
[]
<|
List.map2 toSvg
(List.map getCellKey (Dict.toList model.map))
(List.map (pointsToString << mapPolygonCorners << getCell) (Dict.toList model.map))
{-| Helper to convert points to SVG string coordinates
-}
pointsToString : List Point -> String
pointsToString points =
String.join " " (List.map pointToStringCoords points)
{-| Helper to convert points to SVG string coordinates
-}
pointToStringCoords : Point -> String
pointToStringCoords ( x, y ) =
String.fromFloat x ++ "," ++ String.fromFloat y
getCell : ( Hash, Hex ) -> Hex
getCell ( key, hex ) =
hex
getCellKey : ( Hash, Hex ) -> Hash
getCellKey ( key, hex ) =
key
mapPolygonCorners : Hex -> List Point
mapPolygonCorners =
polygonCorners layout