Microsoft Small Basic

Program Listing: NRS907-3
' TechNet Guru Medals
' Version 0.4
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last Update 2016-01-25
' Program ID NRS907-3
'
' Reference
' [1] James D. Foley, Andries Van Dam "Fundamentals of Interactive Computer Graphics" 1982
'
GraphicsWindow.Title = "TechNet Guru Medals 0.4"
UNDEFINED = "N/A"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
LT = "<"
WQ = Text.GetCharacter(34)
Not = "False=True;True=False;"
colors = "1=Gold;2=Silver;3=#AC6B25;" ' bronze
di = 120 ' diameter
SB_Workaround()
Form()
DrawMedals()
' initialize shapes
Shapes_Init()
' add shapes
scale = di / 200
angle = 0
iMin = 1
iMax = 76
Shapes_Add()
x = 30 + (4 - 1) * (di + 20)
y = 50
Shapes_Move()
Controls.ButtonClicked = OnButtonClicked
While "True"
If buttonClicked Then
di = Controls.GetTextBoxText(tboxSize)
prize = Controls.GetTextBoxText(tboxPrize)
DrawMedals()
scale = di / 200
Shapes_Remove()
Shapes_Add()
x = 30 + (4 - 1) * (di + 20)
y = 50
Shapes_Move()
ShowSVG()
buttonClicked = "False"
While Not[buttonClicked]
Program.Delay(200)
EndWhile
HideSVG()
buttonClicked = "False"
Else
Program.Delay(msWait)
EndIf
EndWhile
Sub OnButtonClicked
buttonClicked = "True"
EndSub
Sub DrawMedals
' param di - diameter of the medal [px]
' param prize - text for the medal
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, ch)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, ch, gw, gh - ch)
fs = 20 * di / 100
GraphicsWindow.FontSize = fs
GraphicsWindow.FontName = fn
th = 8 * di / 100
svg = LT + "svg width=" + WQ + gw + WQ + " height=" + WQ + gh + WQ + ">" + CRLF
svg = svg + sp[2] + LT + "defs>" + CRLF
use = ""
For i = 1 To 3
x0 = 30 + (i - 1) * (di + 20)
y0 = 50
use = use + sp[2] + LT + "use x=" + WQ + x0 + WQ + " y=" + WQ + y0 + WQ
use = use + " xlink:href=" + WQ + "#g" + i + WQ + " />" + CRLF
svg = svg + sp[4] + LT + "g id=" + WQ + "g" + i + WQ + ">" + CRLF
GraphicsWindow.BrushColor = colors[i]
color = GraphicsWindow.BrushColor
SB_ColorWorkaround()
Color_RGBtoHSL()
lsave = lightness
ra = (di - 1) / 2 ' radius
x1 = x0 + ra
y1 = y0 + ra
For a = 0 To 360 Step 2
_a = Math.GetRadians(a)
x3 = x1 + ra * Math.Sin(_a)
y3 = y1 - ra * Math.Cos(_a)
If 0 < a Then
param = "x1=" + x1 + ";y1=" + y1 + ";"
param = param + "x2=" + x2 + ";y2=" + y2 + ";"
param = param + "x3=" + x3 + ";y3=" + y3 + ";"
FillTriangle()
If 90 < a And a < 270 Then
lightness = Math.Abs(lightness - 0.2)
Color_HSLtoRGB()
GraphicsWindow.BrushColor = color
param = "x1=" + x2 + ";y1=" + y2 + ";"
param = param + "x2=" + x3 + ";y2=" + y3 + ";"
param = param + "x3=" + x2 + ";y3=" + (y2 + th) + ";"
FillTriangle()
param = "x1=" + x3 + ";y1=" + y3 + ";"
param = param + "x2=" + x2 + ";y2=" + (y2 + th) + ";"
param = param + "x3=" + x3 + ";y3=" + (y3 + th) + ";"
FillTriangle()
EndIf
EndIf
n = 3
lightness = lsave - (Math.Abs(Math.Remainder(_a + Math.Pi / 4, Math.Pi / n) - Math.Pi / (n * 2)) / (Math.Pi / 2)) * lsave
Color_HSLtoRGB()
GraphicsWindow.BrushColor = color
_a = Math.GetRadians(a - 2)
x2 = x1 + ra * Math.Sin(_a)
y2 = y1 - ra * Math.Cos(_a)
EndFor
GraphicsWindow.BrushColor = colors[i]
color = GraphicsWindow.BrushColor
SB_ColorWorkaround()
Color_RGBtoHSL()
lightness = lsave * 0.2
Color_HSLtoRGB()
If Text.IsSubtext(prize, " ") Then
s = Text.GetIndexOf(prize, " ")
t[1] = Text.GetSubText(prize, 1, s - 1)
t[2] = Text.GetSubTextToEnd(prize, s + 1)
Else
t[1] = prize
t[2] = ""
EndIf
GetTextWidth()
GraphicsWindow.BrushColor = color
param = ""
param["x"] = x0 + (di - w[1]) / 2
param["y"] = y0 + 27 * di / 100
param["text"] = t[1]
DrawText()
param["x"] = x0 + (di - w[2]) / 2
param["y"] = y0 + 47 * di / 100
param["text"] = t[2]
DrawText()
lightness = lsave * 0.8
Color_HSLtoRGB()
GraphicsWindow.BrushColor = color
param["x"] = x0 + (di - w[1]) / 2
param["y"] = y0 + 28 * di / 100
param["text"] = t[1]
DrawText()
param["x"] = x0 + (di - w[2]) / 2
param["y"] = y0 + 48 * di / 100
param["text"] = t[2]
DrawText()
svg = svg + sp[4] + LT + "/g>" + CRLF
EndFor
svg = svg + sp[2] + LT + "/defs>" + CRLF
svg = svg + use
svg = svg + LT + "/svg>" + CRLF
EndSub
Sub DrawText
' param["x"], param["y"] - the co-ordinate of the text start point
' param["text"] - the text to draw
' param color - the text color
GraphicsWindow.DrawText(param["x"], param["y"], param["text"])
RoundParam()
svg = svg + sp[6] + LT + "text x=" + WQ + (param["x"] - x0) + WQ
svg = svg + " y=" + WQ + (param["y"] + fs - y0) + WQ
svg = svg + " fill=" + WQ + color + WQ + CRLF
svg = svg + sp[8] + "font-family=" + WQ + fn + WQ + CRLF
svg = svg + sp[8] + "font-size=" + WQ + fs + WQ + ">"
svg = svg + param["text"] + LT + "/text>" + CRLF
EndSub
Sub FillTriangle
' param["x1"], param["y1"] - the co-ordinate of the first point.
' param["x2"], param["y2"]- the co-ordinate of the second point.
' param["x3"], param["y3"]- the co-ordinate of the third point.
' param x0, y0 - left top co-ordinate of the medal
' param color - fill color for the rectangle
GraphicsWindow.FillTriangle(param["x1"], param["y1"], param["x2"], param["y2"], param["x3"], param["y3"])
RoundParam()
svg = svg + sp[6] + LT + "polygon points=" + WQ
svg = svg + (param["x1"] - x0) + "," + (param["y1"] - y0) + " "
svg = svg + (param["x2"] - x0) + "," + (param["y2"] - y0) + " "
svg = svg + (param["x3"] - x0) + "," + (param["y3"] - y0) + WQ + CRLF
svg = svg + sp[8] + "style=" + WQ + "fill:" + color + ";stroke-width:0" + WQ + " />" + CRLF
EndSub
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
ch = 40 ' controls height
GraphicsWindow.FontName = "Arial"
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, ch)
GraphicsWindow.BrushColor = "Black"
txt = Shapes.AddText("Size")
Shapes.Move(txt, 10, 13)
tboxSize = Controls.AddTextBox(44, 10)
Controls.SetTextBoxText(tboxSize, di)
Controls.SetSize(tboxSize, 50, 22)
txt = Shapes.AddText("Prize")
Shapes.Move(txt, 120, 13)
tboxPrize = Controls.AddTextBox(160, 10)
prize = "TnWiki Guru"
Controls.SetTextBoxText(tboxPrize, prize)
Controls.SetSize(tboxPrize, 140, 22)
Controls.AddButton("Draw", 340, 8)
fn = "Trebuchet MS"
GraphicsWindow.FontName = fn
' width/height [%] of each character in Trebuchet MS font
ratio = "32=30;48=58;49=58;50=58;51=58;52=58;53=58;54=58;55=58;56=58;"
ratio = ratio + "57=58;65=63;66=59;67=61;68=64;69=57;70=58;71=67;"
ratio = ratio + "72=68;73=28;74=53;75=62;76=55;77=74;78=67;79=70;"
ratio = ratio + "80=59;81=71;82=61;83=51;84=61;85=68;86=62;87=88;"
ratio = ratio + "88=60;89=61;90=56;97=53;98=58;99=51;100=58;101=57;"
ratio = ratio + "102=37;103=50;104=59;105=30;106=37;107=55;108=29;"
ratio = ratio + "109=86;110=59;111=56;112=58;113=58;114=43;115=43;"
ratio = ratio + "116=39;117=59;118=53;119=78;120=55;121=53;122=53;"
For n = 2 To 8 Step 2
sp[n] = Text.GetSubText(" ", 1, n)
EndFor
EndSub
Sub GetTextWidth
For j = 1 To 2
txt = t[j]
Text_GetWidthInPx()
w[j] = px
EndFor
EndSub
Sub ShowSVG
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "#80000000"
GraphicsWindow.FontName = "Arial"
GraphicsWindow.FontSize = 12
popup = Shapes.AddRectangle(gw, gh)
GraphicsWindow.BrushColor = "Black"
tboxSVG = Controls.AddMultiLineTextBox(10, 10)
Controls.SetSize(tboxSVG, gw - 20, gh - 54)
Shapes.SetOpacity(tboxSVG, 80)
Controls.SetTextBoxText(tboxSVG, svg)
ok = Controls.AddButton("OK", gw - 40, gh - 34)
GraphicsWindow.BrushColor = "White"
msg = Shapes.AddText("Push Ctrl+A, Ctrl+C to copy SVG above.")
Shapes.Move(msg, 10, gh - 30)
EndSub
Sub HideSVG
Controls.Remove(ok)
Controls.Remove(msg)
Controls.Remove(tboxSVG)
Shapes.Remove(popup)
EndSub
Sub RoundParam
index = Array.GetAllIndices(param)
n = Array.GetItemCount(param)
For j = 1 To n
If Text.StartsWith(index[j], "x") Or Text.StartsWith(index[j], "y") Then
param[index[j]] = Math.Round(param[index[j]] * 100) / 100
EndIf
EndFor
EndSub
Sub Color_ColorToRGB
' Color | Convert Color to RGB
' param color - "#rrggbb"
' return r, g, b - [0, 255]
sr = Text.GetSubText(color, 2, 2)
sg = Text.GetSubText(color, 4, 2)
sb = Text.GetSubText(color, 6, 2)
hex = sr
Math_Hex2Dec()
r = dec
hex = sg
Math_Hex2Dec()
g = dec
hex = sb
Math_Hex2Dec()
b = dec
EndSub
Sub Color_HSLtoRGB
' Color | Convert HSL to RGB
' param hue - [0, 360) or UNDEFINED
' param lightness - [0, 1]
' param saturation - [0, 1]
' return color - "#rrggbb"
If lightness <= 0.5 Then
n2 = lightness * (1 + saturation)
Else
n2 = lightness + saturation - lightness * saturation
EndIf
n1 = 2 * lightness - n2
If saturation = 0 Then
r = Math.Round(lightness * 255)
g = Math.Round(lightness * 255)
b = Math.Round(lightness * 255)
Else
h = hue + 120
Color_Value()
r = value
h = hue
Color_Value()
g = value
h = hue - 120
Color_Value()
b = value
EndIf
color = GraphicsWindow.GetColorFromRGB(r, g, b)
EndSub
Sub Color_Value
' Color | Function value
' param n1, n2
' param h - [-120, 480)
' return value - 0..255
If h >= 360 Then
h = h - 360
EndIF
If h < 0 Then
h = h + 360
EndIF
If h < 60 Then
v = n1 + (n2 - n1) * h / 60
ElseIf h < 180 Then
v = n2
ElseIf h < 240 Then
v = n1 + (n2 - n1) * (240 - h) / 60
Else
v = n1
EndIf
value = Math.Round(v * 255)
EndSub
Sub Color_RGBtoHSL
' Color | Convert RGB to HSL
' param color - "#rrggbb"
' return hue - [0, 360) or UNDEFINED
' return lightness - (0, 1)
' return saturation - (0, 1)
Color_ColorToRGB()
' r = r / 255 ' occurs Math.Max() bug
r = Math.Round(r / 255 * 10000) / 10000
' g = g / 255 ' occurs Math.Max() bug
g = Math.Round(g / 255 * 10000) / 10000
' b = b / 255 ' occurs Math.Max() bug
b = Math.Round(b / 255 * 10000) / 10000
max = Math.Max(r, g)
max = Math.Max(max, b)
min = Math.Min(r, g)
min = Math.Min(min, b)
lightness = (max + min) / 2
If max = min Then ' rR = rG = rB
saturation = 0
hue = UNDEFINED
Else
If lightness <= 0.5 Then
saturation = (max - min) / (max + min)
Else
saturation = (max - min) / (2 - max - min)
EndIf
rc = (max - r) / (max - min)
gc = (max - g) / (max - min)
bc = (max - b) / (max - min)
If r = max Then ' between Yellow and Magenta
hue = bc - gc
ElseIf g = max Then ' between Cyan and Yellow
hue = 2 + rc - bc
ElseIf b = max Then ' between Magenta and Cyan
hue = 4 + gc - rc
Else
TextWindow.WriteLine("Error:")
TextWindow.WriteLine("max=" + max)
TextWindow.WriteLine("r=" + r + ",sr=" + sr)
TextWindow.WriteLine("g=" + g + ",sg=" + sg)
TextWindow.WriteLine("b=" + b + ",sb=" + sb)
EndIf
hue = hue * 60
If hue < 0 Then
hue = hue + 360
EndIf
EndIf
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
ElseIf x = 0 Then
a = 0
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
Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex
' return dec
dec = 0
len = Text.GetLength(hex)
For p = 1 To len
dec = dec * 16 + Text.GetIndexOf("0123456789ABCDEF", Text.GetSubText(hex, p, 1)) - 1
EndFor
EndSub
Sub SB_ColorWorkaround
' Small Basic | Color workaround for Silverlight
' param color
' return color
If Text.GetLength(color) = 9 Then
color = "#" + Text.GetSubTextToEnd(color, 4)
EndIf
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_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 203 ' x offset
shY = 138 ' y offset
shape = ""
shape[1] = "func=rect;x=111;y=179;width=19;height=34;angle=353;bc=#666666;pc=#000000;pw=2;"
shape[2] = "func=line;x=120;y=181;x1=0;y1=0;x2=6;y2=8;pc=#000000;pw=2;"
shape[3] = "func=line;x=112;y=182;x1=0;y1=0;x2=17;y2=15;pc=#000000;pw=2;"
shape[4] = "func=line;x=112;y=191;x1=0;y1=0;x2=17;y2=13;pc=#000000;pw=2;"
shape[5] = "func=line;x=113;y=202;x1=0;y1=0;x2=13;y2=9;pc=#000000;pw=2;"
shape[6] = "func=line;x=113;y=181;x1=0;y1=9;x2=7;y2=0;pc=#000000;pw=2;"
shape[7] = "func=line;x=113;y=182;x1=0;y1=17;x2=14;y2=0;pc=#000000;pw=2;"
shape[8] = "func=line;x=113;y=190;x1=0;y1=18;x2=15;y2=0;pc=#000000;pw=2;"
shape[9] = "func=line;x=116;y=197;x1=0;y1=16;x2=12;y2=0;pc=#000000;pw=2;"
shape[10] = "func=line;x=122;y=204;x1=0;y1=9;x2=7;y2=0;pc=#000000;pw=2;"
shape[11] = "func=rect;x=61;y=176;width=19;height=34;angle=13;bc=#666666;pc=#000000;pw=2;"
shape[12] = "func=line;x=75;y=178;x1=0;y1=0;x2=5;y2=11;pc=#000000;pw=2;"
shape[13] = "func=line;x=69;y=178;x1=0;y1=0;x2=7;y2=17;pc=#000000;pw=2;"
shape[14] = "func=line;x=64;y=184;x1=0;y1=0;x2=10;y2=22;pc=#000000;pw=2;"
shape[15] = "func=line;x=63;y=195;x1=0;y1=0;x2=6;y2=15;pc=#000000;pw=2;"
shape[16] = "func=line;x=64;y=178;x1=0;y1=4;x2=12;y2=0;pc=#000000;pw=2;"
shape[17] = "func=line;x=64;y=182;x1=0;y1=7;x2=17;y2=0;pc=#000000;pw=2;"
shape[18] = "func=line;x=62;y=188;x1=0;y1=9;x2=18;y2=0;pc=#000000;pw=2;"
shape[19] = "func=line;x=60;y=195;x1=0;y1=10;x2=17;y2=0;pc=#000000;pw=2;"
shape[20] = "func=line;x=65;y=201;x1=0;y1=6;x2=12;y2=0;pc=#000000;pw=2;"
shape[21] = "func=ell;x=56;y=79;width=82;height=95;bc=#000000;pw=0;"
shape[22] = "func=ell;x=58;y=145;width=37;height=51;angle=6;bc=#000000;pw=0;"
shape[23] = "func=ell;x=99;y=141;width=36;height=51;angle=350;bc=#000000;pw=0;"
shape[24] = "func=rect;x=18;y=88;width=19;height=39;angle=147.3103;bc=#666666;pc=#000000;pw=2;"
shape[25] = "func=line;x=18;y=93;x1=0;y1=0;x2=12;y2=5;pc=#000000;pw=2;"
shape[26] = "func=line;x=17;y=99;x1=0;y1=0;x2=18;y2=8;pc=#000000;pw=2;"
shape[27] = "func=line;x=18;y=106;x1=0;y1=0;x2=20;y2=8;pc=#000000;pw=2;"
shape[28] = "func=line;x=23;y=115;x1=0;y1=0;x2=18;y2=7;pc=#000000;pw=2;"
shape[29] = "func=line;x=15;y=93;x1=0;y1=11;x2=4;y2=0;pc=#000000;pw=2;"
shape[30] = "func=line;x=19;y=90;x1=0;y1=21;x2=8;y2=0;pc=#000000;pw=2;"
shape[31] = "func=line;x=24;y=99;x1=0;y1=19;x2=9;y2=0;pc=#000000;pw=2;"
shape[32] = "func=line;x=28;y=108;x1=0;y1=16;x2=9;y2=0;pc=#000000;pw=2;"
shape[33] = "func=line;x=35;y=113;x1=0;y1=11;x2=6;y2=0;pc=#000000;pw=2;"
shape[34] = "func=rect;x=139;y=119;width=18;height=40;angle=43;bc=#666666;pc=#000000;pw=2;"
shape[35] = "func=line;x=136;y=141;x1=0;y1=0;x2=1;y2=12;pc=#000000;pw=2;"
shape[36] = "func=line;x=140;y=136;x1=0;y1=0;x2=3;y2=20;pc=#000000;pw=2;"
shape[37] = "func=line;x=146;y=130;x1=0;y1=0;x2=2;y2=20;pc=#000000;pw=2;"
shape[38] = "func=line;x=151;y=125;x1=0;y1=0;x2=3;y2=20;pc=#000000;pw=2;"
shape[39] = "func=line;x=159;y=124;x1=0;y1=0;x2=1;y2=15;pc=#000000;pw=2;"
shape[40] = "func=line;x=151;y=126;x1=0;y1=1;x2=9;y2=0;pc=#000000;pw=2;"
shape[41] = "func=line;x=142;y=131;x1=0;y1=2;x2=22;y2=0;pc=#000000;pw=2;"
shape[42] = "func=line;x=135;y=139;x1=0;y1=2;x2=25;y2=0;pc=#000000;pw=2;"
shape[43] = "func=line;x=129;y=145;x1=0;y1=3;x2=25;y2=0;pc=#000000;pw=2;"
shape[44] = "func=line;x=137;y=153;x1=0;y1=1;x2=11;y2=0;pc=#000000;pw=2;"
shape[45] = "func=ell;x=116;y=138;width=31;height=31;bc=#F6D8A7;pc=#000000;pw=2;"
shape[46] = "func=ell;x=0;y=69;width=30;height=31;bc=#F6D8A7;pc=#000000;pw=2;"
shape[47] = "func=ell;x=20;y=67;width=12;height=22;bc=#F6D8A7;pc=#000000;pw=2;"
shape[48] = "func=tri;x=80;y=93;x1=18;y1=0;x2=0;y2=37;x3=36;y3=37;angle=180;bc=#666666;pw=0;"
shape[49] = "func=line;x=81;y=93;x1=0;y1=6;x2=8;y2=0;pc=#000000;pw=2;"
shape[50] = "func=line;x=86;y=94;x1=0;y1=10;x2=13;y2=0;pc=#000000;pw=2;"
shape[51] = "func=line;x=88;y=95;x1=0;y1=18;x2=19;y2=0;pc=#000000;pw=2;"
shape[52] = "func=line;x=91;y=100;x1=0;y1=20;x2=22;y2=0;pc=#000000;pw=2;"
shape[53] = "func=line;x=95;y=118;x1=0;y1=7;x2=10;y2=0;pc=#000000;pw=2;"
shape[54] = "func=line;x=109;y=93;x1=0;y1=0;x2=5;y2=7;pc=#000000;pw=2;"
shape[55] = "func=line;x=100;y=93;x1=0;y1=0;x2=9;y2=10;pc=#000000;pw=2;"
shape[56] = "func=line;x=93;y=93;x1=0;y1=0;x2=15;y2=17;pc=#000000;pw=2;"
shape[57] = "func=line;x=85;y=93;x1=0;y1=0;x2=20;y2=24;pc=#000000;pw=2;"
shape[58] = "func=line;x=81;y=98;x1=0;y1=0;x2=20;y2=24;pc=#000000;pw=2;"
shape[59] = "func=rect;x=141;y=18;width=26;height=10;angle=335;bc=#666666;pw=0;"
shape[60] = "func=rect;x=164;y=14;width=25;height=10;bc=#666666;pw=0;"
shape[61] = "func=rect;x=145;y=34;width=24;height=10;bc=#666666;pw=0;"
shape[62] = "func=rect;x=166;y=31;width=24;height=10;angle=344;bc=#666666;pw=0;"
shape[63] = "func=ell;x=45;y=0;width=103;height=96;bc=#000000;pw=0;"
shape[64] = "func=rect;x=58;y=39;width=77;height=26;bc=#F6D8A7;pw=0;"
shape[65] = "func=ell;x=73;y=46;width=12;height=16;bc=#000000;pw=0;"
shape[66] = "func=ell;x=108;y=46;width=13;height=16;bc=#000000;pw=0;"
shape[67] = "func=rect;x=57;y=28;width=78;height=14;bc=#666666;pw=0;"
shape[68] = "func=rect;x=78;y=31;width=35;height=9;bc=#999999;pw=0;"
shape[69] = "func=ell;x=131;y=82;width=31;height=60;angle=306;bc=#000000;pw=0;"
shape[70] = "func=ell;x=35;y=82;width=31;height=60;angle=53;bc=#000000;pw=0;"
shape[71] = "func=ell;x=120;y=196;width=26;height=45;angle=283;bc=#000000;pw=0;"
shape[72] = "func=line;x=124;y=224;x1=0;y1=0;x2=21;y2=1;pc=#666666;pw=2;"
shape[73] = "func=line;x=143;y=211;x1=0;y1=0;x2=2;y2=15;pc=#666666;pw=2;"
shape[74] = "func=ell;x=44;y=196;width=26;height=45;angle=79;bc=#000000;pw=0;"
shape[75] = "func=line;x=41;y=211;x1=0;y1=15;x2=2;y2=0;pc=#666666;pw=2;"
shape[76] = "func=line;x=41;y=224;x1=0;y1=0;x2=21;y2=0;pc=#666666;pw=2;"
EndSub
Sub Shapes_Add
' Shapes | add shapes as shapes data
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - 1 if same scale
' return shWidth, shHeight - total size of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
s = scale
For i = iMin To iMax
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", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
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
If silverlight Then
fs = Math.Floor(shp["fs"] * 0.9)
Else
fs = shp["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
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
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
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
shAngle = 0
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
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 rotation
' 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_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' param iMin, iMax - shape indices to add
' return shWidth, shHeight - total size of shapes
For i = iMin To iMax
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_Move
' Shapes | Move shapes
' param iMin, iMax - shape indices to add
' 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)
s = scale
shX = x
shY = y
For i = iMin To iMax
shp = shape[i]
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
_x = shp["wx"]
_y = shp["wy"]
Else
_x = shp["rx"]
_y = shp["ry"]
EndIf
Shapes.Move(shp["obj"], shX + _x * s, shY + _y * s)
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Remove
' Shapes | Remove shapes
' param iMin, iMax - shapes indices to remove
' param shape - array of shapes
Stack.PushValue("local", i)
For i = iMin To iMax
shp = shape[i]
Shapes.Remove(shp["obj"])
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Rotate
' Shapes | Rotate shapes
' param iMin, iMax - shapes indices to rotate
' param shape - array of shapes
' param cx, cy - rotation center
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
s = scale
param["angle"] = angle
If cx <> "" Then
param["cx"] = cx
Else
cx = "" ' to avoid syntax error
param["cx"] = shWidth / 2
EndIf
If cy <> "" Then
param["cy"] = cy
Else
cy = "" ' to avoid syntax error
param["cy"] = shHeight / 2
EndIf
For i = iMin To iMax
shp = shape[i]
param["x"] = shp["x"]
param["y"] = shp["y"]
param["width"] = shp["width"]
param["height"] = shp["height"]
Shapes_CalcRotatePos()
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(angle + shp["angle"])
SB_RotateWorkAround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shp["obj"], angle + shp["angle"])
shape[i] = shp
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub
Sub Text_GetWidthInPx
' param txt - text to get width in px
' param ratio - character width/height in a font
' param fs - font size (height)
' return px - width in px (pixels)
len = Text.GetLength(txt)
px = 0
For p = 1 To len
px = px + ratio[Text.GetCharacterCode(Text.GetSubText(txt, p, 1))]
EndFor
px = Math.Floor(px * fs / 100)
EndSub