summaryrefslogtreecommitdiff
path: root/engine/UI/Menu.go
blob: c376839f2174332761bb3447b961451308fafcc6 (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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 []*GridRow

	cache *layoutCache
}

// The pixel scalin
type layoutCache struct {
	// The resolution the cache was calculated for
	ScreenResolution rl.Vector2
	// Horizontal offset in pixels
	OffsetX float32
	// Vertical offset in pixels
	OffsetY float32
	// The Y size of a row
	Width float32
	// The horizontal start and end of each row. 
	// Index of an array is representive with row index
	RowLocations []rl.Vector2
}

func (base *layoutCache) CalculateOffsets(paddingX float32, paddingY float32) {
	base.OffsetX = (base.ScreenResolution.X * paddingX)
	base.OffsetY = (base.ScreenResolution.Y * paddingY)
	base.Width = base.ScreenResolution.X - base.OffsetX * 2
}

func (base *layoutCache) CalculateRowsLocations (rows []*GridRow, WeightToPixels float32, spacing float32) {
	var spacingPixels = (base.ScreenResolution.Y * spacing)
	base.RowLocations = make([]rl.Vector2, len(rows))
	var y float32 = base.OffsetY
	for i, v := range rows {
		var rowHeight = (WeightToPixels * v.HeightWeight)
		base.RowLocations[i] = rl.Vector2 {
			X: y,
			Y: rowHeight,
		}
		y += rowHeight + spacingPixels
	}
}

func (base *Menu) Create(manager *coreobjects.SceneManager) {
	base.manager = manager
	base.cache = &layoutCache{}
	base.generateLayout()
}

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.CalculateOffsets(base.PaddingX, base.PaddingY)
	var weightToScale = base.getWeightToPixelRatio()
	base.cache.CalculateRowsLocations(base.Rows, weightToScale, base.Spacing)
}

// 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.HeightWeight
	}
	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()
}

type GridRow struct {
	// A portion of the screen the row will occupy
	HeightWeight float32
	// The objects that lay in this row
	Objects []UIElement

	location rl.Rectangle
}

func (base *GridRow) Draw(span rl.Rectangle) {
	// TODO: Add horizontal spacing
	for _, v := range base.Objects {
		v.Draw(&span)
	}
}