[bb] Mouse Helper functions by Ross C [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Mouse Helper functions
Author : Ross C
Posted : 1+ years ago

Description : [UPDATED] - Fixed a typo error, and added a new mode, "just held in"

Hey, I have written this a few times for my editor project, so i thought i'd post it here. Might help someone?

Basically, it makes it a touch easier to recognise what kind of mouse input your getting, instead of using mousehit and mousedown.

When you want your mouse input, simply check:

mouse_1_mode
mouse_2_mode
mouse_3_mode

Depending on what button you want to check of course. I have include const variables so you don't need to remember what the numbers mean. The variables will return:

0 - nothing pressed or hit
1 - the mouse was pressed in this loop
2 - the mouse is currently being held in
3 - the mouse was released from being held in
4 - the mouse was clicked
5 - the mouse was just held in, this loop

You can adjust the mouse timers to give longer peroids before the "held" status gets set. This also affects the click time. If it's not a click, it's held basically. All 3 buttons get the exact same outputs.

Oh, and you might want to change the variable names, incase they clash with your project :)

You can just include this into your project. You need to call ProcessMouseHits() every loop.


Code :
Code (blitzbasic) Select
;mouse modes are:
; 0 = nothing
; 1 = in
; 2 = held
; 3 = out
; 4 = click
; 5 = just this loop being held in
Const mouse_in = 1
Const mouse_held = 2
Const mouse_out = 3
Const mouse_click = 4
Const mouse_just_held = 5

;left mouse button
Global mouse_1_mode = 0
Global mouse_1_hold_time = 150 ; length of time for mouse to be considered held down
Global mouse_1_hold_timer

;right mouse button
Global mouse_2_mode = 0
Global mouse_2_hold_time = 150 ; length of time for mouse to be considered held down
Global mouse_2_hold_timer

; middle mouse button
Global mouse_3_mode = 0
Global mouse_3_hold_time = 5 ; length of time for mouse to be considered held down
Global mouse_3_hold_timer


Function process_mouse_hits()

If mouse_1_mode = mouse_just_held Then
mouse_1_mode = mouse_held
End If
If mouse_2_mode = mouse_just_held Then
mouse_2_mode = mouse_held
End If
If mouse_3_mode = mouse_just_held Then
mouse_3_mode = mouse_held
End If

If MouseDown(1) Then
If mouse_1_mode = 0 Then
mouse_1_mode = mouse_in
mouse_1_hold_timer = MilliSecs()
ElseIf mouse_1_mode = mouse_in Then
If MilliSecs() > mouse_1_hold_timer+mouse_1_hold_time Then
mouse_1_mode = mouse_just_held
End If
End If
Else
If mouse_1_mode = mouse_click Or mouse_1_mode = mouse_out Then ; check for click first, as other may set click below
mouse_1_mode = 0
ElseIf mouse_1_mode = mouse_in Then
mouse_1_mode = mouse_click
ElseIf mouse_1_mode = mouse_held Then
mouse_1_mode = mouse_out
End If
End If


If MouseDown(2) Then
If mouse_2_mode = 0 Then
mouse_2_mode = mouse_in
mouse_2_hold_timer = MilliSecs()
ElseIf mouse_2_mode = mouse_in Then
If MilliSecs() > mouse_2_hold_timer+mouse_2_hold_time Then
mouse_2_mode = mouse_just_held
End If
End If
Else
If mouse_2_mode = mouse_click Or mouse_2_mode = mouse_out Then ; check for click first, as other may set click below
mouse_2_mode = 0
ElseIf mouse_2_mode = mouse_in Then
mouse_2_mode = mouse_click
ElseIf mouse_2_mode = mouse_held Then
mouse_2_mode = mouse_out
End If
End If

If MouseDown(3) Then
If mouse_3_mode = 0 Then
mouse_3_mode = mouse_in
mouse_3_hold_timer = MilliSecs()
ElseIf mouse_3_mode = mouse_in Then
If MilliSecs() > mouse_3_hold_timer+mouse_3_hold_time Then
mouse_3_mode = mouse_just_held
End If
End If
Else
If mouse_3_mode = mouse_click Or mouse_3_mode = mouse_out Then ; check for click first, as other may set click below
mouse_3_mode = 0
ElseIf mouse_3_mode = mouse_in Then
mouse_3_mode = mouse_click
ElseIf mouse_3_mode = mouse_held Then
mouse_3_mode = mouse_out
End If
End If

