Microsoft Small Basic

Program Listing:
Embed this in your website
' Pictogram - Elevater
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Last update 2019-12-14

SB_Workaround()
Init()
' initialize shapes
Shapes_Init_Box()
' add shapes
scale = 1
name = "Box"
Group_Add()
' initialize shapes
Shapes_Init_Man()
' add shapes
name = "Man 1"
Group_Add()
i = nGroup
grp = group[i]
x = grp["x"] - 74
y = grp["y"]
Group_Move()
name = "Man 2"
Group_Add()
name = "Man 3"
Group_Add()
i = nGroup
grp = group[i]
x = grp["x"] + 74
y = grp["y"]
Group_Move()
' initialize shapes
Shapes_Init_Arrow()
' add shapes
name = "Up"
Group_Add()
name = "Down"
Group_Add()
i = nGroup
grp = group[i]
x = grp["x"] - 156
y = grp["y"] + 10
Group_Move()
angle = 180
Group_Rotate()

Sub Init
  GraphicsWindow.Title = "Elevator"
  gw = 600
  gh = 600
  GraphicsWindow.Width = gw
  GraphicsWindow.Height = gh
EndSub

Sub Group_Add
  ' Group | add shapes to a group
  ' param name - group name
  ' param shX, shY, origin of shape array
  ' param shape[] - shape array
  ' param scale - to resize
  ' 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["height"] = shHeight
  grp["cx"] = shWidth / 2
  grp["cy"] = shHeight / 2
  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 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
  grp["shape"] = shape
  group[nGroup] = grp
  y = Stack.PopValue("local")
  x = Stack.PopValue("local")
  i = Stack.PopValue("local")
EndSub

Sub Group_Move
  ' Group | move a group
  ' param group[i] - group To move
  ' param x, y - position To move
  ' return group[i] - updated group
  Stack.PushValue("local", j)
  grp = group[i]
  s = grp["scale"]
  grp["x"] = x
  grp["y"] = y
  shape = grp["shape"]
  n = Array.GetItemCount(shape)
  For j = 1 To n
    shp = shape[j]
    If sbd And (shp["func"] = "line") Then
      _x = shp["wx"]
      _y = shp["wy"]
    ElseIf 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"], grp["x"] + _x * s, grp["y"]  + _y * s)
  EndFor
  group[i] = grp
  j = Stack.PopValue("local")
EndSub

Sub Group_Rotate
  ' Group | rotate a group
  ' param group[i] - group to move
  ' param cx, cy - rotation center (if given)
  ' param angle - to rotate
  Stack.PushValue("local", x)
  Stack.PushValue("local", y)
  Stack.PushValue("local", n)
  grp = group[i]
  shape = grp["shape"]
  moved = "False"
  If cx <> "" Then
    moved = "True"
  Else
    cx = ""   ' to avoid syntax error
  EndIf
  If cy <> "" Then
    moved = "True"
  Else
    cy = ""   ' to avoid syntax error
  EndIf
  If moved Then
    param["x"] = grp["x"]
    param["y"] = grp["y"]
    param["cx"] = cx
    param["cy"] = cy
    param["width"] = grp["width"]
    param["height"] = grp["height"]
    param["scale"] = 1
    param["angle"] = angle
    Shapes_CalcRotateZoomPos()
    grp["x"] = x
    grp["y"] = y
  EndIf
  param["cx"] = grp["width"] / 2
  param["cy"] = grp["height"] / 2
  param["scale"] = grp["scale"]
  grp["angle"] = angle
  param["angle"] = grp["angle"]
  n = Array.GetItemCount(shape)
  Stack.PushValue("local", i)
  For i = 1 To n
    shp = shape[i]
    param["x"] = shp["x"]
    param["y"] = shp["y"]
    param["width"] = shp["width"]
    param["height"] = shp["height"]
    Shapes_CalcRotateZoomPos()
    shp["rx"] = x
    shp["ry"] = y
    alpha = shp["angle"] + grp["angle"]
    If sbd And (shp["func"] = "line") And (alpha <> 0) Then
      x1 = shp["x1"]
      y1 = shp["y1"]
      x2 = shp["x2"]
      y2 = shp["y2"]
      pw = shp["pw"]
      SB_LineWorkaround()
      shp["wx"] = x
      shp["wy"] = y
    ElseIf silverlight And Text.IsSubText("tri|line", shp["func"]) Then
      _alpha = Math.GetRadians(alpha)
      SB_RotateWorkAround()
      shp["wx"] = x
      shp["wy"] = y
    EndIf
    Shapes.Move(shp["obj"], grp["x"] + x, grp["y"] + y)
    Shapes.Rotate(shp["obj"], shp["angle"] + grp["angle"])
    shape[i] = shp
  EndFor
  i = Stack.PopValue("local")
  grp["shape"] = shape
  group[i] = grp
  n = Stack.PopValue("local")
  y = Stack.PopValue("local")
  x = Stack.PopValue("local")
EndSub

Sub Math_CartesianToPolar
  ' Math | convert Cartesian coodinate to polar coordinate
  ' param x, y - Cartesian coordinate
  ' return r, a - polar Coordinate (0<=a<360)
  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 And y = 0 Then
    a = 0
  Else
    a = Math.ArcTan(y / x) * 180 / Math.Pi
  EndIf
  ' at this point -90<=a<=90
  If x < 0 Then
    a = a + 180
  ElseIf x >= 0 And y < 0 Then
    a = a + 360
  EndIf
  ' at this point 0<=a<360
EndSub

