Page 2 of 2
Re: Snap objects while dragging
Posted: Wed Feb 11, 2015 6:44 pm
by jiml
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
Re: Snap objects while dragging
Posted: Wed Feb 11, 2015 10:31 pm
by croivo
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...
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 5:36 am
by dunbarx
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
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 8:11 pm
by jacque
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.
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 8:41 pm
by dunbarx
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
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 8:44 pm
by croivo
Sure... now this is what I was looking for at the beginning

Thank you all very much

Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 9:16 pm
by jacque
@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.

Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 9:50 pm
by croivo
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?
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 11:08 pm
by dunbarx
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
Re: Snap objects while dragging
Posted: Thu Feb 12, 2015 11:32 pm
by croivo
Sure, I just didn't know where to start from

Tnx for advice