help with drag and drop
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
help with drag and drop
Hi,
Could someone point me to a place I could learn how to set up drag-and-drop?
I'd like to have a user click on a button, drag to draw a line, and then "drop" when that line contacts another button. (draw a line connecting buttons)
I think I could do this with mousemove messages to the line, but it seems that the "dragenter" message is what I want to have the line stop at the "contacted" button. Otherwise at each mousemove I'd have to check whether we are within each object on the screen.
I'm sure there is some good way to do this...
Thanks.
-Chuck
Could someone point me to a place I could learn how to set up drag-and-drop?
I'd like to have a user click on a button, drag to draw a line, and then "drop" when that line contacts another button. (draw a line connecting buttons)
I think I could do this with mousemove messages to the line, but it seems that the "dragenter" message is what I want to have the line stop at the "contacted" button. Otherwise at each mousemove I'd have to check whether we are within each object on the screen.
I'm sure there is some good way to do this...
Thanks.
-Chuck
Re: help with drag and drop
Hi.
Try this on a new card with two buttons. I labeled these buttons "startLine" and "endLine". In the script of btn "startLine":
At least this is straightforward to understand, if a little brutish.
Craig Newman
Try this on a new card with two buttons. I labeled these buttons "startLine" and "endLine". In the script of btn "startLine":
Code: Select all
on mouseDown
create grc "newLine"
set the style of grc "newLine" to "line"
drawLine
end mouseDown
on drawLine
set the points of grc "newLine" to the loc of btn "startLine" & "," & the mouseLoc
if the mouse is down then send "drawLine" to me in 1
else
set the points of grc "newLine" to the loc of btn "startLine" & "," & the loc of btn "endLine"
exit to top
end if
end drawLine
Craig Newman
Re: help with drag and drop
Couldn't sleep last night, and then I figured out what was bothering me. If the user releases the mouse without the mouseLoc being within the rect of btn "endLine", you get an errant line sitting somewhere.
So:
Time for a nap.
Craig
So:
Code: Select all
on mouseDown
lock screen
create grc "newLine"
set the style of grc "newLine" to "line"
drawLine
end mouseDown
on drawLine
set the points of grc "newLine" to the loc of btn "startLine" & "," & the mouseLoc
if the mouse is down then send "drawLine" to me in 1
else if the mouseLoc is within the rect of btn "endLine" then
set the points of grc "newLine" to the loc of btn "startLine" & "," & the loc of btn "endLine"
exit to top
else delete grc "newLine"
end drawLine
Craig
Re: help with drag and drop
Thanks for the code. The "while the mouse is down" strategy works well.
I'm still curious about the drag and drop idea. I like the idea of the "target" of the drag and drop getting a message when it get the line.
The whole drag and drop is mysterious, though...I don't understand how you start one, etc.
I'm still curious about the drag and drop idea. I like the idea of the "target" of the drag and drop getting a message when it get the line.
The whole drag and drop is mysterious, though...I don't understand how you start one, etc.
Re: help with drag and drop
Hi.
Not "while the mouse is down..." The "while" implies a loop of a certain style. I just checked a condition.
Anyway, drag and drop speaks to both native functionality, such as with fields where you can select a chunk of text and then drag that chunk somewhere else, even to another field, or to the shenanigans we are dealing with here, where the intentions are implemented under script control.
What are you having issues with?
And by the way, you need to work a bit on that code. For example, do you allow multiple lines between those buttons if the user drags twice? Should the last line be renamed to distinguish among them? Might multiple line be drawn from btn "startLine" to several "endLine" buttons? That sort of thing.
Craig
Not "while the mouse is down..." The "while" implies a loop of a certain style. I just checked a condition.
Anyway, drag and drop speaks to both native functionality, such as with fields where you can select a chunk of text and then drag that chunk somewhere else, even to another field, or to the shenanigans we are dealing with here, where the intentions are implemented under script control.
What are you having issues with?
And by the way, you need to work a bit on that code. For example, do you allow multiple lines between those buttons if the user drags twice? Should the last line be renamed to distinguish among them? Might multiple line be drawn from btn "startLine" to several "endLine" buttons? That sort of thing.
Craig
Re: help with drag and drop
In an old thread I made a quick and dirty sampler
http://forums.livecode.com/viewtopic.ph ... de#p133097
It may give some ideas
http://forums.livecode.com/viewtopic.ph ... de#p133097
It may give some ideas
Re: help with drag and drop
Thanks for the feedback. I don't know why I like the "while the mouse is down" loop better. Maybe its the recursion feels kind of creepy 
All the issues you mention about multiple lines etc. are things to deal with. The reason I looked to drag and drop is I'm trying to track collisions. I don't want you to be able to draw a line through one button to another.
Right now I run a check to see if the drawn line intersects with any button. But I have to do that each iteration. I thought the "Dragenter" message might get me that for free. (I suspect it won't because I want collisions not just at the drag (mouse) point, but along the length of the line, so intersect is probably the way to go. Still, I'm curious)
I'm confused about the whole logic of starting a drag and drop. For example, how do I get controls to know there is a drag going on so they respond to dragenter?
_C

