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: 473
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Click and/or drag ?

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

richmond62 wrote:
Thu Feb 29, 2024 11:29 am
You are quite right: unfortunately I gave up declaring variables donkey's years ago, so had quite forgotten that. 8)
Yes, it was not possible with Hypercard :wink:
But now, it's the same as in Javascript.

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:54 am

Well . . .

HyperMetaRunRevLiveCodeOXTcard all tends to get smushed together in my brain! 8)

Better get out those old ToolBook CDs . . . :shock:
-
clickOR.jpg
clickOR.jpg (6.31 KiB) Viewed 881 times
-
Click or Drag!

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:14 pm

Zax wrote:
Thu Feb 29, 2024 11:11 am
@stam: I tried, without success.
Because you want to track movement from where the user clicked, not the loc of the button, you have to also track the clickLoc. You also don't want the mouse being dragged outside of the button without dragging the button.

The code below drags the button if the user has dragged > 10 px on either x or y axis relative to where they clicked; to ensure the mouse doesn't go far outside the button (in case of small buttons), I also grab it on mouseLeave.

The code below works (includes some debugging code to put the clickLoc in the msg and then the mouseLoc - so you can check if its behaving as expected):

Code: Select all

local sLoc, sClickLoc

on mouseDown
    put the loc of me into sLoc
    put the clickLoc into sClickLoc
    put sClickLoc & return
end mouseDown

on mouseMove x, y
    if the mouse is down then put x & comma & y & return after msg
    if abs(item 1 of sClickLoc - x) > 10 or abs(item 2 of sClickLoc - y) > 10 then
        if sLoc is not empty then grab me
    end if
end mouseMove

on mouseLeave
    if the mouse is down and sLoc is not empty then grab me
end mouseLeave

on mouseUp
    if the loc of me = sLoc then
        answer "mouseUp stuff"
    end if
    put empty into sLoc
    put empty into sClickLoc
end mouseUp

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

Re: Click and/or drag ?

Post by Zax » Thu Feb 29, 2024 4:13 pm

Thank you Stam, your script is great :)

I'll use it like this

Code: Select all

local sLoc, sClickLoc

on mouseDown
   put the loc of me into sLoc
   put the clickLoc into sClickLoc
   //put sClickLoc & return
end mouseDown

on mouseMove x, y
   //if the mouse is down then put x & comma & y & return after msg
   put 4 into minMove // px
   if abs(item 1 of sClickLoc - x) > minMove or abs(item 2 of sClickLoc - y) > minMove then
      if sLoc is not empty then
         grab me
         dragStart
      end if
   end if
end mouseMove

on mouseLeave
   if the mouse is down and sLoc is not empty then
      grab me
      dragStart
   end if
end mouseLeave

on mouseUp
   if the loc of me = sLoc then
      clickOnMe
   else dragEnd
   
   put empty into sLoc
   put empty into sClickLoc
end mouseUp

on dragStart -- stuff to do when drag start
   //show graphic r
   put the seconds && "drag START" ----
end dragStart

on dragEnd -- stuff to do when drag end
   //hide graphic r
   put the seconds && "drag END" ----
end dragEnd

on clickOnMe
   put the seconds && "CLICKED" ----
end clickOnMe

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 6:17 pm

Good stuff :) Glad you found this helpful!
And do post something to advertise whatever you're creating in the 'made with livecode' subforum once it's out there ;)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Click and/or drag ?

Post by jacque » Thu Feb 29, 2024 8:46 pm

Similar but a bit shorter:

Code: Select all

local sDragging

on dragStart
  put true into sDragging
end dragStart

on mouseMove
  if sDragging then grab me
end mouseMove

on mouseUp
  if not sDragging then
    -- do click stuff
  end if
  put false into sDragging
end mouseUp
The default dragDelta is 4 so you probably don't need to calculate that. It's a global property so you can change it at any point and it remains that way until LC restarts.

Re: script locals vs custom props. Script locals are very slightly faster but the difference is generally too small to notice. I usually prefer script local variables though, because they go away by themselves. I usually forget to reset custom properties when I save a stack and that's been a problem in the past, so now I save persistent values in custom properties and transient values in script locals.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Click and/or drag ?

Post by Zax » Fri Mar 01, 2024 8:04 am

Thank you for your script Jacqueline, I didn't know about dragDelta. :)
I completely agree with you on the use of local variables and properties.

@stam: I have made several small apps with LC, but most of them only meet personal needs and they are not finalized well enough to be usable by others - without even talking about their interests. As you probably know, there is a huge amount of work to transform “personal” development into “public” development.

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

Re: Click and/or drag ?

Post by stam » Fri Mar 01, 2024 9:00 am

jacque wrote:
Thu Feb 29, 2024 8:46 pm
Similar but a bit shorter
Thank you Jacque, eloquent as ever and a much nicer, more concise version.
Live and learn!

Stam

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 » Fri Mar 01, 2024 9:24 am

SShot 2024-03-01 at 10.22.20.png
SShot 2024-03-01 at 10.22.20.png (6.32 KiB) Viewed 779 times
-
Indeed: Thank you so much!

BUT, almost inevitably, I shall use this to criticise LC a bit . . . 8)

How the fudgums is chummy, the newbie, going to find out stuff like that?

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 » Fri Mar 01, 2024 12:18 pm

If I set the script of an 80 x 80 button to this:

Code: Select all

on mouseDown
   set the dragDelta to 80
   grab me
end mouseDown
Why does the drag action start immediately I start dragging the button, and NOT when my mouse pointer reaches its edge?

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

Re: Click and/or drag ?

Post by Klaus » Fri Mar 01, 2024 12:36 pm

Because DRAGDELTA will affect the DRAG commands but not the GRAB command.
These two are completely different things.

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

Re: Click and/or drag ?

Post by stam » Fri Mar 01, 2024 1:27 pm

Yeah, as per Jacque’s example, set a flag to grab with the dragstart command and do the actual grabbing in mouseMove.
Jacque’s script works perfectly…

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Click and/or drag ?

Post by jacque » Fri Mar 01, 2024 6:53 pm

richmond62 wrote:
Fri Mar 01, 2024 9:24 am
How the fudgums is chummy, the newbie, going to find out stuff like that?
By typing "drag" into the dictionary search field? Or looking at the "related" entry?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Click and/or drag ?

Post by jacque » Fri Mar 01, 2024 6:58 pm

Klaus wrote:
Fri Mar 01, 2024 12:36 pm
Because DRAGDELTA will affect the DRAG commands but not the GRAB command.
These two are completely different things.
My first attempt used "set the loc of me to the mouseloc" instead of using grab, which was smooth enough on my Mac, but then I noticed that grab calculated the mouse offset automatically so I changed it.

The best advice I ever heard at an LC conference years ago was "let the engine do it."
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Click and/or drag ?

Post by Zax » Sat Mar 02, 2024 8:39 am

OK, now the drag process works, can we talk about the drop process? ;)

I'm ok to "let the engine do it" but I don't really understand dragDrop and dragEnd built-in message: they don't seem to be triggered when a LC control is dropped onto another LC control. Yet, the fullDragData property has a "objects" key dedicated to LC objects.

In other words, what would be the best script to drop a LC object onto another on the same card?
If during the drag process I make a loop to test every other objects' rect, it will be of course very jerky.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”