summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/UI/Menu.go73
-rw-r--r--engine/main.go56
2 files changed, 51 insertions, 78 deletions
diff --git a/engine/UI/Menu.go b/engine/UI/Menu.go
index 8f9328f..fa3888b 100644
--- a/engine/UI/Menu.go
+++ b/engine/UI/Menu.go
@@ -13,10 +13,8 @@ type Menu struct {
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 []UIElement
+ // Base element
+ Contents UIElement
cache *layoutCache
}
@@ -24,11 +22,7 @@ type Menu struct {
func (base *Menu) Create(manager *coreobjects.SceneManager) {
base.manager = manager
base.cache = &layoutCache{}
- base.generateLayout()
-
- for _, v := range base.Rows {
- v.Init(base)
- }
+ base.Contents.Init(base)
}
func (base *Menu) Destroy() {
@@ -38,21 +32,20 @@ func (base *Menu) Update() {
}
func (base *Menu) Draw() {
+ if !base.cache.IsValid() {
+ base.RecalculateCache()
+ }
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,
- })
+ var span = rl.Rectangle {
+ X: base.cache.OffsetX,
+ Y: base.cache.OffsetY,
+ Width: base.cache.Width,
+ Height: base.cache.Height,
}
+ base.Contents.Draw(&span)
}
func (base *Menu) drawPaddings() {
@@ -80,46 +73,22 @@ func (base *Menu) drawPaddings() {
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
- }
+func (base *Menu) GetMousePosition() rl.Vector2 {
+ return rl.GetMousePosition()
+}
- base.cache.ScreenResolution = rl.Vector2{
+func (base *Menu) RecalculateCache() {
+ base.cache.ScreenResolution = rl.Vector2 {
X: float32(rl.GetScreenWidth()),
Y: float32(rl.GetScreenHeight()),
}
- base.cache.SpanResolution = rl.Vector2{
+ 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.cache.SpanResolution.X,
+ base.cache.SpanResolution.Y,
+ base.PaddingX,
base.PaddingY)
- var weightToScale = base.getWeightToPixelRatio()
- var spacingPx = base.cache.Width * base.Spacing
- base.cache.CalculateRowsLocations(base.Rows, weightToScale, spacingPx, base.cache.OffsetY)
-}
-
-// 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.GetOccupationWeight()
- }
- 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()
}
diff --git a/engine/main.go b/engine/main.go
index 9a8030a..de8ff34 100644
--- a/engine/main.go
+++ b/engine/main.go
@@ -38,41 +38,45 @@ func main() {
}
func menu_test() *ui.Menu {
- return &ui.Menu{
+ return &ui.Menu {
PaddingX: 0.05,
PaddingY: 0.1,
- Spacing: 0.025,
- Rows: []ui.UIElement {
- &ui.GridRow {
- HeightWeight: 1,
- Spacing: 0.01,
- Objects: []ui.UIElement {
- &ui.Label{ Text: "First piece" },
- &ui.Label{ Text: "Wide piece", WidthWeight: 2 },
- &ui.Label{ Text: "Another piece" },
+ Contents: &ui.GridColumn{
+ Spacing: 0.025,
+ Objects: []ui.UIElement {
+ &ui.GridRow {
+ HeightWeight: 1,
+ Spacing: 0.01,
+ Objects: []ui.UIElement {
+ &ui.Label{ Text: "First piece" },
+ &ui.Label{ Text: "Wide piece", WidthWeight: 2 },
+ &ui.Label{ Text: "Another piece" },
+ },
},
- },
- &ui.GridRow {
- HeightWeight: 2,
- Objects: []ui.UIElement {
- &ui.Button {
- EventType: ui.OnClick,
- DisplayElement: &ui.Label {
- Text: "Center button",
- Alignment: ui.Center,
- Style: ui.Style {
- BacgroundColor: &rl.Pink,
+ &ui.GridRow {
+ HeightWeight: 2,
+ Objects: []ui.UIElement {
+ &ui.Button {
+ EventType: ui.OnClick,
+ DisplayElement: &ui.Label {
+ Text: "Center button",
+ Alignment: ui.Center,
+ Style: ui.Style {
+ BacgroundColor: &rl.Pink,
+ },
},
},
},
},
- },
- &ui.GridRow {
- HeightWeight: 1,
- Objects: []ui.UIElement {
- &ui.Label{ Text: "A very very long text that is bigger than a window" },
+ &ui.GridRow {
+ HeightWeight: 1,
+ Objects: []ui.UIElement {
+ &ui.Label{ Text: "A very very long text that is bigger than a window" },
+ },
},
},
},
+ // Rows: []ui.UIElement {
+ // },
}
}