Skip to content

Commit

Permalink
refactor(Menus): update all menus to use new state machine pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed Aug 25, 2024
1 parent 12e11f3 commit 0defac3
Show file tree
Hide file tree
Showing 42 changed files with 628 additions and 391 deletions.
2 changes: 1 addition & 1 deletion assets/state/state_machines/global_state_machine.tres
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[ext_resource type="Script" path="res://core/systems/state/state_machine.gd" id="1_dw1uo"]
[ext_resource type="Resource" uid="uid://bmgs1ngma1523" path="res://assets/state/states/in_game_menu.tres" id="2_pg6ge"]
[ext_resource type="Resource" uid="uid://cv3vduo0ojk1u" path="res://assets/state/states/menu.tres" id="3_4n6bo"]
[ext_resource type="Resource" uid="uid://dgbe422crufa4" path="res://assets/state/states/overlay.tres" id="4_j3a4g"]
[ext_resource type="Resource" uid="uid://dgbe422crufa4" path="res://assets/state/states/popup.tres" id="4_j3a4g"]

[resource]
script = ExtResource("1_dw1uo")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[gd_resource type="Resource" script_class="StateMachine" load_steps=7 format=3 uid="uid://cadriyl38ny5y"]

[ext_resource type="Resource" uid="uid://e7bbebwf7guj" path="res://assets/state/states/main_menu.tres" id="1_biinh"]
[ext_resource type="Script" path="res://core/systems/state/state_machine.gd" id="1_twt6m"]
[ext_resource type="Resource" uid="uid://bp807nlks8eq1" path="res://assets/state/states/quick_bar_menu.tres" id="2_suyfm"]
[ext_resource type="Resource" uid="uid://bw0mtk7sso8m2" path="res://assets/state/states/power_menu.tres" id="3_wqmvq"]
[ext_resource type="Resource" uid="uid://dja3m1mevv6xw" path="res://assets/state/states/osk.tres" id="4_hixi7"]
[ext_resource type="Resource" uid="uid://db5gbdl3xgwlq" path="res://assets/state/states/help_menu.tres" id="5_24mgg"]
[ext_resource type="Resource" uid="uid://e7bbebwf7guj" path="res://assets/state/states/main_menu.tres" id="1_7fvmm"]
[ext_resource type="Resource" uid="uid://bp807nlks8eq1" path="res://assets/state/states/quick_bar_menu.tres" id="2_detpa"]
[ext_resource type="Resource" uid="uid://bw0mtk7sso8m2" path="res://assets/state/states/power_menu.tres" id="3_0mhn6"]
[ext_resource type="Resource" uid="uid://dja3m1mevv6xw" path="res://assets/state/states/osk.tres" id="4_qu5fi"]
[ext_resource type="Resource" uid="uid://db5gbdl3xgwlq" path="res://assets/state/states/help_menu.tres" id="5_5ytlq"]
[ext_resource type="Script" path="res://core/systems/state/state_machine.gd" id="6_82te1"]

[resource]
script = ExtResource("1_twt6m")
logger_name = "OverlayStateMachine"
script = ExtResource("6_82te1")
logger_name = "PopupStateMachine"
minimum_states = 0
allowed_states = Array[Resource("res://core/systems/state/state.gd")]([ExtResource("1_biinh"), ExtResource("2_suyfm"), ExtResource("3_wqmvq"), ExtResource("4_hixi7"), ExtResource("5_24mgg")])
allowed_states = Array[Resource("res://core/systems/state/state.gd")]([ExtResource("1_7fvmm"), ExtResource("2_detpa"), ExtResource("3_0mhn6"), ExtResource("4_qu5fi"), ExtResource("5_5ytlq")])
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_resource type="Resource" script_class="State" load_steps=2 format=3 uid="uid://dgbe422crufa4"]

[ext_resource type="Script" path="res://core/systems/state/state.gd" id="1_btrxw"]
[ext_resource type="Script" path="res://core/systems/state/state.gd" id="1_t0m3s"]

[resource]
script = ExtResource("1_btrxw")
name = "overlay"
script = ExtResource("1_t0m3s")
name = "popup"
data = {}
28 changes: 14 additions & 14 deletions core/systems/input/input_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var audio_manager := load("res://core/global/audio_manager.tres") as AudioManage
var input_plumber := load("res://core/systems/input/input_plumber.tres") as InputPlumber

