getJibba - Tutorial

5.Events

Events are things related to your hardware. They are events like mouseMove, buttonClick, keyDown, keyUp and timer tick. Events in Small Basic are Asynchronous. i.e. they happen independently of the program.

You need to register an event and assign it to a handler e.g. GraphicsWindow.MouseDown = OnMouseDown. The handler is a special subroutine that reacts independently when that event happens.

myShapeSize = 60
myShape = Shapes.AddRectangle(myShapeSize, myShapeSize)

GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = OnMouseUp

While "True" 'Main Loop
  mseX = GraphicsWindow.MouseX
  mseY = GraphicsWindow.MouseY
  X = Shapes.GetLeft(myShape)
  Y = Shapes.GetTop(myShape)

  If mouseDown Then
    If mseX > X And mseX < X + myShapeSize Then
      If mseY > Y And mseY < Y + myShapeSize Then
        select = myShape
      EndIf
    EndIf
    mouseDown = "False"
  EndIf

  If mouseUp Then
    select = ""
    mouseUp = "False"
  EndIf

  Shapes.Move(select, mseX, mseY)

  Program.Delay(15)
EndWhile

Sub OnMouseDown
  mouseDown = "True"
EndSub

Sub OnMouseUp
  mouseUp = "True"
EndSub

In the sample (drag and drop) the handle sets a flag mouseDown = "True" then picks this up in the main loop then sets the flag to "false" ready for another event to take place.

button = Controls.AddButton("Click Me", 10, 20)

Controls.ButtonClicked = OnButtonClicked

While 0=0
  If buttonClicked Then
    TextWindow.WriteLine(Controls.LastClickedButton)
    buttonClicked = ""
  EndIf

  Program.Delay(15) 'gives the cpu a small rest
EndWhile

Sub OnButtonClicked
  buttonClicked = "True"
EndSub