lock objects from being dragged out of the screen

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: heatherlaine, Klaus, FourthWorld, kevinmiller

tomerp
Posts: 6
Joined: Thu Mar 09, 2017 8:01 pm

Re: lock objects from being dragged out of the screen

Post by tomerp » Sun May 14, 2017 6:54 am

thank u all of your help. hopfully i will be able to try those things out during the day.
one more thing thought - i've seen a soliter game ("klondike") on "example tutorials" which doesn't let the player drag the mouse out of the screen once he is grabbing something. it's not the kind of code which checks the location and set a new one for the object but more like a "wall" surrounding the card. unfortunatelly i didn't understand the code.
does someone have an idea of how can implement what i described?

thanks again for all your answers!

tomer

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Sun May 14, 2017 9:47 am

a "wall" surrounding the card
Какво? Аз не разбирам.

Can you upload a picture of the game and/or an internet link to this game so it is
possible to play with it an ascertain its functionality?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5424
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: lock objects from being dragged out of the screen

Post by dunbarx » Sun May 14, 2017 3:01 pm

So still playing around, on a new card with one button, put this in the card script.

Code: Select all

on mouseDown
 set loc of btn 1 to  the mouseLoc
end mouseDown

on mouseMove
   if the mouse is down and the mouseLoc is within the rect of btn 1 and the right of btn 1< the right of this cd then then
 set the loc of btn 1 to the mouseLoc
   end if
end mouseMove
Assuming your btn is somewhere in the middle of the card you can drag all day until you try to go too far to the right. Pretty old-fashioned. I tried playing around with the "grab" command, but there is no "unGrab". And sending a "mouseUp" message to the grabbed object does not seem to release it. That was the "right"way to do this, and I might try again to see if I can "unGrab" somehow. Maybe someone else knows how to subvert the grab command while the mouse is still down.

You are stuck once you hit the right edge, so I put the mouseDown handler to allow you to click somewhere in the card to restart. This seems inelegant.

Anyway, this can be expanded to cover all four sides of the card.

Craig
Last edited by dunbarx on Sun May 14, 2017 4:23 pm, edited 1 time in total.

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

Re: lock objects from being dragged out of the screen

Post by jmburnod » Sun May 14, 2017 4:12 pm

Hi Tomer,
i've seen a soliter game ("klondike")
Jaqueline is the author
I take a look at her mousemove script.
She's clever than me is my first conclusion. I need time to examine it.
Here is a little stack that allows to move an object inside a rectangle.
It also use "mousemove" and check intersect between two controls.
I hope this help.
Best regards
Jean-Marc
Attachments
stMoveControlInsideRect001.livecode.zip
Move a control inside a rect using mousemove
(1.86 KiB) Downloaded 83 times

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

Re: lock objects from being dragged out of the screen

Post by jacque » Sun May 14, 2017 4:26 pm

Do you mean this Klondike game?

http://livecodeshare.runrev.com/stack/226/Klondike

It does set the location of the dragged objects repeatedly during a move. The mouseMove message is trapped and if the mouseLoc is not within the bounds of the card it does nothing.

If that's the example you mean I can give you a shorter version of the code. The handlers in the Klondike game are complex because they must handle many different conditions. It is much simpler to drag only one object.

You cannot use the grab command, it doesn't provide enough feedback for this.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 222
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK

Re: lock objects from being dragged out of the screen

Post by thatkeith » Tue Dec 19, 2017 6:45 pm

The sample stack helped a lot, thanks Jean-Marc!

(I do dearly wish it was as simple as SuperCard's grab [with message] [within rect]. I can see how this more complex mouseDown/mouseUp/mouseRelease/mouseMove setup can be really powerful, but surely there's room for something simpler (and in that sense more elegant) in the language as well?)

:-/ k

But still, great! This does the trick. :)
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
President, IVRPA.org

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

Re: lock objects from being dragged out of the screen

Post by bogs » Tue Dec 19, 2017 8:16 pm

thatkeith wrote:
Tue Dec 19, 2017 6:45 pm
...I do dearly wish it was as simple as SuperCard's grab [with message] [within rect].
Mind you, I never read Jacque's code for that game, however the code for something like this can be as simple as what your talking about. Off the cuff, I wrote this (before seeing Jim post the link to here) in the bit of space inbetween my post and Jim's in the other thread, this is all in the button (or whatever object you choose for the slider*) -

Code: Select all

on mouseDown 
// grab the bar/button in this case, could be any object...
      grab me 
end mouseDown

on mouseStillDown
// keeping the top of the bar/button in line with the top of the graphic...
   if the top of me is not the top of graphic "Rectangle" then set the top of me to the top of graphic "Rectangle"
   send mouseStillDown to me in 1 millisecond
end mouseStillDown

on mouseMove
// checking to make sure you haven't gone past the edge of the world...
   if the left of me < the left of graphic "Rectangle" then
      set the left of me to the left of graphic "Rectangle"
   else if the right of me > the right of graphic "Rectangle" then 
      set the right of me to the right of graphic "Rectangle"
   end if
