package ui import rl "github.com/gen2brain/raylib-go/raylib" const GridColumnTypeName string = "gridcolumn" type GridColumn struct { // A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow' Weight float32 // Spaces between the columns Objects []UIElement // The objects that lay in this row Spacing float32 // Styling preferences Style Style `json:"-"` location rl.Rectangle cache layoutCache } func (base *GridColumn) Init(parent *Menu) { base.Style.FillMissing(defaultGridStyle) for _, v := range base.Objects { v.Init(parent) } base.cache = layoutCache { ScreenResolution: rl.Vector2{ X:0, Y:0 }, } } func (base *GridColumn) Destroy() { } // Gets the scale width of the element. Works similar to CSS's 'flex-grow' func (base *GridColumn) GetOccupationWeight() float32 { return base.Weight } // Draw the element with the given size func (base *GridColumn) Draw(span *rl.Rectangle) { if !base.cache.IsValid() { base.RecalculateCache(span) } for i, v := range base.Objects { var offset = base.cache.RowLocations[i] // rl.TraceLog(rl.LogInfo, "Cache: %v", base.cache) var elemSpan = rl.Rectangle { X: span.X + *base.Style.Padding, Y: offset.X, Width: base.cache.Width, Height: offset.Y, } v.Draw(&elemSpan) } } func (base *GridColumn) RecalculateCache(span *rl.Rectangle) { base.cache.SpanResolution = rl.Vector2{ X: span.Width, Y: span.Height, } base.cache.ScreenResolution = rl.Vector2 { X: float32(rl.GetScreenWidth()), Y: float32(rl.GetScreenHeight()), } base.cache.CalculateOffsets(span.Width, span.Height, *base.Style.Padding, *base.Style.Padding) var weightToScale = getWeightToPixelsRatio(base, base.cache.Height) var spacingPx = base.Spacing * span.Height base.cache.CalculateRowsLocations(base.Objects, weightToScale, spacingPx, span.Y) } func (base *GridColumn) GetElements() []UIElement { return base.Objects } func (base *GridColumn) GetSpacing() float32 { return base.Spacing }