Click and/or drag ?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Click and/or drag ?

Post by Zax » Wed Feb 28, 2024 5:13 pm

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.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Click and/or drag ?

Post by Klaus » Wed Feb 28, 2024 5:29 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Click and/or drag ?

Post by dunbarx » Wed Feb 28, 2024 6:26 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Click and/or drag ?

Post by dunbarx » Wed Feb 28, 2024 6:30 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » Wed Feb 28, 2024 8:15 pm

Um, what about using mouseStillDown for the drag, and mouseUp for rhe click?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Click and/or drag ?

Post by dunbarx » Wed Feb 28, 2024 8:52 pm

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

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Click and/or drag ?

Post by stam » Thu Feb 29, 2024 1:10 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » Thu Feb 29, 2024 8:59 am

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.
Attachments
Click or Drag.livecode.zip
(1 KiB) Downloaded 17 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » Thu Feb 29, 2024 9:17 am

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
Attachments
Click or Drag 2.livecode.zip
(1.02 KiB) Downloaded 20 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » Thu Feb 29, 2024 9:21 am

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.

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

Post by Zax » Thu Feb 29, 2024 9:32 am

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.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Click and/or drag ?

Post by stam » Thu Feb 29, 2024 10:34 am

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…

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » 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:

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

Zax
Posts: 474
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

Post by Zax » Thu Feb 29, 2024 11:11 am

@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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Click and/or drag ?

Post by richmond62 » Thu Feb 29, 2024 11:29 am

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)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”