Naming Layers
Naming Layers
I don't understand how to name layers.
Could someone please explain, because I find the documentation confusing.
Could someone please explain, because I find the documentation confusing.
Re: Naming Layers
In the Overview chapter of Layers library you have examples. That doesn't seem complicated to me, please ask specifically.
The general answer: Give the layer a name and use the name instead of the number.
The general answer: Give the layer a name and use the name instead of the number.
Code: Select all
EnableLayers()
Circle(10, 10, 30, #RED, {Name="circle1"})
st = GetLayerStyle("circle1")
MoveLayer("circle1", st.x, st.y, st.x+500, st.y+300,{Speed=#SLOWSPEED})
Re: Naming Layers
Thank you for replying.
The Documentation reads:
"Let’s have a look at a brief example now:
EnableLayers()
DisplayBGPic(2)
DisplayBrush(1, #CENTER, #CENTER)
Plot(100, 100, #RED)
Print("Hello World!")
Box(50, 50, 100, 100, #BLUE)
The above code displays 4 different object types and attaches at the same time 4 layers to
the background picture number 2 because layers were enabled. Every displayed object gets
its own layer now, therefore we have the following layers now for background picture 2:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: A red pixel at 100 : 100
Layer id 3: Text "Hello World!"
Layer id 4: A blue box at 50 : 50 with dimensions 100 : 100
Now you can do everything you like with those layers, e.g. you can hide them, move them,
swap foreground priorities or remove them. Hollywood offers many functions that can
handle layers.
Please note that layer ids are dynamic. For example if the above code would now call the
command
RemoveLayer(2)
then the layer ids would be changed. After this command returns we would have the
following layers for background picture 2:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: Text "Hello World!"
Layer id 3: A blue box at 50 : 50 with dimensions 100 : 100
You see that the text "Hello World!" has now layer id 2 and the box is now at layer 3.
Starting with Hollywood 2.0, there is a new command available: SetLayerName(). You can
use it to give your layers a unique name so you can simply address the layer through its
name a instead of its id. This is very useful if you have many layers and you do not want
to remember their ids. All functions that work with layers accept a name string in addition
to a numeric id now. Here is our example again:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: A red pixel at 100 : 100
Layer id 3: Text "Hello World!"
Layer id 4: A blue box at 50 : 50 with dimensions 100 : 100
Now we do the following:
SetLayerName(1, "brush: 1")
SetLayerName(2, "red pixel")
SetLayerName(3, "text: hello world")
SetLayerName(4, "blue box")
Now we could remove layer 2 by calling
RemoveLayer("red pixel")
We do not have to care about the fact that the layer ids have changed now because all
layers have names and so we can easily address them."
Which doesn't mention your approach at all, unless I missed something.
I followed your example and did:
But:
Says there's no such layer id.
The Documentation reads:
"Let’s have a look at a brief example now:
EnableLayers()
DisplayBGPic(2)
DisplayBrush(1, #CENTER, #CENTER)
Plot(100, 100, #RED)
Print("Hello World!")
Box(50, 50, 100, 100, #BLUE)
The above code displays 4 different object types and attaches at the same time 4 layers to
the background picture number 2 because layers were enabled. Every displayed object gets
its own layer now, therefore we have the following layers now for background picture 2:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: A red pixel at 100 : 100
Layer id 3: Text "Hello World!"
Layer id 4: A blue box at 50 : 50 with dimensions 100 : 100
Now you can do everything you like with those layers, e.g. you can hide them, move them,
swap foreground priorities or remove them. Hollywood offers many functions that can
handle layers.
Please note that layer ids are dynamic. For example if the above code would now call the
command
RemoveLayer(2)
then the layer ids would be changed. After this command returns we would have the
following layers for background picture 2:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: Text "Hello World!"
Layer id 3: A blue box at 50 : 50 with dimensions 100 : 100
You see that the text "Hello World!" has now layer id 2 and the box is now at layer 3.
Starting with Hollywood 2.0, there is a new command available: SetLayerName(). You can
use it to give your layers a unique name so you can simply address the layer through its
name a instead of its id. This is very useful if you have many layers and you do not want
to remember their ids. All functions that work with layers accept a name string in addition
to a numeric id now. Here is our example again:
Layer id 1: Brush 1 at coordinates #CENTER : #CENTER
Layer id 2: A red pixel at 100 : 100
Layer id 3: Text "Hello World!"
Layer id 4: A blue box at 50 : 50 with dimensions 100 : 100
Now we do the following:
SetLayerName(1, "brush: 1")
SetLayerName(2, "red pixel")
SetLayerName(3, "text: hello world")
SetLayerName(4, "blue box")
Now we could remove layer 2 by calling
RemoveLayer("red pixel")
We do not have to care about the fact that the layer ids have changed now because all
layers have names and so we can easily address them."
Which doesn't mention your approach at all, unless I missed something.
I followed your example and did:
Code: Select all
;ICON-SELECTOR BRUSH:
CreateBrush(2, 70, 70, {Name = "icon_selector"})
ExtendBrush(2, 2, 2, 2, 2, {Color = d_col})
Code: Select all
Function p_ToolsMouseOut()
DebugPrint("Mouse Out!")
RemoveLayer("icon_selector")
EndFunction
Re: Naming Layers
@oceanarts
I name layers as soon as I create one, using id "0" which identifies the last layer created. For example:
It is easer for my than keeping track which layer has which id and giving names later.
I name layers as soon as I create one, using id "0" which identifies the last layer created. For example:
Code: Select all
EnableLayers()
SetFillStyle(#FILLCOLOR)
Box(0, 0, 100, 100, #RED) ; create layer 1, which is the last layer created right now
SetLayerName(0, "redbox") ; give the last created layer a name
Box(50, 50, 100, 100, #GREEN) ; create layer 2, which is the last layer created right now
SetLayerName(0, "greenbox") ; give the last created layer a name
PowerBook 5.8 MorphOS 3.18
Mac Mini MorphOS 3.18
Mac Mini MorphOS 3.18
Re: Naming Layers
If you look into the documentation of CreateBrush(), you will find no "Name" argument for the table, so it will be ignored. You can assign the name when creating the visible object (the layer) with DisplayBrush().oceanarts wrote: ↑Fri Nov 29, 2024 9:12 pmCode: Select all
CreateBrush(2, 70, 70, {Name = "icon_selector"})
Code: Select all
DisplayBrush(2, #CENTER, #CENTER, {Name="icon_selector"})
Re: Naming Layers
Okay. Thanks.
I solved it by applying some code Bugala posted a while back (Not sure I quite understand what it does. But I guess it finds the layer ID of the brush in question somehow. ) Thanks, Bugala.
I solved it by applying some code Bugala posted a while back (Not sure I quite understand what it does. But I guess it finds the layer ID of the brush in question somehow. ) Thanks, Bugala.
Code: Select all
Function p_ToolsMouseHover()
DebugPrint("Mouse Hover!")
;DISPLAY ICON-SELECTOR:
DisplayBrush(2, #CENTER, #CENTER + 160)
Local layerid = GetAttribute(#BGPIC, 1, #ATTRLAYERS) ; Bugala Code
SetLayerName(layerid, "icon_selector")
EndFunction
Function p_ToolsMouseOut()
DebugPrint("Mouse Out!")
RemoveLayer("icon_selector")
EndFunction
Re: Naming Layers
GetAttribute() returns the number of layers of this BGPIC, and the last layer (created with DisplayBrush) has this ID. But with my line you could set the name when creating the layer, I think that's a better way.
Re: Naming Layers
You're right, It's better that way. Thanks!Flinx wrote: ↑Tue Dec 03, 2024 5:48 pm GetAttribute() returns the number of layers of this BGPIC, and the last layer (created with DisplayBrush) has this ID. But with my line you could set the name when creating the layer, I think that's a better way.
I was confused as to object ids and layer ids, and I couldn't keep track. But of course, a layer isn't created before an element is displayed.
Re: Naming Layers
Yeah, emeck's and Flinx's ways are proper and suggested, but Bugala's is too complex when there are dedicated methods too.
Re: Naming Layers
Just wanted to pop back and post the "final" code, in case it benefits any other noobs around:
Thanks for all the help!
Code: Select all
;ICON-SELECTOR BRUSH: (REPLACE WITH CUSTOM POLYGON BRUSH(?))
;Brush 2 Created Here:
CreateBrush(2, 70, 70)
ExtendBrush(2, 2, 2, 2, 2, {Color = d_col}
; ***
;FILES-BUTTON FUNCTIONS (attached to a simplebutton):
Function p_FilesMouseOver()
;DISPLAY ICON-SELECTOR:
; Layer containing brush 2 created and named here:
DisplayBrush(2, #CENTER, #CENTER + 160, {name = "icon_selector"})
EndFunction
Function p_FilesMouseOut()
RemoveLayer("icon_selector")
EndFunction