Microsoft Small Basic

Program Listing: BJQ268
' Flag of Sweden
' Copyright © 2020 Nonki Takahashi. The MIT License.

GraphicsWindow.Title = "Flag of Sweden"
SB_Workaround()
scale = 598/ 16
GraphicsWindow.Width = 16 * scale
GraphicsWindow.Height = 10 * scale
Shapes_Init_Sweden()
angle = 0
name = "Sweden"
Sprite_Add()

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 / SBD
' return silverlight - "True" if in remote
' return sbd - "True" if Small Basic Desktop
color = GraphicsWindow.GetPixel(0, 0)
sbd = "False"
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
_gw = GraphicsWindow.Width
_gh = GraphicsWindow.Height
If (_gw = 624) And (_gh = 441) Then
sbd = "True"
EndIf
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 = 1 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_Sweden
' Shapes | initialize sample 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] = "func=rect;width=16;height=10;pw=0;bc=#006aa7;"
shape[2] = "func=rect;x=5;width=2;height=10;pw=0;bc=#fecc00;"
shape[3] = "func=rect;y=4;width=16;height=2;pw=0;bc=#fecc00;"
EndSub

Sub Sprite_Add
' Sprite | add shapes to a sprite
' param name - sprite name
' param shX, shY - origin of shape array
' param scale - to resize
' param shape[] - shape array
' param nSprite - number of sprite
' return nSprite - updated number of sprite
' return sprite[] - sprite array
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
nSprite = nSprite + 1
spr = ""
spr["name"] = name
spr["x"] = shX
spr["y"] = shY
spr["angle"] = 0
spr["dir"] = 1
Shapes_CalcWidthAndHeight()
spr["width"] = shWidth
spr["height"] = shHeight
spr["cx"] = shWidth / 2
spr["cy"] = shHeight / 2
s = scale
spr["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 sbd And (shp["func"] = "line") Then
shp["wx"] = x
shp["wy"] = y
ElseIf 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
spr["shape"] = shape
sprite[nSprite] = spr
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub