aboutsummaryrefslogtreecommitdiff
path: root/client/menu/menu_manager.gd
blob: bba074f670b1e03ef2349de97f790b35dd80e696 (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
# Undercooked - a game about cooking
# Copyright 2024 metamuffin
# Copyright 2024 tpart
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Control
class_name MenuManager

@onready var menus = {
	"main": $MainMenu,
	"credits": $CreditsMenu,
	"settings": $SettingsMenu
}
@onready var transition: SceneTransition = $SceneTransition

var menu_stack = ["main"]

func _ready():
	if not Global.settings["setup_complete"]["value"]: return transition.instant_to("res://menu/setup_menu.tscn")
	get_viewport().gui_focus_changed.connect(Sound.play_hover_maybe)
	Global.focus_first_button(menus[menu_stack.back()])
	
	for m in menus.values():
		Global.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:
		Sound.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()