-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu_manager.go
166 lines (149 loc) · 3.65 KB
/
menu_manager.go
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
166
package glmenu
import (
"errors"
"fmt"
"github.com/4ydx/gltext/v4.1"
"github.com/go-gl/glfw/v3.2/glfw"
)
type MenuManager struct {
Font *v41.Font
StartKey glfw.Key // they key that, when pressed, will display the StartMenu
StartMenu string // the name passed to each NewMenu call
Menus map[string]*Menu
IsFinalized bool
}
// Finalize connects menus together and performs final formatting steps
// this must be run after all menus are prepared
func (mm *MenuManager) Finalize(align Alignment) error {
if mm.IsFinalized {
return errors.New("Menus have already been finalized")
}
for _, menu := range mm.Menus {
menu.Finalize(align)
for _, label := range menu.Labels {
if label.Config.Action == GOTO_MENU {
gotoMenu, ok := mm.Menus[label.Config.Goto]
if ok {
func(from *Menu, to *Menu, l *Label) {
l.onRelease = func(xPos, yPos float64, button MouseClick, inBox bool) {
if inBox {
from.Hide()
to.Show()
}
}
}(menu, gotoMenu, label)
}
}
}
}
mm.IsFinalized = true
return nil
}
func (mm *MenuManager) IsVisible() bool {
for _, menu := range mm.Menus {
if menu.IsVisible {
return true
}
}
return false
}
// Clicked resolves menus that have been clicked
func (mm *MenuManager) MouseClick(xPos, yPos float64, button MouseClick) {
for _, menu := range mm.Menus {
if menu.IsVisible {
menu.MouseClick(xPos, yPos, button)
return
}
}
}
func (mm *MenuManager) MouseRelease(xPos, yPos float64, button MouseClick) {
for _, menu := range mm.Menus {
if menu.IsVisible {
menu.MouseRelease(xPos, yPos, button)
return
}
}
}
func (mm *MenuManager) MouseHover(xPos, yPos float64) {
for _, menu := range mm.Menus {
if menu.IsVisible {
menu.MouseHover(xPos, yPos)
return
}
}
}
func (mm *MenuManager) KeyRelease(key glfw.Key, withShift bool) {
for _, menu := range mm.Menus {
if menu.IsVisible {
menu.KeyRelease(key, withShift)
return
}
}
}
func (mm *MenuManager) Draw() bool {
for _, menu := range mm.Menus {
if menu.IsVisible {
if menu.OnComplete != nil {
menu.OnComplete()
}
return menu.Draw()
}
}
return false
}
func (mm *MenuManager) Release() {
for _, menu := range mm.Menus {
menu.Release()
}
}
func (mm *MenuManager) NewMenu(window *glfw.Window, name string, menuDefaults MenuDefaults, screenPosition ScreenPosition) (*Menu, error) {
m, err := NewMenu(window, name, mm.Font, menuDefaults, screenPosition)
if err != nil {
return nil, err
}
m.MenuManager = mm
if _, ok := mm.Menus[name]; ok {
return nil, errors.New(fmt.Sprintf("The named menu %s already exists.", name))
}
mm.Menus[name] = m
return m, nil
}
func (mm *MenuManager) Hide() {
for _, m := range mm.Menus {
m.Hide()
}
}
func (mm *MenuManager) Show(name string) error {
m, ok := mm.Menus[name]
if !ok {
return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
}
m.Show()
return nil
}
func (mm *MenuManager) Toggle(name string) error {
m, ok := mm.Menus[name]
if !ok {
return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
}
m.Toggle()
return nil
}
func (mm *MenuManager) SetText(name string, index int, text string) error {
m, ok := mm.Menus[name]
if !ok {
return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
}
for i, l := range m.Labels {
if i == index {
l.Text.SetString(text)
}
}
return nil
}
// NewMenuManager handles a tree of menus that interact with one another
func NewMenuManager(font *v41.Font, startKey glfw.Key, startMenu string) *MenuManager {
mm := &MenuManager{Font: font, StartKey: startKey, StartMenu: startMenu}
mm.Menus = make(map[string]*Menu)
return mm
}