Moving button unwanted behaviour

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Thu Jun 16, 2016 2:47 pm

I also have an other issue to solve that i have problems with.

Which is: The buttons I move are part of a group. To prevent a button I am moving to slide behind an already existing button in that group I would like to set the layer to top while moving the button. However because the button is member of a group this is not possible.

Anybody suggestions?

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

Re: Moving button unwanted behaviour

Post by Klaus » Thu Jun 16, 2016 2:53 pm

Hi mrcoollion,
mrcoollion wrote:Anybody suggestions?
yes! :D

...
set the relayerGroupedControls to TRUE
## do your relayer thing here...
set the relayerGroupedControls to FALSE
...
Be careful to not "layer" your button out of the group this way!


Best

Klaus

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Moving button unwanted behaviour

Post by sturgis » Thu Jun 16, 2016 3:11 pm

Yep, what klaus said.

A couple additions.. When you officially start to move the button, you might want to save its current layer so that you can revert after drag.

Also, to keep from layering your way out of the group you can find the correct number to relayer to by getting the layer of the owner of your button (which would be the group) and add the number of controls in the group. (the number of controls in the owner of me) SO if the layer of the group is 5, and there are 5 objects in the group, layering your button to 10 will put it on top of all other grouped objects while still keeping it in the group. Of course it wold get more complicated if you have nested groups..

EDIT.. Heres some code that does the relayering. Modified from one of my earlier posts.

Code: Select all

-- there is still a problem with this method, but it should be easy enough to fix..
-- if you click the button and drag the mouse away before 2 seconds without releasing it,
-- the button will 'jump' to the mouse location after 2 seconds
-- one could add a mouseleave handler that resets the moveDelay if needed.

local MovingDelay,sMoving,sMessage,slayer
constant kDelay=500
on mousedown
   put the millisec into MovingDelay
   put true into sMoving
   send "talkToTheHand" to me in kDelay millisec -- sets the cursor to hand, doesn't rely on mousemove
   put the result into sMessage -- the message id so we can cancel it if the mouse leaves the button before time
end mousedown

command talkToTheHand
   set the cursor to hand
   -- save the current layer of the object being dragged
   put the layer of me into sLayer
   -- allow grouped objects to be relayered
   set the relayerGroupedControls to true
   -- set the layer to the layer of the group + the number of controls in the group.
   set the layer of me to the number of controls in the owner of me  + the layer of the owner of me  
   
   lock cursor
end talkToTheHand
---------------------------------------------------------------------------------
on mouseMove
   if sMoving is not empty and the millisec - MovingDelay > kDelay then
      set  the loc of me to  the mouseLoc
   else
      if the mouseloc is not within the rect of me then 
         cancel sMessage
         put empty into sMoving
         unlock cursor
      end if
   end if
end mouseMove
   ---------------------------------------------------------------------------------
on mouseup
   unlock cursor
   answer information "The mouse was down for" && the millisec - movingDelay && "milliseconds"
   -- revert to the previous layer
   set the layer of me to sLayer
   put empty into sLayer
   put empty into sMoving
end mouseup
---------------------------------------------------------------------------------
on mouseRelease
   -- this is needed on the off chance you move the mouse fast enough to 
   -- be outside the rect of the button on mouseup. In which case, mouseup is not sent
   -- to the button, but mouserelease still is.
   unlock cursor
   answer information "The mouse was down for" && the millisec - movingDelay && "milliseconds"
  -- revert to the previous layer 
  set the layer of me to sLayer
   put empty into sLayer
   put empty into sMoving
end mouseRelease
Also be aware, if you don't want a script in every button you can either assign behaviors, or you can put the script in the group, and use "the target" instead of "of me" to make all your buttons movable from a single script.

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Thu Jun 16, 2016 3:31 pm

Thx Klaus :D , (and sturgis.... our posts crossed :shock: ) ..

Works great.
I am surprised that I can even use 'set the layer of me to top' .. I though this would place the button outside of the group but it seems to work fine.
Below is the way I used it.
In 'on mouseDown' I do the following:

Code: Select all

   put the layer of me into mylayer
   set the relayerGroupedControls to TRUE
   set the layer of me  to top
   set the relayerGroupedControls to FALSE
After I moved the button I set the button to its original layer like this.
The wait and the 'set the visited of me to false' I use to show the visited icon for 1 second. Be aware that the position in the script of the wait and the visited command line are important (in my case at least because the buttons are grouped).
in 'on Mouseup' and 'on mouseRelease' i do the following:

