Microsoft Small Basic

Program Listing: RLW923
' Pictionary - Opacity
' Version 1.7.3
' Copyright © 2020 Nonki Takahashi. The MIT License.
' Last update 2020-07-26

SB_Workaround()
Init()

' initialize caption shapes
Shapes_Init_Caption()
name = "Caption"
Sprite_Add()

' initialize scale shapes
Shapes_Init_Scale()
name = "Scale"
Sprite_Add()
x = shX + shape[24]["x"]
pointer = shape[24]["obj"]

' initialize cool shapes
Shapes_Init_Cool()
name = "Cool"
Sprite_Add()

a = 0
While "True"
Program.Delay(100)
a = a + 5
_a = Math.GetRadians(a)
o = Math.Cos(_a) * 50 + 50
y = 62 + (100 - o) * 2.5
Shapes.SetOpacity(shape[4]["obj"], o)
Shapes.SetOpacity(shape[5]["obj"], o)
Shapes.Move(pointer, x, y)
EndWhile

Sub Init
GraphicsWindow.Title = "Pictionary"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
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 / 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_Caption
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 400 ' x offset
shY = 358 ' y offset
shape = ""
shape[1] = "func=text;x=0;y=0;text=Opacity;fn=Trebuchet MS;fs=50;fb=True;pw=0;bc=Black;"
EndSub

Sub Shapes_Init_Cool
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 140 ' x offset
shY = 70 ' y offset
shape = ""
shape[1] = "func=ell;width=250;height=250;x=0;y=0;pw=8;pc=Orange;bc=Yellow;"
shape[2] = "func=ell;width=20;height=20;x=80;y=110;pw=0;bc=Black;"
shape[3] = "func=ell;width=20;height=20;x=150;y=110;pw=0;bc=Black;"
shape[4] = "func=ell;width=100;height=60;x=20;y=90;pw=0;bc=Black;"
shape[5] = "func=ell;width=100;height=60;x=130;y=90;pw=0;bc=Black;"
shape[6] = "func=ell;width=100;height=60;x=20;y=90;pw=6;pc=Black;bc=Transparent;"
shape[7] = "func=ell;width=100;height=60;x=130;y=90;pw=6;pc=Black;bc=Transparent;"
shape[8] = "func=rect;width=20;height=10;x=115;y=110;pw=6;pc=Black;"
shape[9] = "func=rect;width=10;height=10;x=15;y=110;pw=6;pc=Black;"
shape[10] = "func=rect;width=10;height=10;x=225;y=110;pw=6;pc=Black;"
shape[11] = "func=line;x=110;y=180;x1=0;y1=15;x2=60;y2=0;pw=8;pc=Black;"
EndSub

Sub Shapes_Init_Scale
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 450 ' x offset
shY = 70 ' y offset
shape = ""
shape[1] = "func=line;x=1;y=0;x1=0;y1=0;x2=0;y2=250;pw=2;pc=Black;"
shape[2] = "func=line;x=0;y=0;x1=0;y1=0;x2=20;y2=0;pw=2;pc=Black;"
For i = 3 To 12
shp = shape[i - 1]
shp["y"] = shp["y"] + 25
shape[i] = shp
EndFor
shape[13] = "func=text;x=24;y=-8;text=100;fs=12;fn=Trebuchet MS;bc=Black;"
For i = 14 To 23
shp = shape[i - 1]
shp["y"] = shp["y"] + 25
shp["text"] = shp["text"] - 10
shape[i] = shp
EndFor
shape[24] = "func=tri;x=-2;y=-8;x1=0;y1=0;x2=16;y2=8;x3=0;y3=16;pw=2;pc=Black;bc=Red;"
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
If scale = "" Then
scale = 1
EndIf
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