Page 1 of 1
scripting the level of a button ?
Posted: Mon Jul 18, 2016 5:30 pm
by melristau
I have button size and location "unlocked" and layer mode as "dynamic"
But I can't set level of a btn with this code:
set the layer of btn "x1" to 101
Re: scripting the level of a button ?
Posted: Mon Jul 18, 2016 8:32 pm
by dunbarx
Hi.
There is no reason at all why this should not work. Even if you do not have 101 controls, your handler should bring it to the top.
But it does not.
So I made two buttons. I overlapped the two buttons to see the layers in action. In the script of btn 1:
Code: Select all
on mouseUp
set the layer of btn 2 to 1 -- or "top" or bottom"
put the layer of btn 1 & return & the layer of btn 2
end mouseUp
Button 2 alternately overlaps and underlaps button 1. The layer report in msg always shows btn 2 as layer 2. Substituting "bottom" for "1" does the same thing. And "top" does nothing if the layer of btn 2 is already 2.
What on earth is going on?
LC 6.7.11. I will submit a bug report unless someone chime in...
Craig Newman
Re: scripting the level of a button ?
Posted: Mon Jul 18, 2016 9:08 pm
by Randy Hengst
Hi Craig,
I see the same thing on Mac 10.11.4 with LC 7.1.4.
But, when I use the names of the buttons (I used “one” and “two”), the layer report reflects the layering I see with the buttons themselves…
I think the issue is that once button two becomes layer one, then it’s button 1… my understanding is that using the number rather than the name simply uses the layer in which the buttons sits. So, in your script example what was originally button 1 (because it began in layer 1) becomes button 2 (because it’s been moved to layer 2).
I think I said all the correctly…
randy
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 12:24 am
by mwieder
Randy-
It's really, really, really a bad idea to use reserved keywords as control names.
The parser will resolve "one" as 1, "two" as 2, no matter if you quote it or not.
So button "one" is the same as button 1, button "two" is the same as button 2, etc.
You can refer to button "one" all you want, but you will be referring to the first button.
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 1:00 am
by melristau
My reason for using "set the level of btn to #" is to move an arrangement of 117 buttons to same level.
They are now scattered at great difference of depth. Perhaps it doesn't matter.
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 1:04 am
by Randy Hengst
Mark,
You’re right, of course… I didn’t even think about that.
I was just putting an example too together quickly…
At least in my experimenting it seemed to address the issue Craig noted.
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 1:26 am
by mwieder
meristau-
Ah. Don't think of this as putting controls into a layer ala Photoshop or whatever.
The "layer" refers to the ordering of controls in the z-axis, i.e., what control will be visually in front of another.
And, of course, the control in front (higher layer number and therefore in front in the z-axis) is the one that will get mouse events.
OK - other folks may prefer up- or down- instead of in front, but I hope you get the idea anyway.
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 1:37 am
by dunbarx
My reason for using "set the level of btn to #" is to move an arrangement of 117 buttons to same level.
They are now scattered at great difference of depth. Perhaps it doesn't matter.
That is exactly what the "layer" property is intended to do. Note carefully, though, that the layer refers to all controls, not just buttons, so that if you have a bunch of fields on the card, say, those are interspersed along with those hundred-odd buttons. If you want to order only the buttons themselves, you need to manage that carefully.
@Mark. So what about the thing I am seeing, where the second button toggles front to back?
Craig
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 2:59 am
by mwieder
Craig - your're falling into the same trap.
Try naming the buttons instead of referring to them by their layer number.
Code: Select all
on mouseUp
set the layer of btn "Button_2" to 1
put the layer of btn "Button_1" & return & the layer of btn "Button_2"
end mouseUp
The problem with your original is that by the time you've set button 2 to a layer of 1, it's now button 1.
So when you ask for the layer of button 1, it's going to be one, no matter what its name is.
Same thing with asking for the layer of button 2.
"what's the layer of button layer 2"?
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 3:51 am
by dunbarx
Mark.
I am an idiot.
Craig
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 4:54 am
by melristau
So, using this code I get no change of layer:
Code: Select all
on mouseUp
-- level matrix of bans test
-- buttons are named x1, x2, x3...etc.
repeat with i=1 down to 117
set the layer of btn ("x"&i) to 6000
end repeat
end mouseUp
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 6:25 am
by mwieder
Craig-
Not so.
Think I haven't fallen into that trap as well?

Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 6:29 am
by mwieder
melristau-
What is it you're trying to accomplish?
What problem are you trying to solve?
If you change your code to
repeat with i=117 down to 1
you do get a change of layer.
Re: scripting the LAYER of a button ?
Posted: Tue Jul 19, 2016 9:28 am
by [-hh]
[I changed for this post the title "level" to "layer"]
Hi all.
This is probably caused by a code-line of me in a post of another thread of the OP.
So let me explain in similar words.
@melristau
Imagine each and every object lives on a transparent sheet of its own, that is you have exactly one sheet per object. And your card is a bundle of such sheets.
Now sort them in some order. The resulting sort order gives you
the layer of the controls, counted from bottom (=1) to top (=the num of controls).
That is (roughly) for a control:
layer = rank in sort order of all controls,
counted from bottom, just as counting while you put the sheets on your desktop one after the other, on top of each other (not side by side, the sheets are always as large as your desktop). No two controls can have the same layer.
** A group behaves for that like a single control (see dictionary) **
[Imagine a closed, transparent briefcase of sheets]
Imagine this sheet-bundle for every action described now.
Setting a layer to "top" (=the num of objects):
pulls the sheet out of the sheets bundle and puts it on top of the sheets. So this changes also the layer of all the objects (by -1) that were above the new top object.
Setting a layer to "bottom" (=1):
pulls the sheet out of the sheets bundle and puts it at bottom of the sheets. So this changes also the layer of all the objects (by +1) that were below the new bottom object.
Setting a layer to a number 7 (if the num of controls is at least 7):
pulls the sheet out of the sheets bundle and puts it into sort order 7, counted from bottom. So this changes also (a) the layer (by +1) of all the objects that were below the object now sorted to 7 and above 6th , and (b) the layer (by -1) of all the objects that were above the object now sorted to 7 and below 7th.
If you have 192 controls and set the layer of an object to 6000 then the resulting layer is 192, the same as
min(6000, the num of controls).
Some code examples.
Code: Select all
-- Sorting N buttons ("x"&i) descending to bottom
repeat with i=1 to N
set layer of btn ("x"&i) to "bottom"
end repeat
-- gives, listed bottomUp: xn, ..., x2, x1
Code: Select all
--Sorting N buttons ("x"&i) ascending to top
repeat with i=1 to N
set layer of btn ("x"&i) to "top"
end repeat
--gives, listed bottomUp: x1, x2, ..., xn
Code: Select all
Advanced.
-- Sorting N buttons ("x"&i) descending (for 'ascending' similar)
-- without changing the layer of other controls # <--------
-- At first get the 'slots' of the buttons within all controls
repeat with i=1 to N
put cr & the layer of btn ("x"&i) after lst
end repeat
delete char 1 of lst
-- sort the old 'slot' numbers
sort lst descending numeric # <-- new sort order, here descending
-- put the objects one after the other in the old 'slots'
repeat with i=1 to N
set layer of btn ("x"&i) to line i of lst
end repeat
Re: scripting the level of a button ?
Posted: Tue Jul 19, 2016 8:54 pm
by melristau
Most helpful is learning the simple fact that each object owns a unique layer.
Able to pair layer with btn name (x1 - x117) with this script:
Code: Select all
on mouseUp
repeat with x = 1 to 117
put the short name of btn ("x"&x) into nm
set the layer of btn nm to x
end repeat
end mouseUp
Thanks for your notes and scripts!