help with drag and drop

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

Post Reply
cwkalish
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 78
Joined: Thu Sep 30, 2010 2:24 am

help with drag and drop

Post by cwkalish » Sun May 07, 2017 11:17 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: help with drag and drop

Post by dunbarx » Mon May 08, 2017 2:45 am

Hi.

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
At least this is straightforward to understand, if a little brutish.

Craig Newman

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: help with drag and drop

Post by dunbarx » Mon May 08, 2017 2:06 pm

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:

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
Time for a nap.

Craig

cwkalish
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 78
Joined: Thu Sep 30, 2010 2:24 am

Re: help with drag and drop

Post by cwkalish » Mon May 08, 2017 2:22 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: help with drag and drop

Post by dunbarx » Mon May 08, 2017 2:42 pm

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

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: help with drag and drop

Post by SparkOut » Mon May 08, 2017 5:09 pm

In an old thread I made a quick and dirty sampler

http://forums.livecode.com/viewtopic.ph ... de#p133097

It may give some ideas

cwkalish
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 78
Joined: Thu Sep 30, 2010 2:24 am

Re: help with drag and drop

Post by cwkalish » Mon May 08, 2017 10:27 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: help with drag and drop

Post by dunbarx » Mon May 08, 2017 11:33 pm

@ 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:

Code: Select all

put the mouseControl
So to be able to test where the mouseLoc is during drag time, you can cheat a bit with:

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
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

cwkalish
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 78
Joined: Thu Sep 30, 2010 2:24 am

Re: help with drag and drop

Post by cwkalish » Tue May 09, 2017 4:00 pm

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...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: help with drag and drop

Post by dunbarx » Tue May 09, 2017 9:13 pm

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

Post Reply