Page 2 of 4
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 11:48 am
by Zax
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.
Yes, it was not possible with Hypercard
But now, it's the same as in Javascript.
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 11:54 am
by richmond62
Well . . .
HyperMetaRunRevLiveCodeOXTcard all tends to get smushed together in my brain!
Better get out those old ToolBook CDs . . .
-
- clickOR.jpg (6.31 KiB) Viewed 1233 times
-
Click or Drag!
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 1:14 pm
by stam
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
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 4:13 pm
by Zax
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
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 6:17 pm
by stam
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
Re: Click and/or drag ?
Posted: Thu Feb 29, 2024 8:46 pm
by jacque
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.
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 8:04 am
by Zax
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.
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 9:00 am
by stam
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
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 9:24 am
by richmond62
- SShot 2024-03-01 at 10.22.20.png (6.32 KiB) Viewed 1131 times
-
Indeed: Thank you so much!
BUT, almost inevitably, I shall use this to criticise LC a bit . . .
How the fudgums is chummy, the newbie, going to find out stuff like that?
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 12:18 pm
by richmond62
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?
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 12:36 pm
by Klaus
Because DRAGDELTA will affect the DRAG commands but not the GRAB command.
These two are completely different things.
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 1:27 pm
by stam
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…
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 6:53 pm
by jacque
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?
Re: Click and/or drag ?
Posted: Fri Mar 01, 2024 6:58 pm
by jacque
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."
Re: Click and/or drag ?
Posted: Sat Mar 02, 2024 8:39 am
by Zax
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.