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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package ui
import (
coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
rl "github.com/gen2brain/raylib-go/raylib"
)
// Scene implimetation that contains aligned UI elements
type Menu struct {
manager *coreobjects.SceneManager
// The space between the window border and the grid. Measured in fractions of a screen. [0; 1]
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
cache *layoutCache
}
func (base *Menu) Create(manager *coreobjects.SceneManager) {
base.manager = manager
base.cache = &layoutCache{}
base.generateLayout()
for _, v := range base.Rows {
v.Init(base)
}
}
func (base *Menu) Destroy() {
}
func (base *Menu) Update() {
}
func (base *Menu) Draw() {
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,
})
}
}
func (base *Menu) drawPaddings() {
// Horizontal
rl.DrawRectangle(0, 0,
int32(base.cache.ScreenResolution.X),
int32(base.cache.OffsetY),
rl.Red)
rl.DrawRectangle(0,
int32(base.cache.ScreenResolution.Y) - int32(base.cache.OffsetY),
int32(base.cache.ScreenResolution.X),
int32(base.cache.OffsetY),
rl.Red)
// Vertical
rl.DrawRectangle(0,
int32(base.cache.OffsetY),
int32(base.cache.OffsetX),
int32(base.cache.ScreenResolution.Y - base.cache.OffsetY * 2),
rl.Blue)
rl.DrawRectangle(
int32(base.cache.ScreenResolution.X - base.cache.OffsetX),
int32(base.cache.OffsetY),
int32(base.cache.OffsetX),
int32(base.cache.ScreenResolution.Y - base.cache.OffsetY * 2),
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
}
base.cache.ScreenResolution = rl.Vector2{
X: float32(rl.GetScreenWidth()),
Y: float32(rl.GetScreenHeight()),
}
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.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()
}
|