Page 1 of 1

Is there way to keep checking if button is pressed down?

Posted: Fri Aug 10, 2012 12:06 am
by Bugala
You can maybe get pretty good idea what I am after by looking at this piece of code:

Code: Select all

Switch msg.action

   Case "OnMouseDown":
          oldmousex = MouseX()
          oldmousey = MouseY()
   Case "MouseDown":
	  newmousex = MouseX()
          newmousey = MouseY()
          yellowsquare.x = yellowsquare.x + (newmousex - oldmousex)
          yellowsquare.y = yellowsquare.y + (newmousey - oldmousey)
          oldmousex = newmousex
          oldmousey = newmousey
          T_Squares["yellow"][msg.id] = yellowsquare
          ShowLayer("yellow"..msg.id, yellowsquare.x, yellowsquare.y)

Idea is that I am trying to have a button, that would be movable around by pressing left mousebutton down and holding it down until desired location is found.

However, im bit confused about "MouseDown" option, since i thought that would keep checking if mousebutton is still being pressed down, but i guess the idea is that it will only check it as long as you are inside the button event, that if you jump out of the CheckEvent(), and make the next CheckEvent() Hollywood doesnt understand to check if Leftmousebutton is still pressed down or not and to then jump to right place.

So is there a way to keep checking if MouseButton is still being pressed down inside a button without having to stay inside the button whole time?

Especially is there possibilty to trigger CheckEvent through checking if MouseDown is still happening?

Re: Is there way to keep checking if button is pressed down?

Posted: Fri Aug 10, 2012 10:00 pm
by airsoftsoftwair

Re: Is there way to keep checking if button is pressed down?

Posted: Fri Aug 10, 2012 11:15 pm
by Bugala
Yeah, i did figure that one out, but what i really wanted was to have that part of code in the button.

That the idea would be that as long as the mouse is down, it would go to that button event repeatedly and execute the code in that place.


But so what is the point with that "MouseDown" option at all?

For i dont seem to be able to trigger that one at all?

Re: Is there way to keep checking if button is pressed down?

Posted: Sat Aug 11, 2012 12:31 am
by Allanon
If you want to make the button drag using the Repeat, WaitEvent, Forever loop you should try to use a state variable and a timer:

Code: Select all

Local drag_timer = -1
Local drag_delay = 500 ; 500 milliseconds

Function LMBDown(msg)
   ; Here you need to trigger the drag after some time just to not annoy the user with instant dragging
   drag_timer = StartTimer(Nil)

   ; Your stuff here
   ; ...
EndFunction

Function LMBUp(msg)
   ; The mouse button has been released, any dragging operation must be interrupted and the timer freed
   If GetType(drag_timer) <> #NUMBER
      StopTimer(drag_timer)
      drag_timer = -1
   EndIf

   ; Your stuff here
   ; ...
EndFunction

Function MMove(msg)
   ; Here is where you shoukd handle the dragging only if the timeout has been reached
   If GetType(drag_timer) <> #NUMBER
      If GetTimer(drag_timer) > drag_delay
         ; Here is where you have to update the dragging operations
      EndIf
   EndIf

   ; You stuff here
   ; ...
EndFunction

InstallEventHandler({OnMouseDown = LMBDown, OnMouseUp = LMBUp, OnMouseMove = MMove})

Repeat
   WaitEvent()
Forever
Every time the mouse is moved and the timeout has been reached after a LMB event your dragging code will be execute until the release of the button.
I have not tested the code, but you have an idea how to implement a dragging routine without locking the main loop :)