Microsoft Small Basic

Program Listing: ZSH672-0
' Extensible Paint 0.2 - Group Challenge July 2014
' Program ID ZSH672-0
' ------------------- MAIN ----------------------
GraphicsWindow.Title = "ExPaint 0.2 | F1=Pen F2=PenColor F3=Eraser"
mouseMove = "False"
mouseDown = "False"
keyDown = "False"
key = "F1"
validKeys = "1=F1;2=F2;3=F3"
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.KeyDown = OnKeyDown
Controls.ButtonClicked = OnButtonClicked
While "True"
If keyDown Then
prevKey = key
key = GraphicsWindow.LastKey
keyDown = "False"
Plug_Ins()
Else
Program.Delay(10)
EndIf
EndWhile

Sub Plug_Ins
If key = "F1" Then
PenDraw()
ElseIf key = "F2" Then
SelectPenColor()
ElseIf key = "F3" Then
Eraser()
EndIf
EndSub

Sub OnKeyDown
keyDown = "True"
EndSub

Sub OnMouseDown
mouseDown = "True"
EndSub

Sub OnMouseMove
mouseMove = "True"
EndSub

Sub OnButtonClicked
buttonClicked = "True"
EndSub
' ------------------- PLUG-INS ----------------------
Sub PenDraw
lastKey = key
While key = lastKey
If mouseMove Then
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
If (Mouse.IsLeftButtonDown) Then
GraphicsWindow.DrawLine(prevX, prevY, x, y)
EndIf
prevX = x
prevY = y
mouseMove = "False"
ElseIf mouseDown Then
prevX = GraphicsWindow.MouseX
prevY = GraphicsWindow.MouseY
mouseDown = "False"
ElseIf keyDown Then
_key = GraphicsWindow.LastKey
nKey = Array.GetItemCount(validKeys)
valid = "False"
For i = 1 To nKey
If _key = validKeys[i] Then
valid = "True"
EndIf
EndFor
If valid Then
key = _key
EndIf
Else
Program.Delay(10)
EndIf
EndWhile
EndSub
Sub Eraser
lastKey = key
Stack.PushValue("local", GraphicsWindow.PenWidth)
Stack.PushValue("local", GraphicsWindow.PenColor)
Stack.PushValue("local", GraphicsWindow.BrushColor)
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Gray"
erEll = Shapes.AddEllipse(40, 40)
Shapes.SetOpacity(erEll, 50)
Shapes.Move(erEll, prevX - 20, prevY - 20)
GraphicsWindow.PenWidth = 40
GraphicsWindow.PenColor = "White"
While key = lastKey
If mouseMove Then
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
Shapes.Move(erEll, x - 20, y - 20)
If (Mouse.IsLeftButtonDown) Then
GraphicsWindow.DrawLine(prevX, prevY, x, y)
EndIf
prevX = x
prevY = y
mouseMove = "False"
ElseIf mouseDown Then
prevX = GraphicsWindow.MouseX
prevY = GraphicsWindow.MouseY
mouseDown = "False"
ElseIf keyDown Then
_key = GraphicsWindow.LastKey
nKey = Array.GetItemCount(validKeys)
valid = "False"
For i = 1 To nKey
If _key = validKeys[i] Then
valid = "True"
EndIf
EndFor
If valid Then
key = _key
EndIf
EndIf
EndWhile
Shapes.Remove(erEll)
GraphicsWindow.BrushColor = Stack.PopValue("local")
GraphicsWindow.PenColor = Stack.PopValue("local")
GraphicsWindow.PenWidth = Stack.PopValue("local")
EndSub
Sub SelectPenColor
' return selectedPenColor - returned if new pen color is selected
' draw dialog
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
dw = 260
dh = 160
pw = GraphicsWindow.PenWidth
bc = GraphicsWindow.BrushColor
fs = GraphicsWindow.FontSize
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Gray"
dialog = Shapes.AddRectangle(dw, dh)
Shapes.SetOpacity(dialog, 50)
dLeft = (gw - dw) / 2
dTop = (gh - dh) / 2
Shapes.Move(dialog, dLeft, dTop)
' draw buttons
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 12
dOK = Controls.AddButton("OK", dLeft + 10, dTop + dh - 40)
dCancel = Controls.AddButton("Cancel", dLeft + 50, dTop + dh - 40)
' draw current pen color
pcCandidate = GraphicsWindow.PenColor
GraphicsWindow.BrushColor = pcCandidate
penColor = Shapes.AddRectangle(100, 100)
Shapes.Move(penColor, dLeft + 10, dTop + 10)
' draw palette
palette = "0=Black;1=Red;2=Green;4=Blue;"
pIndex = Array.GetAllIndices(palette)
pNum = Array.GetItemCount(palette)
pLeft = dLeft + 120
pTop = dTop + 10
For i = 1 To pNum
GraphicsWindow.BrushColor = palette[pIndex[i]]
pSquare[i] = Controls.AddButton("■", pLeft + (i - 1) * 30, pTop)
EndFor
continue = "True"
While continue
If buttonClicked Then
button = Controls.LastClickedButton
If button = dOK Then
selectedPenColor = pcCandidate
GraphicsWindow.PenColor = selectedPenColor
continue = "False"
ElseIf button = dCancel Then
continue = "False"
Else
For i = 1 To pNum
If button = pSquare[i] Then
pcCandidate = palette[pIndex[i]]
GraphicsWindow.BrushColor = pcCandidate
Shapes.Remove(penColor)
penColor = Shapes.AddRectangle(100, 100)
Shapes.Move(penColor, dLeft + 10, dTop + 10)
EndIf
EndFor
EndIf
buttonClicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile
' remove controls and dialog
Shapes.Remove(penColor)
For i = 1 To pNum
Controls.Remove(pSquare[i])
EndFor
Controls.Remove(dOK)
Controls.Remove(dCancel)
Shapes.Remove(dialog)
' restore properties
GraphicsWindow.FontSize = fs
GraphicsWindow.PenWidth = pw
GraphicsWindow.BrushColor = bc
EndSub