/
InterfaceView2.elm
1159 lines (1026 loc) · 41.2 KB
/
InterfaceView2.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module InterfaceView2 (view, scaleColorBall
, prevButtonEnabled, nextButtonEnabled -- TODO not great
) where
--Import the little language and its parsing utilities
import Lang exposing (..) --For access to what makes up the Vals
import LangParser2 exposing (parseE, parseV)
import Sync
import Eval
import Utils
import MicroTests
import InterfaceModel exposing (..)
import LangSvg exposing (toNum, toNumTr, addi)
import ExamplesGenerated as Examples
import Config exposing (params)
import VirtualDom
--Core Libraries
import List
import Dict
import String
import Graphics.Element as GE
import Graphics.Collage as GC
import Graphics.Input as GI
import Graphics.Input.Field as GIF
import Text as T exposing (defaultStyle)
import Color
--Signaling Libraries
import Mouse
import Window
import Task exposing (Task, andThen)
--Storage Libraries
import InterfaceStorage exposing (taskMailbox, saveStateLocally, loadLocalState,
checkAndSave, getLocalSaves, clearLocalSaves,
removeDialog, deleteLocalSave)
--Html Libraries
import Html
import Html.Attributes as Attr
import Html.Events as Events
--Svg Libraries
import Svg
import Svg.Attributes
import Svg.Events
import Svg.Lazy
--Error Checking Libraries
import Debug
--------------------------------------------------------------------------------
dimToPix d = String.append (toString d) "px"
interfaceColor = Color.rgba 52 73 94 1.0
textColor = "white"
titleStyle =
{ defaultStyle | typeface <- ["Courier", "monospace"]
, height <- Just 18
, bold <- False
, color <- Color.white}
-- Creates an Html button with the text properly offset
type ButtonStatus = Raised | Highlighted | Depressed | Disabled
-- Currently assumes:
-- font-size is 16px
-- the top of the button occupies 90% of the height of the button
-- the depressed button should move the text down 3/50 of the total height of the
-- button
makeButton : ButtonStatus -> Int -> Int -> String -> GE.Element
makeButton status w h text =
let fontsize = 16
topprop = 0.9
depdip = 0.06
raisedoffset = round <| 0.5 * topprop * toFloat h - 0.5 * fontsize
depressedoffset = round <| toFloat raisedoffset + depdip * toFloat h
(img,dip) = case status of
Raised -> ("button_raised.svg", dimToPix raisedoffset)
Highlighted -> ("button_highlighted.svg", dimToPix raisedoffset)
Depressed -> ("button_depressed.svg", dimToPix depressedoffset)
Disabled -> ("button_disabled.svg", dimToPix raisedoffset)
in
GE.flow GE.outward
[ GE.image w h img
, Html.toElement w h <|
Html.div
[ Attr.style
[ ("color", textColor)
, ("font-family", "sans-serif")
, ("text-align", "center")
, ("width", dimToPix w)
, ("height", dimToPix h)
, ("transform", "translate(0px," ++ dip ++ ")")
]
] [ Html.text text ]
]
--------------------------------------------------------------------------------
-- Zone Options (per shape)
type alias ZoneOptions =
{ showBasic : Bool , addBasic : Bool , addRot : Bool , addColor : Bool
, addDelete : Bool }
zoneOptions0 =
{ showBasic = False , addBasic = False , addRot = False , addColor = False
, addDelete = False }
optionsOf : ShowZones -> ZoneOptions
optionsOf x =
if | x == showZonesNone -> { zoneOptions0 | addBasic <- True }
| x == showZonesBasic -> { zoneOptions0 | addBasic <- True, showBasic <- True }
| x == showZonesRot -> { zoneOptions0 | addRot <- True }
| x == showZonesColor -> { zoneOptions0 | addColor <- True }
| x == showZonesDel -> { zoneOptions0 | addDelete <- True }
--------------------------------------------------------------------------------
-- Compiling to Svg
buildSvg : Bool -> ShowZones -> LangSvg.RootedIndexedTree -> Svg.Svg
buildSvg addZones showZones (i,d) = buildSvg_ addZones showZones d i
buildSvg_ : Bool -> ShowZones -> LangSvg.IndexedTree -> LangSvg.NodeId -> Svg.Svg
buildSvg_ addZones showZones d i =
case Utils.justGet_ ("buildSvg_ " ++ toString i) i d of
LangSvg.TextNode text -> VirtualDom.text text
LangSvg.SvgNode shape attrs js ->
-- TODO: figure out: (LangSvg.attr "draggable" "false")
let (zones, attrs') =
let options = optionsOf showZones in
case (addZones, Utils.maybeRemoveFirst "zones" attrs) of
(False, Nothing) -> ([], attrs)
(False, Just (_, l)) -> ([], l)
(True, Nothing) ->
(makeZones options shape i attrs, attrs)
(True, Just (LangSvg.AString "none", l)) ->
(makeZones zoneOptions0 shape i attrs, l)
(True, Just (LangSvg.AString "basic", l)) ->
let options' = { options | addRot <- False, addColor <- False } in
(makeZones options' shape i attrs, l)
in
let children = List.map (buildSvg_ addZones showZones d) js in
let mainshape = (LangSvg.svg shape) (LangSvg.compileAttrs attrs') children in
if | zones == [] -> mainshape
| otherwise -> Svg.svg [] (mainshape :: zones)
--------------------------------------------------------------------------------
-- Defining Zones
-- compileAttr will throw away the trace anyway
attrNum k n = LangSvg.compileAttr k (LangSvg.ANum (n, dummyTrace))
attrNumTr k nt = LangSvg.compileAttr k (LangSvg.ANum nt)
onMouseDown = Svg.Events.onMouseDown << Signal.message events.address
onMouseUp = Svg.Events.onMouseUp << Signal.message events.address
onMouseOver = Svg.Events.onMouseOver << Signal.message events.address
onMouseOut = Svg.Events.onMouseOut << Signal.message events.address
zoneEvents id shape zone =
[ onMouseDown (SelectObject id shape zone)
, onMouseUp MouseUp
, onMouseOver (UpdateModel (\m -> { m | caption <- Just (Hovering (id, shape, zone)) }))
, onMouseOut (UpdateModel (\m -> { m | caption <- Nothing }))
]
zone svgFunc id shape zone l =
svgFunc (zoneEvents id shape zone ++ l) []
cursorStyle s = LangSvg.attr "cursor" s
-- TODO should take into account disabled zones in Live mode
cursorOfZone zone = if
-- rect zones
| zone == "Interior" -> cursorStyle "move"
| zone == "RightEdge" -> cursorStyle "ew-resize"
| zone == "BotRightCorner" -> cursorStyle "nwse-resize"
| zone == "BotEdge" -> cursorStyle "ns-resize"
| zone == "BotLeftCorner" -> cursorStyle "nesw-resize"
| zone == "LeftEdge" -> cursorStyle "ew-resize"
| zone == "TopLeftCorner" -> cursorStyle "nwse-resize"
| zone == "TopEdge" -> cursorStyle "ns-resize"
| zone == "TopRightCorner" -> cursorStyle "nesw-resize"
-- circle/ellipse zones
| zone == "Edge" -> cursorStyle "pointer"
-- indirect manipulation zones
| zone == "FillBall" -> cursorStyle "pointer"
| zone == "RotateBall" -> cursorStyle "pointer"
-- default
| otherwise -> cursorStyle "default"
-- Stuff for Basic Zones -------------------------------------------------------
-- TODO use zone
zoneBorder svgFunc id shape zone flag show transform =
flip svgFunc [] <<
(++) (zoneEvents id shape zone) <<
(++) transform <<
(++) [ if flag && show
then LangSvg.attr "stroke" "rgba(255,0,0,0.5)"
else LangSvg.attr "stroke" "rgba(0,0,0,0.0)"
, LangSvg.attr "strokeWidth" (if flag then "5" else "0")
, LangSvg.attr "fill" "rgba(0,0,0,0)"
, cursorOfZone zone
]
zonePoint id shape zone show transform =
flip Svg.circle [] <<
(++) (zoneEvents id shape zone) <<
(++) transform <<
(++) [ LangSvg.attr "r" "6"
, if show
then LangSvg.attr "fill" "rgba(255,0,0,0.5)"
else LangSvg.attr "fill" "rgba(0,0,0,0.0)"
, cursorStyle "pointer"
]
zonePoints id shape show transform pts =
flip Utils.mapi pts <| \(i, (x,y)) ->
zonePoint id shape (addi "Point" i) show transform
[ attrNumTr "cx" x, attrNumTr "cy" y ]
zoneLine id shape zone show transform (x1,y1) (x2,y2) =
zoneBorder Svg.line id shape zone True show transform [
attrNumTr "x1" x1 , attrNumTr "y1" y1
, attrNumTr "x2" x2 , attrNumTr "y2" y2
, cursorStyle "pointer"
]
-- Stuff for Rotate Zones ------------------------------------------------------
rotZoneDelta = 20
maybeTransformCmds l =
case Utils.maybeFind "transform" l of
Just (LangSvg.ATransform cmds) -> Just cmds
_ -> Nothing
transformAttr cmds =
[LangSvg.compileAttr "transform" (LangSvg.ATransform cmds)]
maybeTransformAttr l =
case maybeTransformCmds l of
Just cmds -> transformAttr cmds
Nothing -> []
zoneRotate b id shape (cx,cy) r m =
case (b, m) of
(True, Just cmds) -> zoneRotate_ id shape cx cy r cmds
_ -> []
zoneRotate_ id shape cx cy r cmds =
let (a, stroke, strokeWidth, rBall) =
(20, "rgba(192,192,192,0.5)", "5", "7") in
let (fillBall, swBall) = ("silver", "2") in
let transform = transformAttr cmds in
let circle =
flip Svg.circle [] <|
[ LangSvg.attr "fill" "none"
, LangSvg.attr "stroke" stroke , LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "cx" (toString cx) , LangSvg.attr "cy" (toString cy)
, LangSvg.attr "r" (toString r)
]
in
let ball =
flip Svg.circle [] <|
[ LangSvg.attr "stroke" "black" , LangSvg.attr "strokeWidth" swBall
, LangSvg.attr "fill" fillBall
, LangSvg.attr "cx" (toString cx) , LangSvg.attr "cy" (toString (cy - r))
, LangSvg.attr "r" rBall
, cursorOfZone "RotateBall"
] ++ transform
++ zoneEvents id shape "RotateBall"
in
let line =
flip Svg.line [] <|
[ LangSvg.attr "stroke" stroke , LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "x1" (toString cx) , LangSvg.attr "y1" (toString cy)
, LangSvg.attr "x2" (toString cx) , LangSvg.attr "y2" (toString (cy - r))
] ++ transform
in
[circle, line, ball]
halfwayBetween (x1,y1) (x2,y2) = ((x1 + x2) / 2, (y1 + y2) / 2)
distance (x1,y1) (x2,y2) = sqrt ((x2-x1)^2 + (y2-y1)^2)
projPt (x,y) = (fst x, fst y)
halfwayBetween_ pt1 pt2 = halfwayBetween (projPt pt1) (projPt pt2)
distance_ pt1 pt2 = distance (projPt pt1) (projPt pt2)
-- Stuff for Color Zones -------------------------------------------------------
wGradient = 250
scaleColorBall = 1 / (wGradient / LangSvg.maxColorNum)
numToColor = Utils.numToColor wGradient
maybeColorNumAttr k l =
case Utils.maybeFind k l of
Just (LangSvg.AColorNum n) -> Just n
_ -> Nothing
zoneColor b id shape x y rgba =
case (b, rgba) of
(True, Just n) -> zoneColor_ id shape x y n
_ -> []
zoneColor_ id shape x y n =
let rgba = [LangSvg.compileAttr "fill" (LangSvg.AColorNum n)] in
let (w, h, a, stroke, strokeWidth, rBall) =
(wGradient, 20, 20, "silver", "2", "7") in
let yOff = a + rotZoneDelta in
let ball =
let cx = x + (fst n / LangSvg.maxColorNum) * wGradient in
let cy = y - yOff + (h/2) in
flip Svg.circle [] <|
[ LangSvg.attr "stroke" "black" , LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "fill" stroke
, LangSvg.attr "cx" (toString cx) , LangSvg.attr "cy" (toString cy)
, LangSvg.attr "r" rBall
, cursorOfZone "FillBall"
] ++ zoneEvents id shape "FillBall"
in
let box =
flip Svg.rect [] <|
[ LangSvg.attr "fill" "none"
, LangSvg.attr "stroke" stroke , LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "x" (toString x) , LangSvg.attr "y" (toString (y - yOff))
, LangSvg.attr "width" (toString w) , LangSvg.attr "height" (toString h)
]
in
-- TODO would probably be faster with an image...
let gradient =
List.map (\i ->
let (r,g,b) = numToColor i in
let fill =
"rgb" ++ Utils.parens (String.join "," (List.map toString [r,g,b]))
in
flip Svg.rect [] <|
[ LangSvg.attr "fill" fill
, LangSvg.attr "x" (toString (x+i)) , LangSvg.attr "y" (toString (y - yOff))
, LangSvg.attr "width" "1" , LangSvg.attr "height" (toString h)
]) [0 .. w]
in
gradient ++ [box, ball]
-- Stuff for Delete Zones ------------------------------------------------------
zoneDelete b id shape x y transform =
if b then zoneDelete_ id shape x y transform else []
zoneDelete_ id shape x y transform =
let (w, h, stroke, strokeWidth) =
(20, 20, "silver", "2") in
let evt =
let foo old =
{ old | slate <- Utils.mapSnd (Dict.insert id LangSvg.dummySvgNode) old.slate }
in
onMouseDown (UpdateModel foo) in
let lines =
let f x1 y1 x2 y2 =
flip Svg.line [] <|
[ LangSvg.attr "stroke" "darkred", LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "x1" (toString x1) , LangSvg.attr "y1" (toString y1)
, LangSvg.attr "x2" (toString x2) , LangSvg.attr "y2" (toString y2)
, evt
] ++ transform
in
[ f x y (x + w) (y + h) , f x (y + h) (x + w) y ] in
let box =
flip Svg.rect [] <|
[ LangSvg.attr "fill" "white"
, LangSvg.attr "stroke" stroke , LangSvg.attr "strokeWidth" strokeWidth
, LangSvg.attr "x" (toString x) , LangSvg.attr "y" (toString y)
, LangSvg.attr "width" (toString w) , LangSvg.attr "height" (toString h)
, evt
] ++ transform
in
[box] ++ lines
--------------------------------------------------------------------------------
makeZones : ZoneOptions -> String -> LangSvg.NodeId -> List LangSvg.Attr -> List Svg.Svg
makeZones options shape id l =
case shape of
"rect" ->
let transform = maybeTransformAttr l in
let mk zone x_ y_ w_ h_ =
zoneBorder Svg.rect id shape zone True options.showBasic transform <|
[ attrNum "x" x_ , attrNum "y" y_
, attrNum "width" w_ , attrNum "height" h_
]
in
let
[x,y,w,h] = List.map (toNum << Utils.find_ l) ["x","y","width","height"]
gut = 0.125
(x0,x1,x2) = (x, x + gut*w, x + (1-gut)*w)
(y0,y1,y2) = (y, y + gut*h, y + (1-gut)*h)
(wSlim,wWide) = (gut*w, (1-2*gut)*w)
(hSlim,hWide) = (gut*h, (1-2*gut)*h)
in
let zRot =
let c = (x + (w/2), y + (h/2)) in
let r = rotZoneDelta + (h/2) in
zoneRotate options.addRot id shape c r (maybeTransformCmds l)
in
let zColor =
zoneColor options.addColor id shape x y (maybeColorNumAttr "fill" l)
in
[ mk "Interior" x1 y1 wWide hWide
, mk "RightEdge" x2 y1 wSlim hWide
, mk "BotRightCorner" x2 y2 wSlim hSlim
, mk "BotEdge" x1 y2 wWide hSlim
, mk "BotLeftCorner" x0 y2 wSlim hSlim
, mk "LeftEdge" x0 y1 wSlim hWide
, mk "TopLeftCorner" x0 y0 wSlim hSlim
, mk "TopEdge" x1 y0 wWide hSlim
, mk "TopRightCorner" x2 y0 wSlim hSlim
] ++ zRot
++ zColor
++ zoneDelete options.addDelete id shape x y (maybeTransformAttr l)
"circle" -> makeZonesCircle options id l
"ellipse" -> makeZonesEllipse options id l
"line" ->
let transform = maybeTransformAttr l in
let [x1,y1,x2,y2] = List.map (toNumTr << Utils.find_ l) ["x1","y1","x2","y2"] in
let (pt1,pt2) = ((x1,y1), (x2,y2)) in
let zLine = zoneLine id shape "Edge" options.showBasic transform pt1 pt2 in
let zPts = zonePoints id shape options.showBasic transform [pt1,pt2] in
let zRot =
let c = halfwayBetween_ pt1 pt2 in
let r = (distance_ pt1 pt2 / 2) - rotZoneDelta in
zoneRotate options.addRot id shape c r (maybeTransformCmds l) in
zLine :: zPts ++ zRot
"polygon" -> makeZonesPoly options shape id l
"polyline" -> makeZonesPoly options shape id l
"path" -> makeZonesPath options.showBasic shape id l
_ -> []
makeZonesCircle options id l =
let transform = maybeTransformAttr l in
let [cx,cy,r] = List.map (toNum << Utils.find_ l) ["cx","cy","r"] in
let attrs = [ attrNum "cx" cx, attrNum "cy" cy, attrNum "r" r ] in
[zoneBorder Svg.circle id "circle" "Edge" True options.showBasic attrs transform]
++ [zoneBorder Svg.circle id "circle" "Interior" False options.showBasic attrs transform]
++ (zoneRotate options.addRot id "circle" (cx,cy) (r + rotZoneDelta) (maybeTransformCmds l))
++ (zoneColor options.addColor id "circle" (cx - r) (cy - r) (maybeColorNumAttr "fill" l))
makeZonesEllipse options id l =
let transform = maybeTransformAttr l in
let [cx,cy,rx,ry] = List.map (toNum << Utils.find_ l) ["cx","cy","rx","ry"] in
let attrs = [ attrNum "cx" cx, attrNum "cy" cy, attrNum "rx" rx, attrNum "ry" ry ] in
[zoneBorder Svg.ellipse id "ellipse" "Edge" True options.showBasic attrs transform]
++ [zoneBorder Svg.ellipse id "ellipse" "Interior" False options.showBasic attrs transform]
++ (zoneRotate options.addRot id "circle" (cx,cy) (ry + rotZoneDelta) (maybeTransformCmds l))
++ (zoneColor options.addColor id "ellipse" (cx - rx) (cy - ry) (maybeColorNumAttr "fill" l))
makeZonesPoly options shape id l =
let _ = Utils.assert "makeZonesPoly" (shape == "polygon" || shape == "polyline") in
let transform = maybeTransformAttr l in
let pts = LangSvg.toPoints <| Utils.find_ l "points" in
let zPts = zonePoints id shape options.showBasic transform pts in
let zLines =
let pairs = Utils.adjacentPairs (shape == "polygon") pts in
let f (i,(pti,ptj)) = zoneLine id shape (addi "Edge" i) options.showBasic transform pti ptj in
Utils.mapi f pairs in
let zInterior =
zoneBorder Svg.polygon id shape "Interior" False options.showBasic transform [
LangSvg.compileAttr "points" (LangSvg.APoints pts)
] in
let zRot =
let (((x0,_),(y0,_))::_) = pts in
zoneColor options.addColor id shape x0 y0 (maybeColorNumAttr "fill" l) in
let firstEqLast xs = Utils.head_ xs == Utils.head_ (List.reverse xs) in
if | shape == "polygon" -> zInterior :: (zLines ++ zPts ++ zRot)
| firstEqLast pts -> zInterior :: (zLines ++ zPts ++ zRot)
| otherwise -> zLines ++ zPts ++ zRot
makeZonesPath showZones shape id l =
let _ = Utils.assert "makeZonesPoly" (shape == "path") in
let transform = maybeTransformAttr l in
let cmds = fst <| LangSvg.toPath <| Utils.find_ l "d" in
let (mi,pt) +++ acc = case mi of {Nothing -> acc; _ -> pt :: acc} in
let pts =
List.foldr (\c acc -> case c of
LangSvg.CmdZ s -> acc
LangSvg.CmdMLT s pt -> pt +++ acc
LangSvg.CmdHV s n -> acc
LangSvg.CmdC s pt1 pt2 pt3 -> pt1 +++ (pt2 +++ (pt3 +++ acc))
LangSvg.CmdSQ s pt1 pt2 -> pt1 +++ (pt2 +++ acc)
LangSvg.CmdA s a b c d e pt -> pt +++ acc) [] cmds
in
zonePoints id shape showZones transform pts
--------------------------------------------------------------------------------
-- User Interface
strTitle = " sketch-n-sketch " ++ params.strVersion
colorDebug_ c1 c2 =
if | params.debugLayout -> GE.color c1
| otherwise -> GE.color c2
colorDebug c1 = colorDebug_ c1 interfaceColor
codebox : Int -> Int -> Model -> GE.Element
codebox w h model =
let event =
case model.mode of
SyncSelect _ _ _ -> []
_ -> [Events.on "input" Events.targetValue
(Signal.message events.address << CodeUpdate)]
in
codebox_ w h event model.code (not (editingMode model))
highlightThisIf b =
if b
then ("box-shadow", "inset 0 0 10px 4px rgba(231, 76, 60,0.5)")
else ("box-shadow", "inset 0 0 10px 4px darkgray")
codebox_ w h event s readOnly =
let innerPadding = 4
in
Html.toElement w h <|
Html.textarea
([ Attr.id "codeBox"
, Attr.spellcheck False
, Attr.readonly readOnly
, Attr.style
[ ("font-family", params.mainSection.codebox.font)
, ("font-size", params.mainSection.codebox.fontSize)
, ("border", params.mainSection.codebox.border)
, ("whiteSpace", "pre")
, ("height", "100%")
, ("width", "100%")
, ("resize", "none")
, ("overflow", "auto")
-- Horizontal Scrollbars in Chrome
, ("word-wrap", "normal")
, ("background-color", "whitesmoke")
, ("padding", toString innerPadding ++ "px")
-- Makes the 100% for width/height work as intended
, ("box-sizing", "border-box")
, highlightThisIf (not readOnly)
]
, Attr.value s
, Events.onMouseUp events.address MouseUp
] ++ event)
[]
canvas : Int -> Int -> Model -> GE.Element
canvas w h model =
case model.mode of
Print s -> codebox_ w h [] s True
_ -> canvas_ w h model
canvas_ w h model =
let addZones = case (editingMode model, model.mode) of
(False, AdHoc) -> True
(False, Live _) -> True
_ -> False
in
let svg =
let mainCanvas = buildSvg addZones model.showZones model.slate in
case model.mode of
SyncSelect (_,prevSlate) _ _ ->
let old = buildSvg addZones model.showZones prevSlate in
let shadowCanvas = Svg.svg [Attr.style [("opacity", "0.3")]] [old] in
mkSvg addZones (Svg.g [] [shadowCanvas, mainCanvas])
_ ->
mkSvg addZones mainCanvas
in
Html.toElement w h svg
mkSvg hilite svg =
Svg.svg
[ onMouseUp MouseUp
, Attr.style [ ("width", "100%") , ("height", "100%")
, ("border", params.mainSection.canvas.border)
, highlightThisIf hilite
] ]
[ svg ]
middleWidgets w h wWrap hWrap model =
let twoButtons b1 b2 =
let delta = 3 in
let wHalf = (w//2 - delta) in
GE.flow GE.right [ b1 wHalf h, GE.spacer (2 * delta) h, b2 wHalf h ]
in
let threeButtons b1 b2 b3 =
let delta = 3 in
let sep = GE.spacer (2 * delta) h in
let w_ = (w//3 - delta) in
GE.flow GE.right [ b1 w_ h, sep, b2 w_ h, sep, b3 w_ h ]
in
List.map (GE.container wWrap hWrap GE.middle) <|
case (editingMode model, model.mode) of
(False, SyncSelect _ i options) ->
[ gapWidget w h
, gapWidget w h
, prevButton i w h
, chooseButton i options w h
, nextButton i options w h
]
(False, Print _) ->
[ dropdownExamples model w h
, editRunButton model w h
-- , saveButton model w h
-- , saveAsButton model w h
-- , loadButton model w h
, threeButtons (saveButton model) (saveAsButton model) (loadButton model)
, twoButtons (undoButton model) (redoButton model)
-- , outputButton model w h
]
(False, _) ->
[ dropdownExamples model w h
, editRunButton model w h
-- , saveButton model w h
-- , saveAsButton model w h
-- , loadButton model w h
, threeButtons (saveButton model) (saveAsButton model) (loadButton model)
, twoButtons (undoButton model) (redoButton model)
, cleanButton model w h
-- , outputButton model w h
, gapWidget w h
, zoneButton model w h
-- , frozenButton model w h
, modeButton model w h
] ++ (syncButton_ w h model)
(True, _) ->
[ dropdownExamples model w h
, editRunButton model w h
-- , saveButton model w h
-- , saveAsButton model w h
-- , loadButton model w h
, threeButtons (saveButton model) (saveAsButton model) (loadButton model)
]
gapWidget w h = GE.spacer w h
syncButton_ w h model =
case model.mode of
AdHoc -> [syncButton w h]
_ -> []
wBtn = params.mainSection.widgets.wBtn
hBtn = params.mainSection.widgets.hBtn
buttonAttrs w h =
Attr.style
[ ("width", dimToPix w)
, ("height", dimToPix h)
, ("font-family", params.mainSection.widgets.font)
, ("font-size", params.mainSection.widgets.fontSize)
]
gutterForResizing orient w h =
let s = if orient == Vertical then "ew-resize" else "ns-resize" in
colorDebug Color.darkBlue <|
Html.toElement w h <|
Html.div
[ Events.onMouseDown events.address StartResizingMid
, Events.onMouseUp events.address MouseUp
, Attr.style
[ ("width", dimToPix w) , ("height", dimToPix h)
, ("cursor", s) ]
]
[ ]
mainSectionVertical : Int -> Int -> Model -> GE.Element
mainSectionVertical w h model =
let
wGut = params.mainSection.vertical.wGut
wMiddle = wBtn
wCode_ = (w - wMiddle - wGut - wGut) // 2
wCode = wCode_ + model.midOffsetX
wCanvas = wCode_ - model.midOffsetX
hCanvas = h - hZInfo
hZInfo = params.mainSection.canvas.hZoneInfo
hWidget = params.mainSection.widgets.hBtn
+ params.mainSection.vertical.hExtra
in
let codeSection = codebox wCode h model in
let canvasSection =
GE.size wCanvas h <|
GE.flow GE.down
[ canvas wCanvas hCanvas model
, GE.flow GE.left
[ colorDebug Color.red <|
GE.container wBtn (hZInfo+1) GE.middle <|
outputButton model wBtn hBtn
, caption model (wCanvas+1-wBtn) (hZInfo+1) -- NOTE: +1 is a band-aid
]
-- , caption model (wCanvas+1) hZInfo -- NOTE: +1 is a band-aid
]
in
let gutter = gutterForResizing model.orient wGut h in
let middleSection =
colorDebug Color.lightBlue <|
GE.size wMiddle h <|
GE.flow GE.down <|
middleWidgets wBtn hBtn wMiddle hWidget model in
GE.flow GE.right <|
[ codeSection, gutter, middleSection, gutter, canvasSection ]
mainSectionHorizontal : Int -> Int -> Model -> GE.Element
mainSectionHorizontal w h model =
let
hGut = params.mainSection.horizontal.hGut
hMiddle = hBtn
hCode_ = (h - hMiddle - hGut - hGut) // 2
hCode = hCode_ + model.midOffsetY
hCanvas = hCode_ - model.midOffsetY - hZInfo
hZInfo = params.mainSection.canvas.hZoneInfo
wWidget = params.mainSection.widgets.wBtn
+ params.mainSection.horizontal.wExtra
in
let codeSection = codebox w hCode model in
let canvasSection =
GE.size w (hCanvas + hZInfo) <|
GE.flow GE.down
[ canvas w hCanvas model
, GE.flow GE.left
[ colorDebug Color.red <|
GE.container wBtn (hZInfo+1) GE.middle <|
outputButton model wBtn hBtn
, caption model (w-wBtn) (hZInfo+1) -- NOTE: +1 is a band-aid
]
-- , caption model w (hZInfo+1) -- NOTE: +1 is a band-aid
]
in
let gutter = gutterForResizing model.orient w hGut in
let middleSection =
colorDebug Color.lightBlue <|
GE.size w hMiddle <|
GE.flow GE.right <|
middleWidgets wBtn hBtn wWidget hMiddle model in
GE.flow GE.down <|
[ codeSection, gutter, middleSection, gutter, canvasSection ]
simpleButton_
: Signal.Address a -> a -> Bool -> a -> String -> String -> String
-> Int -> Int -> GE.Element
simpleButton_ addy defaultMsg disabled msg value name text w h =
if disabled then
GI.customButton (Signal.message addy defaultMsg)
(makeButton Disabled w h text)
(makeButton Disabled w h text)
(makeButton Disabled w h text)
else
GI.customButton (Signal.message addy msg)
(makeButton Raised w h text)
(makeButton Highlighted w h text)
(makeButton Depressed w h text)
simpleEventButton_ = simpleButton_ events.address Noop
simpleTaskButton_ = simpleButton_ taskMailbox.address (Task.succeed ())
simpleButton = simpleEventButton_ False
simpleTaskButton = simpleTaskButton_ False
-- displayKey s = " " ++ Utils.parens s
displayKey s = " " ++ s
editRunButton model w h =
let disabled = model.mode == AdHoc in
case editingMode model of
True -> simpleEventButton_ disabled Run "Run" "Run" "Run Code" w h
False -> simpleEventButton_ disabled Edit "Edit" "Edit" "Edit Code" w h
outputButton model w h =
let disabled = model.mode == AdHoc in
let cap =
case model.mode of
Print _ -> "[Out] SVG"
_ -> "[Out] Canvas"
in
simpleEventButton_ disabled ToggleOutput "Toggle Output" "Toggle Output" cap w h
syncButton =
simpleButton Sync "Sync" "Sync the code to the canvas" "Sync"
zoneButton model =
let cap =
if | model.showZones == showZonesNone -> "[Zones] Hidden"
| model.showZones == showZonesBasic -> "[Zones] Basic"
| model.showZones == showZonesRot -> "[Zones] Rotation"
| model.showZones == showZonesColor -> "[Zones] Color"
| model.showZones == showZonesDel -> "[Zones] Delete"
in
simpleButton ToggleZones "ToggleZones" "Show/Hide Zones" cap
{-
frozenButton model =
let cap = if model.syncOptions.thawedByDefault then "[Default] n?" else "[Default] n!" in
simpleButton ToggleThawed "ToggleThawed " "Toggle ?/!" cap
-}
chooseButton i ((n1,l1),(n2,l2),_) =
let cap =
let n = n1 + n2 + 1 in
if i == n then "Revert"
else "Select " ++ Utils.parens (toString i ++ "/" ++ toString n)
in
simpleButton SelectOption "Choose" "Choose" (cap ++ displayKey Utils.uniEnter)
prevButtonEnabled i = i > 1
prevButton i =
let enabled = prevButtonEnabled i in
simpleEventButton_
(not enabled) (TraverseOption -1)
"Prev" "Prev" ("Show Prev" ++ displayKey Utils.uniLeft)
nextButtonEnabled i ((n1,l1),(n2,l2),_) = i < n1 + n2 + 1
nextButton i options =
let enabled = nextButtonEnabled i options in
simpleEventButton_
(not enabled) (TraverseOption 1)
"Next" "Next" ("Show Next" ++ displayKey Utils.uniRight)
saveButton : Model -> Int -> Int -> GE.Element
saveButton model w h =
let disabled = List.any ((==) model.exName << fst) Examples.list
dn = "Save"
in
simpleTaskButton_
disabled (saveStateLocally model.exName False model)
dn dn Utils.uniSave w h
saveAsButton : Model -> Int -> Int -> GE.Element
saveAsButton model w h =
let dn = "Save As" in
simpleTaskButton
(saveStateLocally model.exName True model)
dn dn (Utils.uniCamera) w h
loadButton : Model -> Int -> Int -> GE.Element
loadButton model w h =
simpleTaskButton
(loadLocalState model.exName)
"Reload" "Reload" Utils.uniReload w h
undoButton : Model -> Int -> Int -> GE.Element
undoButton model =
let past = fst model.history in
simpleEventButton_ (List.length past == 0) Undo "Undo" "Undo" Utils.uniUndo
redoButton : Model -> Int -> Int -> GE.Element
redoButton model =
let future = snd model.history in
simpleEventButton_ (List.length future == 0) Redo "Redo" "Redo" Utils.uniRedo
dropdownExamples : Model -> Int -> Int -> GE.Element
dropdownExamples model w h =
let
choices = case model.mode of
AdHoc -> [(model.exName, Signal.send events.address Noop)]
_ ->
let foo (name,thunk) = (name, Signal.send events.address (SelectExample name thunk))
bar saveName = (saveName, loadLocalState saveName)
blank = ("", Task.succeed ())
localsaves = case model.localSaves of
[] -> []
l ->
List.concat
[ [ ("Local Saves:", Task.succeed ())
, blank
]
, List.map bar l
, [ blank ]
]
in List.concat
[ localsaves
, [ ("Builtin Examples:", Task.succeed ())
, blank
]
, (List.map foo Examples.list)
, [ blank
, ("*Clear Local Saves*", clearLocalSaves)
]
]
options = List.map (\(name,task) ->
if | name == model.exName ->
Html.option
[ Attr.value name
, Attr.selected True
]
[ Html.text name ]
| otherwise ->
Html.option
[ Attr.value name
]
[ Html.text name ]) choices
findTask name choices = case choices of
(n,t) :: rest -> if | n == name -> t
| otherwise -> findTask name rest
[] -> Debug.crash "Dropdown example does not have associated task"
in Html.toElement 120 24 <| Html.select
[ Attr.style
[ ("pointer-events", "auto")
, ("border", "0 solid")
, ("display", "block")
, ("width", "120px")
, ("height", "24px")
, ("font-family", "sans-serif")
, ("font-size", "1em")
]
, Events.on "change" Events.targetValue
(\selected -> Signal.message taskMailbox.address <|
findTask selected choices)
] options
modeButton model =
if model.mode == AdHoc
then simpleEventButton_ True Noop "SwitchMode" "SwitchMode" "[Mode] Ad Hoc"
else simpleEventButton_ False (SwitchMode AdHoc) "SwitchMode" "SwitchMode" "[Mode] Live"
cleanButton model =
let disabled = case model.mode of {Live _ -> False; _ -> True} in
simpleEventButton_ disabled CleanCode "Clean" "Clean" "Clean Up"
{-
modeToggle : Int -> Int -> Model -> GE.Element
modeToggle w h model =
let opt s m = (s, (SwitchMode m)) in
let optionLive = opt "Live" (mkLive_ model.syncOptions (Utils.fromJust model.inputExp)) in
let optionAdHoc = opt "Ad Hoc" AdHoc in
GI.dropDown (Signal.message events.address) <|
case model.mode of
AdHoc -> [optionAdHoc]
_ -> [optionLive, optionAdHoc]
-}
orientationButton w h model =
let text = "[Orientation] " ++ toString model.orient
in
simpleButton SwitchOrient text text text w h
caption : Model -> Int -> Int -> GE.Element
caption model w h =
let eStr = GE.leftAligned << T.color Color.white << T.monospace << T.fromString in
colorDebug Color.orange <|
GE.container w h GE.topLeft <|
case (model.caption, model.mode, model.mouseMode) of
(Just (Hovering (i,k,z)), Live info, MouseNothing) ->
case hoverInfo info (i,k,z) of
Nothing -> GE.empty
Just l ->
let numLocs = List.map (\(s,n) -> toString n ++ Utils.braces s) l in
let line1 = (k ++ toString i) ++ " " ++ z in
let line2 = Utils.spaces numLocs in
eStr (" " ++ line1 ++ "\n " ++ line2)
(Just (LangError err), _, _) ->
eStr err
_ ->
GE.empty
hoverInfo info (i,k,z) =
let err y = "hoverInfo: " ++ toString y in
flip Utils.bindMaybe (Dict.get i info.assignments) <| \d ->
flip Utils.bindMaybe (Dict.get z d) <| \locs ->
Just <|
List.map (\(lid,_,x) ->
let n = Utils.justGet_ (err (i,z,lid)) lid info.initSubst in
if | x == "" -> ("loc_" ++ toString lid, n)
| otherwise -> (x, n)) locs
-- The pop-up save dialog box
saveElement : Model -> Int -> Int -> GE.Element
saveElement model w h = case model.mode of
SaveDialog x ->
-- Note that dimBox must not be a parent of the pickBox, as
-- opacity of a parent clobbers that of all its children
let dimBox = GE.color Color.black
<| GE.opacity 0.5
<| GE.spacer w h
pickBox = GE.container w h GE.middle