Page 1 of 4

Click and/or drag ?

Posted: Wed Feb 28, 2024 5:13 pm
by Zax
Hello,

I have a button that can be dragged or clicked (with a simple click, like "mouseUp").
The problem is that these two actions interfere with each other. Something like that:

Code: Select all

on mouseDown
 if it is a "real" drag then
 	-- prepare the drag action, with for example changing the icon of the button...
	 grab me // drag end will be handled by mouseUp
 else
	 -- perform the "real" click action
 end if
end mouseDown

on mouseUp
if it was a drag action then
	-- do the drag end process
else
	-- do the "real" click action
end if
end mouseUp
I tried to get around the problem with flags and timers, but it results in unreliable code. I suppose – I hope – there are better ways to do this.

Thank you for your help.

Re: Click and/or drag ?

Posted: Wed Feb 28, 2024 5:29 pm
by Klaus
Hi Zax,

not sure I understand your intention with the "mousedown" handler.

Code: Select all

on mouseDown
  ## At this point noone will know if the user wants to drag the object or not!
   if it is a "real" drag then
 	-- prepare the drag action, with for example changing the icon of the button...
	 grab me // drag end will be handled by mouseUp
  else
        ## And what is a "real" click action "on mousedown"?
	 -- perform the "real" click action
   end if
end mouseDown
:shock:

Best

Klaus

Re: Click and/or drag ?

Posted: Wed Feb 28, 2024 6:26 pm
by dunbarx
I see your problem about conflict of interest. This kludge works:.

Code: Select all

on mouseEnter
   set the initialLoc of me to the loc of me
end mouseEnter

on mouseMove
   if the mouseLoc is within the rect of me and the mouse is down then set the loc of me to the mouseLoc
end mouseMove

on mouseUp
   if the loc of me = the initialLoc of me then beep
end mouseUp
I had thought that trapping the "dragEnd" message would have helped make a much less kludgy solution, but the "dragEnd" message is not sent, I guess, apart from true drag-and-drop actions involving fields, and where text is actually dropped. It does not seem to be sent when one "grabs" a control, moves around a bit, and then stops dragging that control. This in spite of the dictionary stating that it is indeed sent in that situation. I did not see it, and the message watcher also did not.

It should, it seems to me. Then one need only notice if the initial and final locs are the same to allow the mouseUp handler to fire.

Craig

Re: Click and/or drag ?

Posted: Wed Feb 28, 2024 6:30 pm
by dunbarx
Kludginess notwithstanding, if anything like my handlers are adopted, it is likely a good idea to allow the mouseUp handler to fire as long as the mouseLoc is within the rect of the button when the mouse is released, and not just at the initialLoc.

Craig

Re: Click and/or drag ?

Posted: Wed Feb 28, 2024 8:15 pm
by richmond62
Um, what about using mouseStillDown for the drag, and mouseUp for rhe click?

Re: Click and/or drag ?

Posted: Wed Feb 28, 2024 8:52 pm
by dunbarx
Richmond.

Certainly many ways to do things in LC, but I always avoid "mouseStillDown" if possible. I find it jittery, especially when folded into other working gadgetry.

I have always wanted a "mouseStillUp" variant. Some say that is "idle". 8)

Craig

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 1:10 am
by stam
I personally would implement something along the lines of what Craig has done, with perhaps the exception of using a script local variable instead of a custom property. And using grab in mouseMove - but much of a muchness really...

I was initially going to check for mouseUp within the initial rect, but then realised that any movement while the mouse is down should count as a drag, not a click. Obviously if the requirement is to only drag once the mouse is outside the initial rect, then that's different...

This works:

Code: Select all

local sLoc

on mouseDown
    put the loc of me into sLoc
end mouseDown

on mouseMove
    if sLoc is not empty then grab me
end mouseMove

on mouseUp
    if the loc of me = sLoc then -- mouse hasn't moved so 'click' instead, for example:
        answer "mouseUp stuff"
    end if
    put empty into sLoc
end mouseUp

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 8:59 am
by richmond62

Code: Select all

on mouseDown
   put the loc of me into fld "fL"
