summaryrefslogtreecommitdiff
path: root/engine/UI/FlexElement.go
diff options
context:
space:
mode:
authorPhyscik <mynameisgennadiy@vk.com>2026-02-02 02:00:29 +0500
committerPhyscik <mynameisgennadiy@vk.com>2026-02-02 02:00:29 +0500
commit7383f73a5b1589d3257b44407e46adfe987e2530 (patch)
tree8848132dea8f270df9d38ad508792b333b16aae2 /engine/UI/FlexElement.go
parentb4bb2cc1de2004153a1357e2e6678df7377b42e3 (diff)
Span system rework pt 1
Diffstat (limited to 'engine/UI/FlexElement.go')
-rw-r--r--engine/UI/FlexElement.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/engine/UI/FlexElement.go b/engine/UI/FlexElement.go
new file mode 100644
index 0000000..233b89f
--- /dev/null
+++ b/engine/UI/FlexElement.go
@@ -0,0 +1,70 @@
+package ui
+
+import rl "github.com/gen2brain/raylib-go/raylib"
+
+// A dynamically shrinking element that contains other elements
+type flexElement interface {
+ GetElements() []UIElement
+ GetSpacing() float32
+ RecalculateCache(*rl.Rectangle)
+}
+
+// Calculates the element weight to pixel ratio
+func getWeightToPixelsRatio(base flexElement, totalSpace float32) float32 {
+ var elements = base.GetElements()
+ var totalWeights float32 = 0
+ for _, v := range elements {
+ totalWeights += v.GetOccupationWeight()
+ }
+ var spacingPx = totalSpace * base.GetSpacing()
+ var totalSpacing = float32(len(elements) - 1) * spacingPx
+ if totalSpace < 0 {
+ totalSpace = 0
+ }
+ return (totalSpace - totalSpacing) / totalWeights
+}
+
+
+type layoutCache struct {
+ // The screen resolution the cache was calculated for
+ ScreenResolution rl.Vector2
+ // The resolution the cache was calculated for
+ SpanResolution rl.Vector2
+ // Horizontal offset in pixels
+ OffsetX float32
+ // Vertical offset in pixels
+ OffsetY float32
+ // The X size of a row
+ Width float32
+ // The Y size of a row
+ Height float32
+ // The horizontal start and end of each row.
+ // Index of an array is representive with row index
+ RowLocations []rl.Vector2
+}
+
+func (base layoutCache) IsValid() bool {
+ return base.ScreenResolution.X == float32(rl.GetScreenWidth()) &&
+ base.ScreenResolution.Y == float32(rl.GetScreenHeight())
+}
+
+func (base *layoutCache) CalculateOffsets(X float32, Y float32, paddingX float32, paddingY float32) {
+ base.OffsetX = (X * paddingX)
+ base.OffsetY = (Y * paddingY)
+ base.Width = X - base.OffsetX * 2
+ base.Height = Y - base.OffsetY * 2
+}
+
+func (base *layoutCache) CalculateRowsLocations (rows []UIElement, WeightToPixels float32, spacing float32, baseOffset float32) {
+ base.RowLocations = make([]rl.Vector2, len(rows))
+ var ptr float32 = baseOffset
+ for i, v := range rows {
+ // The spacing should be accounted for in the WeightToPixels param
+ var elemSpace = (WeightToPixels * v.GetOccupationWeight())
+ base.RowLocations[i] = rl.Vector2 {
+ X: ptr,
+ Y: elemSpace,
+ }
+ ptr += elemSpace + spacing
+ }
+}