Grab me constrained to horizontal

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

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Grab me constrained to horizontal

Post by Da_Elf » Thu Dec 21, 2017 4:02 am

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.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Thu Dec 21, 2017 4:13 am

Pretty sure these 2 threads go into it in some depth.
Thread 1
Thread 2

*Edit - Thread 3.
Image

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Grab me constrained to horizontal

Post by jmburnod » Thu Dec 21, 2017 5:32 pm

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
https://alternatic.ch

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Fri Dec 22, 2017 12:53 am

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

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Grab me constrained to horizontal

Post by Da_Elf » 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

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


bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Fri Dec 22, 2017 4:20 am

Nice solution.
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Grab me constrained to horizontal

Post by richmond62 » Fri Dec 22, 2017 11:03 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Grab me constrained to horizontal

Post by richmond62 » Fri Dec 22, 2017 1:16 pm

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.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Mon Dec 25, 2017 7:10 am

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
 
Image

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Grab me constrained to horizontal

Post by Da_Elf » Mon Dec 25, 2017 9:48 pm

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.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Grab me constrained to horizontal

Post by bogs » Mon Dec 25, 2017 10:13 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Grab me constrained to horizontal

Post by jacque » Tue Dec 26, 2017 9:09 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Grab me constrained to horizontal

Post by richmond62 » Tue Dec 26, 2017 9:37 pm

the recommended way
by whom? That sounds like an imposition of orthodoxy.

Why "Is" grab me to be avoided?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Grab me constrained to horizontal

Post by jacque » Tue Dec 26, 2017 10:00 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Grab me constrained to horizontal

Post by richmond62 » Tue Dec 26, 2017 10:19 pm

a risk of mouseMove commands piling up in the queue
That probably explains the probs I mentioned with my dodecahedron.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”