All the issues you mention about multiple lines etc. are things to deal with. The reason I looked to drag and drop is I'm trying to track collisions. I don't want you to be able to draw a line through one button to another.
Right now I run a check to see if the drawn line intersects with any button. But I have to do that each iteration. I thought the "Dragenter" message might get me that for free. (I suspect it won't because I want collisions not just at the drag (mouse) point, but along the length of the line, so intersect is probably the way to go. Still, I'm curious)
I'm confused about the whole logic of starting a drag and drop. For example, how do I get controls to know there is a drag going on so they respond to dragenter?
_C
Re: help with drag and drop
@ cwkalish
I have no loop, unless you consider the "recursive" recall of the handler to be one. But still did not use, or see anywhere, any form of "while the mouse is down..."
The "mouseControl" cannot be used dynamically when the mouse is down, so you cannot just, anywhere in that handler:
So to be able to test where the mouseLoc is during drag time, you can cheat a bit with:
This could be used to abort or otherwise divert the handler if the user drew through another button. You have to exempt the start and end btns, of course.
Craig
I have no loop, unless you consider the "recursive" recall of the handler to be one. But still did not use, or see anywhere, any form of "while the mouse is down..."
The "mouseControl" cannot be used dynamically when the mouse is down, so you cannot just, anywhere in that handler:
Code: Select all
put the mouseControl
Code: Select all
on mouseMove
repeat with y = 1 to the number of btns
if the mouseLoc is within the rect of btn y then put the name of btn y else put ""
end repeat
end mouseMove
Craig
Re: help with drag and drop
I guess I was looking for a way to detect collisions without iterating through all the possible collision targets (buttons). Given your response I'm assuming that's just the way to do it.
Because I want to check whether the drawn line touches a button, I iterate through all the buttons using the "intersect" function.
Thanks again for responding to my query. Finding out I'm doing it the right way is the 2nd best outcome...
Because I want to check whether the drawn line touches a button, I iterate through all the buttons using the "intersect" function.
Thanks again for responding to my query. Finding out I'm doing it the right way is the 2nd best outcome...
Re: help with drag and drop
I could be wrong in thinking that since the "mouseControl" is useless when the mouse is down, the only way out is to poll all the buttons. Someone might chime in.
This can be circumvented if the mouse is up, of course, but that requires a non-intuitive drag method. People expect to drag with the mouse down.
So sometimes, even though LC offers many ways to do things, a single expectation may limit options. The good news is that the polling takes little time...
Craig
This can be circumvented if the mouse is up, of course, but that requires a non-intuitive drag method. People expect to drag with the mouse down.
So sometimes, even though LC offers many ways to do things, a single expectation may limit options. The good news is that the polling takes little time...
Craig