Code archives/User Input/Mouse Helper functions

This code has been declared by its author to be Public Domain code.

Download source code

Mouse Helper functions by Ross C2009
[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.
;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 C2009
Oh, and here's some code to demonstrate it working:




Ross C2009
Updated with fixes and new mode.


Code Archives Forum