Simple way to drag a graphic across the screen?- Solved

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

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by Klaus » Sat Sep 09, 2017 12:04 pm

I don't think that triggering a "move" "on mousemove" is a good idea at all. 8)

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by jacque » Sun Sep 10, 2017 4:56 am

Klaus wrote:I don't think that triggering a "move" "on mousemove" is a good idea at all. 8)
Yeah, you're right. Next time I'll test before I post. It does work with "set the loc" though. Instead of testing the state of a modifier key, I used a script local flag to cut down on excessive polling:

Code: Select all

local sDragging

on mouseDown
  put true into sDragging
end mouseDown

on mouseUp
  put false into sDragging
end mouseUp

on mouseMove x,y
  if not sDragging then pass mouseMove
  lock screen
  repeat with i = 1 to the number of btns
    set the loc of btn i to x,item 2 of the loc of btn i
  end repeat
  unlock screen
end mouseMove
It would benefit from an mouse x-offset.

I think if I wanted anything other than straight line moves I'd just group the controls and use "grab".
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by richmond62 » Sun Sep 10, 2017 11:04 am

What am I missing?

script in each graphic object:

on mouseDown
grab me
end mouseDown

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by bogs » Sun Sep 10, 2017 2:42 pm

richmond62 wrote:What am I missing?
Well, if that script is in each graphic objects handler, I think it is missing the pass line, in case there is something else being handled by a mouseDown further down the line, isn't it?

I *think* that being in the objects script, it would only get the mouseDown for that object, but there might be other events you want fired during a mouseDown event that do not relate in general to that one object, such as timers, colors, or something else like bool logic to set a variable.

Not trying to be pedantic, I realize this is just a simplified example to illustrate one aspect of how to do something.
ImageImage

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by jacque » Sun Sep 10, 2017 5:33 pm

richmond62 wrote:What am I missing?

script in each graphic object:

on mouseDown
grab me
end mouseDown
Right, that's the solution the OP settled on. The conversation evolved into a discussion of how to move multiple controls at once.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by jacque » Sun Sep 10, 2017 5:38 pm

if that script is in each graphic objects handler, I think it is missing the pass line, in case there is something else being handled by a mouseDown further down the line, isn't it?
In most cases you don't need to pass messages, and sometimes doing so can cause more problems. It depends on your code and your style. Everyone has their own method but I rarely pass messages unless there's a handler somewhere else that needs to act.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by richmond62 » Sun Sep 10, 2017 5:52 pm

I wonder why this does not work:

on mouseDown
grab img "1782.png"
grab img "5247.png"
grab img "1287.png"
end mouseDown

where the script is in img "1782.png"

all that happens is that img "1782.png" is grabbed

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by bogs » Sun Sep 10, 2017 6:06 pm

Ah, thank you for that Jacque :)

@ richmond62 » I think it doesn't work because Lc can only grab one thing at a time can't it? Just like it can only move one thing at a time?
ImageImage

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by richmond62 » Sun Sep 10, 2017 9:01 pm

Just tried to grab a group: that one didn't work either.

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by jacque » Sun Sep 10, 2017 9:18 pm

I think it doesn't work because Lc can only grab one thing at a time can't it? Just like it can only move one thing at a time?
Right, it can only grab one control at a time. Not so right about moving one thing at a time -- the "lock moves" command is specifically set up for moving multiple objects in one go. The part that didn't work in my first example was because I was using the "move" command inside a mouseMove handler, which backed up the message queue pretty badly, because I forgot to add a parameter. MouseMove is sent a zillion time per second, or somewhere in that range. :)

You can indeed use the "move" command in a mouseMove handler if you remember to add "without waiting":

Code: Select all

on mouseMove x,y
  lock moves
  repeat with i = 1 to the number of btns
    move btn i to x,item 2 of the loc of btn i without waiting
  end repeat
  unlock moves
end mouseMove
This isn't as responsive as "set the loc" though because my movespeed was set to default. It could probably be tinkered with, but "set the loc" works with less scripting. You'd also want to set the script local flag here, since the above causes the move whether the mouse is down or not.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by jacque » Sun Sep 10, 2017 9:21 pm

richmond62 wrote:Just tried to grab a group: that one didn't work either.
It works if you click on a control in the group, same as how other mouse events behave in groups.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sphere
Posts: 465
Joined: Sat Sep 27, 2014 10:32 am
Location: the Netherlands

Re: Simple way to drag a graphic across the screen?- Solved

Post by sphere » Wed Sep 13, 2017 3:02 pm

I was playing with this, and doublemouseup (so when doublemouseup) the object is selected.

How can i leave a button selected, and then select another button (or images), then drag them both.
So you could select any nr of objects and after that drag them?
Global Market Share WorldWide Android 84.82%
Global Market Share WorldWide iOs 12%

Just a wild guess that we need more support and features for Android than iOs...

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

Re: Simple way to drag a graphic across the screen?- Solved

Post by dunbarx » Wed Sep 13, 2017 3:51 pm

This might give you a head start:

Code: Select all

select btn 1 and btn 2
Craig

sphere
Posts: 465
Joined: Sat Sep 27, 2014 10:32 am
Location: the Netherlands

Re: Simple way to drag a graphic across the screen?- Solved

Post by sphere » Wed Sep 13, 2017 7:15 pm

Thanks Craig.

I tried some but came up with this, which almost does it(just same script in 2 btns), just not yet with mousedown but hovering over one btn with the mouse, i want it when both selected and then only when mouse down:

Code: Select all

on mouseDown
   set the backgroundcolor of me to "green"
   grab me
 end mouseDown

 on mouseDoubleUp
    set the backgroundcolor of me to "blue"
    --select me
end mouseDoubleUp

on mouseUp
set the backgroundcolor of me to "lightgray"
end mouseUp

--on mouseMove pNewMouseH, pNewMouseV
--repeat with i =1 to the number buttons
  --    if the backgroundcolor of btn i = "blue" then
  --       grab btn i
   --      else grab me
 --  end repeat
--end mouseMove

on mouseMove x,y
 lock moves
 repeat with i = 1 to the number of btns
    if the backgroundcolor of btn i is "blue" then
       move btn i to x, item 2 of the loc of btn i without waiting
       end if
  end repeat
  unlock moves
end mouseMove

--on mouseDoubleDown pButtonNumber
  --set the backgroundcolor of me to "blue"
--repeat with i =1 to the number buttons
--    if the backgroundcolor of btn i = "blue" then  MoveIt
--  end repeat
--end mouseDoubleDown
Global Market Share WorldWide Android 84.82%
Global Market Share WorldWide iOs 12%

Just a wild guess that we need more support and features for Android than iOs...

sphere
Posts: 465
Joined: Sat Sep 27, 2014 10:32 am
Location: the Netherlands

Re: Simple way to drag a graphic across the screen?- Solved

Post by sphere » Thu Sep 14, 2017 2:30 pm

Global Market Share WorldWide Android 84.82%
Global Market Share WorldWide iOs 12%

Just a wild guess that we need more support and features for Android than iOs...

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”