Handle ui_cancel and the Android back-request notification. Back now dismisses overlays in priority order, closes the win overlay, navigates back, and asks for confirmation before quitting on Home.
285 lines
8.1 KiB
GDScript
285 lines
8.1 KiB
GDScript
extends Control
|
|
|
|
const HomeScene = preload("res://scenes/Home.tscn")
|
|
const GameScene = preload("res://scenes/Game.tscn")
|
|
const GuestBookScene = preload("res://scenes/GuestBook.tscn")
|
|
const RulebookScene = preload("res://scenes/Rulebook.tscn")
|
|
const ProfileScene = preload("res://scenes/Profile.tscn")
|
|
const StreakScene = preload("res://scenes/Streak.tscn")
|
|
const SettingsSheetScene = preload("res://scenes/SettingsSheet.tscn")
|
|
|
|
var home
|
|
var game
|
|
var guest_book
|
|
var rulebook
|
|
var profile
|
|
var streak
|
|
var settings_sheet
|
|
var quit_layer: ColorRect
|
|
var quit_button: Button
|
|
var screens := {}
|
|
var current := "home"
|
|
var nav_stack: Array[String] = []
|
|
|
|
|
|
func _ready() -> void:
|
|
var background := ColorRect.new()
|
|
background.color = Palette.CREAM
|
|
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
background.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
add_child(background)
|
|
|
|
home = HomeScene.instantiate()
|
|
_add_screen(home)
|
|
home.play_pressed.connect(_on_play)
|
|
home.guest_book_pressed.connect(func() -> void: show_screen("guest_book"))
|
|
home.settings_pressed.connect(func() -> void: open_settings("home"))
|
|
home.rules_pressed.connect(func() -> void: show_screen("rules"))
|
|
home.daily_pressed.connect(_on_daily)
|
|
home.profile_pressed.connect(func() -> void: show_screen("profile"))
|
|
home.streak_pressed.connect(func() -> void: show_screen("streak"))
|
|
|
|
game = GameScene.instantiate()
|
|
_add_screen(game)
|
|
game.back_pressed.connect(go_back)
|
|
game.guest_book_pressed.connect(func() -> void: show_screen("guest_book"))
|
|
game.settings_pressed.connect(func() -> void: open_settings("game"))
|
|
|
|
guest_book = GuestBookScene.instantiate()
|
|
_add_screen(guest_book)
|
|
guest_book.back_pressed.connect(go_back)
|
|
|
|
rulebook = RulebookScene.instantiate()
|
|
_add_screen(rulebook)
|
|
rulebook.back_pressed.connect(go_back)
|
|
|
|
profile = ProfileScene.instantiate()
|
|
_add_screen(profile)
|
|
profile.back_pressed.connect(go_back)
|
|
profile.streak_pressed.connect(func() -> void: show_screen("streak"))
|
|
|
|
streak = StreakScene.instantiate()
|
|
_add_screen(streak)
|
|
streak.back_pressed.connect(go_back)
|
|
|
|
settings_sheet = SettingsSheetScene.instantiate()
|
|
add_child(settings_sheet)
|
|
settings_sheet.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
settings_sheet.visible = false
|
|
settings_sheet.closed.connect(close_settings)
|
|
settings_sheet.restart_requested.connect(_on_restart_requested)
|
|
settings_sheet.lobby_requested.connect(_on_lobby_requested)
|
|
settings_sheet.rules_requested.connect(_on_rules_requested)
|
|
|
|
_build_quit_confirm()
|
|
|
|
screens = {
|
|
"home": home,
|
|
"game": game,
|
|
"guest_book": guest_book,
|
|
"rules": rulebook,
|
|
"profile": profile,
|
|
"streak": streak,
|
|
}
|
|
|
|
show_screen("home", false)
|
|
|
|
|
|
func _add_screen(screen: Control) -> void:
|
|
add_child(screen)
|
|
screen.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
screen.visible = false
|
|
|
|
|
|
func show_screen(name: String, push: bool = true) -> void:
|
|
if not screens.has(name):
|
|
return
|
|
if settings_sheet != null and settings_sheet.visible:
|
|
close_settings()
|
|
if name == current:
|
|
screens[name].visible = true
|
|
return
|
|
if push:
|
|
nav_stack.append(current)
|
|
|
|
for key in screens:
|
|
screens[key].visible = key == name
|
|
|
|
current = name
|
|
|
|
if name == "guest_book":
|
|
guest_book.refresh()
|
|
|
|
|
|
func go_back() -> void:
|
|
if nav_stack.is_empty():
|
|
show_screen("home", false)
|
|
return
|
|
var previous: String = nav_stack.pop_back()
|
|
show_screen(previous, false)
|
|
|
|
|
|
func open_settings(mode: String) -> void:
|
|
if settings_sheet == null:
|
|
return
|
|
if mode == "game":
|
|
game.set_ui_paused(true)
|
|
settings_sheet.open_sheet(mode)
|
|
|
|
|
|
func close_settings() -> void:
|
|
if settings_sheet == null:
|
|
return
|
|
if settings_sheet.visible:
|
|
settings_sheet.visible = false
|
|
game.set_ui_paused(false)
|
|
|
|
|
|
func _on_restart_requested() -> void:
|
|
game.restart_room()
|
|
close_settings()
|
|
|
|
|
|
func _on_lobby_requested() -> void:
|
|
close_settings()
|
|
nav_stack.clear()
|
|
current = ""
|
|
show_screen("home", false)
|
|
|
|
|
|
func _on_rules_requested() -> void:
|
|
close_settings()
|
|
show_screen("rules")
|
|
|
|
|
|
func _on_play() -> void:
|
|
var max_index: int = max(0, game.levels.size() - 1)
|
|
var start_index: int = clampi(int(SaveGame.data.get("last_room", 0)), 0, max_index)
|
|
show_screen("game")
|
|
game.start_level(start_index)
|
|
|
|
|
|
func _on_daily() -> void:
|
|
var key := Daily.today_key()
|
|
var index := Daily.level_index(game.levels.size(), key)
|
|
show_screen("game")
|
|
game.start_daily(index, key)
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("ui_cancel"):
|
|
if _handle_back():
|
|
accept_event()
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_WM_GO_BACK_REQUEST:
|
|
_handle_back()
|
|
|
|
|
|
func _handle_back() -> bool:
|
|
if quit_layer != null and quit_layer.visible:
|
|
_hide_quit_confirm()
|
|
return true
|
|
if settings_sheet != null and settings_sheet.visible:
|
|
if settings_sheet.confirm_layer != null and settings_sheet.confirm_layer.visible:
|
|
settings_sheet._hide_confirm()
|
|
else:
|
|
close_settings()
|
|
return true
|
|
if guest_book != null and guest_book.visible and guest_book.detail_layer.visible:
|
|
guest_book._detail_opened_at -= DETAIL_GUARD_BYPASS_MS
|
|
guest_book._try_close_detail()
|
|
return true
|
|
if current == "game" and game != null and game.overlay.visible:
|
|
game.hide_win()
|
|
return true
|
|
if current == "home":
|
|
_show_quit_confirm()
|
|
return true
|
|
go_back()
|
|
return true
|
|
|
|
|
|
const DETAIL_GUARD_BYPASS_MS := 300
|
|
|
|
|
|
func _build_quit_confirm() -> void:
|
|
quit_layer = ColorRect.new()
|
|
quit_layer.color = Color(0.18, 0.12, 0.08, 0.62)
|
|
quit_layer.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
quit_layer.mouse_filter = Control.MOUSE_FILTER_STOP
|
|
quit_layer.gui_input.connect(func(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
_hide_quit_confirm()
|
|
)
|
|
quit_layer.visible = false
|
|
add_child(quit_layer)
|
|
|
|
var center := CenterContainer.new()
|
|
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
center.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
quit_layer.add_child(center)
|
|
|
|
var panel := UI.panel(Palette.CREAM, 24, 16)
|
|
var max_width: int = max(240, int(get_viewport_rect().size.x) - 32)
|
|
panel.custom_minimum_size = Vector2(min(320, max_width), 0)
|
|
panel.mouse_filter = Control.MOUSE_FILTER_STOP
|
|
center.add_child(panel)
|
|
|
|
var box := VBoxContainer.new()
|
|
box.add_theme_constant_override("separation", 10)
|
|
panel.add_child(box)
|
|
box.add_child(UI.heading("Leave Ratdoku?", 20))
|
|
var body := UI.label("Thanks for playing. Come back tomorrow for a new daily room.", 13, Palette.MUTED)
|
|
body.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
|
|
box.add_child(body)
|
|
|
|
var actions := HBoxContainer.new()
|
|
actions.add_theme_constant_override("separation", 8)
|
|
box.add_child(actions)
|
|
var cancel := UI.button("Cancel", false, 46)
|
|
cancel.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
cancel.pressed.connect(_hide_quit_confirm)
|
|
actions.add_child(cancel)
|
|
quit_button = UI.button("Quit", false, 46)
|
|
quit_button.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
_apply_red_style(quit_button)
|
|
quit_button.pressed.connect(func() -> void: get_tree().quit())
|
|
actions.add_child(quit_button)
|
|
|
|
|
|
func _show_quit_confirm() -> void:
|
|
quit_layer.visible = true
|
|
quit_layer.move_to_front()
|
|
|
|
|
|
func _hide_quit_confirm() -> void:
|
|
quit_layer.visible = false
|
|
|
|
|
|
func _apply_red_style(button: Button) -> void:
|
|
var color := Palette.RED
|
|
button.add_theme_stylebox_override("normal", _red_box(color))
|
|
button.add_theme_stylebox_override("hover", _red_box(color.lightened(0.07)))
|
|
button.add_theme_stylebox_override("pressed", _red_box(color.darkened(0.10)))
|
|
button.add_theme_stylebox_override("focus", _red_box(color))
|
|
button.add_theme_color_override("font_color", Palette.WHITE)
|
|
button.add_theme_color_override("font_hover_color", Palette.WHITE)
|
|
button.add_theme_color_override("font_pressed_color", Palette.WHITE)
|
|
button.add_theme_color_override("font_focus_color", Palette.WHITE)
|
|
|
|
|
|
func _red_box(color: Color) -> StyleBoxFlat:
|
|
var style := StyleBoxFlat.new()
|
|
style.bg_color = color
|
|
style.corner_radius_top_left = 26
|
|
style.corner_radius_top_right = 26
|
|
style.corner_radius_bottom_left = 26
|
|
style.corner_radius_bottom_right = 26
|
|
style.content_margin_left = 16
|
|
style.content_margin_right = 16
|
|
style.content_margin_top = 10
|
|
style.content_margin_bottom = 10
|
|
return style
|