Code: Select all

   wait for 1 seconds
   set the relayerGroupedControls to TRUE
   set the layer of me to mylayer
   set the relayerGroupedControls to FALSE
   set the visited of me to false
Thanks again Klaus.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Moving button unwanted behaviour

Post by sturgis » Thu Jun 16, 2016 3:41 pm

Hey, you might look at "relayer" in the dictionary too. As far as "top" goes, its likely that while moving, you pull it out of the group, then you set it back to where it was, inside the group.

My method did the math to find the right layer number, but it looks like with relayer you can do..

relayer me after the owner of me.. GOing to have to explore relayer now. *sigh* So much to learn, so few brain cells to do it with. :)

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Thu Jun 16, 2016 5:13 pm

Even when I know all the layer numbers of all buttons (which is not to difficult to do), when I set the layer of the button I am moving above the highest layer number, don't I place the button out of the group by doing this? And if I do'not make it the highest layer number of all buttons there will always be one button it will slide under.

Am i correct here or not?

So what layer number do I need to set the button I am moving?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Moving button unwanted behaviour

Post by sturgis » Thu Jun 16, 2016 5:18 pm

YOu set the layer to the highest layered control in the group. That way, it takes over that position, (still in the group) and the other controls shift downward to fill the vacated hole.

And if I understand what I read in the dictionary for "relayer" thats exactly what "relayer <control> after the owner of <control>" does to accomplish exactly the same thing.

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Thu Jun 16, 2016 6:11 pm

Ok,

So the the routine must do the following:
1) find out the highest layer and related button name of all objects (buttons) in the group
2) Set the layer of the button I want to move to the highest layer number.
Or when using relayer
2) relayer button "NameMovingButton" after button "NameButtonWithHighestLayernumber"

Thanks :)

I will try it and post the result.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Moving button unwanted behaviour

Post by sturgis » Thu Jun 16, 2016 6:37 pm

Yep. Relayer is interesting. I've never needed to relayer, so its all new to me.

you can relayer before, relayer after, or front, or back or..

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Fri Jun 17, 2016 3:50 pm

Found a strange issue when I tried to get all the button names from the button group using the code below.
In a group I have three buttons named button1 , button2 and button3. The first line of this script does find three buttons so that is ok.
However in the ButtonsString at the end of this script i see the following 'button1,button2,button5' . There is no button5 anywhere to be found !!!! That should be button3.

Code: Select all

   put the number of buttons in the group "groupMenuButtons" of card "Menu" into NumberOfButtons
   repeat with x = 1 to NumberOfButtons 
      put the short name of button x & "," after ButtonsString
   end repeat
   if the last character of ButtonsString is "," then delete the last character of ButtonsString
Any idea's or suggestion for a more reliable routine?
This routine e.g. could be used to find out the (highest or lowest) layers of all the button objects in a group.

Regards,

Paul

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

Re: Moving button unwanted behaviour

Post by Klaus » Fri Jun 17, 2016 4:20 pm

Hi Paul,

this shold do:

Code: Select all

...
put the number of buttons OF group "groupMenuButtons" of card "Menu" into NumberOfButtons
repeat with x = 1 to NumberOfButtons 

   ## You need the correct address of the objects, otherwise the engine presumes the "current" card!
   put the short name of button x OF grp "groupMenuButtons" of card "Menu" & "," after ButtonsString
end repeat
## if the last character of ButtonsString is "," then delete the last character of ButtonsString
## REALLY no need for IF THEN here! ;-)
delete char -1 of ButtonsString
...
Best

Klaus

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Fri Jun 17, 2016 5:20 pm

Stupid mistake of myself :oops: .... should have seen that...
It works now. :D

The IF THEN I placed because there might be no buttons in the string..

Thanks....

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

Re: Moving button unwanted behaviour

Post by Klaus » Fri Jun 17, 2016 5:35 pm

mrcoollion wrote:The IF THEN I placed because there might be no buttons in the string...
Even in that case (empty string!) deleting the last character will do no harm! :D

mrcoollion
Posts: 551
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Moving button unwanted behaviour

Post by mrcoollion » Fri Jun 17, 2016 5:39 pm

Thanks for the info and support (again !)

Post Reply

Return to “Talking LiveCode”