blob: 233b89f9ecf646e11868ef488840105deabe5b5a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
}
}
|