recognizes Left & Rigth button being pressed, held or released; double click of the left button, mouse wheel and dragging.
No external libraries, only fbgfx.
Based upon a snippet by Paul Doe (viewtopic.php?f=15&t=26673)
The class itself:
Code: Select all
'' A simple class to help us deal with the mouse
'based on a snippet by paul doe -- see topic: [url]https://www.freebasic.net/forum/viewtopic.php?f=15&t=26673[/url]
type mouse_proto
public:
declare function lbtn_hold() as boolean
declare function lbtn_pressed() as boolean
declare function lbtn_released() as boolean
declare function lbtn_double_click() as boolean
declare function rbtn_hold() as boolean
declare function rbtn_pressed() as boolean
declare function rbtn_released() as boolean
declare function is_drag() as boolean
declare function get_wheel() as Long
declare function get_x() as Long
declare function get_y() as Long
declare function get_start_x() as Long
declare function get_start_y() as Long
declare sub update()
declare constructor()
private:
as integer res, x, y, clip, start_x, start_y, old_x, old_y
as integer wheel, old_wheel, new_wheel
as boolean new_lbtn, old_lbtn, new_rbtn, old_rbtn, _
old_lbtn_hold, old_lbtn_pressed, old_lbtn_released
as double new_lbtn_pressed_timestamp, old_lbtn_pressed_timestamp,_
dbl_click_grace_time
Union
buttons As Integer
Type
Left:1 As Integer
Right:1 As Integer
middle:1 As Integer
End Type
End Union
end type
constructor mouse_proto()
this.dbl_click_grace_time = 0.5
end constructor
sub mouse_proto.update ()
this.res = GetMouse( this.x, this.y, _
this.wheel, this.buttons,_
this.clip)
if this.res = 0 then
this.old_x = this.x
this.old_y = this.y
this.old_lbtn = new_lbtn
this.old_rbtn = new_rbtn
this.new_lbtn = cbool ( this.buttons and 1 )
this.new_rbtn = cbool ( this.buttons and 2 )
this.old_wheel = this.new_wheel
this.new_wheel = this.wheel
if this.lbtn_pressed() then
this.start_x = this.x
this.start_y = this.y
this.old_lbtn_pressed_timestamp = this.new_lbtn_pressed_timestamp
this.new_lbtn_pressed_timestamp = timer
end if
else
this.x = this.old_x
this.y = this.old_y
end if
end sub
function mouse_proto.get_x() as Long
return this.x
end function
function mouse_proto.get_y() as Long
return this.y
end function
function mouse_proto.get_start_x() as Long
return this.start_x
end function
function mouse_proto.get_start_y() as Long
return this.start_y
end function
function mouse_proto.get_wheel() as Long
if this.new_wheel = this.old_wheel then
return 0
elseif this.new_wheel > this.old_wheel then
return 1
elseif this.new_wheel < this.old_wheel then
return -1
end if
end function
function mouse_proto.is_drag() as boolean
if this.lbtn_hold() andAlso _
this.start_x <> this.x andAlso _
this.start_y <> this.y then
return true
else
return false
end if
end function
function mouse_proto.lbtn_hold() as boolean
return ( cbool ( this.buttons and 1 ) andAlso ( this.res = 0 ))
end function
function mouse_proto.lbtn_pressed() as boolean
return ( this.old_lbtn ) = false andAlso ( this.new_lbtn ) = true andAlso ( this.res = 0 )
end function
function mouse_proto.lbtn_released() as boolean
return ( this.old_lbtn ) = true andAlso ( this.new_lbtn ) = false andAlso ( this.res = 0 )
end function
function mouse_proto.lbtn_double_click() as boolean
return this.lbtn_pressed() = true andAlso _
Cbool((this.new_lbtn_pressed_timestamp - _
this.old_lbtn_pressed_timestamp) < this.dbl_click_grace_time) _
andAlso ( this.res = 0 )
end function
function mouse_proto.rbtn_hold() as boolean
return ( cbool ( this.buttons and 2 ) andAlso ( this.res = 0 ))
end function
function mouse_proto.rbtn_pressed() as boolean
return ( this.old_rbtn ) = false andAlso ( this.new_rbtn ) = true andAlso ( this.res = 0 )
end function
function mouse_proto.rbtn_released() as boolean
return ( this.old_rbtn ) = true andAlso ( this.new_rbtn ) = false andAlso ( this.res = 0 )
end function
A simply test program:
Code: Select all
#include "fbgfx.bi"
Using FB
Sub utility_consmessage (Byref e As String)
Dim As Integer f = Freefile()
Open cons For Output As #f
Print #f, e
Close #f
End Sub
#include "inc/mouse.bi"
dim mouse as mouse_proto
screenres (640, 480, 24)
SetMouse 640\2, 480\2, 0, 1
do
if MULTIKEY (SC_Escape) then exit do
mouse.update()
if mouse.lbtn_pressed() then utility_consmessage ("[X]|[ ] lbtn_pressed")
if mouse.lbtn_released() then utility_consmessage ("[*]|[ ] lbtn released")
if mouse.lbtn_hold() then utility_consmessage ("[-]|[ ] lbtn_hold")
if mouse.rbtn_pressed() then utility_consmessage ("[ ]|[X] rbtn_pressed")
if mouse.rbtn_released() then utility_consmessage ("[ ]|[*] released")
if mouse.rbtn_hold() then utility_consmessage ("[ ]|[-] rbtn_hold")
if mouse.get_wheel() > 0 then utility_consmessage ("[ ]^[ ] wheel " + str(mouse.get_wheel))
if mouse.get_wheel() < 0 then utility_consmessage ("[ ]v[ ] wheel " + str(mouse.get_wheel))
if mouse.lbtn_double_click() then utility_consmessage ("{#}|[ ] ** double click **")
screenlock
cls
if mouse.is_drag() then
line (mouse.get_x(), mouse.get_y())-(mouse.get_start_x(), mouse.get_start_y()),&hFF00FF, b
draw string (mouse.get_start_x(), mouse.get_start_y()), str (mouse.get_start_x()) + ", " + str (mouse.get_start_y())
draw string (mouse.get_x(), mouse.get_y()), str (mouse.get_x()) + ", " + str (mouse.get_y())
utility_consmessage ("[@]|[ ] drag")
end if
line (mouse.get_x()-5, mouse.get_y())-step(10,0)
line (mouse.get_x(), mouse.get_y()-5)-step(0,10)
screenunlock
sleep 20,1
LOOP