diff options
Diffstat (limited to 'engine/UI/Menu.go')
| -rw-r--r-- | engine/UI/Menu.go | 54 |
1 files changed, 14 insertions, 40 deletions
diff --git a/engine/UI/Menu.go b/engine/UI/Menu.go index e046a0e..8f9328f 100644 --- a/engine/UI/Menu.go +++ b/engine/UI/Menu.go @@ -16,46 +16,11 @@ type Menu struct { // Spaces between the rows Spacing float32 // A horizontal line of elements - Rows []*GridRow + Rows []UIElement 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{} @@ -81,7 +46,7 @@ func (base *Menu) Draw() { for i, v := range base.Rows { var verticalSpacing = base.cache.RowLocations[i] - v.Draw(rl.Rectangle{ + v.Draw(&rl.Rectangle{ X: base.cache.OffsetX, Y: verticalSpacing.X, Width: base.cache.Width, @@ -126,9 +91,18 @@ func (base *Menu) generateLayout() { X: float32(rl.GetScreenWidth()), Y: float32(rl.GetScreenHeight()), } - base.cache.CalculateOffsets(base.PaddingX, base.PaddingY) + base.cache.SpanResolution = rl.Vector2{ + X: float32(rl.GetScreenWidth()), + Y: float32(rl.GetScreenHeight()), + } + base.cache.CalculateOffsets( + float32(rl.GetScreenWidth()), + float32(rl.GetScreenHeight()), + base.PaddingX, + base.PaddingY) var weightToScale = base.getWeightToPixelRatio() - base.cache.CalculateRowsLocations(base.Rows, weightToScale, base.Spacing) + var spacingPx = base.cache.Width * base.Spacing + base.cache.CalculateRowsLocations(base.Rows, weightToScale, spacingPx, base.cache.OffsetY) } // Sums up all the weights @@ -136,7 +110,7 @@ func (base *Menu) getWeightToPixelRatio() float32 { var rowsAmount = len(base.Rows) var sum float32 = 0 for _, v := range base.Rows { - sum += v.HeightWeight + sum += v.GetOccupationWeight() } var spacingPixels = base.cache.ScreenResolution.Y * base.Spacing var spacing = float32(rowsAmount - 1) * spacingPixels |
