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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package ui
import (
"encoding/json"
"fmt"
"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 {
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 totalSpacing < 0 {
totalSpacing = 0
}
// rl.TraceLog(rl.LogInfo, "SpacingPx %v \t Total spacing: %v", spacingPx, totalSpacing)
return (totalSpace - totalSpacing) / totalWeights
}
// A list of values that are used in the span calculations
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())
}
// Fills in the offsets and width/height of a layout
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
}
// Fills in the RowLocations field of the cache.
// Requires rows, WeightToPixels multiplyer, spacing (in px) and base offset (in px) that will be used as a start of a span.
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
}
}
func (base layoutCache) String() string {
var outp, jsonErr = json.Marshal(base)
if jsonErr != nil {
return fmt.Sprintf("Failed to parse layour cache: %s", jsonErr.Error())
}
return string(outp)
}
|