Microsoft Small Basic

Program Listing: DMX970-0
GraphicsWindow.Title = "Turtle Graphics
GraphicsWindow.BackgroundColor="#bbccdd
Init()
dbg="False"
Opening()
dmt="0=Add/Chng...;1=Diamond;2=MultiDiamnds;3=Lace;4=Spyrall;5=Star5"
dmm=6
LDDialogs.AddRightClickMenu(dmt,"")

deff[1]="{10!S 0!R 108!{5 [!R 72!F 80!]
deff[2]="{5!S 0!{10[!R 108!{5 [!R 72!F 80!]!]!R 144!U!F 210!D
deff[3]="{25!S 0!F 30!{3 [!R 95!F 140!]
deff[4]="{95!S 1.025!R 91.5!F 40
deff[5]="{5!R 144!F 140!]

LDDialogs.RightClickMenu=rmm

Main()

Sub rmm
dd= LDDialogs.LastRightClickMenuItem
if dd>0 Then
Controls.SetTextBoxText(pgm, LDText.Replace ( deff[dd],"!",CR)) ' Nonki: ESLText.CRLF
Else
ib=Dialogs.AskForTextLine ("Name (x=exit, l=load, s=save):","New Menu Item")
If ib="s" Then
' The following line could be harmful and has been automatically commented.
' File.WriteContents ("f:\defs.txt",deff)
' The following line could be harmful and has been automatically commented.
' File.WriteContents ("f:\defm.txt",dmt)
Dialogs.ShowMessageBox("Saved ok.","Menu","OK","Information")
elseIf ib="l" Then
' The following line could be harmful and has been automatically commented.
' deff=File.ReadContents ("f:\defs.txt")
' The following line could be harmful and has been automatically commented.
' dmt=File.ReadContents ("f:\defm.txt")
Dialogs.ShowMessageBox("Load ok.","Menu","OK","Information")
LDDialogs.AddRightClickMenu(dmt,"")
elseIf ib<>"x" then
li= LDText.Split (Text.ConvertToUpperCase(src),CR)
att=""
For t=1 To Array.GetItemCount (li)
att=att+li[t]+"!"
endfor
deff[dmm]= att
dmt[dmm]=ib
dmm=dmm+1
LDDialogs.AddRightClickMenu(dmt,"")


endif
EndIf

EndSub

Sub Main
rrr:
GraphicsWindow.PenWidth=1
ch=0
l=1
sc=1
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.FontSize = 20
pgm = Controls.AddMultiLineTextBox(10, 10)
Controls.SetSize(pgm, 180, gh - 60)
Controls.SetTextBoxText(pgm, src)
GraphicsWindow.BrushColor = "Black"
c1= Controls.AddButton("RUN", 10, gh - 46)
c2=Controls.AddButton("CLR", 70, gh - 46)
clicked = "False"
Controls.ButtonClicked = OnButtonClicked
GraphicsWindow.PenColor = "DimGray"
GraphicsWindow.PenWidth = 2
Turtle.Show()
Turtle.PenUp()
Turtle.MoveTo (500,300)
Turtle.PenDown()
Turtle.Angle=0

While "True"
If rst=1 Then
src = Controls.GetTextBoxText(pgm)
For i = 1 To 1000 ' Nonki: GraphicsWindow.Clear()
Shapes.Remove("_turtleLine" + i)
EndFor
rst=0
Goto rrr

elseIf clicked Then
src = Controls.GetTextBoxText(pgm)
If Text.GetSubText(src,1,1)="{" or Text.GetSubText(src,1,1)="#" then
If Text.GetSubText(src,1,1)="#" then
dbg="True"
Else
dbg="False"
endif
line= LDText.Split (Text.ConvertToUpperCase(src),CR) ' Nonki: ESLText.CRLF
rr=MathPlus.ToNumber ( text.GetSubTextToEnd(line[1],2))
nLines =Array.GetItemCount (line)

For tt=1 To rr
For i = 2 To nLines
linee=line[i]
DoLine()
EndFor
EndFor

Else
Run()
EndIf
clicked = "False"
Else
Program.Delay(2)
EndIf

EndWhile
EndSub