end mouseMove
I am sure this is not the most elegant way to do something like this, and as I said in the other thread, the dictionary does have within and you can use 'with messages' and such. I didn't explore within very much, but I am pretty sure what I put up there could be condensed considerably.

* I also forgot to ask why you wouldn't just use a slider, progress bar, or scroller for this since it is an already made control, and set north to whatever value you got from it?

*Edit - I also took the time this morning to check that 'within' actually works like its supposed to, changing the mouseMove code to this -

Code: Select all

on mouseMove
   if the topLeft of me is not within the rect of grc "Rectangle" then 
      if the left of me < the left of  of grc "Rectangle" then
         set the left of me to the left of grc "Rectangle"
      else if  the right of me > the right of  of grc "Rectangle" then
         set the right of me to the right of grc "Rectangle"
      end if
   end if
end mouseMove
Since that works, I suspect you could have written it however you would have written it in Sc as well. Again, the above is not the most elegant code on the planet and has faults.
Image

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

Re: lock objects from being dragged out of the screen

Post by jmburnod » Tue Dec 19, 2017 11:59 pm

Hi Bogs,
Yes. That seems simpler but I feel that is better to check that all corners are inside area BEFORE moving.
@ thatkeith
but surely there's room for something simpler (and in that sense more elegant) in the language as well?)
Yes, after watching my script I think a function should be better than a large mousemove message
Best regards
Jean-Marc

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

Re: lock objects from being dragged out of the screen

Post by bogs » Wed Dec 20, 2017 3:07 am

jmburnod wrote:
Tue Dec 19, 2017 11:59 pm
Hi Bogs,
Yes. That seems simpler but I feel that is better to check that all corners are inside area BEFORE moving.
Heh, I absolutely agree with that sentiment, the above was a q&d version whipped up to test specifically what was asked about, in particular - "SuperCard's grab [with message] [within rect]. "

I definitely would not recommend using it without cleaning it up, as I said a few times, it only gets the job done, it is neither elegant nor even good :mrgreen:

For one thing, you can see the button bobbing past the top of the rect momentarily at times (I suspect setting the "send to me in 0" would probably eliminate that), and for another it can be dragged past the edges (although it will snap back).

I still wonder why the slider, scrollbar, or progress bar wouldn't suffice for the description of the problem in this case though. Specifically, if I remember correctly**, he wanted the moving bar to align with north. Setting the value on any of those from 0-360 (I assume this is for that panoramic photo stuff) and then moving the picture opposite the value progress would be smoother and better looking.

** I was not too far off :)
thatkeith wrote:
Mon Dec 18, 2017 10:34 pm
I'd like to have the user grab and drag a simple object across the card, but constrained to move in just one plane and also within a specific span. Specifically, a vertical line, dragged horizontally across an image, so the user can specify where North is in the image.
Actually, I thought of an even simpler way, which I posted back in the other thread.
Image

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Wed Dec 20, 2017 4:53 pm

Here are frame constraints . . .
DragAct.jpg
DRAG ACT.livecode.zip
(26.12 KiB) Downloaded 19 times
I'll have a bash at single-plane constraint just as soon as I get myself a cup of coffee 8)

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Wed Dec 20, 2017 5:15 pm

SisterAct.png
Sister.png
SISTER ACT.livecode.zip
(77.41 KiB) Downloaded 23 times

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

Re: lock objects from being dragged out of the screen

Post by jacque » Thu Dec 21, 2017 7:35 am

@Richmond, have some pity and save people some time -- just paste the script into the forum. It will be easier to read, it can be copied and pasted, the user doesn't have to transcribe, and it will show up in the RSS feed along with the rest of the message. It's also less work for you than taking a snapshot and uploading an image.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Thu Dec 21, 2017 11:35 am

have some pity
What? Me? 8)

Point taken . . . will bear that in mind in future.

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

Re: lock objects from being dragged out of the screen

Post by bogs » Thu Dec 21, 2017 1:27 pm

richmond62 wrote:
Thu Dec 21, 2017 11:35 am
have some pity
Point taken . . . will bear that in mind in future.
Now I feel bad, since I often do that myself (of course, not knowing very much, the little code I post isn't as much to type in :wink: )
Image

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

Re: lock objects from being dragged out of the screen

Post by bogs » Thu Dec 21, 2017 1:42 pm

bogs wrote:
Wed Dec 20, 2017 3:07 am
For one thing, you can see the button bobbing past the top of the rect momentarily at times (I suspect setting the "send to me in 0" would probably eliminate that)
What I actually found eliminates that is -1, as in (from a remembered conversation with Jacque in a thread far away) -

Code: Select all

on mouseStillDown
   if the top of me is not the top of graphic "Rectangle" then
      set the top of me to the top of graphic "Rectangle"
   end if
   send mouseStillDown to me in -1
end mouseStillDown
Changing that kept the button from bobbing up on occasion, I'm sure locking the screen during the move would help as well.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”