Sub SB_LineWorkaround
  ' Small Basic | line rotate workaround for SBD
  ' param x, y - coordinate of the position of the line
  ' param x1, y1 - coordinate of the first point
  ' param x2, y2 - coordinate of the second point
  ' param pw - pen width
  ' param alpha - to rotate [degree]
  ' return x, y - workaround value for the coordinate
  Stack.PushValue("local", x)
  Stack.PushValue("local", y)
  x = x1 - x2
  y = y1 - y2
  Math_CartesianToPolar()
  y = Stack.PopValue("local")
  x = Stack.PopValue("local")
  _a = Math.GetRadians(a)
  _alpha = Math.GetRadians(a - alpha)
  Δx = pw / 4 * (Math.Sin(_alpha) - Math.Sin(_a))
  Δy = pw / 4 * (Math.Cos(_alpha) - Math.Cos(_a))
  x = x - Δx
  y = y - Δy
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 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_CalcRotateZoomPos
  ' Shapes | calculate position for rotated and zoomed 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
  ' param ["scale"] - zoom scale
  ' 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) * param["scale"]
  y = r * Math.Sin(a * Math.Pi / 180) * param["scale"]
  _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 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_DumpArray
  order = "1=func;2=x;3=y;4=width;5=height;6=x1;7=y1;8=x2;9=y2;"
  order = order + "10=x3;11=y3;12=txt;13=fn;14=fs;15=fb;16=fi;"
  order = order + "17=angle;18=pw;19=pc;20=bc;"
  nOrder = Array.GetItemCount(order)
  WQ = Text.GetCharacter(34)
  LF = Text.GetCharacter(10)
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FontName = "Consolas"
  GraphicsWindow.FontSize = 14
  tbox = Controls.AddMultiLineTextBox(gw / 2, 10)
  Controls.SetSize(tbox, gw / 2 - 10, gh - 20)
  buf = " shX = " + shX + " ' x offset" + LF
  buf = buf + " shY = " + shY + " ' y offset" + LF
  buf = buf + " shape = " + WQ + WQ + LF
  For i = 1 To Array.GetItemCount(shape)
    buf = buf + " shape[" + i + "] = " + WQ
    shp = shape[i]
    For j = 1 To nOrder
      If shp[order[j]] <> "" Then
        buf = buf + order[j] + "=" + shp[order[j]] + ";"
      EndIf
    EndFor
    buf = buf + WQ + LF
  EndFor
  Controls.SetTextBoxText(tbox, buf)
  Shapes.SetOpacity(tbox, 50)
EndSub

Sub Shapes_Init_Arrow
  ' Shapes | initialize shapes data
  ' return shX, shY - current position of shapes
  ' return shape - array of shapes
  shX = 296.28 ' x offset
  shY = 43.52 ' y offset
  shape = ""
  shape[1] = "func=rect;x=66.22;y=21.48;width=30;height=150;pw=0;bc=Black;"
  shape[2] = "func=rect;x=44.36;y=0.00;width=21.21;height=95.45;angle=45;pw=0;bc=Black;"
  shape[3] = "func=rect;x=96.86;y=0.00;width=21.21;height=95.45;angle=-45;pw=0;bc=Black;"
  shape[4] = "func=tri;x=0.00;y=70.87;x1=21.21;y1=0;x2=0;y2=21.21;x3=42.42;y3=21.21;angle=-45;pw=0;bc=Black;"
  shape[5] = "func=tri;x=120.00;y=70.87;x1=21.21;y1=0;x2=0;y2=21.21;x3=42.42;y3=21.21;angle=45;pw=0;bc=Black;"
EndSub

Sub Shapes_Init_Box
  ' Shapes | initialize shapes data
  ' return shX, shY - current position of shapes
  ' return shape - array of shapes
  shX = 150 ' x offset
  shY = 232 ' y offset
  shape = ""
  shape[1] = "func=rect;x=0;y=0;width=300;height=300;pw=30;pc=Black;bc=Transparent;"
EndSub

Sub Shapes_Init_Man
  ' Shapes | initialize shapes data
  ' return shX, shY - current position of shapes
  ' return shape - array of shapes
  shX = 267 ' x offset
  shY = 305 ' y offset
  shape = ""
  shape[1] = "func=ell;x=19.8;y=0;width=26.4;height=26.4;pw=0;bc=Black;"
  shape[2] = "func=ell;x=0;y=34.32;width=39.6;height=39.6;pw=0;bc=Black;"
  shape[3] = "func=ell;x=26.4;y=34.32;width=39.6;height=39.6;pw=0;bc=Black;"
  shape[4] = "func=rect;x=19.8;y=34.32;width=26.4;height=39.6;pw=0;bc=Black;"
  shape[5] = "func=tri;x=0;y=54.12;x1=3.66;y1=0;x2=0;y2=52.8;x3=7.33;y3=52.8;angle=180;pw=0;bc=Black;"
  shape[6] = "func=tri;x=58.66;y=54.12;x1=3.66;y1=0;x2=0;y2=52.8;x3=7.33;y3=52.8;angle=180;pw=0;bc=Black;"
  shape[7] = "func=rect;x=3.66;y=54.12;width=58.66;height=52.8;pw=0;bc=Black;"
  shape[8] = "func=rect;x=11.88;y=106.92;width=11.88;height=78.08;pw=0;bc=Black;"
  shape[9] = "func=tri;x=17.82;y=106.92;x1=5.94;y1=0;x2=0;y2=78.08;x3=11.88;y3=78.08;angle=180;pw=0;bc=Black;"
  shape[10] = "func=rect;x=42.24;y=106.92;width=11.88;height=78.08;pw=0;bc=Black;"
  shape[11] = "func=tri;x=36.3;y=106.92;x1=5.94;y1=0;x2=0;y2=78.08;x3=11.88;y3=78.08;angle=180;pw=0;bc=Black;"
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.