mic
mic

Reputation: 4475

Why doesn't my stylebox get overwritten correctly to my panel

I need to use a programmatic stylebox overwrite, but for some reason it's not working. It says it's been overwritten, but continues to point to the default stylebox.

Here is some test code:

var panel = PanelContainer.new()
print(panel.has_theme_stylebox_override("normal"), panel.get_theme_stylebox("normal", "normal"))
    
var stylebox = StyleBoxFlat.new()
stylebox.bg_color = Color.from_string("#000000a0", Color.BLACK)
stylebox.corner_radius_bottom_left = 10
panel.add_theme_stylebox_override("normal", stylebox)
print(panel.has_theme_stylebox_override("normal"), panel.get_theme_stylebox("normal", "normal"))

Here is the output:

false<StyleBoxFlat#-9223372011084970892>
true<StyleBoxFlat#-9223372011084970892>

As you can see, despite being overwritten the stylebox is still pointing to the default for the theme.

Upvotes: 1

Views: 1613

Answers (2)

David Graham
David Graham

Reputation: 468

I had a little trouble figuring out similar trying to change the BG Color.

Standard RGB parameters didn't work but from an Html string it's ok.

This C# version demonstrates duplicating and overriding the themes panel.

// Called when the node enters the scene tree for the first time. -- Godot 4
public override void _Ready()
{
    //get labels from scene tree
    _nameLbl = GetNode<Label>("%NameLabel");
    _numLbl  = GetNode<Label>("%NumLabel");

    //get the stylebox & duplicate from this controls themes panel
    StyleBoxFlat sbf = GetThemeStylebox("panel")
        .Duplicate() as StyleBoxFlat;

    //print color to see what we have
    GD.Print("background colour panel stylebox: ", sbf.BgColor);

    //set color from html. Typical RGB values didn't work
    sbf.BgColor = Color.FromHtml("017f01");

    //add the override
    this.AddThemeStyleboxOverride("panel", sbf);
}

Upvotes: 1

mic
mic

Reputation: 4475

The issue was that I was using the wrong theme type. According to the docs, the correct type is panel.

i.e. the fixed solution is:

var panel = PanelContainer.new()
var stylebox = StyleBoxFlat.new()
stylebox.bg_color = Color.from_string("#000000a0", Color.BLACK)
stylebox.corner_radius_bottom_left = 10
panel.add_theme_stylebox_override("panel", stylebox)

Upvotes: 3

Related Questions