From 95b7170acdfa73c5842db002ec64f78125fe2f90 Mon Sep 17 00:00:00 2001 From: Physcik Date: Fri, 30 Jan 2026 03:04:43 +0500 Subject: Text formation --- engine/UI/GridRow.go | 25 +++++++++++++++++++++++++ engine/UI/Label.go | 41 ++++++++++++++++++++++++++++++++++++++--- engine/UI/Menu.go | 22 ++++------------------ engine/UI/Style.go | 24 ++++++++++++++++++++++++ engine/UI/UIElement.go | 4 +++- engine/main.go | 10 ++++++++-- 6 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 engine/UI/GridRow.go create mode 100644 engine/UI/Style.go diff --git a/engine/UI/GridRow.go b/engine/UI/GridRow.go new file mode 100644 index 0000000..bb9ffe3 --- /dev/null +++ b/engine/UI/GridRow.go @@ -0,0 +1,25 @@ +package ui + +import rl "github.com/gen2brain/raylib-go/raylib" + +type GridRow struct { + // A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow' + HeightWeight float32 + // The objects that lay in this row + Objects []UIElement + + location rl.Rectangle +} + +func (base *GridRow) Init(parent *Menu) { + for _, v := range base.Objects { + v.Init(parent) + } +} + +func (base *GridRow) Draw(span rl.Rectangle) { + // TODO: Add horizontal spacing + for _, v := range base.Objects { + v.Draw(&span) + } +} diff --git a/engine/UI/Label.go b/engine/UI/Label.go index f0a3c97..a1fdf00 100644 --- a/engine/UI/Label.go +++ b/engine/UI/Label.go @@ -2,11 +2,27 @@ package ui import rl "github.com/gen2brain/raylib-go/raylib" +var defaultLabelPadding float32 = 5 + +// If the text formation formula is applied without it the text is +// overlowing on the right due to rounding errors +const overflowMultiplyer float32 = 1.1 + +var defaultLabelStyle = &Style{ + BacgroundColor: &rl.LightGray, + FontColor: &rl.DarkGray, + Padding: &defaultLabelPadding, +} + type Label struct { - Text string + WidthWeight float32 + Text string + + Style Style } -func (base *Label) Init(Menu) { +func (base *Label) Init(*Menu) { + base.Style.FillMissing(defaultLabelStyle) } func (base *Label) Destroy() { @@ -17,5 +33,24 @@ func (base *Label) Update() { 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.DrawRectangleRec(*position, rl.Green) + rl.DrawRectangleRec(*position, *base.Style.BacgroundColor) + rl.DrawText(base.Text, + int32(position.X+*base.Style.Padding), + int32(position.Y+*base.Style.Padding), + base.getTextHeight(position), + *base.Style.FontColor) +} + +func (base *Label) GetOccupationWeight() float32 { + return base.WidthWeight +} + +func (base *Label) getTextHeight(position *rl.Rectangle) int32 { + var width = position.Width - 2 * *base.Style.Padding + var outp = position.Height - 2 * *base.Style.Padding + var maxWidthSize = rl.MeasureText(base.Text, int32(outp)) + if maxWidthSize > int32(width) { + outp /= (float32(maxWidthSize) / width) * overflowMultiplyer + } + return int32(outp) } diff --git a/engine/UI/Menu.go b/engine/UI/Menu.go index c376839..e046a0e 100644 --- a/engine/UI/Menu.go +++ b/engine/UI/Menu.go @@ -60,6 +60,10 @@ func (base *Menu) Create(manager *coreobjects.SceneManager) { base.manager = manager base.cache = &layoutCache{} base.generateLayout() + + for _, v := range base.Rows { + v.Init(base) + } } func (base *Menu) Destroy() { @@ -145,21 +149,3 @@ func (base *Menu) getWeightToPixelRatio() float32 { func (base *Menu) GetMousePosition() rl.Vector2 { return rl.GetMousePosition() } - -type GridRow struct { - // A portion of the screen the row will occupy - HeightWeight float32 - // The objects that lay in this row - Objects []UIElement - - location rl.Rectangle -} - -func (base *GridRow) Draw(span rl.Rectangle) { - // TODO: Add horizontal spacing - for _, v := range base.Objects { - v.Draw(&span) - } -} - - diff --git a/engine/UI/Style.go b/engine/UI/Style.go new file mode 100644 index 0000000..1901e4b --- /dev/null +++ b/engine/UI/Style.go @@ -0,0 +1,24 @@ +package ui + +import ( + rl "github.com/gen2brain/raylib-go/raylib" +) + +type Style struct { + BacgroundColor *rl.Color + FontColor *rl.Color + Padding *float32 +} + +// Fills up not specifiend elements with default ones +func (base *Style) FillMissing(defaultStyle *Style) { + if base.BacgroundColor == nil { + base.BacgroundColor = defaultLabelStyle.BacgroundColor + } + if base.Padding == nil { + base.Padding = defaultLabelStyle.Padding + } + if base.FontColor == nil { + base.FontColor = defaultLabelStyle.FontColor + } +} diff --git a/engine/UI/UIElement.go b/engine/UI/UIElement.go index 86475f7..b6f29ca 100644 --- a/engine/UI/UIElement.go +++ b/engine/UI/UIElement.go @@ -3,9 +3,11 @@ package ui import rl "github.com/gen2brain/raylib-go/raylib" type UIElement interface { - Init(Menu) + Init(*Menu) Destroy() Update() + // Gets the scale width of the element. Works similar to CSS's 'flex-grow' + GetOccupationWeight() float32 // Draw the element with the given size Draw(*rl.Rectangle) } diff --git a/engine/main.go b/engine/main.go index 50e0326..7df8cae 100644 --- a/engine/main.go +++ b/engine/main.go @@ -9,6 +9,7 @@ import ( render "github.com/DegustatorPonos/RuinesOfRafdolon/Render" settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings" ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" + rl "github.com/gen2brain/raylib-go/raylib" ) func main() { @@ -49,9 +50,14 @@ func menu_test() *ui.Menu { }, }, { - HeightWeight: 1, + HeightWeight: 2, Objects: []ui.UIElement { - &ui.Label{ Text: "Ruines of Rafdolon" }, + &ui.Label{ + Text: "Center div", + Style: ui.Style{ + BacgroundColor: &rl.Pink, + }, + }, }, }, { -- cgit v1.3