November 30, 2020, 02:24:14 AM

Author Topic: [bb] Mouse Helper functions by Ross C [ 1+ years ago ]  (Read 541 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Mouse Helper functions by Ross C [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
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
  1. ;mouse modes are:
  2. ; 0 = nothing
  3. ; 1 = in
  4. ; 2 = held
  5. ; 3 = out
  6. ; 4 = click
  7. ; 5 = just this loop being held in
  8. Const mouse_in = 1
  9. Const mouse_held = 2
  10. Const mouse_out = 3
  11. Const mouse_click = 4
  12. Const mouse_just_held = 5
  13.  
  14. ;left mouse button
  15. Global mouse_1_mode = 0
  16. Global mouse_1_hold_time = 150 ; length of time for mouse to be considered held down
  17. Global mouse_1_hold_timer
  18.  
  19. ;right mouse button
  20. Global mouse_2_mode = 0
  21. Global mouse_2_hold_time = 150 ; length of time for mouse to be considered held down
  22. Global mouse_2_hold_timer
  23.  
  24. ; middle mouse button
  25. Global mouse_3_mode = 0
  26. Global mouse_3_hold_time = 5 ; length of time for mouse to be considered held down
  27. Global mouse_3_hold_timer
  28.  
  29.  
  30. Function process_mouse_hits()
  31.  
  32.         If mouse_1_mode = mouse_just_held Then
  33.                 mouse_1_mode = mouse_held
  34.         End If
  35.         If mouse_2_mode = mouse_just_held Then
  36.                 mouse_2_mode = mouse_held
  37.         End If
  38.         If mouse_3_mode = mouse_just_held Then
  39.                 mouse_3_mode = mouse_held
  40.         End If
  41.  
  42.         If MouseDown(1) Then
  43.                 If mouse_1_mode = 0 Then
  44.                         mouse_1_mode = mouse_in
  45.                         mouse_1_hold_timer = MilliSecs()
  46.                 ElseIf mouse_1_mode = mouse_in Then
  47.                         If MilliSecs() > mouse_1_hold_timer+mouse_1_hold_time Then
  48.                                 mouse_1_mode = mouse_just_held
  49.                         End If
  50.                 End If
  51.         Else
  52.                 If mouse_1_mode = mouse_click Or mouse_1_mode = mouse_out Then ; check for click first, as other may set click below
  53.                         mouse_1_mode = 0
  54.                 ElseIf mouse_1_mode = mouse_in Then
  55.                         mouse_1_mode = mouse_click
  56.                 ElseIf mouse_1_mode = mouse_held Then
  57.                         mouse_1_mode = mouse_out
  58.                 End If
  59.         End If
  60.  
  61.  
  62.         If MouseDown(2) Then
  63.                 If mouse_2_mode = 0 Then
  64.                         mouse_2_mode = mouse_in
  65.                         mouse_2_hold_timer = MilliSecs()
  66.                 ElseIf mouse_2_mode = mouse_in Then
  67.                         If MilliSecs() > mouse_2_hold_timer+mouse_2_hold_time Then
  68.                                 mouse_2_mode = mouse_just_held
  69.                         End If
  70.                 End If
  71.         Else
  72.                 If mouse_2_mode = mouse_click Or mouse_2_mode = mouse_out Then ; check for click first, as other may set click below
  73.                         mouse_2_mode = 0
  74.                 ElseIf mouse_2_mode = mouse_in Then
  75.                         mouse_2_mode = mouse_click
  76.                 ElseIf mouse_2_mode = mouse_held Then
  77.                         mouse_2_mode = mouse_out
  78.                 End If
  79.         End If
  80.  
  81.         If MouseDown(3) Then
  82.                 If mouse_3_mode = 0 Then
  83.                         mouse_3_mode = mouse_in
  84.                         mouse_3_hold_timer = MilliSecs()
  85.                 ElseIf mouse_3_mode = mouse_in Then
  86.                         If MilliSecs() > mouse_3_hold_timer+mouse_3_hold_time Then
  87.                                 mouse_3_mode = mouse_just_held
  88.                         End If
  89.                 End If
  90.         Else
  91.                 If mouse_3_mode = mouse_click Or mouse_3_mode = mouse_out Then ; check for click first, as other may set click below
  92.                         mouse_3_mode = 0
  93.                 ElseIf mouse_3_mode = mouse_in Then
  94.                         mouse_3_mode = mouse_click
  95.                 ElseIf mouse_3_mode = mouse_held Then
  96.                         mouse_3_mode = mouse_out
  97.                 End If
  98.         End If
  99.  
  100. End Function


Comments :


Ross C(Posted 1+ years ago)

 Oh, and here's some code to demonstrate it working:
Code: [Select]
;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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal