Microsoft Small Basic

Program Listing: RHP722
' Addition by Writing
' Version 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Last update 2016-03-14
'
GraphicsWindow.Title = "Addition 0.1"
num1 = 12345
num2 = 67890
interval = 1000 ' [ms]
Form()
GraphicsWindow.PenColor = "LightGray"
DrawGrid()
While "True"
If buttonClicked Then
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
GraphicsWindow.PenColor = "LightGray"
DrawGrid()
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Black"
' Input numbers
num1 = Controls.GetTextBoxText(tboxN1)
num2 = Controls.GetTextBoxText(tboxN2)
interval = Controls.GetTextBoxText(tboxInt)
' Calculate each length and positon (column)
len1 = Text.GetLength(num1)
len2 = Text.GetLength(num2)
len = Math.Max(len1, len2)
col1 = 2 + (len - len1)
col2 = 2 + (len - len2)
' Write numbers
param = "col=" + col1 + ";row=1;text=" + num1 + ";"
DrawText()
Program.Delay(interval)
param = "col=" + col2 + ";row=2;text=" + num2 + ";"
DrawText()
Program.Delay(interval)
' Write a sign +
param = "col=1;row=2;text=+;"
DrawText()
Program.Delay(interval)
' Draw a line
param = "col=1;row=2;len=" + (len + 1) + ";"
DrawUnderline()
Program.Delay(interval)
' Add numbers from ones places
c = 0 ' carry
For i = 1 To len
d1 = Text.GetSubText(num1, len1 - i + 1, 1)
d2 = Text.GetSubText(num2, len2 - i + 1, 1)
AddDigit()
param = "col=" + (2 + len - i) + ";row=3;text=" + d + ";"
DrawText()
If 0 < c Then
param = "col=" + (1 + len - i) + ";row=3;text=" + c + ";"
If i < len Then
DrawCarry()
Else
DrawText()
EndIf
EndIf
Program.Delay(interval)
EndFor
buttonClicked = "False"
Else
Program.Delay(200) ' wait next button
EndIf
EndWhile
Sub AddDigit
' param d1 - digit1
' param d2 - digit2
' param c - carry
' return d - digit
' return c - new carry
d = d1 + d2 + c
If 1 < Text.GetLength(d) Then
c = Text.GetSubText(d, 1, 1)
d = Text.GetSubText(d, 2, 1)
Else
c = 0
EndIf
EndSub
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.FontName = "Courier New"
fs = 60
fw = fs * 0.6
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.PenWidth = 0
panel = Shapes.AddRectangle(gw, gh - fs * 5)
Shapes.SetOpacity(panel, 50)
Shapes.Move(panel, 0, fs * 5)
GraphicsWindow.FontSize = fs / 3
GraphicsWindow.BrushColor = "Black"
txtN1 = Shapes.AddText("Number1")
Shapes.Move(txtN1, fw, fs * 5.3)
tboxN1 = Controls.AddTextBox(fw * 3.5, fs * 5.26)
Controls.SetTextBoxText(tboxN1, num1)
txtN2 = Shapes.AddText("Number2")
Shapes.Move(txtN2, fw, fs * 6.3)
tboxN2 = Controls.AddTextBox(fw * 3.5, fs * 6.26)
Controls.SetTextBoxText(tboxN2, num2)
txtInt = Shapes.AddText("Interval")
Shapes.Move(txtInt, fw * 8.5, fs * 5.3)
tboxInt = Controls.AddTextBox(fw * 11.5, fs * 5.26)
Controls.SetTextBoxText(tboxInt, interval)
Controls.AddButton("Calc", fw * 14, fs * 6.22)
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "Black"
GraphicsWindow.FontSize = fs
EndSub
Sub OnButtonClicked
buttonClicked = "True"
EndSub
Sub DrawCarry
' param["col"] - column
' param["row"] - row
' param["text"] - text for carry
' return carry - shape
_x = param["col"] * fw
_y = param["row"] * fs
GraphicsWindow.FontSize = fs * 0.25
GraphicsWindow.DrawText(_x + fw * 0.75, _y, param["text"])
GraphicsWindow.FontSize = fs
EndSub
Sub DrawGrid
' param gw, gh - width and height for the graphics window
' param fw, fs - font width and font size (height)
For _x = 0 To gw Step fw
GraphicsWindow.DrawLine(_x, 0, _x, gh)
EndFor
For _y = 0 To gh Step fs
GraphicsWindow.DrawLine(0, _y, gw, _y)
EndFor
EndSub
Sub DrawText
' param["col"] - column
' param["row"] - row
' param["text"] - text
_x = param["col"] * fw
_y = param["row"] * fs
GraphicsWindow.DrawText(_x, _y, param["text"])
EndSub
Sub DrawUnderline
' param["col"] - column
' param["row"] - row
' param["len"] - length
_x = param["col"] * fw
_y = (param["row"] + 1) * fs
_len = param["len"] * fw
GraphicsWindow.DrawLine(_x, _y, _x + _len, _y)
EndSub