diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-03-23 18:43:07 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-03-23 18:43:07 +0500 |
| commit | 3e3e1f480f87192cf1042bec0ee0d4bb7c356e62 (patch) | |
| tree | 7998bacf5657c83cbee0cb97f5b0a65c66cabe3d | |
| parent | a4d30665a9aeddc9777761a322a48d56d9f44378 (diff) | |
Recursive UI elements support
| -rw-r--r-- | assets/MainMenu.json | 10 | ||||
| -rw-r--r-- | engine/Dynamic/Descriptors/UIElement.go | 16 | ||||
| -rw-r--r-- | engine/UI/Label.go | 30 |
3 files changed, 45 insertions, 11 deletions
diff --git a/assets/MainMenu.json b/assets/MainMenu.json index d44ab69..9054627 100644 --- a/assets/MainMenu.json +++ b/assets/MainMenu.json @@ -14,12 +14,12 @@ "Spacing": 0.01, "Children": [ { - "Type": "image", - "TextureName": "testPackage/water.png" - }, - { "Type": "label", - "Text": "Ruines of Rafdalon" + "Text": "Ruines of Rafdalon", + "BackgroundElement": { + "Type": "image", + "TextureName": "testPackage/water.png" + } }, { "Type": "label", 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 } |
