Microsoft Small Basic

Program Listing: XXN689-5
' Circuit 0.2b
' Copyright (c) 2013 Nonki Takahashi. All rights reserved.
'
' History:
' 0.2b 2013/02/18 Modified switches to work. (XXN689-5)
' 0.15a 2013/02/18 Minor change for Silverlight. (XXN689-4)
' 0.1a 2013/02/18 Created as alpha version. (XXN689)
' 0.0 2013/02/04 18:12:18 Shapes generated by Shapes 1.5.
'
GraphicsWindow.Title = "Circuit 0.2b"
GraphicsWindow.BackgroundColor = "Black"
Circuit_Init()
SB_Workaround()
scale = 1
angle = 0
count = Array.GetItemCount(circuit)
slot = Array.GetAllIndices(circuit)
nSwitch = 0
For i = 1 To count
cir = circuit[slot[i]]
par = part[cir["part"]]
If cir["part"] = "line" Then
Shapes_Init()
ElseIf cir["part"] = "corner" Then
Corner_Init()
ElseIf cir["part"] = "branch" Then
Branch_Init()
ElseIf cir["part"] = "switch" Then
Switch_Init()
ElseIf cir["part"] = "battery" Then
Battery_Init()
ElseIf cir["part"] = "light" Then
Light_Init()
EndIf
Shapes_Add()
Circuit_CalcSlotParam()
Shapes_Move()
If cir["angle"] <> "" Then
angle = cir["angle"]
Shapes_Rotate()
EndIf
If cir["part"] = "switch" Then
cir["obj1"] = shape[4]["obj"]
cir["obj2"] = shape[5]["obj"]
cir["obj3"] = shape[13]["obj"]
cir["obj4"] = shape[14]["obj"]
num = slot[i]
circuit[num] = cir
nSwitch = nSwitch + 1
switch[nSwitch] = num
If cir["state"] = "off" Then
Switch_Off()
EndIf
EndIf
If cir["part"] = "light" Then
cir["obj1"] = shape[9]["obj"]
cir["obj2"] = shape[13]["obj"]
num = slot[i]
circuit[num] = cir
nLight = nLight + 1
light[nLight] = num
If cir["state"] = "on" Then
Light_On()
EndIf
EndIf
EndFor
GraphicsWindow.MouseDown = OnMouseDown
Sub OnMouseDown
GraphicsWindow.MouseDown = DoNothing
col = Math.Floor((GraphicsWindow.MouseX - x0) / size) + 1
row = Math.Floor((GraphicsWindow.MouseY - y0) / size) + 1
num = (row - 1) * 6 + col
If circuit[num]["part"] = "switch" Then
If circuit[num]["state"] = "on" Then
Switch_Off()
Light_Check()
ElseIf circuit[num]["state"] = "off" Then
Switch_On()
Light_Check()
EndIf
EndIf
GraphicsWindow.MouseDown = OnMouseDown
EndSub
Sub DoNothing
OnMouseDown = 0
EndSub
Sub Switch_On
Shapes.SetOpacity(circuit[num]["obj1"], 100)
Shapes.SetOpacity(circuit[num]["obj2"], 0)
Shapes.SetOpacity(circuit[num]["obj3"], 40)
Shapes.SetOpacity(circuit[num]["obj4"], 0)
circuit[num]["state"] = "on"
EndSub
Sub Switch_Off
Shapes.SetOpacity(circuit[num]["obj1"], 0)
Shapes.SetOpacity(circuit[num]["obj2"], 100)
Shapes.SetOpacity(circuit[num]["obj3"], 0)
Shapes.SetOpacity(circuit[num]["obj4"], 40)
circuit[num]["state"] = "off"
EndSub
Sub Light_Check
Stack.PushValue("local", num)
swOn = ""
If circuit[switch[1]]["state"] = "on" Then
swOn[1] = "True"
EndIf
If circuit[switch[2]]["state"] = "on" Then
swOn[2] = "True"
EndIf
If circuit[switch[3]]["state"] = "on" Then
swOn[3] = "True"
EndIf
If circuit[switch[4]]["state"] = "on" Then
swOn[4] = "True"
EndIf
If (swOn[1] And swOn[2]) Or (swOn[3] And swOn[4]) Then
num = light[1]
Light_On()
Else
num = light[1]
Light_Off()
EndIf
num = Stack.PopValue("local")
EndSub
Sub Light_On
If circuit[num]["state"] = "off" Then
For level = 0 To 100 Step 10
Shapes.SetOpacity(circuit[num]["obj1"], level)
Shapes.SetOpacity(circuit[num]["obj2"], level)
Program.Delay(50)
EndFor
Else
Shapes.SetOpacity(circuit[num]["obj1"], 100)
Shapes.SetOpacity(circuit[num]["obj2"], 100)
EndIf
circuit[num]["state"] = "on"
EndSub
Sub Light_Off
If circuit[num]["state"] = "on" Then
For level = 100 To 0 Step -10
Shapes.SetOpacity(circuit[num]["obj1"], level)
Shapes.SetOpacity(circuit[num]["obj2"], level)
Program.Delay(50)
EndFor
Else
Shapes.SetOpacity(circuit[num]["obj1"], 0)
Shapes.SetOpacity(circuit[num]["obj2"], 0)
EndIf
circuit[num]["state"] = "off"
EndSub
Sub Circuit_Init
x0 = 12
y0 = 22
size = 100
part["line"] = "connect=EW;" ' wire
part["corner"] = "connect=NW;" ' wire
part["branch"] = "connect=NEW;" ' wire
part["switch"] = "connect=EW;"
part["battery"] = "connect=EW;"
part["light"] = "connect=EW;"
circuit[1] = "part=corner;angle=180;"
circuit[2] = "part=branch;angle=-90;"
circuit[3] = "part=switch;state=off;"
circuit[4] = "part=switch;state=off;"
circuit[5] = "part=branch;angle=-90;"
circuit[6] = "part=corner;angle=-90;"
circuit[7] = "part=battery;"
circuit[8] = "part=corner;angle=90;"
circuit[9] = "part=switch;state=off;"
circuit[10] = "part=switch;state=off;"
circuit[11] = "part=corner;"
circuit[12] = "part=light;angle=90;state=off;"
circuit[13] = "part=corner;angle=90;"
circuit[14] = "part=line;"
circuit[15] = "part=line;"
circuit[16] = "part=line;"
circuit[17] = "part=line;"
circuit[18] = "part=corner;"
For s = 1 To 24
slot[i] = s
Circuit_CalcSlotParam()
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(x + 1, y + 1, size - 2, size - 2)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.DrawText(x + 5, y + 5, (row - 1) * 6 + col)
EndFor
EndSub
Sub Circuit_CalcSlotParam
' param size - size of part
' param slot[i] - slot number
' returns row, col - row and column
' returns x, y - top left coordinate for the slot
row = Math.Floor((slot[i] - 1) / 6) + 1
col = Math.Remainder(slot[i] - 1, 6) + 1
y = (row - 1) * size + y0
x = (col - 1) * size + x0
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"
Else
silverlight = "False"
EndIf
EndSub
Sub SB_RotateWorkaround
' Small Basic | Rotate workaround for Silverlight
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shape[i]["func"] = "tri" Then
x1 = -Math.Floor(shape[i]["x3"] / 2)
y1 = -Math.Floor(shape[i]["y3"] / 2)
ElseIf shape[i]["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shape[i]["x1"] - shape[i]["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shape[i]["y1"] - shape[i]["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 Shapes_Init
' Shapes Line | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=0;y=48;width=100;height=4;bc=#A1A1A1;pw=0;"
EndSub
Sub Switch_Init
' Switch | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=0;y=48;width=25;height=4;bc=#A1A1A1;pw=0;"
shape[3] = "func=ell;x=23;y=43;width=14;height=14;bc=#A1A1A1;pw=0;"
shape[4] = "func=rect;x=25;y=40;width=50;height=4;bc=#A1A1A1;pw=0;"
shape[5] = "func=rect;x=25;y=30;width=50;height=4;bc=#A1A1A1;op=0;pw=0;"
shape[6] = "func=ell;x=63;y=43;width=14;height=14;bc=#A1A1A1;pw=0;"
shape[7] = "func=rect;x=75;y=48;width=25;height=4;bc=#A1A1A1;pw=0;"
shape[8] = "func=ell;x=25;y=65;width=20;height=20;bc=#666666;pw=0;"
shape[9] = "func=rect;x=35;y=65;width=30;height=20;bc=#666666;pw=0;"
shape[10] = "func=ell;x=55;y=65;width=20;height=20;bc=#666666;pw=0;"
shape[11] = "func=text;x=30;y=70;text=O;bc=#FFFFFF;fs=10;fn=Arial;"
shape[12] = "func=text;x=64;y=70;text=I;bc=#FFFFFF;fs=10;fn=Arial;"
shape[13] = "func=ell;x=55;y=65;width=20;height=20;bc=#FFFFFF;op=40;pw=0;"
shape[14] = "func=ell;x=25;y=65;width=20;height=20;bc=#FFFFFF;op=0;pw=0;"
EndSub
Sub Branch_Init
' Branch | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=0;y=48;width=50;height=4;bc=#A1A1A1;pw=0;"
shape[3] = "func=rect;x=48;y=0;width=4;height=100;bc=#A1A1A1;pw=0;"
EndSub
Sub Corner_Init
' Corner | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=0;y=48;width=52;height=4;bc=#A1A1A1;pw=0;"
shape[3] = "func=rect;x=48;y=0;width=4;height=52;bc=#A1A1A1;pw=0;"
EndSub
Sub Battery_Init
' Battery | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=48;y=0;width=4;height=100;bc=#A1A1A1;pw=0;"
shape[3] = "func=rect;x=39;y=11;width=22;height=20;bc=#666666;pc=#000000;pw=1;"
shape[4] = "func=rect;x=23;y=21;width=53;height=66;bc=#262626;pc=;pw=1;"
shape[5] = "func=text;x=28;y=42;text=1.5V;fs=20;fn=Arial;bc=#FFFFFF;angle=90;"
EndSub
Sub Light_Init
' Light | 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] = "func=rect;x=0;y=0;width=100;height=100;bc=#008080;op=20;pw=0;"
shape[2] = "func=rect;x=50;y=48;width=50;height=4;bc=#A1A1A1;pw=0;"
shape[3] = "func=rect;x=0;y=48;width=25;height=4;bc=#A1A1A1;pw=0;"
shape[4] = "func=rect;x=25;y=48;width=4;height=40;bc=#A1A1A1;pw=0;"
shape[5] = "func=rect;x=25;y=84;width=25;height=4;bc=#A1A1A1;pw=0;"
shape[6] = "func=ell;x=43;y=77;width=14;height=14;bc=#A1A1A1;pw=0;"
shape[7] = "func=ell;x=43;y=69;width=14;height=14;bc=#666666;pc=#000000;pw=1;"
shape[8] = "func=rect;x=41;y=68;width=17;height=10;bc=#000000;pw=0;"
shape[9] = "func=ell;x=29;y=7;width=42;height=42;bc=#FFFFCC;op=0;pw=0;"
shape[10] = "func=ell;x=29;y=7;width=42;height=42;bc=#CCCCCC;op=30;pc=#000000;pw=1;"
shape[11] = "func=rect;x=40;y=46;width=20;height=29;bc=#666666;pc=#000000;pw=1;"
shape[12] = "func=line;x=42;y=26;x1=0;y1=0;x2=15;y2=0;pc=#000000;pw=1;"
shape[13] = "func=line;x=42;y=26;x1=0;y1=0;x2=15;y2=0;pc=#FFCC00;op=0;pw=3;"
shape[14] = "func=line;x=42;y=25;x1=0;y1=0;x2=5;y2=21;pc=#000000;pw=1;"
shape[15] = "func=line;x=52;y=24;x1=4;y1=0;x2=0;y2=22;pc=#000000;pw=1;"
shape[16] = "func=line;x=40;y=47;x1=18;y1=0;x2=0;y2=9;pc=#000000;pw=1;"
shape[17] = "func=line;x=41;y=54;x1=18;y1=0;x2=0;y2=10;pc=#000000;pw=1;"
shape[18] = "func=line;x=41;y=63;x1=18;y1=0;x2=0;y2=9;pc=#000000;pw=1;"
EndSub
Sub Shapes_Add
' Shapes | Add shapes as shapes data
' param shape - array of shapes
' param scale - to zoom
' return nShapes - number of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
Stack.PushValue("local", index)
nShapes = Array.GetItemCount(shape)
index = Array.GetAllIndices(shape)
s = scale
For j = 1 To nShapes
i = index[j]
GraphicsWindow.PenWidth = shape[i]["pw"] * s
If shape[i]["pw"] > 0 Then
GraphicsWindow.PenColor = shape[i]["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) Then
GraphicsWindow.BrushColor = shape[i]["bc"]
EndIf
If shape[i]["func"] = "rect" Then
shape[i]["obj"] = Shapes.AddRectangle(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "ell" Then
shape[i]["obj"] = Shapes.AddEllipse(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "tri" Then
shape[i]["obj"] = Shapes.AddTriangle(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s, shape[i]["x3"] * s, shape[i]["y3"] * s)
ElseIf shape[i]["func"] = "line" Then
shape[i]["obj"] = Shapes.AddLine(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s)
ElseIf shape[i]["func"] = "text" Then
If silverlight Then
fs = Math.Floor(shape[i]["fs"] * 0.9)
Else
fs = shape[i]["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shape[i]["fn"]
shape[i]["obj"] = Shapes.AddText(shape[i]["text"])
EndIf
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
x = shape[i]["x"]
y = shape[i]["y"]
alpha = Math.GetRadians(shape[i]["angle"])
SB_RotateWorkAround()
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Else
Shapes.Move(shape[i]["obj"], shX + shape[i]["x"] * s, shY + shape[i]["y"] * s)
EndIf
If Text.IsSubText("rect|ell|tri|line|text", shape[i]["func"]) And shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
If shape[i]["op"] <> "" Then
Shapes.SetOpacity(shape[i]["obj"], shape[i]["op"])
EndIf
shape[i]["rx"] = shape[i]["x"]
shape[i]["ry"] = shape[i]["y"]
EndFor
shAngle = 0
index = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub
Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes for rotation
' param nShapes - number of shapes
' return shWidth, shHeight - total size of shapes
For i = 1 To nShapes
If shape[i]["func"] = "tri" Or shape[i]["func"] = "line" Then
xmin = shape[i]["x1"]
xmax = shape[i]["x1"]
ymin = shape[i]["y1"]
ymax = shape[i]["y1"]
If shape[i]["x2"] < xmin Then
xmin = shape[i]["x2"]
EndIf
If xmax < shape[i]["x2"] Then
xmax = shape[i]["x2"]
EndIf
If shape[i]["y2"] < ymin Then
ymin = shape[i]["y2"]
EndIf
If ymax < shape[i]["y2"] Then
ymax = shape[i]["y2"]
EndIf
If shape[i]["func"] = "tri" Then
If shape[i]["x3"] < xmin Then
xmin = shape[i]["x3"]
EndIf
If xmax < shape[i]["x3"] Then
xmax = shape[i]["x3"]
EndIf
If shape[i]["y3"] < ymin Then
ymin = shape[i]["y3"]
EndIf
If ymax < shape[i]["y3"] Then
ymax = shape[i]["y3"]
EndIf
EndIf
shape[i]["width"] = xmax - xmin
shape[i]["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shape[i]["x"] + shape[i]["width"]
shHeight = shape[i]["y"] + shape[i]["height"]
Else
If shWidth < shape[i]["x"] + shape[i]["width"] Then
shWidth = shape[i]["x"] + shape[i]["width"]
EndIf
If shHeight < shape[i]["y"] + shape[i]["height"] Then
shHeight = shape[i]["y"] + shape[i]["height"]
EndIf
EndIf
EndFor
EndSub
Sub Shapes_CalcRotatePos
' Shapes | Calculate position for rotated shape
' param["x"], param["y"] - position of a shape
' param["width"], param["height"] - size of a shape
' param["cx"], param["cy"] - center of shapes
' param["angle"] - rotate angle
' return x, y - rotated position of a shape
_cx = param["x"] + param["width"] / 2
_cy = param["y"] + param["height"] / 2
x = _cx - param["cx"]
y = _cy - param["cy"]
Math_CartesianToPolar()
a = a + param["angle"]
x = r * Math.Cos(a * Math.Pi / 180)
y = r * Math.Sin(a * Math.Pi / 180)
_cx = x + param["cx"]
_cy = y + param["cy"]
x = _cx - param["width"] / 2
y = _cy - param["height"] / 2
EndSub
Sub Shapes_Move
' Shapes | Move shapes
' param shape - array of shapes
' param scale - to zoom
' param x, y - position to move
' return shX, shY - new position of shapes
Stack.PushValue("local", i)
shX = x
shY = y
For i = 1 To nShapes
_x = shape[i]["rx"]
_y = shape[i]["ry"]
Shapes.Move(shape[i]["obj"], shX + _x * s, shY + _y * s)
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Remove
' Shapes | Remove shapes
' param shape - array of shapes
Stack.PushValue("local", i)
For i = 1 To nShapes
Shapes.Remove(shape[i]["obj"])
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Rotate
' Shapes | Rotate shapes
' param shape - array of shapes
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
s = scale
param["angle"] = angle
param["cx"] = shWidth / 2
param["cy"] = shHeight / 2
For i = 1 To nShapes
param["x"] = shape[i]["x"]
param["y"] = shape[i]["y"]
param["width"] = shape[i]["width"]
param["height"] = shape[i]["height"]
Shapes_CalcRotatePos()
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
alpha = Math.GetRadians(angle + shape[i]["angle"])
SB_RotateWorkAround()
EndIf
shape[i]["rx"] = x
shape[i]["ry"] = y
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shape[i]["obj"], angle + shape[i]["angle"])
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub
Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
EndSub