summaryrefslogtreecommitdiff
path: root/engine/UI
diff options
context:
space:
mode:
authorPhyscik <mynameisgennadiy@vk.com>2026-02-02 13:53:39 +0500
committerPhyscik <mynameisgennadiy@vk.com>2026-02-02 13:53:39 +0500
commit5f62643b0dfa6e33177116082acd47c1a93a9940 (patch)
tree838e3674dfe9aede2a6af70e31d937d58ae8ce92 /engine/UI
parent7d0b734ddf915d2f585d972a347c5203e8ef2ba7 (diff)
Flex column
Diffstat (limited to 'engine/UI')
-rw-r--r--engine/UI/FlexElement.go16
-rw-r--r--engine/UI/GridColumn.go59
-rw-r--r--engine/UI/GridRow.go12
3 files changed, 69 insertions, 18 deletions
diff --git a/engine/UI/FlexElement.go b/engine/UI/FlexElement.go
index 233b89f..b8d1b38 100644
--- a/engine/UI/FlexElement.go
+++ b/engine/UI/FlexElement.go
@@ -1,6 +1,20 @@
package ui
-import rl "github.com/gen2brain/raylib-go/raylib"
+import (
+ "image/color"
+
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+var defaultGridPadding float32 = 0
+var defaultGridRoundness float32 = 0
+
+var defaultGridStyle = &Style {
+ BacgroundColor: &color.RGBA{0, 0, 0, 0},
+ FontColor: &rl.DarkGray,
+ Padding: &defaultGridPadding,
+ Roundness: &defaultGridRoundness,
+}
// A dynamically shrinking element that contains other elements
type flexElement interface {
diff --git a/engine/UI/GridColumn.go b/engine/UI/GridColumn.go
index 02d441f..359d0d4 100644
--- a/engine/UI/GridColumn.go
+++ b/engine/UI/GridColumn.go
@@ -3,14 +3,27 @@ package ui
import rl "github.com/gen2brain/raylib-go/raylib"
type GridColumn struct {
- Weight float32
+ // A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow'
+ WidthWeight float32
+ // The objects that lay in this row
Spacing float32
- Elements []UIElement
+ // Spaces between the columns
+ Objects []UIElement
+ // Styling preferences
+ Style *Style
+ location rl.Rectangle
cache layoutCache
}
-func (base *GridColumn) Init(*Menu) {
+func (base *GridColumn) Init(parent *Menu) {
+ base.Style = InitStyle(base.Style, defaultGridStyle)
+ for _, v := range base.Objects {
+ v.Init(parent)
+ }
+ base.cache = layoutCache {
+ ScreenResolution: rl.Vector2{ X:0, Y:0 },
+ }
}
func (base *GridColumn) Destroy() {
@@ -18,9 +31,45 @@ 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
+ return base.WidthWeight
}
// Draw the element with the given size
-func (base *GridColumn) Draw(*rl.Rectangle) {
+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]
+ 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
}
diff --git a/engine/UI/GridRow.go b/engine/UI/GridRow.go
index fed6051..3139d97 100644
--- a/engine/UI/GridRow.go
+++ b/engine/UI/GridRow.go
@@ -1,21 +1,9 @@
package ui
import (
- "image/color"
-
rl "github.com/gen2brain/raylib-go/raylib"
)
-var defaultGridPadding float32 = 0
-var defaultGridRoundness float32 = 0
-
-var defaultGridStyle = &Style {
- BacgroundColor: &color.RGBA{0, 0, 0, 0},
- FontColor: &rl.DarkGray,
- Padding: &defaultGridPadding,
- Roundness: &defaultGridRoundness,
-}
-
type GridRow struct {
// A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow'
HeightWeight float32