Microsoft Small Basic

Program Listing: TFD171
' Flag of China
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-10-20

GraphicsWindow.Title = "Flag of China"
SB_Workaround()
Init()
' initialize error shapes
Shapes_Init()
name = "China"
scale = gw / 360
Group_Add()

Sub Init
gw = 598
gh = gw / 1.5
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
angle = 0
EndSub

Sub Group_Add
' Group | add shapes to a group
' param name - group name
' param shX, shY, origin of shape array
' param scale - scale of group
' param shape[] - shape array
' param nGroup - number of group
' return nGroup - updated number of group
' return group - group array
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
nGroup = nGroup + 1
grp = ""
grp["name"] = name
grp["x"] = shX
grp["y"] = shY
grp["angle"] = 0
grp["dir"] = 1
Shapes_CalcWidthAndHeight()
grp["width"] = shWidth
grp["cx"] = shWidth / 2
grp["height"] = shHeight
s = scale
grp["scale"] = s
For i = 1 To Array.GetItemCount(shape)
shp = shape[i]
GraphicsWindow.PenWidth = shp["pw"] * s
If shp["pw"] > 0 Then
GraphicsWindow.PenColor = shp["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text|btn", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
EndIf
If Text.IsSubText("text|btn", shp["func"]) Then
If silverlight Then
fs = Math.Floor(shp["fs"] * 0.9)
Else
fs = shp["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
If shp["fb"] = "False" Then
GraphicsWindow.FontBold = "False"
Else
GraphicsWindow.FontBold = "True"
EndIf
EndIf
If shp["func"] = "rect" Then
shp["obj"] = Shapes.AddRectangle(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "ell" Then
shp["obj"] = Shapes.AddEllipse(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "tri" Then
shp["obj"] = Shapes.AddTriangle(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s, shp["x3"] * s, shp["y3"] * s)
ElseIf shp["func"] = "line" Then
shp["obj"] = Shapes.AddLine(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s)
ElseIf shp["func"] = "text" Then
shp["obj"] = Shapes.AddText(shp["text"])
EndIf
x = shp["x"]
y = shp["y"]
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(shp["angle"])
SB_RotateWorkaround()
shp["wx"] = x
shp["wy"] = y
EndIf
If shp["func"] = "btn" Then
shp["obj"] = Controls.AddButton(shp["caption"], shX + x * s, shY + y * s)
Else
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
EndIf
If Text.IsSubText("rect|ell|tri|text", shp["func"]) And (shp["angle"] <> 0) And (shp["angle"] <> "") Then
Shapes.Rotate(shp["obj"], shp["angle"])
EndIf
shape[i] = shp
EndFor
grp["shape"] = shape
group[nGroup] = grp
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub SB_RotateWorkaround
' Small Basic | Rotate workaround for Silverlight
' param shp - current shape
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shp["func"] = "tri" Then
x1 = -Math.Floor(shp["x3"] / 2)
y1 = -Math.Floor(shp["y3"] / 2)
ElseIf shp["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shp["x1"] - shp["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shp["y1"] - shp["y2"]) / 2)
EndIf
ox = x - x1
oy = y - y1
x = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + ox
y = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + oy
EndSub

Sub SB_Workaround
' Small Basic | Workaround for Silverlight
' returns silverlight - "True" if in remote
color = GraphicsWindow.GetPixel(0, 0)
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
EndIf
EndSub

Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' param shape[] - shape array
' return shWidth, shHeight - total size of shapes
For i = i To Array.GetItemCount(shape)
shp = shape[i]
If shp["func"] = "tri" Or shp["func"] = "line" Then
xmin = shp["x1"]
xmax = shp["x1"]
ymin = shp["y1"]
ymax = shp["y1"]
If shp["x2"] < xmin Then
xmin = shp["x2"]
EndIf
If xmax < shp["x2"] Then
xmax = shp["x2"]
EndIf
If shp["y2"] < ymin Then
ymin = shp["y2"]
EndIf
If ymax < shp["y2"] Then
ymax = shp["y2"]
EndIf
If shp["func"] = "tri" Then
If shp["x3"] < xmin Then
xmin = shp["x3"]
EndIf
If xmax < shp["x3"] Then
xmax = shp["x3"]
EndIf
If shp["y3"] < ymin Then
ymin = shp["y3"]
EndIf
If ymax < shp["y3"] Then
ymax = shp["y3"]
EndIf
EndIf
shp["width"] = xmax - xmin
shp["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shp["x"] + shp["width"]
shHeight = shp["y"] + shp["height"]
Else
If shWidth < shp["x"] + shp["width"] Then
shWidth = shp["x"] + shp["width"]
EndIf
If shHeight < shp["y"] + shp["height"] Then
shHeight = shp["y"] + shp["height"]
EndIf
EndIf
shape[i] = shp
EndFor
EndSub

Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 0 ' x offset
shY = 0 ' y offset
shape = ""
shape[1] = "pw=0;bc=#DE2910;func=rect;width=360;height=240;x=0;y=0;"
shape[2] = "pw=0;bc=#FFDE00;func=tri;x1=14.59;y1=0;x2=0;y2=44.92;x3=29.19;y3=44.92;x=45.4;y=23.99;"
shape[3] = "pw=0;bc=#FFDE00;func=tri;x1=14.59;y1=0;x2=0;y2=44.92;x3=29.19;y3=44.92;x=58.27;y=33.35;angle=72;"
shape[4] = "pw=0;bc=#FFDE00;func=tri;x1=14.59;y1=0;x2=0;y2=44.92;x3=29.19;y3=44.92;x=53.36;y=48.49;angle=144;"
shape[5] = "pw=0;bc=#FFDE00;func=tri;x1=14.59;y1=0;x2=0;y2=44.92;x3=29.19;y3=44.92;x=37.44;y=48.49;angle=216;"
shape[6] = "pw=0;bc=#FFDE00;func=tri;x1=14.59;y1=0;x2=0;y2=44.92;x3=29.19;y3=44.92;x=32.52;y=33.35;angle=288;"
shape[7] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=111.26;y=18.83;angle=-120.97;"
shape[8] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=111.73;y=13.54;angle=-48.97;"
shape[9] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=116.9;y=12.35;angle=23.03;"
shape[10] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=119.62;y=16.9;angle=95.03;"
shape[11] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=116.14;y=20.91;angle=167.03;"
shape[12] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=134.66;y=41.15;angle=-98.14;"
shape[13] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=137.14;y=36.46;angle=-26.14;"
shape[14] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=142.37;y=37.37;angle=45.86;"
shape[15] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=143.12;y=42.62;angle=117.86;"
shape[16] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=138.36;y=44.95;angle=189.86;"
shape[17] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=134.79;y=75.27;angle=-74.06;"
shape[18] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=138.97;y=72;angle=-2.06;"
shape[19] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=143.37;y=74.96;angle=69.94;"
shape[20] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=141.91;y=80.06;angle=141.94;"
shape[21] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=136.61;y=80.25;angle=213.94;"
shape[22] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=111.61;y=97.69;angle=-51.35;"
shape[23] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=116.72;y=96.29;angle=20.65;"
shape[24] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=119.64;y=100.72;angle=92.65;"
shape[25] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=116.32;y=104.86;angle=164.65;"
shape[26] = "pw=0;bc=#FFDE00;func=tri;x1=4.86;y1=0;x2=0;y2=14.97;x3=9.73;y3=14.97;x=111.36;y=102.99;angle=236.65;"
EndSub