ChocolateBird/main.gd

90 lines
2.7 KiB
GDScript3
Raw Normal View History

extends Control
#warning-ignore-all:return_value_discarded
const globals = preload('res://globals.gd') # Make LSP shut up about non-const
const Menu = globals.Menu
var packed_menus = {}
var menus = {}
var active_menu_stack = []
func instantiate_menu_list(menu_list) -> void:
for key in menu_list:
packed_menus[key] = load(globals.MENUS[key][0])
menus[key] = packed_menus[key].instance()
2024-07-15 02:31:36 +09:30
if menus[key].has_signal('exit'):
menus[key].connect('exit', self, 'pop_menu')
var is_rom_loaded := false
func _on_rom_loaded() -> void:
instantiate_menu_list(globals.POST_ROM_MENUS)
menus[Menu.DEBUG].connect('button_pressed', self, 'push_menu')
is_rom_loaded = true
var is_savefile_loaded := false
func _on_savefile_loaded(dicts) -> void:
push_menu(Menu.LOAD_SAVE)
menus[Menu.LOAD_SAVE].set_data(dicts)
menus[Menu.LOAD_SAVE].connect('slot_activated', self, '_on_saveslot_loaded')
func _on_saveslot_loaded(dict) -> void:
push_menu(Menu.PARTY)
menus[Menu.PARTY].update_labels(dict)
func _ready() -> void:
if OS.get_name() != 'HTML5' or !OS.has_feature('JavaScript'):
packed_menus[Menu.LOADER] = preload('res://widgets/FileSelect.tscn')
else:
packed_menus[Menu.LOADER] = preload('res://widgets/FileSelectWeb.tscn')
menus[Menu.LOADER] = packed_menus[Menu.LOADER].instance()
push_menu(Menu.LOADER)
instantiate_menu_list(globals.INITIAL_MENUS)
RomLoader.connect('rom_loaded', self, '_on_rom_loaded')
menus[Menu.LOADER].connect('debug_pressed', self, 'push_debug_menu')
menus[Menu.LOADER].connect('savefile_loaded', self, '_on_savefile_loaded')
Common.update_window_scale()
func push_debug_menu():
push_menu(Menu.DEBUG)
func push_menu(menu_type):
if len(active_menu_stack) > 0:
if active_menu_stack[-1] == menu_type:
return
remove_child(menus[active_menu_stack[-1]])
active_menu_stack.append(menu_type)
self._present_menu(menus[menu_type])
func pop_menu():
if len(active_menu_stack) > 0:
remove_child(menus[active_menu_stack.pop_back()])
if len(active_menu_stack) > 0:
self._present_menu(menus[active_menu_stack[-1]])
else:
push_menu(Menu.LOADER)
func _present_menu(menu) -> void:
add_child(menu)
if menu.has_method('_on_menu_presented'):
menu._on_menu_presented()
func _input(event: InputEvent) -> void:
if event is InputEventKey and event.pressed:
match event.scancode:
KEY_BACKSPACE:
pop_menu()
KEY_D:
if is_rom_loaded:
push_menu(Menu.DEBUG)
2024-07-26 22:11:04 +09:30
func _notification(what) -> void:
match what:
NOTIFICATION_WM_GO_BACK_REQUEST: # Android back button/gesture
if len(active_menu_stack) < 2:
print('main received NOTIFICATION_WM_GO_BACK_REQUEST, quitting')
get_tree().call_deferred('quit')
else:
print('main received NOTIFICATION_WM_GO_BACK_REQUEST, popping menu')
call_deferred('pop_menu')