Detecting a Mouse drag

BlitzMax Forums/MaxGUI Module/Detecting a Mouse drag

TAS(Posted 2012) [#1]
'this is how i do it is there a better way?

'*****  main control loop 
Repeat
  WaitEvent()
  Select EventID()
 Case EVENT_MOUSEDOWN 	
 	If Main_Map_drag()=0 Then Main_MouseUp()
.........

Function Main_Map_drag()
Local t0,x0,y0,ms,dx,dy
'Left mouse button down
'if time expires without a mouse release or if the pointer moves far enough  start the drag operation

t0=MilliSecs()
ms=150	'may need to be longer for slower 'clickers'
'remember starting mouse position	
x0=EventX()
y0=EventY()
Repeat
	WaitEvent()   'looking for mouseup before time expires to start drag operation
	Select EventID()
	Case EVENT_MOUSEUP 
	Return False	'passes control to MouseUP handlier
	Case EVENT_MOUSEMOVE
'the other exit, a long move with the button down
	If Abs(x0-EventX())>150 Or Abs(y0-EventY())>150
 '150 =# pixels moved to be considered a drag
	   Exit
	EndIf
	End Select 
Until MilliSecs()-t0>ms
'Time up treat as mouse drag


Last edited 2012

Last edited 2012


col(Posted 2012) [#2]
Hiya,

I'd personally do it using a simple flag to call the Main_Map_Drag() code...

Global MouseDownFlag
Global MouseDownX,MouseDownY

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_MOUSEDOWN

			MouseDownFlag = True
			MouseDownX = EventX(),MouseDownY = EventY()
			
		Case EVENT_MOUSEUP
			MouseDownFlag = False
			Main_MouseUp()

	EndSelect
	
	If MouseDownFlag Main_Map_Drag(EventX(),EventY())
Forever

Function Main_Map_Drag(X,Y)
	If Abs(X - MouseDownX)....
		........
		........
	EndIf
EndFunction


Do you specifically need a single/double click action too? I'm curious why you have a comment about using a delay?

ps. Hint - To post code examples see here under Code and Code Box, just makes it easier to read thats all.

Last edited 2012


jsp(Posted 2012) [#3]
Hmm, you didn't specify a source Gadget for the EVENT_MOUSEDOWN, thus not totally clear from that structure what to expect.
Is this a drag operation on a canvas, for your own stuff drawn?

I would try to avoid a second WaitEvent() loop, here in your function Main_Map_drag().
While it is easier at the moment it limits the possibilities later in the program.
150 pixels sound quite a lot, but I don't know the program so why not...


TAS(Posted 2012) [#4]
Dave - Thanks for tip on formatting codes.

- The code is for canvas gadget.
- The 150 works well for 1920X1080 screen resolution but should probably be proportionally smaller for smaller screens.
- I am still getting false drag detections but that may be due to code not shown here.


col(Posted 2012) [#5]
As jsp suggests...

In the example code your'e not specifying when/if the mouse is over a specific gadget ( EventSource() ), although you do mention a canvas, so I suggest looking into that next.
Select EventID()
	Case EVENT_MOUSEDOWN
		If EventSource() = myCanvas
		...
		EndIf
EndSelect

or
Select EventSource()
	Case myCanvas
		If EventID() = EVENT_MOUSEDOWN
		...
		EndIf
EndSelect

or even
Select EventSource()
	Case myCanvas
		Select EventID()
			Case EVENT_MOUSEDOWN
			...



There are different ways to build up your event handling code routines. One way is using a single Select..Case method as shown above and in most small examples this is enough. But as the project gets larger you'll find this way of handling things will bloat out into a huge cumbersome Select..Case statement. If doing a large project I'd think about looking into the 'event hook function' way of doing things. It can result in a little more code, and you may find it awkward at first, but you'll find you can organise your code into separate, more 'gadget specific' functions. You can have a separate function for each gadget which can keep all event handling code for that gadget separate from the other gadgets, ultimately making things easier to manage. Just a suggestion.

I still dont understand why you need a to put a delay though? What is happening for you to feel you need it?

Last edited 2012


TAS(Posted 2012) [#6]
Thanks all,
The delay is needed to give the user time to release the button and trigger a mouseup event instead. Otherwise all events will be trap by the drag function or both the drag and up functions depending on code implementation.