blob: 58f35885907d8e13621f3a7569d574ef7f26c878 (
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
|
extends Control
class_name MenuManager
@onready var menus = {
"main": $MainMenu,
"credits": $CreditsMenu,
"settings": $SettingsMenu
}
@onready var transition = $SceneTransition
@onready var hover_sound = $Hover
@onready var click_sound = $Click
var menu_stack = ["main"]
func _ready():
get_viewport().gui_focus_changed.connect(play_hover_maybe)
Global.focus_first_button(menus[menu_stack.back()])
for m in menus.values():
connect_button_sounds(m)
if Global.fade_next:
Global.fade_next = false
transition.fade_in()
func _input(_event):
if Input.is_action_just_pressed("ui_cancel") && menu_stack.size() > 1:
play_click()
go_back()
func goto(menu_name: String):
show_menu(menu_name)
menu_stack.push_back(menu_name)
print("Go to called. Stack: " + str(menu_stack))
func go_back():
menu_stack.pop_back()
if menu_stack.is_empty():
Global.showError("Menu stack empty")
show_menu(menu_stack.back())
print("Go back called. Stack: " + str(menu_stack))
func show_menu(menu_name: String):
await transition.fade_out()
for k in menus.keys():
if k == menu_name:
menus[k].show()
if menus[k].has_method("prepare"):
menus[k].prepare() # Optionally run some code
Global.focus_first_button(menus[k])
else:
menus[k].hide()
await transition.fade_in()
func connect_button_sounds(node: Node):
if node is Button:
node.pressed.connect(play_click)
if node is Button or node is LineEdit or node is Slider:
node.mouse_entered.connect(play_hover)
for c in node.get_children():
connect_button_sounds(c)
func play_click():
click_sound.play()
func play_hover():
hover_sound.play()
func play_hover_maybe(element):
if Global.focus_auto_changed:
Global.focus_auto_changed = false
return
if element is Button:
if element.is_hovered():
return
play_hover()
|