desck 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Zone.elm ADDED
@@ -0,0 +1,364 @@
1
+ module Zone exposing (..)
2
+
3
+ import Palette exposing (Color(..), colorFromString)
4
+ import Rect exposing (Rect, Rotation, angled, rect4)
5
+ import Util exposing (Id)
6
+ import Vec exposing (Vec2)
7
+
8
+
9
+ type alias Border =
10
+ { color : Color
11
+ , width : Int
12
+ , dash : Int
13
+ }
14
+
15
+
16
+ type Head
17
+ = None
18
+ | Triangle
19
+ | Cross
20
+
21
+
22
+ headString : Head -> String
23
+ headString h =
24
+ case h of
25
+ None ->
26
+ "None"
27
+
28
+ Triangle ->
29
+ "Triangle"
30
+
31
+ Cross ->
32
+ "Cross"
33
+
34
+
35
+ headFromString : String -> Head
36
+ headFromString s =
37
+ case s of
38
+ "None" ->
39
+ None
40
+
41
+ "Triangle" ->
42
+ Triangle
43
+
44
+ "Cross" ->
45
+ Cross
46
+
47
+ _ ->
48
+ Triangle
49
+
50
+
51
+ type alias Stamp =
52
+ { pos : Vec2
53
+ , id : Id
54
+ , text : Text
55
+ , fill : Color
56
+ , radius : Float
57
+ }
58
+
59
+
60
+ type alias Arrow =
61
+ { from : String
62
+ , to : String
63
+ , id : String
64
+ , fill : Color
65
+ , width : Float
66
+ , head : Head
67
+
68
+ -- TODO Arrows - text : Text
69
+ }
70
+
71
+
72
+ defaultArrow : Arrow
73
+ defaultArrow =
74
+ { from = ""
75
+ , to = ""
76
+ , id = ""
77
+ , fill = Orange
78
+ , width = 30.0
79
+ , head = Triangle
80
+ }
81
+
82
+
83
+ type Anchor
84
+ = Center
85
+ | Left
86
+ | Right
87
+ | Top
88
+ | Bottom
89
+
90
+
91
+ anchorString : Anchor -> String
92
+ anchorString a =
93
+ case a of
94
+ Left ->
95
+ "Left"
96
+
97
+ Right ->
98
+ "Right"
99
+
100
+ Top ->
101
+ "Top"
102
+
103
+ Bottom ->
104
+ "Bottom"
105
+
106
+ Center ->
107
+ "Center"
108
+
109
+
110
+ anchorFromString : String -> Anchor
111
+ anchorFromString s =
112
+ case s of
113
+ "Left" ->
114
+ Left
115
+
116
+ "Right" ->
117
+ Right
118
+
119
+ "Top" ->
120
+ Top
121
+
122
+ "Bottom" ->
123
+ Bottom
124
+
125
+ "Center" ->
126
+ Center
127
+
128
+ _ ->
129
+ Center
130
+
131
+
132
+ type FontType
133
+ = PrimaryFont
134
+ | SecondaryFont
135
+
136
+
137
+ type alias Text =
138
+ { label : String
139
+ , color : Color
140
+ , size : Int
141
+ , primary : Bool
142
+ , anchor : Anchor
143
+ }
144
+
145
+
146
+ type alias Grid =
147
+ { rows : Int
148
+ , columns : Int
149
+ }
150
+
151
+
152
+ type alias Zone =
153
+ { rect : Rect
154
+ , rot : Rotation
155
+ , id : Id
156
+ , fill : Color
157
+ , border : Border
158
+ , text : Text
159
+ , grid : Grid
160
+ }
161
+
162
+
163
+ defaultText : Text
164
+ defaultText =
165
+ { label = "zone"
166
+ , color = LightGray
167
+ , size = 70
168
+ , primary = True
169
+ , anchor = Center
170
+ }
171
+
172
+
173
+ defaultStamp : Stamp
174
+ defaultStamp =
175
+ { pos = Vec.zero
176
+ , id = ""
177
+ , text = { defaultText | label = "A", color = Black }
178
+ , radius = 70
179
+ , fill = Green
180
+ }
181
+
182
+
183
+ defaultZone : Zone
184
+ defaultZone =
185
+ { rect = rect4 0 0 100 100
186
+ , rot = angled 0
187
+ , id = ""
188
+ , fill = DarkGray
189
+ , text = defaultText
190
+ , grid = { rows = 1, columns = 1 }
191
+ , border =
192
+ { dash = 0
193
+ , width = 10
194
+ , color = MidGray
195
+ }
196
+ }
197
+
198
+
199
+ type ZoneField
200
+ = ZoneLabel
201
+ | ZoneFill
202
+ | ZoneStroke
203
+ | ZoneText
204
+ | ZoneWidth
205
+ | ZoneDash
206
+ | ZoneFontSize
207
+ | ZoneRows
208
+ | ZoneColumns
209
+ | ZoneAnchor
210
+ | ZoneFont
211
+
212
+
213
+ updateZoneString : ZoneField -> String -> Zone -> Zone
214
+ updateZoneString field string z =
215
+ let
216
+ b =
217
+ z.border
218
+
219
+ g =
220
+ z.grid
221
+
222
+ color =
223
+ colorFromString string
224
+
225
+ int =
226
+ Maybe.withDefault 16 <| String.toInt string
227
+
228
+ text =
229
+ z.text
230
+
231
+ bool =
232
+ if string == "True" then
233
+ True
234
+
235
+ else
236
+ False
237
+ in
238
+ case field of
239
+ ZoneFont ->
240
+ { z | text = { text | primary = bool } }
241
+
242
+ ZoneFill ->
243
+ { z | fill = color }
244
+
245
+ ZoneAnchor ->
246
+ { z | text = { text | anchor = anchorFromString string } }
247
+
248
+ ZoneLabel ->
249
+ { z | text = { text | label = string } }
250
+
251
+ ZoneText ->
252
+ { z | text = { text | color = color } }
253
+
254
+ ZoneFontSize ->
255
+ { z | text = { text | size = int } }
256
+
257
+ ZoneStroke ->
258
+ { z | border = { b | color = color } }
259
+
260
+ ZoneDash ->
261
+ { z | border = { b | dash = int } }
262
+
263
+ ZoneWidth ->
264
+ { z | border = { b | width = int } }
265
+
266
+ ZoneRows ->
267
+ { z | grid = { g | rows = int } }
268
+
269
+ ZoneColumns ->
270
+ { z | grid = { g | columns = int } }
271
+
272
+
273
+ textAttributes : Vec2 -> Text -> ( Vec2, ( String, String ), Int -> String -> String )
274
+ textAttributes size t =
275
+ let
276
+ lines =
277
+ String.split "\n" t.label
278
+
279
+ c =
280
+ Vec.half size
281
+
282
+ s =
283
+ toFloat t.size
284
+
285
+ lc =
286
+ List.length lines |> toFloat |> max 1.0
287
+
288
+ offset =
289
+ s * 0.0
290
+ in
291
+ case t.anchor of
292
+ Center ->
293
+ ( c
294
+ , ( "middle", "middle" )
295
+ , \i _ ->
296
+ if i > 0 then
297
+ "1em"
298
+
299
+ else
300
+ (toFloat (List.length lines - 1)
301
+ * -0.5
302
+ |> String.fromFloat
303
+ )
304
+ ++ "em"
305
+ )
306
+
307
+ Top ->
308
+ ( Vec.vec2 c.x
309
+ (-(s * lc)
310
+ - offset
311
+ - s
312
+ * (if lc > 1 then
313
+ 1.0
314
+
315
+ else
316
+ 0.75
317
+ )
318
+ )
319
+ , ( "middle", "alphabetic" )
320
+ , \_ _ ->
321
+ "1em"
322
+ )
323
+
324
+ Bottom ->
325
+ ( Vec.vec2 c.x (size.y + offset + s * 0.75)
326
+ -- TODO make multiplier smaller if only lowercase?
327
+ , ( "middle", "hanging" )
328
+ , \i _ ->
329
+ if i == 0 then
330
+ "0em"
331
+
332
+ else
333
+ "1em"
334
+ )
335
+
336
+ Right ->
337
+ ( Vec.vec2 (size.x + offset + s * 0.75) c.y
338
+ , ( "start", "middle" )
339
+ , \i _ ->
340
+ if i > 0 then
341
+ "1em"
342
+
343
+ else
344
+ (toFloat (List.length lines - 1)
345
+ * -0.5
346
+ |> String.fromFloat
347
+ )
348
+ ++ "em"
349
+ )
350
+
351
+ Left ->
352
+ ( Vec.vec2 (-offset - s * 0.75) c.y
353
+ , ( "end", "middle" )
354
+ , \i _ ->
355
+ if i > 0 then
356
+ "1em"
357
+
358
+ else
359
+ (toFloat (List.length lines - 1)
360
+ * -0.5
361
+ |> String.fromFloat
362
+ )
363
+ ++ "em"
364
+ )