Sub OnButtonClicked
If Controls.LastClickedButton=c2 then
rst=1
else
clicked = "True"
endif
EndSub

Sub Run
line= LDText.Split (Text.ConvertToUpperCase(src),CR)
if dbg then
TextWindow.WriteLine (line)
endif
l = 1 ' level
nLines =Array.GetItemCount (line)
For i = 1 To nLines
linee=line[i]
DoLine()
EndFor
EndSub



Sub DoLine
' param line - array of command lines
' param i - index to do for the line
GraphicsWindow.PenColor=LDColours.HSLtoRGB(math.Remainder (ch,360) ,0.9,0.4)
ch=ch+1
If Text.StartsWith(linee, "F") Then
distance = Text.GetSubTextToEnd(linee, 3)
Turtle.Move(math.Floor (distance*sc))
if dbg then
TextWindow.WriteLine(math.Floor (distance*sc))
endif
ElseIf Text.StartsWith(linee, "S") Then
sc=sc* Text.GetSubTextToEnd(linee, 3)
If sc=0 then
sc=1
EndIf
ElseIf Text.StartsWith(linee, "U") Then
Turtle.PenUp()
ElseIf Text.StartsWith(linee, "D") Then

Turtle.PenDown()
ElseIf Text.StartsWith(linee, "R") Then
angle = Text.GetSubTextToEnd(linee, 3)
Turtle.Turn(angle)
ElseIf Text.StartsWith(linee, "L") Then
angle = -Text.GetSubTextToEnd(linee, 3)
Turtle.Turn(angle)
ElseIf Text.StartsWith(linee, "{") Then
b = Text.GetIndexOf(linee, "[")
count[l] = mathplus.ToNumber ( Text.GetSubText(linee, 2, b - 2))
iStart[l] = i + 1
iEnd[l] = nLines
nest = 0
if dbg then
TextWindow.WriteLine ("For #/b/e:"+l+"\"+count[l]+"# "+(i+1)+":"+nLines )
endif
For k = iStart[l] To nLines
'TextWindow.WriteLine(">>"+k)
If Text.StartsWith(line[k], "{") Then
nest = nest + 1
ElseIf Text.StartsWith(line[k], "]") Then
If nest = 0 Then
iEnd[l] = k - 1
k = nLines
Else
nest = nest - 1
EndIf
EndIf
EndFor

l = l + 1
if dbg then
TextWindow.WriteLine ("<:"+count[l - 1])
endif
For j = 1 To count[l - 1]
Stack.PushValue("local", j)
For i = iStart[l - 1] To iEnd[l - 1]
linee=line[i]
If dbg then
TextWindow.WriteLine (i+">>"+linee)
endif
DoLine()
EndFor
j = Stack.PopValue("local")
EndFor
l = l - 1
i = iEnd[l] + 1
EndIf
EndSub

Sub Init
gw = 800
gh = 800
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
CR = Text.GetCharacter(13) ' Nonki: ESLText.CRLF
src = "{5"+cr+"F 10" + CR + "{3 ["+cr+"R 95" + CR + "F 40"+cr+"]"

EndSub

Sub Opening
ms = "TITLE=550;F=1;R=10;CL=400;HELP=5000;" ' Nonki: 2400;"
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.FontSize = 15
GraphicsWindow.FontName = "Tahoma"
GraphicsWindow.DrawText(10, 0, "TURTLE GRAPHICS")
GraphicsWindow.BrushColor = "#cccccc"
For i = 1 To 5
GraphicsWindow.FillRectangle(0, i*40+25, 150, 35)
EndFor
Turtle.Speed=10
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.DrawText(10, 30, "COMMANDS:")
GraphicsWindow.DrawText(10, 70, "F distance")
GraphicsWindow.DrawText(10, 110, "L|R angle")
GraphicsWindow.DrawText(10, 150, "D)wn | U)p")
GraphicsWindow.DrawText(10, 190, "rpt:{count [cmds]")
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.DrawText(10, 230, "HAVE FUN!")
Program.Delay(ms["HELP"])
GraphicsWindow.Clear()
EndSub
Sub OnKeyDown
If GraphicsWindow.LastKey = "Space" Then
ms = ""
EndIf
EndSub