From 3e3e1f480f87192cf1042bec0ee0d4bb7c356e62 Mon Sep 17 00:00:00 2001 From: physcik Date: Mon, 23 Mar 2026 18:43:07 +0500 Subject: Recursive UI elements support --- engine/Dynamic/Descriptors/UIElement.go | 16 +++++++++++++++- engine/UI/Label.go | 30 +++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) (limited to 'engine') diff --git a/engine/Dynamic/Descriptors/UIElement.go b/engine/Dynamic/Descriptors/UIElement.go index da523e9..0e59c9a 100644 --- a/engine/Dynamic/Descriptors/UIElement.go +++ b/engine/Dynamic/Descriptors/UIElement.go @@ -14,6 +14,9 @@ type UIElementDescriptor struct { Weight float32 Style StyleDescriptor + // For recursive elements + BackgroundElement *UIElementDescriptor + // For labels Text string Alignment string @@ -64,11 +67,22 @@ func parseAsLabel(base *UIElementDescriptor) ui.UIElement { alignment = ui.Start } + var child ui.UIElement = nil + if base.BackgroundElement != nil { + var parsed, err = base.BackgroundElement.Parse() + if err == nil { + child = parsed + } else { + rl.TraceLog(rl.LogWarning, "Failed to parse a background element of a label: %v", err.Error()) + } + } + return &ui.Label { Weight: base.Weight, Text: base.Text, Alignment: alignment, Style: base.Style.Parse(), + BackgroundElement: child, } } @@ -111,7 +125,7 @@ func parseAsGridColumn(base *UIElementDescriptor) ui.UIElement { func parseAsImage(base *UIElementDescriptor) ui.UIElement { var texture, err = components.Resources.Textures.GetTextureByName(base.TextureName) if err != nil { - rl.TraceLog(rl.LogWarning, "Failed to parse a texture %s requested by an inage UI element: %v", base.TextureName, err.Error()) + rl.TraceLog(rl.LogWarning, "Failed to parse a texture %s requested by an image UI element: %v", base.TextureName, err.Error()) return nil } var outp = ui.Image { diff --git a/engine/UI/Label.go b/engine/UI/Label.go index 8f60f10..9d47187 100644 --- a/engine/UI/Label.go +++ b/engine/UI/Label.go @@ -37,13 +37,17 @@ type Label struct { Alignment TextAlignment Style Style `json:"-"` + BackgroundElement UIElement } -func (base *Label) Init(*Menu) { +func (base *Label) Init(menu *Menu) { base.Style.FillMissing(defaultLabelStyle) if base.Weight == 0 { base.Weight = 1 } + if base.BackgroundElement != nil { + base.BackgroundElement.Init(menu) + } } func (base *Label) Destroy() { @@ -51,10 +55,7 @@ func (base *Label) Destroy() { func (base *Label) Draw(position *rl.Rectangle) { // rl.TraceLog(rl.LogInfo, "Drawn at %v/%v/%v/%v", position.X, position.Y, position.Width, position.Height) - rl.DrawRectangleRounded(*position, - *base.Style.Roundness, - 0, // Assume the segments param is always 0 - it doesn't really matter without a border - *base.Style.BacgroundColor) + base.drawBackground(position) var textHeight = base.getTextHeight(position) var textY = int32(position.Y + *base.Style.Padding) if base.Alignment == Center { @@ -67,6 +68,25 @@ func (base *Label) Draw(position *rl.Rectangle) { *base.Style.FontColor) } +func (base *Label) drawBackground(position *rl.Rectangle) { + if base.BackgroundElement == nil { + base.drawSimpleBackground(position) + } else { + base.drawBackgroundElement(position) + } +} + +func (base *Label) drawSimpleBackground(position *rl.Rectangle) { + rl.DrawRectangleRounded(*position, + *base.Style.Roundness, + 0, // Assume the segments param is always 0 - it doesn't really matter without a border + *base.Style.BacgroundColor) +} + +func (base *Label) drawBackgroundElement(position *rl.Rectangle) { + base.BackgroundElement.Draw(position) +} + func (base *Label) GetOccupationWeight() float32 { return base.Weight } -- cgit v1.3