Grab me constrained to horizontal
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Grab me constrained to horizontal
Im guessing the title says it all. I want to be using a grab "me" command but only allow it to be constrained to horizontal movement. I tried on the mouseDown when i start the grab me to store the top of me into a local variable and on mousemove set the top of me to that local variable.
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Grab me constrained to horizontal
I think you need mousemove instead grab to do that.
Something like that:
script control you want move
card script:
Best regards
Jean-Marc
Something like that:
script control you want move
Code: Select all
on mousedown
deRedBallMD
end mousedown
on mouseup
deRedBallMU
end mouseup
on mouserelease
deRedBallMR
end mouserelease
Code: Select all
local sMove,sCurObj,sMyX
on opencard
put false into sWorkCache
put empty into sCurObj
end opencard
--•• Mousedown on redBall
on deRedBallMD
put true into sMove
put the target into sCurObj
put (the mouseH - item 1 of the loc of sCurObj) into sMyX
end deRedBallMD
on mousemove x,y
if sMove and sCurObj <> empty then
put x-sMyX into tNewH
put item 2 of the loc of sCurObj into tNewV
set loc of sCurObj to tNewH,tNewV
end if
end mousemove
--•• MouseUp on redBall stop mousemove
on deRedBallMU
put false into sMove
put empty into sCurObj
end deRedBallMU
-- Mouserelease on redBall Discs
on deRedBallMR
put false into sMove
end deRedBallMR
Jean-Marc
https://alternatic.ch
Re: Grab me constrained to horizontal
OK i got it done and it worked out so that i can even restrict it to go right only
Code: Select all
local moveit
local origY
local XDif
local origPos
local origX
on mouseDown
put the loc of the owner of me into origPos
put true into moveit
put item 2 of the loc of the owner of me into origY
put item 1 of the mouseLoc into mouseX
put item 1 of the loc of the owner of me into origX
put mouseX - origX into XDif
end mouseDown
on mouseUp
put false into moveit
put the loc of the owner of me into newlocation
put item 1 of newlocation into xclocation
put item 1 of origPos into xrlocation
if xclocation > (xrlocation + 200) then
--do what needs to be done
answer "Swipped off"
end if
set the location of the owner of me to origPos
end mouseUp
on mouseMove x,y
if moveit then
if (x-XDif) > origX then
set the loc of the owner of me to (x-XDif),origY
end if
end if
end mouseMove
on mouseLeave
put false into moveit
end mouseLeave
Re: Grab me constrained to horizontal
Nice solution.
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Grab me constrained to horizontal
Yes, but possibly a bit overly complicated:Nice solution.
Code: Select all
on mouseDown
grab me
set the top of img "XX" to 260
end mouseDown
on mouseMove
set the top of img "XX" to 260
end mouseMove
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Grab me constrained to horizontal
HOWEVER . . . even that is a slight pain as the end-user,
on grabbing one of the images can move it way off its
alignment path for a split second.
on grabbing one of the images can move it way off its
alignment path for a split second.
Re: Grab me constrained to horizontal
Well, I finally came to a solution myself (dang I'm slow ).
This is a button and a rectangle, but could just as easily be any combination of objects.
I put the code in the button, but again, could just as easily be anywhere else.
All of it depends on only 2 tests, however, either of the 2 parts is optional, depending on whether you want to make it look like the object was grabbed, or whether you just want it to move horizontally in either direction, and of course there is plenty of room for fine tuning.
The main thing this gets done is the button doesn't bob up and down, no matter what the mouse is doing, and of course the button doesn't go outside of the rectangle no matter what else happens.
I also found this lesson, which shows another way to do the same kind of thing using cprops.
The -5 in the code is the width of the button /2.
This is a button and a rectangle, but could just as easily be any combination of objects.
I put the code in the button, but again, could just as easily be anywhere else.
All of it depends on only 2 tests, however, either of the 2 parts is optional, depending on whether you want to make it look like the object was grabbed, or whether you just want it to move horizontally in either direction, and of course there is plenty of room for fine tuning.
The main thing this gets done is the button doesn't bob up and down, no matter what the mouse is doing, and of course the button doesn't go outside of the rectangle no matter what else happens.
I also found this lesson, which shows another way to do the same kind of thing using cprops.
The -5 in the code is the width of the button /2.
Code: Select all
on mouseDown
if the mouseLoc is within the rect of me then
set the left of me to item 1 of the mouseLoc -5
end if
end mouseDown
on mouseStillDown
if the mouseLoc is within the rect of graphic "Rectangle" then
set the left of me to item 1 of the mouseLoc -5
end if
end mouseStillDown
Re: Grab me constrained to horizontal
ill give it a try. i was having coordinate trouble since im moving right inside of a group locked in size and if it doesnt move the right distance it resets to its original position.
Re: Grab me constrained to horizontal
Well, if you do try it, the only thing I noticed that 'could' be a problem is that the mouse can be moved faster than the object moving updates its position. I am sure this can be overcome in many different ways, it just wasn't enough of a nuisance for me to refine it.
-
- VIP Livecode Opensource Backer
- Posts: 7238
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Grab me constrained to horizontal
Your working code is the recommended way to do it. Even though it may appear "complex" it avoids a lot of issues, particularly tracking and checking the status of the mouse, which is time-consuming. You could shave a tiny bit more off (though it's pretty insignificant) by referring to the variable rather than repeatedly checking the location of the owner:
Code: Select all
on mouseDown
put the loc of the owner of me into origPos
put true into moveit
put item 2 of origPos into origY
put item 1 of the mouseLoc into mouseX
put item 1 of origPos into origX
put mouseX - origX into XDif
end mouseDown
Code: Select all
on mouseRelease
put false into moveIt
end mouseRelease
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Grab me constrained to horizontal
by whom? That sounds like an imposition of orthodoxy.the recommended way
Why "Is" grab me to be avoided?
-
- VIP Livecode Opensource Backer
- Posts: 7238
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Grab me constrained to horizontal
Recommended by MC and the LC team for engine and CPU efficiency. "Grab" isn't to be avoided but it has limitations. It can also be blocking depending on how it's used. In the example you provided, there's a risk of mouseMove commands piling up in the queue. Not a huge deal in this case, it's just overkill, but it could matter in other implementations. Setting a flag is safer.
But like many things, there is more than one way to accomplish a task in LC.
But like many things, there is more than one way to accomplish a task in LC.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Grab me constrained to horizontal
That probably explains the probs I mentioned with my dodecahedron.a risk of mouseMove commands piling up in the queue