Snap objects while dragging

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

jiml
Posts: 339
Joined: Sat Dec 09, 2006 1:27 am

Re: Snap objects while dragging

Post by jiml » Wed Feb 11, 2015 6:44 pm

Just add

Code: Select all

    if the mouse is not down then exit mouseMove
to Craig's code and I think you'll have what you want.

Code: Select all

on mouseMove
    if the mouse is not down then exit mouseMove
   switch
      case abs(item 1 of the mouseLoc - the left of grc "vline") < 10
         set the loc of me to the left of grc "vline" & "," & item 2 of the mouseLoc
         break
      case  abs(item 2 of the mouseLoc - the top of grc "hline") < 10
         set the loc of me to  item 1 of the mouseLoc & "," & the top of grc "hline"
         break
      case abs(item 1 of the mouseLoc - the left of grc "vline") > 10 or abs(item 2 of the mouseLoc - the top of grc "hline") > 10
         set the loc of me to the mouseLoc
         break
   end switch
end mouseMove
JimL

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Snap objects while dragging

Post by croivo » Wed Feb 11, 2015 10:31 pm

Now there is no glitch! This is almost perfect :)
This latest code does the job very well... the only thing I can't manage to work is to snap button to the intersect point of "vline" and "hline"... Now it only snaps on "vline" when the button is in the area where vertical line intersects horizontal line... Need to somehow change the order of 'cases' or something...

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

Re: Snap objects while dragging

Post by dunbarx » Thu Feb 12, 2015 5:36 am

Just like scrabble. When you think you have a good play, look for a better one. At least this keeps me from paying attention at work. Same two graphics, one button with this in its script:

Code: Select all

on mouseMove x,y
   put the left of grc "vLine" into h
   put the top of grc "hLine" into v      
   put H & "," &  v into origin
   put abs(x-h) into hWindow
   put abs(y-v) into vWindow
   switch
      case vWindow < 10 and hWindow < 10
         set the loc of me to origin
         break
      case vWindow <= 10
         set the loc of me to x &  "," & v
         break
      case hWindow <= 10
         set the loc of me to  h & "," & y
         break
      case hWindow > 10 and vWindow > 10
           set the loc of me to the mouseLoc
          break
   end switch
end mouseMove
The mouse need not be down, which is where I think we started. The lesson here is that the ordering of the case statements really matters.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Snap objects while dragging

Post by jacque » Thu Feb 12, 2015 8:11 pm

Nice, Craig. I gave it a stab myself but didn't come up with anything that elegant. You do need the check for the mouse status at the top of the handler though, otherwise you can't let go of the button. I was glad to see it uses the x,y coordinates that mousemove passes, I was going to mention that. Using those makes the script much tighter.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Snap objects while dragging

Post by dunbarx » Thu Feb 12, 2015 8:41 pm

Jacque.

Why, all you need to do is to move the mouse in a quick jerk off the button, and you will leave it behind. Isn't this in the Apple User Interface guidelines?

I just focussed on the snap stuff, and used the "jerk" method to terminate. Worked just fine. There is the opposite case of that "jerk" thing, that is, there is not a lot of forgiveness with the speed of the mouse when dragging that button. It is in fact quite easy to leave the button behind.

So, croivo, can you think of a way for the button to follow the cursor (mouse being down; this is certainly desirable) even if you outrun it?

Craig

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Snap objects while dragging

Post by croivo » Thu Feb 12, 2015 8:44 pm

Sure... now this is what I was looking for at the beginning :) Thank you all very much :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Snap objects while dragging

Post by jacque » Thu Feb 12, 2015 9:16 pm

@Craig, on a fast machine it's pretty hard to jerk quickly enough, and your script doesn't lag for me at all. The button keeps up without any trouble.

Edit: I take it back. I can leave it behind if I'm really quick. But checking the mouse status is more satisfying. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Snap objects while dragging

Post by croivo » Thu Feb 12, 2015 9:50 pm

Hey another question :) Let's say I have 5 horizontal and 5 vertical lines, there will be a lot lines intersecting points and manually snapping to every intersecting point would take a lot of code (lot of combinations)... But I'm sure it's possible to do with looping ('repeat' command, right?). Any tips for doing this?

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

Re: Snap objects while dragging

Post by dunbarx » Thu Feb 12, 2015 11:08 pm

Croivo.

You probably do not want to use any "repeat" loops. Look up "the target" in the dictionary. Your main stumbling block will likely be that you can no longer explicitly name the graphics which you are tracking. This little function is your friend.

My tip: Work on this for a while, come back when you get stuck.

Craig

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Snap objects while dragging

Post by croivo » Thu Feb 12, 2015 11:32 pm

Sure, I just didn't know where to start from :D Tnx for advice

Post Reply