Page 1 of 2

Grab me constrained to horizontal

Posted: Thu Dec 21, 2017 4:02 am
by Da_Elf
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.

Re: Grab me constrained to horizontal

Posted: Thu Dec 21, 2017 4:13 am
by bogs
Pretty sure these 2 threads go into it in some depth.
Thread 1
Thread 2

*Edit - Thread 3.

Re: Grab me constrained to horizontal

Posted: Thu Dec 21, 2017 5:32 pm
by jmburnod
I think you need mousemove instead grab to do that.
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
card script:

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
Best regards
Jean-Marc

Re: Grab me constrained to horizontal

Posted: Fri Dec 22, 2017 12:53 am
by bogs
jmburnod wrote:
Thu Dec 21, 2017 5:32 pm
I think you need mousemove instead grab to do that.
Hm, wouldn't "drag" also work for this? Off the top of my head, something like "drag [object] from (point) to (point)"
Just a thought.

Re: Grab me constrained to horizontal

Posted: Fri Dec 22, 2017 3:58 am
by Da_Elf
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

Posted: Fri Dec 22, 2017 4:20 am
by bogs
Nice solution.

Re: Grab me constrained to horizontal

Posted: Fri Dec 22, 2017 11:03 am
by richmond62
Nice solution.
Yes, but possibly a bit overly complicated:

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
Yo.png
More Dragging.livecode.zip
(35.79 KiB) Downloaded 249 times

Re: Grab me constrained to horizontal

Posted: Fri Dec 22, 2017 1:16 pm
by richmond62
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.

Re: Grab me constrained to horizontal

Posted: Mon Dec 25, 2017 7:10 am
by bogs
Well, I finally came to a solution myself (dang I'm slow :roll: ).
Image
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

Posted: Mon Dec 25, 2017 9:48 pm
by Da_Elf
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

Posted: Mon Dec 25, 2017 10:13 pm
by bogs
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.

Re: Grab me constrained to horizontal

Posted: Tue Dec 26, 2017 9:09 pm
by jacque
Da_Elf wrote:
Fri Dec 22, 2017 3:58 am
OK i got it done and it worked out so that i can even restrict it to go right only
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
And if might be slightly more reliable to use "mouseRelease" rather than "mouseLeave":

Code: Select all

on mouseRelease
  put false into moveIt
end mouseRelease
Otherwise, your script is how I'd do it too.

Re: Grab me constrained to horizontal

Posted: Tue Dec 26, 2017 9:37 pm
by richmond62
the recommended way
by whom? That sounds like an imposition of orthodoxy.

Why "Is" grab me to be avoided?

Re: Grab me constrained to horizontal

Posted: Tue Dec 26, 2017 10:00 pm
by jacque
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.

Re: Grab me constrained to horizontal

Posted: Tue Dec 26, 2017 10:19 pm
by richmond62
a risk of mouseMove commands piling up in the queue
That probably explains the probs I mentioned with my dodecahedron.