## State machine to use to switch menu states in response to input events.
var overlay_state_machine := (
preload("res://assets/state/state_machines/overlay_state_machine.tres") as StateMachine
var popup_state_machine := (
preload("res://assets/state/state_machines/popup_state_machine.tres") as StateMachine
)
var in_game_menu_state := preload("res://assets/state/states/in_game_menu.tres") as State
var main_menu_state := preload("res://assets/state/states/main_menu.tres") as State
Expand Down Expand Up @@ -188,15 +188,15 @@ func _main_menu_input(event: InputEvent) -> void:
return

# Open the main menu
var state := overlay_state_machine.current_state()
var state := popup_state_machine.current_state()
var menu_state := main_menu_state

if state == menu_state:
overlay_state_machine.pop_state()
popup_state_machine.pop_state()
elif state in [quick_bar_state, osk_state]:
overlay_state_machine.replace_state(menu_state)
popup_state_machine.replace_state(menu_state)
else:
overlay_state_machine.push_state(menu_state)
popup_state_machine.push_state(menu_state)


## Handle quick bar menu events to open the quick bar menu
Expand All @@ -205,13 +205,13 @@ func _on_quick_bar_open(event: InputEvent) -> void:
if not event.is_pressed():
return

var state := overlay_state_machine.current_state()
var state := popup_state_machine.current_state()
if state == quick_bar_state:
overlay_state_machine.pop_state()
popup_state_machine.pop_state()
elif state in [main_menu_state, in_game_menu_state, osk_state]:
overlay_state_machine.replace_state(quick_bar_state)
popup_state_machine.replace_state(quick_bar_state)
else:
overlay_state_machine.push_state(quick_bar_state)
popup_state_machine.push_state(quick_bar_state)


## Handle OSK events for bringing up the on-screen keyboard
Expand All @@ -224,13 +224,13 @@ func _osk_input(event: InputEvent) -> void:
context.type = KeyboardContext.TYPE.X11
osk.open(context)

var state := overlay_state_machine.current_state()
var state := popup_state_machine.current_state()
if state == osk_state:
overlay_state_machine.pop_state()
popup_state_machine.pop_state()
elif state in [main_menu_state, in_game_menu_state, quick_bar_state]:
overlay_state_machine.replace_state(osk_state)
popup_state_machine.replace_state(osk_state)
else:
overlay_state_machine.push_state(osk_state)
popup_state_machine.push_state(osk_state)


## Handle audio input events such as mute, volume up, and volume down
Expand Down
16 changes: 16 additions & 0 deletions core/systems/input/input_watcher.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ signal input_released
## If true, consumes the event, marking it as handled so no other nodes
## try to handle this input event.
@export var stop_propagation: bool
## Always process inputs or only when parent node is visible
@export_flags("When visible", "When child focused") var process_input_mode: int = 3

## Name of the input action in the InputMap to watch for
var action: String

@onready var logger := Log.get_logger("InputWatcher", Log.LEVEL.INFO)


func _ready() -> void:
if action.is_empty():
set_process_input(false)

if process_input_mode == 0:
return

# Only process input if the parent node is visible
var parent := get_parent()
if parent is Control:
Expand All @@ -39,13 +46,22 @@ func _input(event: InputEvent) -> void:
if not event.is_action(action):
return

# Only process input if a child node has focus
if (process_input_mode & 2):
var focus_owner := get_viewport().gui_get_focus_owner()
var parent := get_parent()
if not parent.is_ancestor_of(focus_owner):
return

if event.is_pressed():
input_pressed.emit()
elif event.is_released():
input_released.emit()

# Stop the event from propagating
if stop_propagation:
var parent := get_parent()
logger.debug("Consuming input event '{action}' for node {n}".format({"action": action, "n": str(parent)}))
get_viewport().set_input_as_handled()


Expand Down
41 changes: 18 additions & 23 deletions core/ui/card_ui/card_ui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var state_machine := (
preload("res://assets/state/state_machines/global_state_machine.tres") as StateMachine
)
var menu_state_machine := preload("res://assets/state/state_machines/menu_state_machine.tres") as StateMachine
var overlay_state_machine := preload("res://assets/state/state_machines/overlay_state_machine.tres") as StateMachine
var popup_state_machine := preload("res://assets/state/state_machines/popup_state_machine.tres") as StateMachine
var menu_state := preload("res://assets/state/states/menu.tres") as State
var overlay_state := preload("res://assets/state/states/overlay.tres") as State
var popup_state := preload("res://assets/state/states/popup.tres") as State
var first_boot_state := preload("res://assets/state/states/first_boot_menu.tres") as State
var home_state := preload("res://assets/state/states/home.tres") as State
var in_game_state := preload("res://assets/state/states/in_game.tres") as State
Expand Down Expand Up @@ -88,14 +88,17 @@ func _ready() -> void:
var on_menu_state_entered := func(_from: State):
menu_state_machine.refresh()
menu_state.state_entered.connect(on_menu_state_entered)
var on_menu_state_removed := func(_to: State):
menu_state_machine.clear_states()
menu_state.state_removed.connect(on_menu_state_removed)

# Whenever an overlay state is pushed, update the global state
var on_overlay_state_changed := func(_from: State, to: State):
# Whenever an popup state is pushed, update the global state
var on_popup_state_changed := func(_from: State, to: State):
if to:
state_machine.push_state(overlay_state)
state_machine.push_state(popup_state)
else:
state_machine.remove_state(overlay_state)
overlay_state_machine.state_changed.connect(on_overlay_state_changed)
state_machine.remove_state(popup_state)
popup_state_machine.state_changed.connect(on_popup_state_changed)

# Show/hide the overlay when we enter/exit the in-game state
in_game_state.state_entered.connect(_on_game_state_entered)
Expand All @@ -114,21 +117,13 @@ func _ready() -> void:
input_plumber.composite_device_changed.connect(on_device_changed)

# Set the theme if one was set
var theme_path := settings_manager.get_value("general", "theme", "") as String
if theme_path == "":
logger.debug("No theme set. Using default theme.")

var current_theme = get_theme()
if theme_path != "" && current_theme.resource_path != theme_path:
logger.debug("Setting theme to: " + theme_path)
var loaded_theme = load(theme_path)
if loaded_theme != null:
# TODO: This is a workaround, themes aren't properly set the first time.
call_deferred("set_theme", loaded_theme)
call_deferred("set_theme", current_theme)
call_deferred("set_theme", loaded_theme)
else:
logger.debug("Unable to load theme")
var theme_path := settings_manager.get_value("general", "theme", "res://assets/themes/card_ui-dracula.tres") as String
logger.debug("Setting theme to: " + theme_path)
var loaded_theme = load(theme_path)
if loaded_theme != null:
set_theme(loaded_theme)
else:
logger.debug("Unable to load theme")


func _on_focus_changed(control: Control) -> void:
Expand Down Expand Up @@ -239,7 +234,7 @@ func _input(event: InputEvent) -> void:
if event.is_action_pressed("ogui_power"):
var open_power_menu := func():
logger.info("Power menu requested")
overlay_state_machine.push_state(power_state)
popup_state_machine.push_state(power_state)
power_timer.timeout.connect(open_power_menu, CONNECT_ONE_SHOT)
power_timer.start()
return
Expand Down
8 changes: 3 additions & 5 deletions core/ui/card_ui/card_ui.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[gd_scene load_steps=39 format=3 uid="uid://fhriwlhm0lcj"]
[gd_scene load_steps=38 format=3 uid="uid://fhriwlhm0lcj"]

[ext_resource type="PackedScene" uid="uid://n83wlhmmsu3j" path="res://core/systems/input/input_manager.tscn" id="1_34t85"]
[ext_resource type="Theme" uid="uid://ehplgpp70vxa" path="res://assets/themes/card_ui-dracula.tres" id="1_ajgj2"]
[ext_resource type="Script" path="res://core/ui/card_ui/card_ui.gd" id="1_f8851"]
[ext_resource type="PackedScene" uid="uid://dlegwm7jqfe2i" path="res://core/systems/boxart/boxart_local.tscn" id="2_600i0"]
[ext_resource type="PackedScene" uid="uid://ch6qw6obetalo" path="res://core/systems/library/library_desktop.tscn" id="3_68bes"]
Expand Down Expand Up @@ -83,7 +82,6 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = ExtResource("1_ajgj2")
script = ExtResource("1_f8851")

[node name="InputManager" parent="." instance=ExtResource("1_34t85")]
Expand Down Expand Up @@ -305,7 +303,7 @@ mouse_filter = 2

[node name="FadeTexture" type="TextureRect" parent="."]
unique_name_in_owner = true
z_index = 20
z_index = 21
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
Expand All @@ -322,7 +320,7 @@ libraries = {

[node name="BootVideoPlayer" type="VideoStreamPlayer" parent="."]
unique_name_in_owner = true
z_index = 20
z_index = 21
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
Expand Down
19 changes: 12 additions & 7 deletions core/ui/card_ui/gamepad/gamepad_mapper.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[gd_scene load_steps=22 format=3 uid="uid://b3o3wo40sfih1"]
[gd_scene load_steps=23 format=3 uid="uid://b3o3wo40sfih1"]

[ext_resource type="Script" path="res://core/ui/card_ui/gamepad/gamepad_mapper.gd" id="1_alala"]
[ext_resource type="PackedScene" uid="uid://shvyhrv5sx3v" path="res://core/systems/state/state_watcher.tscn" id="2_4osef"]
[ext_resource type="PackedScene" uid="uid://c6fg6uvng0ovi" path="res://core/systems/input/input_watcher.tscn" id="2_o3mis"]
[ext_resource type="PackedScene" uid="uid://b76dvfuouhlwd" path="res://core/systems/state/state_updater.tscn" id="3_2kcao"]
[ext_resource type="Resource" uid="uid://c4er7pfmn7x50" path="res://assets/state/state_machines/gamepad_settings_state_machine.tres" id="3_wgp64"]
[ext_resource type="PackedScene" uid="uid://bw8113ocotx2r" path="res://core/systems/effects/fade_effect.tscn" id="4_scwwv"]
[ext_resource type="Resource" uid="uid://46cu324n427u" path="res://assets/state/states/gamepad_change_input.tres" id="4_sv30y"]
[ext_resource type="PackedScene" uid="uid://uljtdvmuol3l" path="res://core/systems/input/focus_group_setter.tscn" id="5_i6uqd"]
[ext_resource type="PackedScene" uid="uid://ccd4sw84h1qbc" path="res://core/systems/input/back_input_handler.tscn" id="5_roi3c"]
[ext_resource type="Texture2D" uid="uid://dyemqkvdtk43e" path="res://assets/images/gamepad/xbox/xbox_button_color_a.svg" id="8_5jaa6"]
[ext_resource type="PackedScene" uid="uid://cgmb4kr2ec4ha" path="res://core/ui/components/tabs_header.tscn" id="8_okgql"]
[ext_resource type="PackedScene" uid="uid://8m20p2s0v5gb" path="res://core/systems/input/focus_group.tscn" id="9_an8os"]
Expand Down Expand Up @@ -34,6 +35,15 @@ grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_alala")

[node name="InputWatcher" parent="." instance=ExtResource("2_o3mis")]
stop_propagation = true
action = "ogui_east"

[node name="StateUpdater" parent="InputWatcher" instance=ExtResource("3_2kcao")]
state_machine = ExtResource("3_wgp64")
action = 2
on_signal = "input_released"

[node name="StateWatcher" parent="." instance=ExtResource("2_4osef")]
state = ExtResource("4_sv30y")

Expand All @@ -47,11 +57,6 @@ on_signal = "state_entered"
target = NodePath("../../VBoxContainer/TabContainer/Gamepad/MarginContainer/HBoxContainer/PanelContainer/MarginContainer/ScrollContainer/GamepadInputContainer/GamepadFocusGroup")
on_signal = "state_entered"

[node name="BackInputHandler" parent="." instance=ExtResource("5_roi3c")]
state_machine = ExtResource("3_wgp64")
process_input_during = Array[Resource("res://core/systems/state/state.gd")]([ExtResource("4_sv30y")])
minimum_states = 0

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 15
Expand Down
2 changes: 1 addition & 1 deletion core/ui/card_ui/gamepad/gamepad_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var in_game_state := load("res://assets/state/states/in_game.tres") as State
var launch_manager := load("res://core/global/launch_manager.tres") as LaunchManager
var notification_manager := load("res://core/global/notification_manager.tres") as NotificationManager
var settings_manager := load("res://core/global/settings_manager.tres") as SettingsManager
var global_state_machine := load("res://assets/state/state_machines/global_state_machine.tres") as StateMachine
var global_state_machine := load("res://assets/state/state_machines/menu_state_machine.tres") as StateMachine
var state_machine := load("res://assets/state/state_machines/gamepad_settings_state_machine.tres") as StateMachine
var input_plumber := load("res://core/systems/input/input_plumber.tres") as InputPlumber
var input_icons := load("res://core/systems/input/input_icon_manager.tres") as InputIconManager
Expand Down
2 changes: 1 addition & 1 deletion core/ui/card_ui/gamepad/gamepad_settings.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ script = ExtResource("1_o4hh5")

[node name="InputWatcher" parent="." instance=ExtResource("2_ck60w")]
stop_propagation = true
action = "ogui_back"
action = "ogui_east"

[node name="StateUpdater" parent="InputWatcher" instance=ExtResource("3_yygaq")]
state_machine = ExtResource("4_wxnal")
Expand Down
12 changes: 7 additions & 5 deletions core/ui/card_ui/help/help_menu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[ext_resource type="PackedScene" uid="uid://c6fg6uvng0ovi" path="res://core/systems/input/input_watcher.tscn" id="2_cxu4x"]
[ext_resource type="PackedScene" uid="uid://b76dvfuouhlwd" path="res://core/systems/state/state_updater.tscn" id="3_10mqr"]
[ext_resource type="Resource" uid="uid://db5gbdl3xgwlq" path="res://assets/state/states/help_menu.tres" id="3_hidel"]
[ext_resource type="Resource" uid="uid://bcr6c0281lb5b" path="res://assets/state/state_machines/menu_state_machine.tres" id="4_8croa"]
[ext_resource type="Resource" uid="uid://cadriyl38ny5y" path="res://assets/state/state_machines/popup_state_machine.tres" id="4_x24i3"]
[ext_resource type="PackedScene" uid="uid://shvyhrv5sx3v" path="res://core/systems/state/state_watcher.tscn" id="5_1uaoh"]
[ext_resource type="PackedScene" uid="uid://b0cyl6fdqxevn" path="res://core/systems/input/scroller_joystick.tscn" id="5_uocjd"]
[ext_resource type="PackedScene" uid="uid://dithv38oqgy58" path="res://core/ui/components/section_label.tscn" id="5_wfsen"]
Expand All @@ -14,6 +14,7 @@
[ext_resource type="PackedScene" uid="uid://ekhjpmat02f8" path="res://core/systems/effects/slide_effect.tscn" id="8_ut6gk"]

[node name="HelpMenu" type="Control"]
z_index = 20
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
Expand All @@ -24,10 +25,11 @@ script = ExtResource("1_7fti5")

[node name="InputWatcher" parent="." instance=ExtResource("2_cxu4x")]
stop_propagation = true
action = "ogui_back"
process_input_mode = 1
action = "ogui_east"

[node name="StateUpdater" parent="InputWatcher" instance=ExtResource("3_10mqr")]
state_machine = ExtResource("4_8croa")
state_machine = ExtResource("4_x24i3")
action = 2
on_signal = "input_released"

Expand All @@ -42,8 +44,8 @@ on_signal = "state_entered"

[node name="SlideEffect" parent="StateWatcher" node_paths=PackedStringArray("target") instance=ExtResource("8_ut6gk")]
target = NodePath("../../MarginContainer")
slide_speed = 0.2
direction = "down"
slide_speed = 0.25
direction = "up"
on_signal = "state_entered"
slide_out_signal = "state_exited"
on_signal = "state_entered"
Expand Down
4 changes: 2 additions & 2 deletions core/ui/card_ui/home/cardui_home.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var InstallManager := preload("res://core/global/install_manager.tres")
var BoxArtManager := load("res://core/global/boxart_manager.tres") as BoxArtManager
var LibraryManager := load("res://core/global/library_manager.tres") as LibraryManager
var state_machine := preload("res://assets/state/state_machines/menu_state_machine.tres") as StateMachine
var overlay_state_machine := preload("res://assets/state/state_machines/overlay_state_machine.tres") as StateMachine
var popup_state_machine := preload("res://assets/state/state_machines/popup_state_machine.tres") as StateMachine
var home_state := preload("res://assets/state/states/home.tres") as State
var main_menu_state := preload("res://assets/state/states/main_menu.tres") as State
var launcher_state := preload("res://assets/state/states/game_launcher.tres") as State
Expand Down Expand Up @@ -107,7 +107,7 @@ func _input(event: InputEvent) -> void:
get_viewport().set_input_as_handled()

# Push the main menu state when the back button is pressed
overlay_state_machine.push_state(main_menu_state)
popup_state_machine.push_state(main_menu_state)


# When an install is queued, connect signals to show a progress bar on the library
Expand Down
Loading

0 comments on commit 0defac3

Please sign in to comment.