summaryrefslogtreecommitdiff
path: root/engine/UI/Menu.go
blob: 32e0dcce82ab8ef2796de72a94d9d61e35f42278 (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
package ui

import (
	"encoding/json"
	"fmt"

	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
	// Base element
	Contents UIElement

	cache *layoutCache
}

func (base *Menu) Create(manager *coreobjects.SceneManager) {
	base.manager = manager
	base.cache = &layoutCache{}
	base.Contents.Init(base)
	// rl.TraceLog(rl.LogInfo, "Parsed: %s", base)
}

func (base *Menu) Destroy() {
}

func (base *Menu) Update() {
}

func (base *Menu) Draw() {
	if !base.cache.IsValid() {
		base.RecalculateCache()
	}
	rl.ClearBackground(rl.Black)
	if settings.State.DrawColliders {
		base.drawPaddings()
	}
	var span = rl.Rectangle {
		X: base.cache.OffsetX,
		Y: base.cache.OffsetY,
		Width: base.cache.Width,
		Height: base.cache.Height,
	}
	base.Contents.Draw(&span)
}

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) 
}

func (base *Menu) GetMousePosition() rl.Vector2 {
	return rl.GetMousePosition()
}

func (base *Menu) RecalculateCache() {
	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(
		base.cache.SpanResolution.X, 
		base.cache.SpanResolution.Y, 
		base.PaddingX,
		base.PaddingY)
}

func (base Menu) String() string {
	var outp, jsonErr = json.MarshalIndent(base, "", " ")
	if jsonErr != nil {
		return fmt.Sprintf("Failed to parse: %s", jsonErr.Error())
	}
	return string(outp)
}