Mouse Routine

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

Mouse Routine

Post by NorbyDroid »

Here is some code I put together based on an old Programming Language. It is currently just basic (no pun intended). A Demo is included here to show off the results.

Rat.bi:

Code: Select all

Type NewMouse
  As Integer Grab, xRat, yRat, ButtonRat
  As String*1 ButtonStr

  Declare Function Button as Boolean

  Declare Function RatX as Integer
  Declare Function RatY as Integer

  Declare Function ClickRect(x1 As Integer, y1 As Integer, _
                             x2 As Integer, y2 As Integer) As Boolean

  Declare Function MouseRect(x1 As Integer, y1 As Integer, _
                             x2 As Integer, y2 As Integer) As Boolean

  Declare Sub Update
End Type

' The Button function returns a value of TRUE when the Rat button is
' clicked.
Function NewMouse.Button As Boolean
  Update: Return ButtonRat
End Function

' The RatX function returns the X coordinate value of the current
' Rat location.
Function NewMouse.RatX As Integer
  Update: Return xRat
End Function

' The RatY function returns the Y coordinate value of the current
' Rat location.
Function NewMouse.RatY As Integer
  Update: Return yRat
End Function

' The ClickRect function will return TRUE if the last place the Rat
' button was clicked falls within the given rectangle.
Function NewMouse.ClickRect(x1 As Integer, y1 As Integer, _
                       x2 As Integer, y2 As Integer) As Boolean

  Update
  If ButtonRat<>0 and MouseRect(x1, y1, x2, y2)<>0 Then Return 1 else Return 0
End Function

' The RatRect function returns a value of TRUE when the Rat cursor
' is located in the specified rectangle.
Function NewMouse.MouseRect(x1 As Integer, y1 As Integer, _
                       x2 As Integer, y2 As Integer) As Boolean

  Update
  If (xRat>=x1 And xRat<=x2) And (yRat>=y1 And yRat<=y2) Then Return 1
  
  Return 0
End Function

Sub NewMouse.Update
  Grab=GetMouse(xRat, yRat, , ButtonRat)

  ButtonStr=" "
  If ButtonRat then
    If ButtonRat and 1 then ButtonStr="L"
    If ButtonRat and 2 then ButtonStr="R"
    If ButtonRat and 4 then ButtonStr="M"
  EndIf
end sub
DemoRat.bas:

Code: Select all

#Include Once "Rat.bi"
Dim Rat as NewMouse

ScreenRes 640, 480, 8

Dim as String Str1, Str2

Dim as Boolean Done=0
Line(480, 10)-(500, 230), 6, bf

Locate 4, 2, 0: Print "Move the Rat around and it will show the position.";
Locate 10, 2, 0: Print "Move the Rat in and out of the Box and Click inside."

While Not Done
 If Inkey=Chr(27) then Done=1
   
 Locate 2, 2, 0: Print "MouseX:"; Rat.RatX; "   ", "MouseY:"; Rat.RatY; "   ";

 Str1=Space(6): Str2=Space(5)
 If Rat.ButtonStr="L" then Str1=" Left "
 If Rat.ButtonStr="M" then Str1="Middle"
 If Rat.ButtonStr="R" then Str1="Right "
 
 If Rat.MouseRect(480, 10, 500, 230) then
   If Rat.ClickRect(480, 10, 500, 230) then Str2=" ate " Else Str2="Found"
 endif

 Locate 8, 2: Print "The "+Str1+" Rat has "+Str2+" the cheese.";
wend
If anyone can think of anything that could be added improved, etc. Let me know. Enjoy.
Post Reply