End Function


Comments :


Ross C(Posted 1+ years ago)

 Oh, and here's some code to demonstrate it working:
;mouse modes are:
; 0 = nothing
; 1 = in
; 2 = held
; 3 = out
; 4 = click
Const mouse_in = 1
Const mouse_held = 2
Const mouse_out = 3
Const mouse_click = 4

;left mouse button
Global mouse_1_mode = 0
Global mouse_1_hold_time = 80 ; length of time for mouse to be considered held down
Global mouse_1_hold_timer

;right mouse button
Global mouse_2_mode = 0
Global mouse_2_hold_time = 80 ; length of time for mouse to be considered held down
Global mouse_2_hold_timer

; middle mouse button
Global mouse_3_mode = 0
Global mouse_3_hold_time = 80 ; length of time for mouse to be considered held down
Global mouse_3_hold_timer




Graphics 800,600
SetBuffer BackBuffer()

Global msg1$ = "Waiting left mouse input..."
Global msg2$ = "Waiting right mouse input..."

While Not KeyHit(1)

Cls

ProcessMouseHits()

If mouse_1_mode = mouse_in Then
msg1 = "Left Mouse button pressed in"
ElseIf mouse_1_mode = mouse_held Then
msg1 = "Left Mouse button held in"
ElseIf mouse_1_mode = mouse_out Then
msg1 = "Left Mouse button let go"
ElseIf mouse_1_mode = mouse_click Then
msg1 = "Left Mouse button clicked"
End If

If mouse_2_mode = mouse_in Then
msg2 = "Right Mouse button pressed in"
ElseIf mouse_2_mode = mouse_held Then
msg2 = "Right Mouse button held in"
ElseIf mouse_2_mode = mouse_out Then
msg2 = "Right Mouse button let go"
ElseIf mouse_2_mode = mouse_click Then
msg2 = "Right Mouse button clicked"
End If


Text 0,0,msg1
Text 0,20,msg2
Flip
Wend

Function ProcessMouseHits()

If MouseDown(1) Then
If mouse_1_mode = 0 Then
mouse_1_mode = mouse_in
mouse_1_hold_timer = MilliSecs()
ElseIf mouse_1_mode = mouse_in Then
If MilliSecs() > mouse_1_hold_timer+mouse_1_hold_time Then
mouse_1_mode = mouse_held
End If
End If
Else
If mouse_1_mode = mouse_click Or mouse_1_mode = mouse_out Then ; check for click first, as other may set click below
mouse_1_mode = 0
ElseIf mouse_1_mode = mouse_in Then
mouse_1_mode = mouse_click
ElseIf mouse_1_mode = mouse_held Then
mouse_1_mode = mouse_out
End If
End If

If MouseDown(2) Then
If mouse_2_mode = 0 Then
mouse_2_mode = mouse_in
mouse_2_hold_timer = MilliSecs()
ElseIf mouse_2_mode = mouse_in Then
If MilliSecs() > mouse_2_hold_timer+mouse_2_hold_time Then
mouse_2_mode = mouse_held
End If
End If
Else
If mouse_2_mode = mouse_click Or mouse_2_mode = mouse_out Then ; check for click first, as other may set click below
mouse_2_mode = 0
ElseIf mouse_2_mode = mouse_in Then
mouse_2_mode = mouse_click
ElseIf mouse_2_mode = mouse_held Then
mouse_2_mode = mouse_out
End If
End If

If MouseDown(3) Then
If mouse_3_mode = 0 Then
mouse_3_mode = mouse_in
mouse_3_hold_timer = MilliSecs()
ElseIf mouse_3_mode = mouse_in Then
If MilliSecs() > mouse_3_hold_timer+mouse_3_hold_time Then
mouse_3_mode = mouse_held
End If
End If
Else
If mouse_3_mode = mouse_click Or mouse_3_mode = mouse_out Then ; check for click first, as other may set click below
mouse_3_mode = 0
ElseIf mouse_3_mode = mouse_in Then
mouse_3_mode = mouse_click
ElseIf mouse_3_mode = mouse_held Then
mousee_3_mode = mouse_out
End If
End If

End Function



Ross C(Posted 1+ years ago)

 Updated with fixes and new mode. [/i]