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

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

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

Post 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?
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post by airsoftsoftwair »

Bugala
Posts: 1400
Joined: Sun Feb 14, 2010 7:11 pm

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

Post 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?
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

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

Post 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 :)
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
Post Reply