Naming Layers

Find quick help here to get you started with Hollywood
Post Reply
oceanarts
Posts: 58
Joined: Mon May 27, 2024 10:42 pm

Naming Layers

Post by oceanarts »

I don't understand how to name layers.
Could someone please explain, because I find the documentation confusing.
Flinx
Posts: 255
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Naming Layers

Post by Flinx »

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.

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})
oceanarts
Posts: 58
Joined: Mon May 27, 2024 10:42 pm

Re: Naming Layers

Post by oceanarts »

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:

Code: Select all

;ICON-SELECTOR BRUSH: 
	CreateBrush(2, 70, 70, {Name = "icon_selector"})
	ExtendBrush(2, 2, 2, 2, 2, {Color = d_col})
But:

Code: Select all

Function p_ToolsMouseOut()
	DebugPrint("Mouse Out!")
	RemoveLayer("icon_selector")
EndFunction
Says there's no such layer id.
User avatar
emeck
Posts: 187
Joined: Fri Apr 03, 2015 3:17 pm

Re: Naming Layers

Post by emeck »

@oceanarts

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
It is easer for my than keeping track which layer has which id and giving names later.
PowerBook 5.8 MorphOS 3.18
Mac Mini MorphOS 3.18
Flinx
Posts: 255
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Naming Layers

Post by Flinx »

oceanarts wrote: Fri Nov 29, 2024 9:12 pm

Code: Select all

CreateBrush(2, 70, 70, {Name = "icon_selector"})
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().

Code: Select all

DisplayBrush(2, #CENTER, #CENTER, {Name="icon_selector"})
oceanarts
Posts: 58
Joined: Mon May 27, 2024 10:42 pm

Re: Naming Layers

Post by oceanarts »

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. :)

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
Flinx
Posts: 255
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Naming Layers

Post by Flinx »

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.
oceanarts
Posts: 58
Joined: Mon May 27, 2024 10:42 pm

Re: Naming Layers

Post by oceanarts »

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.
You're right, It's better that way. Thanks! :)
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. :oops:
User avatar
jPV
Posts: 674
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Naming Layers

Post by jPV »

Yeah, emeck's and Flinx's ways are proper and suggested, but Bugala's is too complex when there are dedicated methods too.
oceanarts
Posts: 58
Joined: Mon May 27, 2024 10:42 pm

Re: Naming Layers

Post by oceanarts »

Just wanted to pop back and post the "final" code, in case it benefits any other noobs around:

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
Thanks for all the help! :)
Post Reply