117 lines
4.1 KiB
GDScript
117 lines
4.1 KiB
GDScript
extends CanvasLayer
|
|
|
|
const MODAL := preload("res://scripts/ui/menu_modal.gd")
|
|
var _dialog: CanvasLayer
|
|
var _status: Label
|
|
var _previous_pause := false
|
|
var _options: CanvasLayer
|
|
var _needs_refresh := false
|
|
|
|
func _ready() -> void:
|
|
process_mode = Node.PROCESS_MODE_ALWAYS
|
|
layer = 120
|
|
GameSettings.settings_changed.connect(_refresh_language)
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventKey and event.pressed and not event.echo:
|
|
if event.keycode == KEY_F6 and not is_instance_valid(_options):
|
|
if not is_instance_valid(_dialog): _open()
|
|
_open_save_browser()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
if event is InputEventKey and event.pressed and not event.echo and event.keycode == KEY_ESCAPE:
|
|
if not is_instance_valid(_dialog) and not is_instance_valid(_options):
|
|
_open()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
func _open() -> void:
|
|
if is_instance_valid(_dialog): return
|
|
_previous_pause = get_tree().paused
|
|
get_tree().paused = true
|
|
_dialog = CanvasLayer.new()
|
|
_dialog.set_script(MODAL)
|
|
_dialog.heading = "Pauza"
|
|
_dialog.subtitle = "Twoja wyprawa czeka. Wybierz, co chcesz zrobić."
|
|
_dialog.marker = "ŁĄCZE / MENU POKŁADOWE"
|
|
_dialog.preferred_size = Vector2(590,650)
|
|
_dialog.connect("closed",_on_dialog_closed)
|
|
add_child(_dialog)
|
|
var explanation := MODAL.label(_dialog.body,tr("Zapis przywraca początek bieżącego poziomu. Cooldowny i efekty czasowe są zerowane."),12,MODAL.MUTED)
|
|
explanation.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
MODAL.button(_dialog.body,tr("Zapisz grę"),_open_save_browser,true)
|
|
MODAL.button(_dialog.body,tr("Wczytaj grę"),_open_load_browser)
|
|
MODAL.button(_dialog.body,tr("OPCJE"),_open_options)
|
|
MODAL.button(_dialog.body,tr("Zapisz i wróć do menu"),_main_menu)
|
|
MODAL.button(_dialog.body,tr("Zapisz i wyjdź"),_quit)
|
|
_status = MODAL.label(_dialog.body,"",12,MODAL.GOLD)
|
|
_status.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
MODAL.button(_dialog.footer,tr("Wróć do gry"),_close,true)
|
|
|
|
func _save() -> bool:
|
|
var error := GameState.save_game()
|
|
if is_instance_valid(_status):
|
|
_status.text = tr("Zapisano grę.") if error == OK else tr("Nie udało się zapisać gry.")+" "+error_string(error)
|
|
return error == OK
|
|
|
|
func _open_options() -> void:
|
|
_open_child(preload("res://scripts/ui/options_dialog.gd"))
|
|
|
|
func _open_child(script: Script, save_mode: bool = false) -> CanvasLayer:
|
|
if is_instance_valid(_options): return null
|
|
_dialog.hide()
|
|
_options = CanvasLayer.new()
|
|
_options.set_script(script)
|
|
if script == preload("res://scripts/ui/save_browser.gd"): _options.save_mode = save_mode
|
|
_options.connect("closed",_restore_pause)
|
|
add_child(_options)
|
|
return _options
|
|
|
|
func _open_save_browser(after_saved: Callable = Callable()) -> void:
|
|
var browser := _open_child(preload("res://scripts/ui/save_browser.gd"),true)
|
|
if browser == null: return
|
|
browser.connect("saved",func() -> void:
|
|
if is_instance_valid(_status): _status.text = tr("Zapisano grę.")
|
|
if after_saved.is_valid(): after_saved.call())
|
|
|
|
func _open_load_browser() -> void:
|
|
var browser := _open_child(preload("res://scripts/ui/save_browser.gd"))
|
|
if browser == null: return
|
|
browser.connect("load_requested",func(id: String) -> void:
|
|
if not GameState.load_game(id):
|
|
browser._status.text = tr("Brak poprawnego zapisu gry.")
|
|
browser._status.show()
|
|
return
|
|
get_tree().paused = false
|
|
get_tree().reload_current_scene())
|
|
|
|
func _restore_pause() -> void:
|
|
_options = null
|
|
if not is_inside_tree(): return
|
|
if _needs_refresh and is_instance_valid(_dialog):
|
|
_needs_refresh = false
|
|
_dialog.queue_free()
|
|
_dialog = null
|
|
get_tree().paused = _previous_pause
|
|
_open()
|
|
elif is_instance_valid(_dialog): _dialog.show()
|
|
|
|
func _refresh_language() -> void:
|
|
if is_instance_valid(_dialog): _needs_refresh = true
|
|
|
|
func _on_dialog_closed() -> void:
|
|
_dialog = null
|
|
get_tree().paused = _previous_pause
|
|
|
|
func _close() -> void:
|
|
if is_instance_valid(_dialog): _dialog._close()
|
|
|
|
func _main_menu() -> void:
|
|
_open_save_browser(func() -> void:
|
|
get_tree().paused = false
|
|
get_tree().change_scene_to_file("res://scenes/ui/main_menu.tscn"))
|
|
|
|
func _quit() -> void:
|
|
_open_save_browser(func() -> void: get_tree().quit())
|
|
|