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

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

lock objects from being dragged out of the screen

Post by tomerp » Thu May 11, 2017 7:36 pm

hi!
i have an app in which the user can move object around. what will be the most efficient way of preventing them from dragging the object out of the screen?

tnx
tomer

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
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 » Thu May 11, 2017 9:36 pm

Hi.

How are you moving the objects around?

In any case, the "mouseMove" message is sent whether in edit or run mode, so you could (pseudo):

Code: Select all

on mouseMove
  put the target
  if the mouseLoc is beyond the borders of this card then do not allow this to happen
end mouseMove
Silly, eh? But can you take it from there? To check either the mouseLoc in relation to the edges of the card, or, if you want to get fancy (and I always recommend fancifulness) check the edges of the object in relation to the edges of the card. The "target" is your friend here.

Write back with your fancies.

Craig Newman

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
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 » Thu May 11, 2017 9:37 pm

Hi,
You may define a rect and test if all points of your control is within this rect at mousemove
Best regards
Jean-Marc
https://alternatic.ch

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: lock objects from being dragged out of the screen

Post by SparkOut » Thu May 11, 2017 9:54 pm

Learning to do this yourself is the best idea, but Animation Engine includes some very nice features including constraints to location.

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

Re: lock objects from being dragged out of the screen

Post by tomerp » Fri May 12, 2017 12:05 pm

i am moving the object with:

Code: Select all

grab me
where should i put this code that u wrote? in the card script?

Code: Select all

on mouseMove
  put the target
  if the mouseLoc is beyond the borders of this card then do not allow this to happen
end mouseMove

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Thu Jun 29, 2006 4:16 pm

Re: lock objects from being dragged out of the screen

Post by Randy Hengst » Fri May 12, 2017 1:47 pm

Try something like this in the script of the object you’re moving:

local tStartLoc

on mouseDown
put the loc of target into tStartLoc
grab me
end mouseDown

on mouseUp
if the loc of target is not within the rect of this card then
move target to tStartLoc
end if
end mouseUp

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: lock objects from being dragged out of the screen

Post by Klaus » Fri May 12, 2017 4:28 pm

Hi Tomerp,

what Randy wrote! :D

Important hint:
.. so you could (pseudo):
PSEUDO in this case means, this is what it should do, but is really NOT the actual script!


Best

Klaus

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Fri May 12, 2017 6:14 pm

I did it DIFFERENTLY . . . .
Troll.png
Stack removed as updated, improved version uploaded.
Last edited by richmond62 on Fri May 12, 2017 7:24 pm, edited 1 time in total.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: lock objects from being dragged out of the screen

Post by Klaus » Fri May 12, 2017 6:22 pm

You ALWAYS do it differently! :D

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Fri May 12, 2017 7:25 pm

This version allows one to resize the stack . . . .
TROLL2.png
What A Drag.livecode.zip
Here's the stack.
(65.36 KiB) Downloaded 287 times

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: lock objects from being dragged out of the screen

Post by Newbie4 » Fri May 12, 2017 7:32 pm

...and if you want it to work for any size card, you can generalize the tests for right and bottom. (other than an 800x600 sized card in Richmond's example)

give the card a name (e.g. "main") and modify the code to check for

Code: Select all

... > the right of card "main"
instead of just

Code: Select all

... > 800
.

and the

Code: Select all

... > the bottom of card "main"
instead of 600.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: lock objects from being dragged out of the screen

Post by Newbie4 » Fri May 12, 2017 8:16 pm

RATS,

I tried to get there with a more universal solution before you did (Richmond). But I went back to edit it and while I was editing it, you uploaded your new stack. So your time stamp was earlier than mine.

I like your idea of using variables for the right and bottom locations. It makes the code simpler reading.

Good job, nice example
:wink:
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Fri May 12, 2017 8:17 pm

It makes the code simpler reading.
That's because I really don't understand complex code.

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: lock objects from being dragged out of the screen

Post by Newbie4 » Fri May 12, 2017 8:38 pm

Yes, I do too but sometimes when teaching newbies, you have to step through the process in logical steps (as you help them think thru it) Some people can not make the jump to the better code in one step.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

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

Re: lock objects from being dragged out of the screen

Post by richmond62 » Fri May 12, 2017 9:54 pm

To understand what happens when the stack is resized, and so that
the TROLL doesn't "get lost" when it is resized, you need to look at
the stackScript:
Screen Shot 2017-05-12 at 11.52.18 pm.png

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”