end mouseDown

on mouseStillDown
   grab me
end mouseStillDown

on mouseUp
   put the loc of me into LOK
   put fld "fL" into LOQ
   if LOQ = LOK then
      put "CLICK" into fld "f1"
      wait 3 secs
      put empty into fld "f1"
   end if
end mouseUp
-
SShot 2024-02-29 at 9.58.07.png
-
Mind you I think I would put the initial location into a customProp to speed things up.

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 9:17 am
by richmond62
Yup: this is much better:

Code: Select all

on mouseDown
   set the LOQ of btn "Button" to the loc of me
end mouseDown

on mouseStillDown
   grab me
end mouseStillDown

on mouseUp
   put the loc of me into LOK
   put the LOQ of me into LOQQ
   if LOQQ = LOK then
      put "CLICK" into fld "f1"
      wait 3 secs
      put empty into fld "f1"
   end if
end mouseUp
-
SShot 2024-02-29 at 10.15.13.png

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 9:21 am
by richmond62
The reason I, first, used a field, and second, a custom property of the button to 'hold' the initial location of the button is because had I written something like this:

BAD CODE:

on mouseDown
put the loc of me into LOQQ
end mouseDown

on mouseStillDown
grab me
end mouseStillDown

on mouseUp
put the loc of me into LOK
if LOQQ = LOK then
put "CLICK" into fld "f1"
wait 3 secs
put empty into fld "f1"
end if
end mouseUp

LOQQ would have vanished between the mouseDown and the mouseUp as all local variables are labile.

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 9:32 am
by Zax
Thanks for your replies :)
Klaus wrote:
Wed Feb 28, 2024 5:29 pm
not sure I understand your intention with the "mousedown" handler.

Code: Select all

on mouseDown
  ## At this point noone will know if the user wants to drag the object or not!
That is a part of the problem Klaus, as we don't have some kind of "grabEnd" event.

I made some quick tests with stam solution and the second one from richmond92: they seem to work correctly and respond to my request :)

Ideally, we should better manage the case of "slow" mouseUp: if the user clicks very slowly and moves the mouse a few pixels, he thinks he is making a click when he is dragging (don't laugh, I I've seen this case before!).

I will put in place the solutions proposed for my project and tell you which one seems most appropriate.

@richmond92: a variable local to a script does not lose its value between different handlers from the same script. I often admit to preferring local variables over properties in cases like this, but that's probably a matter of habit.

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 10:34 am
by stam
Zax wrote:
Thu Feb 29, 2024 9:32 am
Ideally, we should better manage the case of "slow" mouseUp: if the user clicks very slowly and moves the mouse a few pixels, he thinks he is making a click when he is dragging (don't laugh, I I've seen this case before!).
In effect what you’re really wanting is for the drag to start after a few pixels movement. I’d probably adjust the mouseMove handler to only grab if it exceeds your chosen number of pixels (eg from -2 to +2 px in the x or y axis). Should be easy to implement…

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 11:01 am
by richmond62
a variable local to a script does not lose its value between different handlers from the same script.
Quite possibly, but THIS did NOT work for me:

Code: Select all

on mouseDown
   put the loc of btn "Button" into LOQQ
end mouseDown

on mouseStillDown
   grab me
end mouseStillDown

on mouseUp
   put the loc of me into LOK
   if LOQQ = LOK then
      put "CLICK" into fld "f1"
      wait 3 secs
      put empty into fld "f1"
   end if
end mouseUp

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 11:11 am
by Zax
@stam: I tried, without success.



richmond62 wrote:
Thu Feb 29, 2024 11:01 am
a variable local to a script does not lose its value between different handlers from the same script.
Quite possibly, but THIS did NOT work for me:
@richmond62: you forgot to declare LOQQ outside the handlers, like in stam's script

Code: Select all

local LOQQ

Re: Click and/or drag ?

Posted: Thu Feb 29, 2024 11:29 am
by richmond62
you forgot to declare LOQQ outside the handlers
You are quite right: unfortunately I gave up declaring variables donkey's years ago, so had quite forgotten that. 8)