-
Notifications
You must be signed in to change notification settings - Fork 3
/
Shapes.sb
3578 lines (3465 loc) · 97.3 KB
/
Shapes.sb
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
' Shapes
' Version 2.6.1
' Copyright © 2012-2023 Nonki Takahashi. The MIT License.
' Last update 2023-05-29
' Repository https://github.com/nonkit/Shapes
'
' TODO:
' [✓] #31 Support to get Shapes_Init
' [ ] #25 Support sprite array specification
' [ ] #11 Support transparent brush color
' [ ] #7 Support text tag (element)
' [ ] #6 Check illegal behavior in Silverlight environment
' [ ] #3 Bug fix for Silverlight: Lines move after click
' [ ] #2 Bug fix for Silverlight: Rotated triangles move after click
' [ ] #1 Bug fix for Silverlight: Lines come different place
'
title = "Shapes 2.6.1"
pgmname = "Shapes.sb"
GraphicsWindow.Title = "Untitled - " + title
debug = "False"
Not = "False=True;True=False;"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
Not = "False=True;True=False;"
WQ = Text.GetCharacter(34)
qt = WQ
CR = Text.GetCharacter(13)
LF = Text.GetCharacter(10)
LT = "<"
UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LOWER = "abcdefghijkomnopqrstuvwxyz"
DIGIT = "0123456789"
LCHAR = UPPER + LOWER + "_"
TCHAR = LCHAR + DIGIT
Popup_Init()
CS_InitPalette() ' initialize palette for color slider
DrawMenu()
shape = ""
nShapes = 0
iMin = 1
iMax = nShapes
cont = "True" ' continue
Mouse_Init()
KB_Init()
param = "down=True;move=False;up=True;" ' wait to click
Mouse_SetHandler()
While "True"
If clicked Then ' mouse clicked
DetectClickedObject()
DoObject()
param = "down=True;move=False;up=True;"
Mouse_SetHandler()
ElseIf in > out Then ' key input buffer is not empty
KB_InKey()
If c = "^x" Then
obj = "menu" + icut
DoMenu()
ElseIf c = "^c" Then
obj = "menu" + icopy
DoMenu()
ElseIf c = "^v" Then
obj = "menu" + ipaste
DoMenu()
ElseIf c = "^n" Then
New()
ElseIf c = "DEL" Then
If selectedshape <> "" Then
DeleteSelectedShape()
i = selectedshape
select = "False"
ShapeSelect() ' removes pinches
EndIf
ElseIf c = "LEFT" Or c = "RIGHT" Or c = "UP" Or c="DOWN" Then
If selectedshape <> "" Then
i = selectedshape
shp = shape[i]
_x = shp["x"] + arrow_dx[c]
_y = shp["y"] + arrow_dy[c]
MoveShape()
EndIf
EndIf
param = "down=True;move=False;up=True;"
Mouse_SetHandler()
Else
Program.Delay(100)
EndIf
EndWhile
Sub CalcDetectBorder
' param i - index of shapes
shp = shape[i]
If shp["func"] = "line" Then ' line
x = shp["x2"] - shp["x1"]
y = shp["y2"] - shp["y1"]
Math_CartesianToPolar()
If 180 <= a Then
a = a - 180
EndIf
shp["angle"] = a
cx = shp["x"] + Math.Abs(x) / 2
cy = shp["y"] + Math.Abs(y) / 2
len = Math.SquareRoot(x * x + y * y)
shp["_x0"] = Math.Floor(cx - len / 2)
shp["_x1"] = Math.Floor(cx + len / 2)
shp["_y0"] = cy - 4
shp["_y1"] = cy + 4
Else ' rectangle, ellipse or triangle
If shp["func"] = "tri" Then ' triangle
shp["width"] = shp["x3"]
shp["height"] = shp["y2"]
EndIf
shp["_x0"] = shp["x"]
shp["_y0"] = shp["y"]
shp["_x1"] = shp["x"] + shp["width"]
shp["_y1"] = shp["y"] + shp["height"]
EndIf
shape[i] = shp
EndSub
Sub CalcPinchPos
' param i - shape index
' return mxM, myM - center of pinch
shp = shape[i]
_x = shp["x"]
_y = shp["y"]
width = shp["width"]
height = shp["height"]
angle = shp["angle"]
param = "width=0;height=0;angle=" + angle + ";"
param["cx"] = _x + width / 2
param["cy"] = _y + height / 2
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Stack.PushValue("local", _x)
Stack.PushValue("local", _y)
param["x"] = param["cx"]
param["y"] = param["cy"] - 10
Shapes_CalcRotatePos()
mxM = x
myM = y
_y = Stack.PopValue("local")
_x = Stack.PopValue("local")
y = Stack.PopValue("local")
x = Stack.PopValue("local")
EndSub
Sub CalcVertexes
' param i - pinch index
' param angle - angle of a shape
' param selectedshape - parent shape index of pinch
' param shape[] - shape data
' return mxM, myM - free vertex
' return mxD, myD - fixed vertex
shp = shape[selectedshape]
If shp["func"] = "line" Then
If i = 1 Then
mxM = shp["x"] + shp["x1"]
myM = shp["y"] + shp["y1"]
mxD = shp["x"] + shp["x2"]
myD = shp["y"] + shp["y2"]
ElseIf i = 2 Then
mxM = shp["x"] + shp["x2"]
myM = shp["y"] + shp["y2"]
mxD = shp["x"] + shp["x1"]
myD = shp["y"] + shp["y1"]
EndIf
Else
mxM = shp["x"]
myM = shp["y"]
mxD = mxM
myD = myM
If i = 1 Or i = 3 Then
mxD = mxD + shp["width"]
EndIf
If i = 1 Or i = 2 Then
myD = myD + shp["height"]
EndIf
If i = 2 Or i = 4 Then
mxM = mxM + shp["width"]
EndIf
If i = 3 Or i = 4 Then
myM = myM + shp["height"]
EndIf
param = "x=" + mxD + ";y=" + myD + ";width=0;height=0;angle=" + angle
param = param + ";cx=" + (mxD + mxM) / 2 + ";cy=" + (myD + myM) / 2 + ";"
Shapes_CalcRotatePos()
mxD = x
myD = y
param["x"] = mxM
param["y"] = myM
Shapes_CalcRotatePos()
mxM = x
myM = y
EndIf
EndSub
Sub DeleteSelectedShape
' param selectedshape
shp = shape[selectedshape]
Shapes.Remove(shp["obj"])
nShapes = nShapes - 1
For _i = selectedshape To nShapes
shape[_i] = shape[_i + 1]
EndFor
shape[nShapes + 1] = ""
selectedshape = ""
EndSub
Sub DetectClickedObject
' mxD, myD - clicked position
' return obj - name of object (menu, shape or pinch)
Stack.PushValue("local", i)
obj = ""
For i = 1 To nPinch
pnch = pinch[i]
If pnch["_x0"] <= mxD And mxD <= pnch["_x1"] And pnch["_y0"] <= myD And myD <= pnch["_y1"] Then
obj = "pinch" + i
Goto dco_exit
EndIf
EndFor
For i = nShapes To 1 Step -1
shp = shape[i]
param = "x=" + mxD + ";y=" + myD + ";width=0;height=0;"
param["cx"] = (shp["_x0"] + shp["_x1"]) / 2
param["cy"] = (shp["_y0"] + shp["_y1"]) / 2
param["angle"] = -shp["angle"]
Shapes_CalcRotatePos()
If shp["_x0"] <= x And x <= shp["_x1"] And shp["_y0"] <= y And y <= shp["_y1"] Then
If shp["func"] = "rect" Or shp["func"] = "line" Then
obj = "shape" + i
Goto dco_exit
ElseIf shp["func"] = "ell" Then
x = (x - param["cx"]) / shp["width"] * 2
y = (y - param["cy"]) / shp["height"] * 2
r = Math.SquareRoot(x * x + y * y)
If r <= 1 Then
obj = "shape" + i
Goto dco_exit
EndIf
ElseIf shp["func"] = "tri" Then
x = (x - param["cx"]) / shp["width"] * 2
y = (y - shp["_y1"]) / shp["height"]
r = Math.Abs(x) + Math.Abs(y)
If r <= 1 And y <= 0 Then
obj = "shape" + i
Goto dco_exit
EndIf
EndIf
EndIf
EndFor
For i = 1 To nMenu
mnu = menu[i]
If mnu["func"] <> "" And mnu["_x0"] <= mxD And mxD <= mnu["_x1"] And mnu["_y0"] <= myD And myD <= mnu["_y1"] Then
obj = "menu" + i
Goto dco_exit
EndIf
EndFor
dco_exit:
If obj = "" And selectedshape <> "" Then
i = selectedshape
select = "False"
ShapeSelect()
EndIf
i = Stack.PopValue("local")
EndSub
Sub DoMenu ' if a menu item clicked do the funciton
If Text.StartsWith(obj, "menu") Then
param = "down=False;move=False;up=False;" ' wait button pushed
Mouse_SetHandler()
i = Text.GetSubTextToEnd(obj, 5)
obj = ""
mnu = menu[i]
func = mnu["func"]
If i <= nMenu Then
select = "True"
ItemSelect() ' shows menu item frame
EndIF
If selecteditem = i And Text.IsSubText("rect|ell|tri|line", func) Then
If mode = "repeat" Then
mode = "single"
Shapes.HideShape(repeat[func])
Else
mode = "repeat"
Shapes.ShowShape(repeat[func])
EndIf
Else
mode = "single"
EndIf
selecteditem = i
If Text.IsSubText("open|save|cut|paste|rect|ell|tri|line", func) And selectedshape <> "" Then
i = selectedshape
select = "False"
ShapeSelect() ' removes pinches if a shape selected
If func = "cut" Then
selectedshape = i
EndIf
EndIf
If func = "open" Then ' open shapes from file
ReadShapes()
ElseIf func = "save" Then ' save shapes to file
WriteShapes()
ElseIf func = "cut" Then
If selectedshape <> "" Then
clipboard = shape[selectedshape]
DeleteSelectedShape()
EndIf
ElseIf func = "copy" Then
If selectedshape <> "" Then
clipboard = shape[selectedshape]
index = "1=x;2=y;3=_x0;4=_x1;5=_y0;6=_y1;"
For _i = 1 To 6
clipboard[index[_i]] = clipboard[index[_i]] + 10
EndFor
EndIf
ElseIf func = "paste" Then
If clipboard <> "" Then
nShapes = nShapes + 1
shape[nShapes] = clipboard
index = "1=x;2=y;3=_x0;4=_x1;5=_y0;6=_y1;"
For _i = 1 To 6
clipboard[index[_i]] = clipboard[index[_i]] + 10
EndFor
iMin = nShapes
iMax = nShapes
scale = 1
shX = 0
shY = 0
Shapes_Add()
i = nShapes
select = "True"
ShapeSelect()
EndIf
ElseIf func = "rect" Or func = "ell" Or func = "tri" Then
' rectangle, ellipse or triangle
While "True"
WaitToClick() ' to get mxD, myD
DetectClickedObject()
If Text.StartsWith(obj, "menu") Then
Goto dm_exit ' cancel to register shape
EndIf
obj = ""
mxM = mxD
myM = myD
angle = 0
WaitToReleaseS() ' to get mxU, myU
resize = "False"
nShapes = nShapes + 1
i = nShapes ' to set shape[nShapes]
RegisterShapeData()
If w = 0 And h = 0 Then
shape[i] = ""
nShapes = nShapes - 1
Goto dm_exit ' cansel to register zero sized shape
EndIf
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
GraphicsWindow.PenColor = pcolor
EndIf
shp = shape[nShapes]
If func = "rect" Then
shp["obj"] = Shapes.AddRectangle(w, h)
ElseIf func = "ell" Then
shp["obj"] = Shapes.AddEllipse(w, h)
ElseIf func = "tri" Then
shp["obj"] = Shapes.AddTriangle(xt, 0, 0, h, w, h)
EndIf
Shapes.Move(shp["obj"], xmin, ymin)
shape[nShapes] = shp
If mode = "single" Then
Goto dm_exit
EndIf
EndWhile
ElseIf func = "line" Then ' line
While "True"
WaitToClick() ' to get mxD, myD
DetectClickedObject()
If Text.StartsWith(obj, "menu") Then
Goto dm_exit ' cancel to register line
EndIf
obj = ""
mxM = mxD
myM = myD
WaitToReleaseS() ' to get mxU, myU
nShapes = nShapes + 1
i = nShapes ' to set shape[nShapes]
RegisterShapeData()
If x1 = x2 And y1 = y2 Then
shape[i] = ""
nShapes = nShapes - 1
Goto dm_exit ' cansel to register zero sized line
EndIf
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
GraphicsWindow.PenColor = pcolor
EndIf
shp = shape[nShapes]
shp["obj"] = Shapes.AddLine(x1, y1, x2, y2)
Shapes.Move(shp["obj"], xmin, ymin)
shape[nShapes] = shp
If mode = "single" Then
Goto dm_exit
EndIf
EndWhile
ElseIf func = "pw" Then ' pen width
If pen = nPen Then
pen = 1
Else
pen = pen + 1
EndIf
pwidth = pw[pen]
mnu = menu[i]
x = mnu["_x0"]
y = mnu["_y0"]
size = mnu["_x1"] - x
GraphicsWindow.BrushColor = "#EEEEEE"
GraphicsWindow.FillRectangle(x, y, size, size)
DrawMenuItem()
ElseIf func = "pc" Then ' pen color
color = pcolor
CS_ShowPopup()
pcolor = color
DrawMenuItem()
ElseIf func = "bc" Then ' brush color
color = bcolor
CS_ShowPopup()
bcolor = color
DrawMenuItem()
ElseIf func = "gr" Then ' grid
' grid = 0 ... no grid
' grid = 1 ... light grid
' grid = 2 ... dark grid
grid = grid + 1
If 2 < grid Then
grid = 0
EndIf
If grid <> 1 Then
GraphicsWindow.BrushColor = bgColor[grid]
GraphicsWindow.FillRectangle(0, sizeMenu + 20, gw, gh - sizeMenu - 20)
EndIf
If 0 < grid Then
colorGrid = colorGrids[grid]
DrawGrid()
EndIf
ElseIf func = "menubar" And selectedshape <> "" Then
i = selectedshape
select = "False"
ShapeSelect()
EndIf
If selectedshape <> "" And (func = "pw" Or func = "pc" Or func = "bc") Then
i = selectedshape
select = "False"
ShapeSelect()
shp = shape[i]
If func = "pw" Then
shp["pw"] = pwidth
If pwidth = 0 And shp["pc"] <> "" Then
shp["pc"] = ""
ElseIf 0 < pwidth And shp["pc"] = "" Then
shp["pc"] = pcolor
EndIf
ElseIf func = "pc" Then
shp["pc"] = pcolor
ElseIf func = "bc" Then
shp["bc"] = color
EndIf
shape[i] = shp
iMin = i ' to re-size Shapes
iMax = nShapes ' to keep z-order of Shapes
Shapes_Remove()
scale = 1
shX = 0
shY = 0
Shapes_Add()
select = "True"
ShapeSelect()
EndIf
dm_exit:
i = selecteditem
select = "False"
ItemSelect() ' removes menu item frame
If Text.IsSubText("rect|ell|tri|line", func) Then
Shapes.HideShape(repeat[func])
EndIf
EndIf
EndSub
Sub DoObject
' param obj - clicked object
While obj <> ""
fromMenu = "False"
DoMenu()
If obj <> "" Then
DoShape()
EndIf
If obj <> "" Then
DoPinch()
EndIf
EndWhile
EndSub
Sub DoPinch ' if a pinch clicked then rotate or re-size a shape
' param obj - clicked object
' param selectedshape - parent shape index of the pinch
If Text.StartsWith(obj, "pinch") Then
i = Text.GetSubTextToEnd(obj, 6)
obj = ""
If i = 5 Then ' rotate a shape
i = selectedshape
WaitToReleaseR() ' to get angle
shp = shape[i]
Shapes.Rotate(shp["obj"], angle)
shp["angle"] = Math.Floor(angle)
shape[i] = shp
select = "False"
ShapeSelect() ' remove pinches
select = "True"
ShapeSelect() ' show pinches
Else ' re-size a shape
shp = shape[selectedshape]
angle = shp["angle"]
CalcVertexes() ' to get mxM, myM, mxD, myD
func = shp["func"]
WaitToReleaseS() ' to get mxU, myU
i = selectedshape
select = "False"
ShapeSelect() ' remove pinches
' selectedshape is broken in ShapeSelect() so use i instead
RegisterShapeMetrics() ' re-size shape[i]
iMin = i ' to re-size Shapes
iMax = nShapes ' to keep z-order of Shapes
Shapes_Remove()
scale = 1
shX = 0
shY = 0
Shapes_Add()
select = "True"
ShapeSelect() ' show pinches
EndIf
EndIf
EndSub
Sub DoShape ' if a shape clicked then move it
If Text.StartsWith(obj, "shape") Then
If selectedshape <> "" Then ' if other shape selected
i = selectedshape
select = "False"
ShapeSelect() ' removes pinches
EndIf
i = Text.GetSubTextToEnd(obj, 6) ' shape index
select = "True"
ShapeSelect() ' shows pinches
Mouse_SetHandler()
WaitToReleaseM() ' for moving a shape
clicked = "False"
obj = ""
EndIf
EndSub
Sub DrawGrid
' param colorGrid - color for grid
GraphicsWindow.PenWidth = 1
For x = 0 To gw - 1 Step 10
If Math.Remainder(x, 100) = 0 Then
GraphicsWindow.PenColor = colorGrid[100]
ElseIf Math.Remainder(x, 50) = 0 Then
GraphicsWindow.PenColor = colorGrid[50]
Else
GraphicsWindow.PenColor = colorGrid[10]
EndIf
GraphicsWindow.DrawLine(x, sizeMenu + 20, x, gh)
EndFor
For y = sizeMenu + 20 To gh - 1 Step 10
If Math.Remainder(y, 100) = 0 Then
GraphicsWindow.PenColor = colorGrid[100]
ElseIf Math.Remainder(y, 50) = 0 Then
GraphicsWindow.PenColor = colorGrid[50]
Else
GraphicsWindow.PenColor = colorGrid[10]
EndIf
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub DrawMenu
' return menu[] - array of menu data
pwidth = GraphicsWindow.PenWidth
cxMenu = 6
cyMenu = 6
sizeMenu = 40
nMenu = 13
grid = 0
bgColor = "0=White;2=#000033;" ' background for grid
colorGrids[1] = "10=#11000000;50=#55000000;100=#AA000000;"
colorGrids[2] = "10=#11FFFFFF;50=#55FFFFFF;100=#AAFFFFFF;"
GraphicsWindow.BrushColor = "#EEEEEE"
GraphicsWindow.FillRectangle(0, 0, gw, 20 + sizeMenu)
pw = "1=2;2=4;3=8;4=16;5=0;6=1;" ' pen width
pen = 1 ' pen width index
nPen = 6 ' number of pen width
For i = 1 To nMenu
xMenu = cxMenu + Math.Floor((i - 1) / 1) * (sizeMenu + 4)
yMenu = cyMenu + Math.Remainder(i - 1, 1) * (sizeMenu + 14)
GraphicsWindow.BrushColor = "#EEEEEE"
GraphicsWindow.FillRectangle(xMenu, yMenu, sizeMenu, sizeMenu)
mnu = menu[i]
mnu["_x0"] = xMenu
mnu["_y0"] = yMenu
mnu["_x1"] = xMenu + sizeMenu
mnu["_y1"] = yMenu + sizeMenu
menu[i] = mnu
DrawMenuItem()
EndFor
nMenu = nMenu + 1
imenubar = nMenu
mnu = menu[i]
mnu["_x0"] = 0
mnu["_y0"] = 0
mnu["_x1"] = gw
mnu["_y1"] = 20 + sizeMenu
mnu["func"] = "menubar"
menu[i] = mnu
EndSub
Sub DrawMenuItem
' param i - item number
' param pwidth - pen width
' param pcolor - pen color
' param bcolor - brush color
margin = 4
mnu = menu[i]
x = mnu["_x0"]
y = mnu["_y0"]
size = mnu["_x1"] - x
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 2
GraphicsWindow.FontBold = "False"
GraphicsWindow.FontSize = 8
url = "http://www.nonkit.com/smallbasic.files/"
If i = 1 Then
mnu["func"] = "open"
GraphicsWindow.DrawImage(url + "open.png", x, y)
itemname[i] = "Open"
ElseIf i = 2 Then
mnu["func"] = "save"
GraphicsWindow.DrawImage(url + "save.png", x, y)
Program.Delay(msWait) ' ? for Silverlight
itemname[i] = "Save"
ElseIf i = 3 Then
icut = i ' for short cut key
mnu["func"] = "cut"
' initialize shapes
Shapes_Init()
nShapes = Array.GetItemCount(shape)
' add shapes
scale = 0.11
iMin = 1
iMax = nShapes
Shapes_Add()
x = x + 14
Program.Delay(msWait)
Shapes_Move()
x = x - 14
iMax = 3
For t = 0 To 360 * 0.3
angle = 30 - 30 * Math.Cos(t * Math.Pi / 180)
Shapes_Rotate()
EndFor
itemname[i] = "Cut"
ElseIf i = 4 Then
icopy = i ' for short cut key
mnu["func"] = "copy"
GraphicsWindow.DrawImage(url + "copy.png", x, y)
Program.Delay(msWait) ' ? for Silverlight
itemname[i] = "Copy"
ElseIf i = 5 Then
ipaste = i ' for short cut key
mnu["func"] = "paste"
GraphicsWindow.DrawImage(url + "paste.png", x, y)
Program.Delay(msWait) ' ? for Silverlight
itemname[i] = "Paste"
ElseIf i = 6 Then
mnu["func"] = "rect"
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
itemname[i] = "Rectangle"
repeat["rect"] = Shapes.AddImage(url + "repeat.png")
Shapes.Move(repeat["rect"], x, y) ' for consecutive shapes addition
Shapes.HideShape(repeat["rect"])
ElseIf i = 7 Then
mnu["func"] = "ell"
GraphicsWindow.DrawEllipse(x + margin, y + margin, size - margin * 2, size - margin * 2)
itemname[i] = "Ellipse"
repeat["ell"] = Shapes.AddImage(url + "repeat.png")
Shapes.Move(repeat["ell"], x, y) ' for consecutive shapes addition
Shapes.HideShape(repeat["ell"])
ElseIf i = 8 Then
mnu["func"] = "tri"
x1 = x + size / 2
y1 = y + margin
x2 = x + margin
y2 = y + size - margin
x3 = x + size - margin
y3 = y + size - margin
GraphicsWindow.DrawTriangle(x1, y1, x2, y2, x3, y3)
itemname[i] = "Triangle"
repeat["tri"] = Shapes.AddImage(url + "repeat.png")
Shapes.Move(repeat["tri"], x, y) ' for consecutive shapes addition
Shapes.HideShape(repeat["tri"])
ElseIf i = 9 Then
mnu["func"] = "line"
x1 = x + margin
y1 = y + margin
x2 = x + size - margin
y2 = y + size - margin
GraphicsWindow.DrawLine(x1, y1, x2, y2)
itemname[i] = "Line"
repeat["line"] = Shapes.AddImage(url + "repeat.png")
Shapes.Move(repeat["line"], x, y) ' for consecutive shapes addition
Shapes.HideShape(repeat["line"])
ElseIf i = 10 Then
mnu["func"] = "pw"
GraphicsWindow.PenWidth = pwidth
x1 = x + margin
y1 = y + size / 2
x2 = x + size - margin
y2 = y + size / 2
GraphicsWindow.DrawLine(x1, y1, x2, y2)
itemname[i] = "Pen Width"
ElseIf i = 11 Then
mnu["func"] = "pc"
margin = 6
GraphicsWindow.PenWidth = 4
GraphicsWindow.PenColor = pcolor
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
itemname[i] = "Pen Color"
ElseIf i = 12 Then
mnu["func"] = "bc"
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.FillRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 2
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
itemname[i] = "Brush Color"
ElseIf i = 13 Then
mnu["func"] = "gr"
GraphicsWindow.PenColor = "#AAAAAA"
GraphicsWindow.PenWidth = 1
For _x = x + margin To x + size - margin Step (size - margin * 2) / 4
GraphicsWindow.DrawLine(_x, y + margin, _x, y + size - margin)
EndFor
For _y = y + margin To y + size - margin Step (size - margin * 2) / 4
GraphicsWindow.DrawLine(x + margin, _y, x + size - margin, _y)
EndFor
itemname[i] = "Grid"
EndIf
menu[i] = mnu
If itemname[i] <> "" And oItem[i] = "" Then
GraphicsWindow.BrushColor = "Black"
oItem[i] = Shapes.AddText(itemname[i])
Shapes.Move(oItem[i], x + margin, y + size)
EndIf
GraphicsWindow.FontBold = "True"
GraphicsWindow.FontSize = 12
EndSub
Sub DumpBuf
If sp[10] = "" Then
For i = 1 To 10
sp[10] = sp[10] + " "
EndFor
EndIf
TextWindow.WriteLine(Text.GetSubText(buf, p - 10, 21))
TextWindow.WriteLine(sp[10] + "^" + sp[10])
EndSub
Sub DumpShape
TextWindow.WriteLine("shX=" + shX)
TextWindow.WriteLine("shY=" + shY)
TextWindow.WriteLine("nShapes=" + nShapes)
TextWindow.WriteLine("Array.GetItemCount(shape)=" + Array.GetItemCount(shape))
TextWindow.WriteLine("Array.GetAllIndices(shape)=" + Array.GetAllIndices(shape))
TextWindow.WriteLine("iMin=" + iMin)
TextWindow.WriteLine("iMax=" + iMax)
_index = Array.GetAllIndices(shape)
For i = 1 To Array.GetItemCount(shape)
TextWindow.WriteLine("shape[" + _index[i] + "]=" + WQ + shape[i] + WQ)
EndFor
EndSub
Sub GenerateSB
Stack.PushValue("local", filename)
buf = "' "
buf = buf + Clock.Year + "-"
month = Clock.month
If Text.GetLength(month) = 1 Then
buf = buf + "0"
EndIf
buf = buf + month + "-"
day = Clock.Day
If Text.GetLength(day) = 1 Then
buf = buf + "0"
EndIf
buf = buf + day + " " + Clock.Time + " Shapes generated"
buf = buf + " by " + title + "." + CR + LF
buf = buf + "'" + CR + LF
buf = buf + "SB_Workaround()" + CR + LF
buf = buf + "gw = 598" + CR + LF
buf = buf + "gh = 428" + CR + LF
buf = buf + "GraphicsWindow.Width = gw" + CR + LF
buf = buf + "GraphicsWindow.Height = gh" + CR + LF
buf = buf + "' initialize shapes" + CR + LF
buf = buf + "Shapes_Init()" + CR + LF
buf = buf + "' add shapes" + CR + LF
buf = buf + "scale = 1" + CR + LF
buf = buf + "angle = 0" + CR + LF
buf = buf + "iMin = 1" + CR + LF
buf = buf + "iMax = " + nShapes + CR + LF
buf = buf + "Shapes_Add()" + CR + LF
buf = buf + "Sub " + "Shapes_Init" + CR + LF
buf = buf + " ' Shapes | Initialize shapes data" + CR + LF
buf = buf + " ' return shX, shY - current position of shapes" + CR + LF
buf = buf + " ' return shape - array of shapes" + CR + LF
If nShapes > 0 Then
shp = shape[1]
xmin = shp["x"]
ymin = shp["y"]
xmax = shp["x"]
ymax = shp["y"]
EndIf
For i = 2 To nShapes
shp = shape[i]
If shp["x"] < xmin Then
xmin = shp["x"]
EndIf
If shp["y"] < ymin Then
ymin = shp["y"]
EndIf
If xmax < shp["x"] Then
xmax = shp["x"]
EndIf
If ymax < shp["y"] Then
ymax = shp["y"]
EndIf
EndFor
buf = buf + " shX = " + xmin + " ' x offset" + CR + LF
buf = buf + " shY = " + ymin + " ' y offset" + CR + LF
buf = buf + " shape = " + WQ + WQ + CR + LF
For i = 1 To nShapes
shp = shape[i]
If shp["func"] = "rect" Or shp["func"] = "ell" Then
buf = buf + " shape[" + i + "] = " + WQ + "func=" + shp["func"] + ";"
buf = buf + "x=" + (shp["x"] - xmin) + ";"
buf = buf + "y=" + (shp["y"] - ymin) + ";"
buf = buf + "width=" + shp["width"] + ";"
buf = buf + "height=" + shp["height"] + ";"
If shp["angle"] <> 0 And shp["angle"] <> "" Then
buf = buf + "angle=" + shp["angle"] + ";"
EndIf
buf = buf + "bc=" + shp["bc"] + ";"
ElseIf shp["func"] = "tri" Then
buf = buf + " shape[" + i + "] = " + WQ + "func=tri;"
buf = buf + "x=" + (shp["x"] - xmin) + ";"
buf = buf + "y=" + (shp["y"] - ymin) + ";"
buf = buf + "x1=" + shp["x1"] + ";"
buf = buf + "y1=" + shp["y1"] + ";"
buf = buf + "x2=" + shp["x2"] + ";"
buf = buf + "y2=" + shp["y2"] + ";"
buf = buf + "x3=" + shp["x3"] + ";"
buf = buf + "y3=" + shp["y3"] + ";"
If shp["angle"] <> 0 And shp["angle"] <> "" Then
buf = buf + "angle=" + shp["angle"] + ";"
EndIf
buf = buf + "bc=" + shp["bc"] + ";"
ElseIf shp["func"] = "line" Then
buf = buf + " shape[" + i + "] = " + WQ + "func=line;"
buf = buf + "x=" + (shp["x"] - xmin) + ";"
buf = buf + "y=" + (shp["y"] - ymin) + ";"
buf = buf + "x1=" + shp["x1"] + ";"
buf = buf + "y1=" + shp["y1"] + ";"
buf = buf + "x2=" + shp["x2"] + ";"
buf = buf + "y2=" + shp["y2"] + ";"
ElseIf shp["func"] = "text" Then
buf = buf + " shape[" + i + "] = " + WQ + "func=text;"
buf = buf + "x=" + (shp["x"] - xmin) + ";"
buf = buf + "y=" + (shp["y"] - ymin) + ";"
buf = buf + "text=" + shp["text"] + ";"
buf = buf + "fs=" + shp["fs"] + ";"
If shp["fn"] <> "" Then
buf = buf + "fn=" + shp["fn"] + ";"
EndIf
If shp["fb"] <> "" Then
buf = buf + "fb=" + shp["fb"] + ";"
EndIf
If shp["fi"] <> "" Then
buf = buf + "fi=" + shp["fi"] + ";"
EndIf
buf = buf + "bc=" + shp["bc"] + ";"
EndIf
If shp["pw"] > 0 Then
buf = buf + "pc=" + shp["pc"] + ";"
EndIf
If shp["func"] <> "text" Then
buf = buf + "pw=" + shp["pw"] + ";"
EndIf
buf = buf + WQ + CR + LF
EndFor
buf = buf + "EndSub" + CR + LF
filename = Program.Directory + "\" + pgmname
subname = "Math_CartesianToPolar"
SB_AppendSub()
buf = buf + CR + LF
subname = "SB_RotateWorkaround"
SB_AppendSub()
buf = buf + CR + LF
subname = "SB_Workaround"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_Add"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_CalcRotatePos"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_CalcWidthAndHeight"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_Move"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_Remove"
SB_AppendSub()
buf = buf + CR + LF
subname = "Shapes_Rotate"
SB_AppendSub()
buf = buf + CR + LF
filename = Stack.PopValue("local")
EndSub
Sub GenerateSVG
sp[4] = " "
sp[6] = " "
sp[8] = " "
buf = LT + "svg width=" + WQ + gw + WQ + " "
buf = buf + "height=" + WQ + gh + WQ + ">" + CR + LF
buf = buf + " " + LT + "defs>" + CR + LF
buf = buf + sp[4] + LT + "g id=" + WQ + "g1" + WQ + ">" + CR + LF
If nShapes > 0 Then
shp = shape[1]
xmin = shp["x"]
ymin = shp["y"]
xmax = shp["x"]
ymax = shp["y"]
EndIf
For i = 2 To nShapes
shp = shape[i]
If shp["x"] < xmin Then
xmin = shp["x"]
EndIf
If shp["y"] < ymin Then
ymin = shp["y"]
EndIf
If xmax < shp["x"] Then
xmax = shp["x"]
EndIf
If ymax < shp["y"] Then
ymax = shp["y"]
EndIf
EndFor
For i = 1 To nShapes
shp = shape[i]
If shp["func"] = "rect" Then
buf = buf + sp[6] + LT + "rect "
buf = buf + "x=" + WQ + Math.Floor(shp["x"] + shp["pw"] / 2 - xmin) + WQ + " "
buf = buf + "y=" + WQ + Math.Floor(shp["y"] + shp["pw"] / 2 - ymin) + WQ + " "
buf = buf + "width=" + WQ + (shp["width"] - shp["pw"]) + WQ + " "
buf = buf + "height=" + WQ + (shp["height"] - shp["pw"]) + WQ + " "
If shp["angle"] <> 0 And shp["angle"] <> "" Then
buf = buf + CR + LF + sp[8]
buf = buf + "transform=" + WQ + "rotate(" + shp["angle"] + " "
buf = buf + Math.Floor(shp["width"] / 2 + shp["x"] - xmin) + " "
buf = buf + Math.Floor(shp["height"] / 2 + shp["y"] - ymin) + ")" + WQ + " "
EndIf
buf = buf + CR + LF + sp[8]
buf = buf + "style=" + WQ + "fill:" + Text.ConvertToLowerCase(shp["bc"]) + ";"
ElseIf shp["func"] = "ell" Then
buf = buf + sp[6] + LT + "ellipse "
cx = Math.Floor(shp["width"] / 2 + shp["x"] - xmin)
cy = Math.Floor(shp["height"] / 2 + shp["y"] - ymin)
rx = Math.Floor((shp["width"] - shp["pw"]) / 2)
ry = Math.Floor((shp["height"] - shp["pw"]) / 2)
buf = buf + "cx=" + WQ + cx + WQ + " "
buf = buf + "cy=" + WQ + cy + WQ + " "
buf = buf + "rx=" + WQ + rx + WQ + " "
buf = buf + "ry=" + WQ + ry + WQ + " "
If shp["angle"] <> 0 And shp["angle"] <> "" Then