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/Label.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'engine/UI/Label.go') 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) } -- cgit v1.3