package ui import ( coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects" settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings" rl "github.com/gen2brain/raylib-go/raylib" ) // Scene implimetation that contains aligned UI elements type Menu struct { manager *coreobjects.SceneManager // The space between the window border and the grid. Measured in fractions of a screen. [0; 1] PaddingX float32 // The space between the window border and the grid. Measured in fractions of a screen. [0; 1] PaddingY float32 // Spaces between the rows Spacing float32 // A horizontal line of elements Rows []*GridRow cache *layoutCache } // The pixel scalin type layoutCache struct { // The resolution the cache was calculated for ScreenResolution rl.Vector2 // Horizontal offset in pixels OffsetX float32 // Vertical offset in pixels OffsetY float32 // The Y size of a row Width float32 // The horizontal start and end of each row. // Index of an array is representive with row index RowLocations []rl.Vector2 } func (base *layoutCache) CalculateOffsets(paddingX float32, paddingY float32) { base.OffsetX = (base.ScreenResolution.X * paddingX) base.OffsetY = (base.ScreenResolution.Y * paddingY) base.Width = base.ScreenResolution.X - base.OffsetX * 2 } func (base *layoutCache) CalculateRowsLocations (rows []*GridRow, WeightToPixels float32, spacing float32) { var spacingPixels = (base.ScreenResolution.Y * spacing) base.RowLocations = make([]rl.Vector2, len(rows)) var y float32 = base.OffsetY for i, v := range rows { var rowHeight = (WeightToPixels * v.HeightWeight) base.RowLocations[i] = rl.Vector2 { X: y, Y: rowHeight, } y += rowHeight + spacingPixels } } 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() { } func (base *Menu) Update() { } func (base *Menu) Draw() { rl.ClearBackground(rl.Black) base.generateLayout() if settings.State.DrawColliders { base.drawPaddings() } for i, v := range base.Rows { var verticalSpacing = base.cache.RowLocations[i] v.Draw(rl.Rectangle{ X: base.cache.OffsetX, Y: verticalSpacing.X, Width: base.cache.Width, Height: verticalSpacing.Y, }) } } func (base *Menu) drawPaddings() { // Horizontal rl.DrawRectangle(0, 0, int32(base.cache.ScreenResolution.X), int32(base.cache.OffsetY), rl.Red) rl.DrawRectangle(0, int32(base.cache.ScreenResolution.Y) - int32(base.cache.OffsetY), int32(base.cache.ScreenResolution.X), int32(base.cache.OffsetY), rl.Red) // Vertical rl.DrawRectangle(0, int32(base.cache.OffsetY), int32(base.cache.OffsetX), int32(base.cache.ScreenResolution.Y - base.cache.OffsetY * 2), rl.Blue) rl.DrawRectangle( int32(base.cache.ScreenResolution.X - base.cache.OffsetX), int32(base.cache.OffsetY), int32(base.cache.OffsetX), int32(base.cache.ScreenResolution.Y - base.cache.OffsetY * 2), rl.Blue) } // Generates all the offsets and scales for the rendering func (base *Menu) generateLayout() { if rl.GetScreenWidth() == int(base.cache.ScreenResolution.X) && rl.GetScreenHeight() == int(base.cache.ScreenResolution.Y) { return } base.cache.ScreenResolution = rl.Vector2{ X: float32(rl.GetScreenWidth()), Y: float32(rl.GetScreenHeight()), } base.cache.CalculateOffsets(base.PaddingX, base.PaddingY) var weightToScale = base.getWeightToPixelRatio() base.cache.CalculateRowsLocations(base.Rows, weightToScale, base.Spacing) } // Sums up all the weights func (base *Menu) getWeightToPixelRatio() float32 { var rowsAmount = len(base.Rows) var sum float32 = 0 for _, v := range base.Rows { sum += v.HeightWeight } var spacingPixels = base.cache.ScreenResolution.Y * base.Spacing var spacing = float32(rowsAmount - 1) * spacingPixels if spacing < 0 { spacing = 0 } return (base.cache.ScreenResolution.Y - 2 * base.cache.OffsetY - spacing) / sum } func (base *Menu) GetMousePosition() rl.Vector2 { return rl.GetMousePosition() }