-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidTerm-03-14.fsx
1385 lines (1282 loc) · 60 KB
/
MidTerm-03-14.fsx
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
// Francesco Landolfi
// Matr. 444151
//
// Sviluppato con:
// - Linux Mint 16 (Ubuntu 13.10)
// - MonoDevelop 4.2.2
// - Mono 3.2.3
// - F# 3.1 (Open Source Edition)
open System.Windows.Forms
open System.Drawing
type ButtonType =
| Clear
| AddState
| AddTransition
| Initial
| Delete
| UpArrow
| RightArrow
| DownArrow
| LeftArrow
| ZoomIn
| ZoomOut
| Reload
| Play
| Pause
| Stop
| Slower
| Faster
type Status =
| Disabled
| Clicked
| Normal
type Button (bt:ButtonType) =
let mutable area = new RectangleF()
let trs = new ResizeArray<Drawing2D.GraphicsPath>()
let btColor = Brushes.White
let btColorDisabled = Brushes.Gray
let bgColorDark = Brushes.Black
let bgColor = Brushes.Gray
let bgColorLight = Brushes.DarkGray
let mutable hasFocus = false
let mutable bgC = bgColorDark
let mutable btC = btColor
let mutable (status:Status) = Normal
let mutable descr = ""
let mutable customFun = (fun args -> ())
let initPolygons side =
trs.Clear()
match bt with
| Play ->
let gp = new Drawing2D.GraphicsPath()
gp.AddPolygon([| new PointF(side*0.2f, side*0.2f); new PointF(side*0.8f, side*0.5f); new PointF(side*0.2f, side*0.8f); |])
trs.Add(gp)
| Faster ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.2f, side*0.2f); new PointF(side*0.5f, side*0.5f); new PointF(side*0.2f, side*0.8f); |])
gp2.AddPolygon([| new PointF(side*0.5f, side*0.2f); new PointF(side*0.8f, side*0.5f); new PointF(side*0.5f, side*0.8f); |])
trs.Add(gp1)
trs.Add(gp2)
| Slower ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.8f, side*0.2f); new PointF(side*0.5f, side*0.5f); new PointF(side*0.8f, side*0.8f); |])
gp2.AddPolygon([| new PointF(side*0.5f, side*0.2f); new PointF(side*0.2f, side*0.5f); new PointF(side*0.5f, side*0.8f); |])
trs.Add(gp1)
trs.Add(gp2)
| Clear ->
let gp = new Drawing2D.GraphicsPath()
gp.AddPolygon([| new PointF(side*0.4f, 0.f); new PointF(side, 0.f); new PointF(side, side*0.6f); |])
trs.Add(gp)
| LeftArrow ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.5f, side*0.5f); new PointF(0.f, side*0.97f); new PointF() |])
gp2.AddPolygon([| new PointF(side*0.1f, side*0.5f); new PointF(side*0.3f, side*0.4f); new PointF(side*0.3f, side*0.6f); |])
trs.Add(gp1)
trs.Add(gp2)
| RightArrow ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.5f, side*0.5f); new PointF(side*0.97f, 0.f); new PointF(side*0.97f, side*0.97f) |])
gp2.AddPolygon([| new PointF(side*0.9f, side*0.5f); new PointF(side*0.7f, side*0.6f); new PointF(side*0.7f, side*0.4f); |])
trs.Add(gp1)
trs.Add(gp2)
| UpArrow ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.5f, side*0.5f); new PointF(); new PointF(side, 0.f) |])
gp2.AddPolygon([| new PointF(side*0.5f, side*0.1f); new PointF(side*0.6f, side*0.3f); new PointF(side*0.4f, side*0.3f); |])
trs.Add(gp1)
trs.Add(gp2)
| DownArrow ->
let gp1 = new Drawing2D.GraphicsPath()
let gp2 = new Drawing2D.GraphicsPath()
gp1.AddPolygon([| new PointF(side*0.5f, side*0.5f); new PointF(side*0.97f, side*0.97f); new PointF(0.f, side*0.97f) |])
gp2.AddPolygon([| new PointF(side*0.5f, side*0.9f); new PointF(side*0.4f, side*0.7f); new PointF(side*0.6f, side*0.7f); |])
trs.Add(gp1)
trs.Add(gp2)
| Reload ->
let t1 = new Drawing2D.GraphicsPath()
let t2 = new Drawing2D.GraphicsPath()
let d = side*(0.5f + single(System.Math.Sqrt(0.08)))
t1.AddPolygon([| new PointF(side*0.5f, side*0.5f); new PointF(side*0.97f, side*0.97f); new PointF(0.f, side*0.97f) |])
t2.AddPolygon([| new PointF(side*0.6f, side*0.6f); new PointF(side*0.6f, d); new PointF(d, d) |])
trs.Add(t1)
trs.Add(t2)
| ZoomIn | ZoomOut ->
let rect = new Drawing2D.GraphicsPath()
let mat = new Drawing2D.Matrix()
mat.Rotate(-45.f)
mat.Translate(side*0.4f, side*0.5f, Drawing2D.MatrixOrder.Append)
rect.AddRectangle(new RectangleF(0.f, 0.f, side*0.12f, side*0.4f))
rect.Transform(mat)
trs.Add(rect)
| AddTransition ->
let t = new Drawing2D.GraphicsPath()
t.AddPolygon([| new PointF(side*0.67f, side*0.67f); new PointF(side*0.67f, side*0.74f); new PointF(side*0.74f, side*0.74f) |])
trs.Add(t)
| _ -> ()
do
match bt with
| Initial ->
descr <- "Mark selected state as Initial State"
| AddTransition ->
descr <- "Add a new Transition to the current Diagram"
| Delete ->
descr <- "Delete selected Transition or State (all its transitions will be lost)"
| Reload ->
descr <- "Change the Input to the Machine"
| Play ->
descr <- "Start Animation"
| Pause ->
descr <- "Pause Animation"
| Stop ->
descr <- "Stop Animation"
| Faster ->
descr <- "Accelerate Animation"
| Slower ->
descr <- "Decelerate Animation"
| ZoomIn ->
descr <- "Zoom In"
| ZoomOut ->
descr <- "Zoom Out"
| AddState ->
descr <- "Add a new State to the current Diagram"
| Clear ->
descr <- "New Finite-State Machine (FSM) Diagram (Clear current Diagram)"
| LeftArrow ->
descr <- "Scroll Left"
| RightArrow ->
descr <- "Scroll Right"
| UpArrow ->
descr <- "Scroll Up"
| DownArrow ->
descr <- "Scroll Down"
member this.Type = bt
member this.HasFocus
with get() = hasFocus
and set(b) = hasFocus <- b
member this.Description
with get() = descr
and set(s) = descr <- s
member this.Status
with get() = status
and set(s) =
match s with
| Disabled ->
btC <- btColorDisabled
bgC <- bgColorDark
| Clicked ->
bgC <- bgColorLight
btC <- btColor
| Normal ->
bgC <- bgColorDark
btC <- btColor
status <- s
member this.CustomFun
with get() = customFun
and set(f) = customFun <- f
member this.Location
with get() = area.Location
and set(l) = area.Location <- l
member this.Side
with get() = area.Width
and set(s) =
area.Width <- s
area.Height <- s
initPolygons s
member this.Contains(p:PointF) =
match bt with
| LeftArrow | RightArrow | UpArrow | DownArrow ->
let pTemp = new PointF(p.X - area.X, p.Y - area.Y)
hasFocus <- trs.[0].IsVisible(pTemp)
| _ ->
hasFocus <- area.Contains(p)
if hasFocus && status = Normal then
bgC <- bgColor
else
match status with
| Disabled ->
bgC <- bgColorDark
| Clicked ->
bgC <- bgColorLight
| Normal ->
bgC <- bgColorDark
hasFocus
member this.Paint(g:Graphics) =
if ((bt <> LeftArrow) && (bt <> RightArrow) && (bt <> UpArrow) && (bt <> DownArrow)) then
g.FillRectangle(bgC, area)
g.TranslateTransform(area.X, area.Y)
match bt with
| LeftArrow | RightArrow | UpArrow | DownArrow ->
g.FillPath(bgC, trs.[0])
g.FillPath(btC, trs.[1])
| Play ->
g.FillPath(btC, trs.[0])
| Pause ->
g.FillRectangle(btC, area.Width*0.2f, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f)
g.FillRectangle(btC, area.Width*0.6f, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f)
| Stop ->
g.FillRectangle(btC, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f)
| Faster | Slower ->
g.FillPath(btC, trs.[0])
g.FillPath(btC, trs.[1])
| AddState ->
use p = new Pen(btC, area.Width*0.05f)
g.DrawEllipse(p, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f)
g.DrawLine(p, area.Width*0.2f, area.Width*0.5f, area.Width*0.8f, area.Width*0.5f)
g.DrawString("Sn", new Font("Courier", area.Width*0.15f, FontStyle.Bold), btC, new PointF(area.Width*0.35f, area.Width*0.25f))
g.DrawString("010", new Font("Courier", area.Width*0.14f), btC, new PointF(area.Width*0.34f, area.Width*0.54f))
| Initial ->
use p = new Pen(btC, area.Width*0.05f)
g.DrawEllipse(p, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f)
g.DrawLine(p, area.Width*0.2f, area.Width*0.5f, area.Width*0.8f, area.Width*0.5f)
g.DrawEllipse(p, area.Width*0.3f, area.Width*0.3f, area.Width*0.4f, area.Width*0.4f)
| AddTransition ->
use p = new Pen(btC, area.Width*0.05f)
p.StartCap <- Drawing2D.LineCap.ArrowAnchor
g.DrawArc(p, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f, 45.f, -270.f)
g.DrawString("010", new Font("Courier", area.Width*0.15f, FontStyle.Bold), btC, new PointF(area.Width*0.3f, area.Width*0.4f))
g.FillPath(btC, trs.[0])
| Delete ->
use p = new Pen(btC, area.Width*0.05f)
g.DrawLine(p, area.Width*0.2f, area.Width*0.2f, area.Width*0.8f, area.Width*0.8f)
g.DrawLine(p, area.Width*0.2f, area.Width*0.8f, area.Width*0.8f, area.Width*0.2f)
| Clear ->
g.FillRectangle(btC, area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f)
g.FillPath(bgC, trs.[0])
g.DrawRectangle(new Pen(bgC), area.Width*0.6f, 0.f, area.Width*0.4f, area.Width*0.4f)
| ZoomIn | ZoomOut ->
g.FillPath(btC, trs.[0])
g.FillEllipse(btC, area.Width*0.1f, area.Width*0.1f, area.Width*0.5f, area.Width*0.5f)
g.FillEllipse(bgC, area.Width*0.2f, area.Width*0.2f, area.Width*0.3f, area.Width*0.3f)
g.FillRectangle(btC, area.Width*0.7f, area.Width*0.25f, area.Width*0.2f, area.Width*0.1f)
if bt = ZoomIn then
g.FillRectangle(btC, area.Width*0.75f, area.Width*0.2f, area.Width*0.1f, area.Width*0.2f)
| Reload ->
g.DrawEllipse(new Pen(btC, area.Width*0.1f), area.Width*0.2f, area.Width*0.2f, area.Width*0.6f, area.Width*0.6f)
g.FillPath(bgC, trs.[0])
g.FillPath(btC, trs.[1])
g.TranslateTransform(-area.X, -area.Y)
type Toolbar() as this =
let mutable area = new RectangleF()
let radRatio = 0.2f
let mutable side = 40.f
let bgC = new SolidBrush(Color.Black)
let bts = new ResizeArray<Button>()
let textBox = new TextBox()
let mutable index = -1
let statuses = new ResizeArray<Status>()
let mutable textBoxStatus = false
do
bts.Add(new Button(Clear, Status = Disabled))
bts.Add(new Button(AddState))
bts.Add(new Button(AddTransition, Status = Disabled))
bts.Add(new Button(Initial, Status = Disabled))
bts.Add(new Button(Delete, Status = Disabled))
bts.Add(new Button(UpArrow))
bts.Add(new Button(RightArrow))
bts.Add(new Button(DownArrow))
bts.Add(new Button(LeftArrow))
bts.Add(new Button(ZoomIn))
bts.Add(new Button(ZoomOut))
bts.Add(new Button(Reload, Status = Disabled))
bts.Add(new Button(Play, Status = Disabled))
bts.Add(new Button(Pause, Status = Disabled))
bts.Add(new Button(Stop, Status = Disabled))
bts.Add(new Button(Slower, Status = Disabled))
bts.Add(new Button(Faster, Status = Disabled))
this.Side <- side
textBox.BorderStyle <- BorderStyle.FixedSingle
textBox.TextAlign <- HorizontalAlignment.Right
textBox.Enabled <- false
member this.Buttons = bts
member this.TextBox = textBox
member this.Index = index
member this.DisableAll() =
for b in bts do
match b.Type with
| UpArrow | RightArrow | DownArrow | LeftArrow | ZoomIn | ZoomOut -> ()
| _ -> b.Status <- Disabled
textBox.Enabled <- false
member this.Save() =
statuses.Clear()
for b in bts do
statuses.Add(b.Status)
textBoxStatus <- textBox.Enabled
member this.Restore() =
textBox.Enabled <- textBoxStatus
statuses |> Seq.iteri(fun i s -> bts.[i].Status <- s)
statuses.Clear()
member this.Contains(p:PointF) =
let pTemp = new PointF(p.X - area.X, p.Y - area.Y)
let mutable ret = false
let mutable i = 0
index <- -1
for b in bts do
let bool = b.Contains(pTemp)
if bool then
index <- i
ret <- bool || ret
i <- i + 1
ret
member this.Location
with get() = area.Location
and set(p) =
area.Location <- p
textBox.Location <- new Point(int(side*10.3f + p.X), int(side*0.27f + p.Y))
member this.Size
with get() = area.Size
member this.Side
with get() = side
and set(s) =
side <- s
textBox.Size <- new Size(int(side*2.3f), textBox.Size.Height)
area.Size <- new SizeF(19.f*s, s)
let mutable i = 0.f
let mutable count = 0.f
let mutable gap = 0.f
for b in bts do
b.Side <- s
match b.Type with
| UpArrow | RightArrow | DownArrow | LeftArrow ->
b.Location <- new PointF(s*5.f + s/3.f, 0.f)
count <- 3.f
gap <- s/3.f
| ZoomOut ->
b.Location <- new PointF(s*(i - count) + gap, 0.f)
count <- -1.f
gap <- s*2.f/3.f
| Reload ->
b.Location <- new PointF(s*(i - count) + gap, 0.f)
gap <- s
| _ ->
b.Location <- new PointF(s*(i - count) + gap, 0.f)
i <- i + 1.f
member this.Paint(g:Graphics) =
g.FillRectangle(bgC, area)
g.TranslateTransform(area.X, area.Y)
g.FillEllipse(bgC, -side*radRatio, 0.f, side*radRatio*2.f, side*radRatio*2.f)
g.FillEllipse(bgC, -side*radRatio, side - side*radRatio*2.f, side*radRatio*2.f, side*radRatio*2.f)
g.FillEllipse(bgC, area.Width - side*radRatio, 0.f, side*radRatio*2.f, side*radRatio*2.f)
g.FillEllipse(bgC, area.Width - side*radRatio, side - side*radRatio*2.f, side*radRatio*2.f, side*radRatio*2.f)
g.FillRectangle(bgC, -side*radRatio, side*radRatio, side*radRatio, side*(1.f - 2.f*radRatio))
g.FillRectangle(bgC, area.Width, side*radRatio, side*radRatio, side*(1.f - 2.f*radRatio))
for b in bts do
b.Paint(g)
g.DrawLine(Pens.DarkGray, side*31.f/6.f, side/6.f, side*31.f/6.f, side*5.f/6.f)
g.DrawLine(Pens.DarkGray, side*51.f/6.f, side/6.f, side*51.f/6.f, side*5.f/6.f)
g.DrawLine(Pens.DarkGray, side*83.f/6.f, side/6.f, side*83.f/6.f, side*5.f/6.f)
g.DrawString("INPUT:", new Font("Verdana", side*0.3f), Brushes.White, side*8.7f, side/4.5f)
if ((index <> -1) && (bts.[index].Status <> Disabled)) then
g.DrawString(bts.[index].Description, new Font("Verdana", side*0.2f), Brushes.Black, 0.f, side*1.1f)
g.TranslateTransform(-area.X, -area.Y)
type State() =
let mutable radius = 30.f
let mutable center = new PointF(400.f, 150.f)
let mutable area = new RectangleF(center.X - radius, center.Y - radius, radius*2.f, radius*2.f)
let mutable name = ""
let mutable entryAction = ""
let bgC = Brushes.White
let lineUnselected = Pens.Black
let lineSelected = Pens.Red
let mutable lnC = lineUnselected
let mutable selected = false
let mutable initial = false
let fontS = new Font("Courier", radius*0.3f, FontStyle.Bold)
let fontEA = new Font("Courier", radius*0.3f)
let transitions = new ResizeArray<Transition>()
member this.Transitions = transitions
member this.AddTransition(dest:State, cond:string) =
if transitions |> Seq.exists(fun t -> t.Destination = dest) then
let (t:Transition) = transitions |> Seq.find(fun t -> t.Destination = dest)
if not (t.Conditions |> Seq.exists(fun c -> c = cond)) then
t.Conditions.Add(cond)
else
transitions.Add(new Transition(this, Destination = dest, ConditionsString = cond))
member this.Initial
with get() = initial
and set(b) = initial <- b
member this.Contains(p:PointF) =
((radius*radius) >= ((p.X - center.X)*(p.X - center.X) + (p.Y - center.Y)*(p.Y - center.Y)))
member this.Selected
with get() = selected
and set(b) =
selected <- b
if b then
lnC <- lineSelected
else
lnC <- lineUnselected
member this.Radius
with get() = radius
and set(r) =
radius <- r
area.Size <- new SizeF(radius*2.f, radius*2.f)
area.Location <- new PointF(center.X - radius, center.Y - radius)
member this.Location
with get() = center
and set(p) =
center <- p
area.Location <- new PointF(p.X - radius, p.Y - radius)
member this.Name
with get() = name
and set(s) = name <- s
member this.EntryAction
with get() = entryAction
and set(s) = entryAction <- s
member this.Paint(g:Graphics) =
let angle = single(45.*System.Math.PI/180.)
let rName = new RectangleF(center.X - radius*cos(angle), center.Y - radius*sin(angle), radius*2.f*cos(angle), radius*sin(angle))
let rEntry = new RectangleF(center.X - radius*cos(angle), center.Y, radius*2.f*cos(angle), radius*sin(angle))
g.FillEllipse(bgC, area)
g.DrawEllipse(lnC, area)
if initial then
let p = 0.1f
g.DrawEllipse(lnC, new RectangleF(area.X + radius*p, area.Y + radius*p, radius*(2.f - p*2.f), radius*(2.f - p*2.f)))
g.DrawLine(lnC, center.X - radius, center.Y, center.X + radius, center.Y)
g.DrawString(name, fontS, Brushes.Black, rName,
new StringFormat(Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center))
g.DrawString(entryAction, fontEA, Brushes.Black, rEntry,
new StringFormat(Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center))
and Transition(s:State) =
let mutable dest = new State()
let conditions = new ResizeArray<string>()
let lnSelected = Brushes.Red
let lnUnselected = Brushes.Gray
let mutable lnC = new Pen(lnUnselected, 2.f)
let mutable bgC = lnUnselected
let lnWidth = s.Radius*0.3f
let font = new Font("Courier", lnWidth, FontStyle.Bold)
let mutable selected = false
let mutable radius = 0.f
let mutable arcAngle = 0.f
let mutable rtAngle = 0.f
let mutable isLine = true
let mutable rect = new RectangleF()
let w2v = new Drawing2D.Matrix()
let v2w = new Drawing2D.Matrix()
let dist() = sqrt((s.Location.X - dest.Location.X)*(s.Location.X - dest.Location.X) +
(s.Location.Y - dest.Location.Y)*(s.Location.Y - dest.Location.Y))
let chordAngle(c) = 2.f*asin(0.5f*c/radius)*180.f/single(System.Math.PI)
let regMax = new Drawing2D.GraphicsPath()
let regMin = new Drawing2D.GraphicsPath()
let arrow() =
let dim = 0.3f
if isLine then
let cosAlpha = (s.Location.X - dest.Location.X)/dist()
let sinAlpha = (s.Location.Y - dest.Location.Y)/dist()
[| new PointF(dest.Location.X + s.Radius*cosAlpha, dest.Location.Y + s.Radius*sinAlpha);
new PointF(dest.Location.X + s.Radius*((dim + 1.f)*cosAlpha + 0.5f*dim*sinAlpha), dest.Location.Y + s.Radius*((dim + 1.f)*sinAlpha - 0.5f*dim*cosAlpha));
new PointF(dest.Location.X + s.Radius*((dim + 1.f)*cosAlpha - 0.5f*dim*sinAlpha), dest.Location.Y + s.Radius*((dim + 1.f)*sinAlpha + 0.5f*dim*cosAlpha)); |]
else
let t = (1.f - chordAngle(s.Radius)/arcAngle)
let t2 = (1.f - chordAngle(s.Radius*(dim + 1.f)/arcAngle))
let alpha = (-90.f - arcAngle*0.5f + t*arcAngle)*single(System.Math.PI)/180.f
let alpha2 = (-90.f - arcAngle*0.5f + t2*arcAngle)*single(System.Math.PI)/180.f
let pt = [| new PointF(rect.X + rect.Width*0.5f + radius*cos(alpha), rect.Y + rect.Height*0.5f + radius*sin(alpha));
new PointF(rect.X + rect.Width*0.5f + (radius + 0.5f*dim*s.Radius)*cos(alpha2),
rect.Y + rect.Height*0.5f + (radius + 0.5f*s.Radius*dim)*sin(alpha2));
new PointF(rect.X + rect.Width*0.5f + (radius - 0.5f*dim*s.Radius)*cos(alpha2),
rect.Y + rect.Height*0.5f + (radius - 0.5f*s.Radius*dim)*sin(alpha2)); |]
w2v.TransformPoints(pt)
pt
member this.Selected
with get() = selected
and set(b) =
selected <- b
if b then
lnC <- new Pen(lnSelected, 2.f)
bgC <- lnSelected
else
lnC <- new Pen(lnUnselected, 2.f)
bgC <- lnUnselected
member this.Conditions : ResizeArray<string> = conditions
member this.ConditionsString
with get() = "\"" + (String.concat "\", \"" conditions) + "\""
and set(s:string) = conditions.Add(s)
member this.Destination
with get() = dest
and set(d) = dest <- d
member this.Source
with get() = s
member this.Contains(p:PointF) =
if isLine then
let dist = dist()
if dist = 0.f then
rtAngle <- 0.f
else
let h = acos((dest.Location.X - s.Location.X)/dist)*180.f/single(System.Math.PI)
if s.Location.Y > dest.Location.Y then
rtAngle <- -h
else
rtAngle <- h
w2v.Reset()
w2v.Translate(s.Location.X, s.Location.Y)
w2v.Rotate(rtAngle)
regMax.Reset()
regMax.AddRectangle(new RectangleF(0.f, -lnWidth*0.5f, dist, lnWidth))
regMax.Transform(w2v)
regMax.IsVisible(p)
else
regMax.Transform(w2v)
regMin.Transform(w2v)
regMax.IsVisible(p) && not(regMin.IsVisible(p))
member this.PointAt(t) =
if isLine then
let d = dist()
let cosAlpha = (dest.Location.X - s.Location.X)/d
let sinAlpha = (dest.Location.Y - s.Location.Y)/d
let p = new PointF(s.Location.X + t*d*cosAlpha, s.Location.Y + t*d*sinAlpha)
p
else
let alpha = (-90.f - arcAngle*0.5f + t*arcAngle)*single(System.Math.PI)/180.f
let pt = [| new PointF(rect.X + rect.Width*0.5f + radius*cos(alpha), rect.Y + rect.Height*0.5f + radius*sin(alpha)); |]
w2v.TransformPoints(pt)
pt.[0]
member this.Paint(g:Graphics) =
let dist = dist()
let cons = this.ConditionsString
let sSize = g.MeasureString(cons, font)
if dist <= 3.f*s.Radius || dest.Transitions |> Seq.exists(fun t -> t.Destination = s) then
isLine <- false
let k = single(System.Math.PI)/180.f
arcAngle <- 720.f*s.Radius/(dist + 2.f*s.Radius)
let side = max (s.Radius*2.f) (dist/sin(0.5f*arcAngle*k))
radius <- side*0.5f
rect <- new RectangleF((dist - side)*0.5f, radius*(cos(arcAngle*0.5f*k) - 1.f), side, side)
let rMax = new RectangleF(rect.X - lnWidth*0.5f, rect.Y - lnWidth*0.5f, rect.Width + lnWidth, rect.Height + lnWidth)
let rMin = new RectangleF(rect.X + lnWidth*0.5f, rect.Y + lnWidth*0.5f, rect.Width - lnWidth, rect.Height - lnWidth)
if dist = 0.f then
rtAngle <- 0.f
else
let h = acos((dest.Location.X - s.Location.X)/dist)/k
if s.Location.Y > dest.Location.Y then
rtAngle <- -h
else
rtAngle <- h
let gs = g.Save()
v2w.Reset()
w2v.Reset()
w2v.Translate(s.Location.X, s.Location.Y)
w2v.Rotate(rtAngle)
v2w.Translate(-s.Location.X, -s.Location.Y, Drawing2D.MatrixOrder.Append)
v2w.Rotate(-rtAngle, Drawing2D.MatrixOrder.Append)
g.MultiplyTransform(w2v)
g.ResetClip()
g.DrawArc(lnC, rect, -90.f - arcAngle*0.5f, arcAngle)
regMax.Reset()
regMin.Reset()
regMax.AddArc(rMax, -90.f - arcAngle*0.5f, arcAngle)
regMin.AddArc(rMin, -90.f - arcAngle*0.5f, arcAngle)
g.Restore(gs)
else
isLine <- true
g.DrawLine(lnC, s.Location, dest.Location)
g.FillPolygon(bgC, arrow())
let sPos = this.PointAt(0.5f)
g.DrawString(cons, font, Brushes.DarkRed, sPos.X - sSize.Width*0.5f, sPos.Y - sSize.Height*0.5f)
type ReturnType =
| Abort
| Ok
| DeleteState
| None
type RequestType =
| NewState
| NewTransition
type MessageBox() =
let mutable reqType = NewState
let mutable side = 20.f
let mutable area = new RectangleF()
let textBox = new TextBox()
let bgC = Brushes.Black
let btC = Brushes.White
let btCDisabled = Brushes.Gray
let abortBt = new Button(Delete)
let okBt = new Button(Play)
let delBt = new Button(Delete)
let mutable message = ""
let mutable title = ""
let mutable s1 = new State()
let mutable s2 = new State()
let mutable tRect = new RectangleF()
let mutable mRect = new RectangleF()
let mutable s1Rect = new RectangleF()
let mutable s2Rect = new RectangleF()
let mutable trState = new Drawing2D.GraphicsPath()
let mutable trTransition = new Drawing2D.GraphicsPath()
let mutable points = new PointF()
let mutable isVisible = false
let mutable s1Enabled = true
let mutable s2Enabled = false
do
area <- new RectangleF(0.f, 0.f, side*6.5f, side*4.f)
tRect <- new RectangleF(0.f, 0.f, side*5.f, side)
mRect <- new RectangleF(0.f, side*1.5f, side*6.5f, side)
s1Rect <- new RectangleF(0.f, 4.f*side, side*5.f, side)
s2Rect <- new RectangleF(0.f, 5.5f*side, side*5.f, side)
okBt.Side <- side
okBt.Location <- new PointF(5.5f*side, 3.f*side)
okBt.Status <- Disabled
abortBt.Side <- side
abortBt.Location <- new PointF(5.5f*side, 0.f)
delBt.Side <- side*0.5f
delBt.Location <- new PointF(5.5f*side, 4.25f*side)
delBt.Status <- Disabled
trState.AddPolygon([| new PointF(0.f, 4.5f*side); new PointF(0.5f*side, 5.f*side); new PointF(1.f*side, 4.5f*side) |])
trTransition.AddPolygon([| new PointF(4.f*side, 8.5f*side); new PointF(4.5f*side, 9.f*side); new PointF(5.f*side, 8.5f*side) |])
textBox.BorderStyle <- BorderStyle.FixedSingle
textBox.TextAlign <- HorizontalAlignment.Right
textBox.Hide()
member this.TextBox
with get() = textBox
member this.AbortButton
with get() = abortBt
member this.OkButton
with get() = okBt
member this.DeleteButton
with get() = delBt
member this.IsVisible
with get() = isVisible
and set(b) = isVisible <- b
member this.SndStringEnabled
with get() = s2Enabled
and set(b) = s2Enabled <- b
member this.FstStringEnabled
with get() = s1Enabled
and set(b) = s1Enabled <- b
member this.FstState
with get() = s1
and set(s) = s1 <- s
member this.SndState
with get() = s2
and set(s) = s2 <- s
member this.RequestType
with get() = reqType
and set(rt) =
reqType <- rt
match rt with
| NewState ->
area.Size <- new SizeF(side*6.5f, side*4.f)
tRect.Size <- new SizeF(side*5.f, side)
mRect.Size <- new SizeF(side*6.5f, side)
okBt.Location <- new PointF(5.5f*side, 3.f*side)
abortBt.Location <- new PointF(5.5f*side, 0.f)
okBt.HasFocus <- false
okBt.Status <- Disabled
textBox.Enabled <- true
| NewTransition ->
area.Size <- new SizeF(side*6.5f, side*8.f)
tRect.Size <- new SizeF(side*5.f, side)
mRect.Size <- new SizeF(side*6.5f, 2.f*side)
abortBt.Location <- new PointF(5.5f*side, 0.f)
okBt.Location <- new PointF(5.5f*side, 7.f*side)
textBox.Enabled <- false
delBt.Location <- new PointF(5.5f*side, 4.25f*side)
delBt.Status <- Disabled
okBt.HasFocus <- false
okBt.Status <- Disabled
s1Enabled <- true
s2Enabled <- false
s1 <- new State()
s2 <- new State()
member this.Show() =
isVisible <- true
textBox.Text <- ""
textBox.Show()
member this.Hide() =
isVisible <- false
textBox.Hide()
member this.Title
with get() = title
and set(s) = title <- s
member this.Message
with get() = message
and set(s) = message <- s
member this.Contains(p:PointF) =
area.Contains(p)
member this.ButtonSelected(p:PointF) : ReturnType =
let pt = new PointF(p.X - area.X, p.Y - area.Y)
if abortBt.Contains(pt) then
Abort
elif okBt.Contains(pt) && okBt.Status <> Disabled then
Ok
elif delBt.Contains(pt) then
DeleteState
else
None
member this.Points
with get() = points
and set(p:PointF) =
points <- p
match reqType with
| NewState ->
area.Location <- new PointF(p.X - side*0.5f, p.Y - side*5.f)
textBox.Location <- new Point(int(p.X - side*0.5f), int(p.Y - side*2.f))
| NewTransition ->
area.Location <- new PointF(p.X - side*4.5f, p.Y - side*9.f)
textBox.Location <- new Point(int(p.X - side*4.5f), int(p.Y - side*2.f))
member this.Side
with get() = side
and set(s) =
side <- s
match reqType with
| NewState ->
area.Size <- new SizeF(side*6.5f, side*4.f)
tRect.Size <- new SizeF(side*5.f, side)
mRect.Size <- new SizeF(side*6.5f, side)
okBt.Location <- new PointF(5.5f*side, 3.f*side)
textBox.Bounds <- new Rectangle(int(points.X - side*0.5f), int(points.Y - side*2.f), int(side*5.f), int(side))
abortBt.Location <- new PointF(5.5f*side, 0.f)
abortBt.Side <- s
okBt.Location <- new PointF(5.5f*side, 3.f*side)
okBt.Side <- s
| NewTransition ->
area.Size <- new SizeF(side*6.5f, side*8.f)
tRect.Size <- new SizeF(side*5.f, side)
mRect.Size <- new SizeF(side*6.5f, 2.f*side)
s1Rect.Size <- new SizeF(side*5.f, side)
s2Rect.Size <- new SizeF(side*5.f, side)
abortBt.Location <- new PointF(5.5f*side, 0.f)
textBox.Bounds <- new Rectangle(int(points.X - side*4.5f), int(points.Y - side*9.f), int(side*5.f), int(side))
abortBt.Location <- new PointF(5.5f*side, 0.f)
abortBt.Side <- s
okBt.Location <- new PointF(5.5f*side, 7.f*side)
okBt.Side <- s
delBt.Location <- new PointF(5.5f*side, 4.25f*side)
delBt.Side <- s*0.5f
member this.Paint(g:Graphics) =
if isVisible then
match reqType with
| NewState ->
g.TranslateTransform(area.X, area.Y)
g.FillEllipse(bgC, -side*0.5f, -side*0.5f, side, side)
g.FillEllipse(bgC, -side*0.5f, side*3.5f, side, side)
g.FillEllipse(bgC, side*6.f, -side*0.5f, side, side)
g.FillEllipse(bgC, side*6.f, side*3.5f, side, side)
g.FillRectangle(bgC, -side*0.5f, 0.f, area.Width + side, area.Height)
g.FillRectangle(bgC, 0.f, -side*0.5f, area.Width, area.Height + side)
g.FillPath(bgC, trState)
g.DrawString(title, new Font("Verdana", side*0.5f, FontStyle.Bold), btC, tRect)
g.DrawString(message, new Font("Verdana", side*0.4f), btC, mRect)
abortBt.Paint(g)
okBt.HasFocus <- false
okBt.Paint(g)
g.TranslateTransform(-area.X, -area.Y)
| NewTransition ->
g.TranslateTransform(area.X, area.Y)
g.FillEllipse(bgC, -side*0.5f, -side*0.5f, side, side)
g.FillEllipse(bgC, -side*0.5f, side*7.5f, side, side)
g.FillEllipse(bgC, side*6.f, -side*0.5f, side, side)
g.FillEllipse(bgC, side*6.f, side*7.5f, side, side)
g.FillRectangle(bgC, -side*0.5f, 0.f, area.Width + side, area.Height)
g.FillRectangle(bgC, 0.f, -side*0.5f, area.Width, area.Height + side)
g.FillPath(bgC, trTransition)
g.DrawString(title, new Font("Verdana", side*0.5f, FontStyle.Bold), btC, tRect)
g.DrawString(message, new Font("Verdana", side*0.4f), btC, mRect)
if s1Enabled then
g.DrawString("S1: " + s1.Name, new Font("Verdana", side*0.4f, FontStyle.Bold), btC, s1Rect)
else
g.DrawString("S1: " + s1.Name, new Font("Verdana", side*0.4f, FontStyle.Bold), btCDisabled, s1Rect)
if s2Enabled then
g.DrawString("S2: " + s2.Name, new Font("Verdana", side*0.4f, FontStyle.Bold), btC, s2Rect)
else
g.DrawString("S2: " + s2.Name, new Font("Verdana", side*0.4f, FontStyle.Bold), btCDisabled, s2Rect)
abortBt.Paint(g)
okBt.Paint(g)
if not s1Enabled then
delBt.Paint(g)
g.TranslateTransform(-area.X, -area.Y)
type Activity =
| Idle
| RequestingName
| RequestingEntryAction
| RequestingFstState
| RequestingSndState
| RequestingCondition
type Animation =
| OnState
| OnTransition
type Interface() as this =
inherit UserControl()
let tools = new Toolbar()
let mutable lastBtClicked = -1
let mutable lastStClicked = -1
let mutable lastMsClicked = None
let mutable off = new PointF()
let mutable input = ""
let states = new ResizeArray<State>()
let mutable zOrder = []
let mBox = new MessageBox()
let mutable activity = Idle
let mutable ind = -1
let v2w = new Drawing2D.Matrix()
let w2v = new Drawing2D.Matrix()
let timer = new Timer(Interval = 30)
let mutable scrolling = false
let mutable animating = false
let zoomIn = 1.25f
let zoomOut = 1.f/zoomIn
let scroll = 3.f
let mutable mBoxPos = new PointF()
let mutable mBoxTrPos = new PointF()
let mutable lastTrSelected = new Transition(new State())
let mutable trClicked = false
let mutable currentState = new State()
let mutable currentTransition = new Transition(new State())
let mutable t = 0.f
let mutable speed = 1.f
let step = 0.01f
let markerDim = 5.f
let mutable anim = OnState
let mutable animationStatus = ""
let font = new Font("Verdana", 8.f)
let mutable random = false
let rec gradientCircle(g:Graphics, center:PointF, radius:float32, color:Color) =
if int(color.A) > 0 then
let ratio = 1.f
let step = 20
let rect = new RectangleF(center.X - radius, center.Y - radius, radius*2.f, radius*2.f)
gradientCircle(g, center, radius + ratio, Color.FromArgb(max 0 (int(color.A) - step), color))
g.DrawEllipse(new Pen(color, ratio), rect)
let alphaFun(color:Color, t) =
let s = min 1. (max 0. (System.Math.Sin(System.Math.PI*10.*double(t))))
Color.FromArgb(int(s*s*255.), color)
let animation(g:Graphics) =
if animating then
let animC = Color.FromArgb(255, Color.Red)
if t >= 1.f then
t <- 0.f
match anim with
| OnState ->
let founds = currentState.Transitions |> Seq.where(fun (t:Transition) -> t.Conditions |> Seq.exists(fun c -> c = input)) |> Seq.toArray
let n = founds |> Seq.length
if n > 1 then
let rnd = System.Random()
random <- true
currentTransition <- founds.[rnd.Next(n)]
anim <- OnTransition
elif n = 1 then
random <- false
currentTransition <- founds.[0]
anim <- OnTransition
else
animating <- false
if not scrolling then
timer.Stop()
currentState <- states.Find(fun s -> s.Initial)
if lastStClicked <> -1 then
states.[lastStClicked].Selected <- false
lastStClicked <- -1
if trClicked then
trClicked <- false
lastTrSelected.Selected <- false
currentState <- states.Find(fun s -> s.Initial)
tools.Buttons.[0].Status <- Normal
tools.Buttons.[1].Status <- Normal
tools.Buttons.[2].Status <- Disabled
tools.Buttons.[3].Status <- Disabled
tools.Buttons.[3].Status <- Disabled
tools.Buttons.[12].Status <- Normal
tools.Buttons.[13].Status <- Disabled
tools.Buttons.[14].Status <- Disabled
tools.Buttons.[15].Status <- Disabled
tools.Buttons.[16].Status <- Disabled
speed <- 1.0f
animationStatus <- ""
| OnTransition ->
currentState <- currentTransition.Destination
anim <- OnState
else
match anim with
| OnState ->
gradientCircle(g, currentState.Location, currentState.Radius, alphaFun(animC, t))
animationStatus <- "State: " + currentState.Name + "\nTransiting to: -\nInput: " + input
| OnTransition ->
gradientCircle(g, currentTransition.PointAt(t), 0.f, alphaFun(animC, t))
animationStatus <- "State: " + currentState.Name + "\nTransiting to: " + currentTransition.Destination.Name
+ (if random then " (Random)" else "") + "\nInput: " + input
do
this.SetStyle(ControlStyles.OptimizedDoubleBuffer ||| ControlStyles.AllPaintingInWmPaint, true)
this.Controls.Add(tools.TextBox)
this.Controls.Add(mBox.TextBox)
timer.Tick.Add(fun _ ->
if scrolling then