From 6b45505ee0af21eb5696e319ebc6643a26f4b7d5 Mon Sep 17 00:00:00 2001 From: juliohq Date: Sun, 3 Sep 2023 07:35:34 -0300 Subject: [PATCH 1/8] Remove `Callable(self, function_name)` Removes `Callable(self, function_name)` in favor of just `function_name`, which is way clear and more readable --- Assets/Player/CameraControls.gd | 2 +- Assets/Player/Minimap/MinimapShipLayer.gd | 2 +- Assets/Player/PlayerCamera.gd | 8 +++---- Assets/UI/BookMenu/BookMenu.gd | 21 +++++++++++-------- Assets/UI/Pages/HelpUI/HelpUI.gd | 8 ++----- Assets/UI/Pages/NewGameUI/ColorSelection.gd | 2 +- Assets/UI/Pages/OptionsUI/OptionsUI.gd | 12 +++++------ .../MainButton/MainButtons/GameSpeedLabel.gd | 2 +- .../UI/TabWidgets/Switches/SwitchTabWidget.gd | 2 +- Assets/UI/TabWidgets/TabWidget.gd | 6 +++--- Assets/UI/TabWidgets/WidgetDetail.gd | 2 +- Assets/World/Buildings/Building.gd | 2 +- 12 files changed, 34 insertions(+), 35 deletions(-) diff --git a/Assets/Player/CameraControls.gd b/Assets/Player/CameraControls.gd index 9c13790c..947c787b 100644 --- a/Assets/Player/CameraControls.gd +++ b/Assets/Player/CameraControls.gd @@ -28,7 +28,7 @@ var _drag_pos: Vector2 var enabled: bool = true : set = set_enabled, get = get_enabled func _ready() -> void: - if not _viewport.size_changed.connect(Callable(self, "_on_viewport_size_changed")): + if not _viewport.size_changed.connect(_on_viewport_size_changed): push_error("Failed To connect viewport") _basis = _get_basis() diff --git a/Assets/Player/Minimap/MinimapShipLayer.gd b/Assets/Player/Minimap/MinimapShipLayer.gd index e3460914..719ad3c5 100644 --- a/Assets/Player/Minimap/MinimapShipLayer.gd +++ b/Assets/Player/Minimap/MinimapShipLayer.gd @@ -29,7 +29,7 @@ func draw_layer(): timer = $Timer minimap = get_parent() as Minimap - timer.timeout.connect(Callable(self, "get_ships")) + timer.timeout.connect(get_ships) func get_ships(): # get player ship(s) diff --git a/Assets/Player/PlayerCamera.gd b/Assets/Player/PlayerCamera.gd index 1b5d45c0..31a9465c 100644 --- a/Assets/Player/PlayerCamera.gd +++ b/Assets/Player/PlayerCamera.gd @@ -152,10 +152,10 @@ func switch_context(new_context: InteractionContext) -> void: if active_context: active_context._on_exit() active_context = new_context - if not active_context.switch_context.is_connected(Callable(self, "switch_context")): - active_context.switch_context.connect(Callable(self, "switch_context")) - if not active_context.context_aborted.is_connected(Callable(self, "abort_context")): - active_context.context_aborted.connect(Callable(self, "abort_context")) + if not active_context.switch_context.is_connected(switch_context): + active_context.switch_context.connect(switch_context) + if not active_context.context_aborted.is_connected(abort_context): + active_context.context_aborted.connect(abort_context) active_context._on_enter() func abort_context() -> void: diff --git a/Assets/UI/BookMenu/BookMenu.gd b/Assets/UI/BookMenu/BookMenu.gd index 5b3069e1..bc419f98 100644 --- a/Assets/UI/BookMenu/BookMenu.gd +++ b/Assets/UI/BookMenu/BookMenu.gd @@ -19,12 +19,12 @@ func _ready() -> void: for page in %Pages.get_children(): if %Pages.get_tab_count() > 1: page_control = page.find_child("PageControl") - page_control.get_node("PrevButton").pressed.connect(Callable(self, "_on_PrevButton_pressed")) - page_control.get_node("NextButton").pressed.connect(Callable(self, "_on_NextButton_pressed")) + page_control.get_node("PrevButton").pressed.connect(_on_PrevButton_pressed) + page_control.get_node("NextButton").pressed.connect(_on_NextButton_pressed) page_control.visible = true - %Pages.tab_changed.connect(Callable(self, "_on_Pages_tab_changed")) + %Pages.tab_changed.connect(_on_Pages_tab_changed) # Force-call to determine initial state of page controls (enabled/disabled) %Pages.tab_changed.emit(%Pages.current_tab) @@ -39,8 +39,9 @@ func set_has_delete_button(new_has_delete_button: bool) -> void: for page in %Pages.get_children(): var delete_button := page.find_child("DeleteButton") as TextureButton - if not delete_button.pressed.is_connected(Callable(self, callback_function)): - delete_button.pressed.connect(Callable(self, callback_function)) + var _callback_function = Callable(self, callback_function) + if not delete_button.pressed.is_connected(_callback_function): + delete_button.pressed.connect(_callback_function) delete_button.visible = has_delete_button func set_has_cancel_button(new_has_cancel_button: bool) -> void: @@ -61,8 +62,9 @@ func set_has_cancel_button(new_has_cancel_button: bool) -> void: cancel_button = page.find_child("RightPageControls").find_child("CancelButton") as TextureButton else: cancel_button = page.find_child("CancelButton") as TextureButton - if not cancel_button.is_connected(pressed_signal, Callable(self, callback_function)): - cancel_button.connect(pressed_signal, Callable(self, callback_function)) + var _callback_function = Callable(self, callback_function) + if not cancel_button.is_connected(pressed_signal, _callback_function): + cancel_button.connect(pressed_signal, _callback_function) cancel_button.visible = has_cancel_button @@ -77,8 +79,9 @@ func set_has_ok_button(new_has_ok_button: bool) -> void: for page in %Pages.get_children(): var ok_button := page.find_child("OKButton") as TextureButton - if not ok_button.is_connected(pressed_signal, Callable(self, callback_function)): - ok_button.connect("pressed", Callable(self, "_on_OKButton_pressed")) + var _callback_function = Callable(self, callback_function) + if not ok_button.is_connected(pressed_signal, _callback_function): + ok_button.connect("pressed", _on_OKButton_pressed) ok_button.visible = has_ok_button func _on_PrevButton_pressed() -> void: diff --git a/Assets/UI/Pages/HelpUI/HelpUI.gd b/Assets/UI/Pages/HelpUI/HelpUI.gd index 2b2b5df5..91820b5b 100644 --- a/Assets/UI/Pages/HelpUI/HelpUI.gd +++ b/Assets/UI/Pages/HelpUI/HelpUI.gd @@ -11,12 +11,8 @@ func _ready() -> void: page_control.get_node("NextButton").disabled = true # These contain hyperlinks. - (%Pages/HelpUIInfo.find_child("RichTextLabel5") as RichTextLabel).meta_clicked.connect( - Callable(self, "_on_RichTextLabel_meta_clicked") - ) - (%Pages/HelpUIInfo.find_child("RichTextLabel6") as RichTextLabel).meta_clicked.connect( - Callable(self, "_on_RichTextLabel_meta_clicked") - ) + (%Pages/HelpUIInfo.find_child("RichTextLabel5") as RichTextLabel).meta_clicked.connect(_on_RichTextLabel_meta_clicked) + (%Pages/HelpUIInfo.find_child("RichTextLabel6") as RichTextLabel).meta_clicked.connect(_on_RichTextLabel_meta_clicked) func _on_RichTextLabel_meta_clicked(meta: Variant) -> void: print(meta) diff --git a/Assets/UI/Pages/NewGameUI/ColorSelection.gd b/Assets/UI/Pages/NewGameUI/ColorSelection.gd index 464bfe34..b512a109 100644 --- a/Assets/UI/Pages/NewGameUI/ColorSelection.gd +++ b/Assets/UI/Pages/NewGameUI/ColorSelection.gd @@ -5,7 +5,7 @@ extends HBoxContainer func _ready() -> void: for choice in choices.get_children(): - choice.gui_input.connect(Callable(self, "_on_choice_gui_input").bind(choice)) + choice.gui_input.connect(_on_choice_gui_input.bind(choice)) if choice.color_to_faction == Global.faction: selected_color.color = choice.color diff --git a/Assets/UI/Pages/OptionsUI/OptionsUI.gd b/Assets/UI/Pages/OptionsUI/OptionsUI.gd index cb362db8..f71fd7b2 100644 --- a/Assets/UI/Pages/OptionsUI/OptionsUI.gd +++ b/Assets/UI/Pages/OptionsUI/OptionsUI.gd @@ -73,7 +73,7 @@ func _ready() -> void: # Window mode settings["WindowMode"].options = Global.WINDOW_MODES.values() settings["WindowMode"].selected = Config.window_mode - settings["WindowMode"].item_selected.connect(Callable(self, "_on_WindowMode_item_selected")) + settings["WindowMode"].item_selected.connect(_on_WindowMode_item_selected) settings["WindowMode"].item_selected.emit(Config.window_mode) # Populate with available resolutions @@ -81,20 +81,20 @@ func _ready() -> void: for screen_resolution_index in SCREEN_RESOLUTIONS.size(): if Config.screen_resolution == SCREEN_RESOLUTIONS[screen_resolution_index]: settings["ScreenResolution"].selected = screen_resolution_index - settings["ScreenResolution"].item_selected.connect(Callable(self, "_on_ScreenResolution_item_selected")) + settings["ScreenResolution"].item_selected.connect(_on_ScreenResolution_item_selected) # Audio parameters settings["MasterVolume"].value = Config.master_volume - settings["MasterVolume"].value_changed.connect(Callable(self, "_on_MasterVolume_value_changed")) + settings["MasterVolume"].value_changed.connect(_on_MasterVolume_value_changed) settings["MusicVolume"].value = Config.music_volume - settings["MusicVolume"].value_changed.connect(Callable(self, "_on_MusicVolume_value_changed")) + settings["MusicVolume"].value_changed.connect(_on_MusicVolume_value_changed) settings["EffectsVolume"].value = Config.effects_volume - settings["EffectsVolume"].value_changed.connect(Callable(self, "_on_EffectsVolume_value_changed")) + settings["EffectsVolume"].value_changed.connect(_on_EffectsVolume_value_changed) settings["VoiceVolume"].value = Config.voice_volume - settings["VoiceVolume"].value_changed.connect(Callable(self, "_on_VoiceVolume_value_changed")) + settings["VoiceVolume"].value_changed.connect(_on_VoiceVolume_value_changed) func populate_dropdown(dropdown: OptionButton, items: Dictionary) -> void: for item in items.values(): diff --git a/Assets/UI/TabWidgets/Buttons/MainButton/MainButtons/GameSpeedLabel.gd b/Assets/UI/TabWidgets/Buttons/MainButton/MainButtons/GameSpeedLabel.gd index fbec6dfd..4d438225 100644 --- a/Assets/UI/TabWidgets/Buttons/MainButton/MainButtons/GameSpeedLabel.gd +++ b/Assets/UI/TabWidgets/Buttons/MainButton/MainButtons/GameSpeedLabel.gd @@ -9,7 +9,7 @@ func _ready() -> void: call_deferred("connect_game") # waiting for Global.Game to be set func connect_game() -> void: - Global.Game.game_speed_changed.connect(Callable(self, "_on_game_speed_changed")) + Global.Game.game_speed_changed.connect(_on_game_speed_changed) func _on_game_speed_changed(new_game_speed: float) -> void: text = "%1.1fx" % new_game_speed diff --git a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd index cb5829f9..b3270a0f 100644 --- a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd +++ b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd @@ -17,7 +17,7 @@ class_name SwitchTabWidget # #func _listen_to_other_switches() -> void: # for sibling in get_parent().get_children(): -# sibling.tab_changed.connect(Callable(self, "_on_SwitchTabWidget_tab_changed")) +# sibling.tab_changed.connect(_on_SwitchTabWidget_tab_changed) func _draw() -> void: # if texture_normal: diff --git a/Assets/UI/TabWidgets/TabWidget.gd b/Assets/UI/TabWidgets/TabWidget.gd index f6d58a11..b84046a9 100644 --- a/Assets/UI/TabWidgets/TabWidget.gd +++ b/Assets/UI/TabWidgets/TabWidget.gd @@ -29,9 +29,9 @@ func _ready() -> void: if body.get_child_count() > 0: for child_container in body.get_children(): #prints("Attach signals to", child_container.name, "of", self.name) - #child_container.resized.connect(Callable(self, "_on_TabContainer_resized")) - #child_container.draw.connect(Callable(self, "_on_TabContainer_draw")) - child_container.sort_children.connect(Callable(self, "_on_TabContainer_sort_children")) + #child_container.resized.connect(_on_TabContainer_resized) + #child_container.draw.connect(_on_TabContainer_draw) + child_container.sort_children.connect(_on_TabContainer_sort_children) #func _process(_delta: float) -> void: # if Engine.is_editor_hint(): diff --git a/Assets/UI/TabWidgets/WidgetDetail.gd b/Assets/UI/TabWidgets/WidgetDetail.gd index 68322b52..de7f9dfe 100644 --- a/Assets/UI/TabWidgets/WidgetDetail.gd +++ b/Assets/UI/TabWidgets/WidgetDetail.gd @@ -7,7 +7,7 @@ extends VBoxContainer const WIDGET_DETAIL_REQUIRED_Y_POSITION = 157 func _ready() -> void: - %Body/TabContainer.resized.connect(Callable(self, "_on_TabContainer_resized")) + %Body/TabContainer.resized.connect(_on_TabContainer_resized) func _process(_delta: float) -> void: if Engine.is_editor_hint(): diff --git a/Assets/World/Buildings/Building.gd b/Assets/World/Buildings/Building.gd index dee63a26..0d04f112 100644 --- a/Assets/World/Buildings/Building.gd +++ b/Assets/World/Buildings/Building.gd @@ -23,7 +23,7 @@ var rotation_offset := 0 func _ready(): super() add_child(timer) - timer.timeout.connect(Callable(self, "_on_Timer_timeout")) + timer.timeout.connect(_on_Timer_timeout) timer.start(1.001 - anim_speed) func _process(_delta: float) -> void: From 38ea85de70f705d9a76ce170f5415a08b700d8ba Mon Sep 17 00:00:00 2001 From: juliohq Date: Sun, 3 Sep 2023 07:52:23 -0300 Subject: [PATCH 2/8] Fix setting theme overrides to `null` --- Assets/UI/BasicControls/OptionButton.gd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Assets/UI/BasicControls/OptionButton.gd b/Assets/UI/BasicControls/OptionButton.gd index 2734a2e8..3317e6c7 100644 --- a/Assets/UI/BasicControls/OptionButton.gd +++ b/Assets/UI/BasicControls/OptionButton.gd @@ -5,8 +5,7 @@ func _on_OptionButton_item_selected(_index: int) -> void: func _add_hover() -> void: # Restore color override hack when previously unhovered. - #add_theme_color_override("font_hover_color", null) # Why does not null work? How to clear properly? - set("custom_colors/font_hover_color", null) # Use this then. + remove_theme_color_override("font_hover_color") if has_theme_icon("arrow_hover", "OptionButton") and not has_theme_icon_override("arrow"): add_theme_icon_override("arrow", load(ProjectSettings.get_setting("gui/theme/custom")).get_icon("arrow_hover", "OptionButton")) @@ -16,7 +15,7 @@ func _remove_hover() -> void: add_theme_color_override("font_hover_color", load(ProjectSettings.get_setting("gui/theme/custom")).get_color("font_color", "OptionButton")) if has_theme_icon_override("arrow"): - add_theme_icon_override("arrow", null) + remove_theme_icon_override("arrow") func _notification(what: int) -> void: match what: From 1c1b2bcd456a8ed67eb87346cd0400f4f213e2ca Mon Sep 17 00:00:00 2001 From: juliohq Date: Sun, 3 Sep 2023 08:15:06 -0300 Subject: [PATCH 3/8] Fix signal already connected errors --- Assets/UI/BookMenu/BookMenu.gd | 6 +++--- Assets/UI/Pages/OptionsUI/OptionsUI.gd | 12 ++++++------ Utils/SignalUtils.gd | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 Utils/SignalUtils.gd diff --git a/Assets/UI/BookMenu/BookMenu.gd b/Assets/UI/BookMenu/BookMenu.gd index bc419f98..47ed1422 100644 --- a/Assets/UI/BookMenu/BookMenu.gd +++ b/Assets/UI/BookMenu/BookMenu.gd @@ -19,12 +19,12 @@ func _ready() -> void: for page in %Pages.get_children(): if %Pages.get_tab_count() > 1: page_control = page.find_child("PageControl") - page_control.get_node("PrevButton").pressed.connect(_on_PrevButton_pressed) - page_control.get_node("NextButton").pressed.connect(_on_NextButton_pressed) + SignalUtils.ensure_connected(page_control.get_node("PrevButton").pressed, _on_PrevButton_pressed) + SignalUtils.ensure_connected(page_control.get_node("NextButton").pressed, _on_NextButton_pressed) page_control.visible = true - %Pages.tab_changed.connect(_on_Pages_tab_changed) + SignalUtils.ensure_connected(%Pages.tab_changed, _on_Pages_tab_changed) # Force-call to determine initial state of page controls (enabled/disabled) %Pages.tab_changed.emit(%Pages.current_tab) diff --git a/Assets/UI/Pages/OptionsUI/OptionsUI.gd b/Assets/UI/Pages/OptionsUI/OptionsUI.gd index f71fd7b2..62952213 100644 --- a/Assets/UI/Pages/OptionsUI/OptionsUI.gd +++ b/Assets/UI/Pages/OptionsUI/OptionsUI.gd @@ -73,7 +73,7 @@ func _ready() -> void: # Window mode settings["WindowMode"].options = Global.WINDOW_MODES.values() settings["WindowMode"].selected = Config.window_mode - settings["WindowMode"].item_selected.connect(_on_WindowMode_item_selected) + SignalUtils.ensure_connected(settings["WindowMode"].item_selected, _on_WindowMode_item_selected) settings["WindowMode"].item_selected.emit(Config.window_mode) # Populate with available resolutions @@ -81,20 +81,20 @@ func _ready() -> void: for screen_resolution_index in SCREEN_RESOLUTIONS.size(): if Config.screen_resolution == SCREEN_RESOLUTIONS[screen_resolution_index]: settings["ScreenResolution"].selected = screen_resolution_index - settings["ScreenResolution"].item_selected.connect(_on_ScreenResolution_item_selected) + SignalUtils.ensure_connected(settings["ScreenResolution"].item_selected, _on_ScreenResolution_item_selected) # Audio parameters settings["MasterVolume"].value = Config.master_volume - settings["MasterVolume"].value_changed.connect(_on_MasterVolume_value_changed) + SignalUtils.ensure_connected(settings["MasterVolume"].value_changed, _on_MasterVolume_value_changed) settings["MusicVolume"].value = Config.music_volume - settings["MusicVolume"].value_changed.connect(_on_MusicVolume_value_changed) + SignalUtils.ensure_connected(settings["MusicVolume"].value_changed, _on_MusicVolume_value_changed) settings["EffectsVolume"].value = Config.effects_volume - settings["EffectsVolume"].value_changed.connect(_on_EffectsVolume_value_changed) + SignalUtils.ensure_connected(settings["EffectsVolume"].value_changed, _on_EffectsVolume_value_changed) settings["VoiceVolume"].value = Config.voice_volume - settings["VoiceVolume"].value_changed.connect(_on_VoiceVolume_value_changed) + SignalUtils.ensure_connected(settings["VoiceVolume"].value_changed, _on_VoiceVolume_value_changed) func populate_dropdown(dropdown: OptionButton, items: Dictionary) -> void: for item in items.values(): diff --git a/Utils/SignalUtils.gd b/Utils/SignalUtils.gd new file mode 100644 index 00000000..04e5c334 --- /dev/null +++ b/Utils/SignalUtils.gd @@ -0,0 +1,15 @@ +class_name SignalUtils + + +## Makes sure the given signal is connected to the given callable and connects it otherwise. +static func ensure_connected(_signal: Signal, callable: Callable): + assert(callable.is_valid()) + if not _signal.is_connected(callable): + _signal.connect(callable) + + +## Makes sure the given signal is not connected to the given callable and disconnects it otherwise. +static func ensure_disconnected(_signal: Signal, callable: Callable): + assert(callable.is_valid()) + if _signal.is_connected(callable): + _signal.disconnect(callable) From 7290b2c25670cdac38a7689d3904b87153d17564 Mon Sep 17 00:00:00 2001 From: juliohq Date: Sun, 3 Sep 2023 08:23:10 -0300 Subject: [PATCH 4/8] Fix misunderstanding signal already connected error --- Assets/Player/CameraControls.gd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Assets/Player/CameraControls.gd b/Assets/Player/CameraControls.gd index 947c787b..f15b9b93 100644 --- a/Assets/Player/CameraControls.gd +++ b/Assets/Player/CameraControls.gd @@ -28,8 +28,7 @@ var _drag_pos: Vector2 var enabled: bool = true : set = set_enabled, get = get_enabled func _ready() -> void: - if not _viewport.size_changed.connect(_on_viewport_size_changed): - push_error("Failed To connect viewport") + _viewport.size_changed.connect(_on_viewport_size_changed) _basis = _get_basis() func _process(delta: float) -> void: From 66f1c0bf07dd8103d74aaed94b0db8e81a64f765 Mon Sep 17 00:00:00 2001 From: juliohq Date: Sun, 3 Sep 2023 08:40:44 -0300 Subject: [PATCH 5/8] Replace remaining `is_connected` checks --- Assets/Player/PlayerCamera.gd | 6 ++---- Assets/UI/BookMenu/BookMenu.gd | 20 +++---------------- .../UI/TabWidgets/Switches/SwitchTabWidget.gd | 3 +-- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Assets/Player/PlayerCamera.gd b/Assets/Player/PlayerCamera.gd index 31a9465c..7b1aef77 100644 --- a/Assets/Player/PlayerCamera.gd +++ b/Assets/Player/PlayerCamera.gd @@ -152,10 +152,8 @@ func switch_context(new_context: InteractionContext) -> void: if active_context: active_context._on_exit() active_context = new_context - if not active_context.switch_context.is_connected(switch_context): - active_context.switch_context.connect(switch_context) - if not active_context.context_aborted.is_connected(abort_context): - active_context.context_aborted.connect(abort_context) + SignalUtils.ensure_connected(active_context.switch_context, switch_context) + SignalUtils.ensure_connected(active_context.context_aborted, abort_context) active_context._on_enter() func abort_context() -> void: diff --git a/Assets/UI/BookMenu/BookMenu.gd b/Assets/UI/BookMenu/BookMenu.gd index 47ed1422..f42f14b1 100644 --- a/Assets/UI/BookMenu/BookMenu.gd +++ b/Assets/UI/BookMenu/BookMenu.gd @@ -35,13 +35,9 @@ func set_has_delete_button(new_has_delete_button: bool) -> void: has_delete_button = new_has_delete_button - var callback_function = "_on_DeleteButton_pressed" - for page in %Pages.get_children(): var delete_button := page.find_child("DeleteButton") as TextureButton - var _callback_function = Callable(self, callback_function) - if not delete_button.pressed.is_connected(_callback_function): - delete_button.pressed.connect(_callback_function) + SignalUtils.ensure_connected(delete_button.pressed, _on_DeleteButton_pressed) delete_button.visible = has_delete_button func set_has_cancel_button(new_has_cancel_button: bool) -> void: @@ -50,9 +46,6 @@ func set_has_cancel_button(new_has_cancel_button: bool) -> void: has_cancel_button = new_has_cancel_button - var pressed_signal := "pressed" - var callback_function := "_on_CancelButton_pressed" - for page in %Pages.get_children(): # Place CancelButton on # left side for one-paged menus @@ -62,9 +55,7 @@ func set_has_cancel_button(new_has_cancel_button: bool) -> void: cancel_button = page.find_child("RightPageControls").find_child("CancelButton") as TextureButton else: cancel_button = page.find_child("CancelButton") as TextureButton - var _callback_function = Callable(self, callback_function) - if not cancel_button.is_connected(pressed_signal, _callback_function): - cancel_button.connect(pressed_signal, _callback_function) + SignalUtils.ensure_connected(cancel_button.pressed, _on_CancelButton_pressed) cancel_button.visible = has_cancel_button @@ -74,14 +65,9 @@ func set_has_ok_button(new_has_ok_button: bool) -> void: has_ok_button = new_has_ok_button - var pressed_signal := "pressed" - var callback_function := "_on_OKButton_pressed" - for page in %Pages.get_children(): var ok_button := page.find_child("OKButton") as TextureButton - var _callback_function = Callable(self, callback_function) - if not ok_button.is_connected(pressed_signal, _callback_function): - ok_button.connect("pressed", _on_OKButton_pressed) + SignalUtils.ensure_connected(ok_button.pressed, _on_OKButton_pressed) ok_button.visible = has_ok_button func _on_PrevButton_pressed() -> void: diff --git a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd index b3270a0f..5501febf 100644 --- a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd +++ b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd @@ -35,8 +35,7 @@ func get_tab_container() -> TabContainer: for switch in get_parent().get_children(): switch.tab_container = tab_container - if !tab_container.tab_changed.is_connected(Callable(switch, "_on_TabContainer_tab_changed")): - tab_container.tab_changed.connect(Callable(switch, "_on_TabContainer_tab_changed")) + SignalUtils.ensure_connected(tab_container.tab_changed, _on_TabContainer_tab_changed) return tab_container From 165849d515ee65dc6b231f2cc987368982ef201e Mon Sep 17 00:00:00 2001 From: IcyLeave6109 <33421921+juliohq@users.noreply.github.com> Date: Mon, 4 Sep 2023 06:37:37 -0300 Subject: [PATCH 6/8] Remove redundant line Co-authored-by: Arthur S. Muszynski --- Utils/SignalUtils.gd | 1 - 1 file changed, 1 deletion(-) diff --git a/Utils/SignalUtils.gd b/Utils/SignalUtils.gd index 04e5c334..9f8250c6 100644 --- a/Utils/SignalUtils.gd +++ b/Utils/SignalUtils.gd @@ -1,6 +1,5 @@ class_name SignalUtils - ## Makes sure the given signal is connected to the given callable and connects it otherwise. static func ensure_connected(_signal: Signal, callable: Callable): assert(callable.is_valid()) From 7482cf7834ed034805cfb15a46501e2f79afdb8d Mon Sep 17 00:00:00 2001 From: IcyLeave6109 <33421921+juliohq@users.noreply.github.com> Date: Mon, 4 Sep 2023 06:37:51 -0300 Subject: [PATCH 7/8] Remove another redundant line Co-authored-by: Arthur S. Muszynski --- Utils/SignalUtils.gd | 1 - 1 file changed, 1 deletion(-) diff --git a/Utils/SignalUtils.gd b/Utils/SignalUtils.gd index 9f8250c6..61ba6c98 100644 --- a/Utils/SignalUtils.gd +++ b/Utils/SignalUtils.gd @@ -6,7 +6,6 @@ static func ensure_connected(_signal: Signal, callable: Callable): if not _signal.is_connected(callable): _signal.connect(callable) - ## Makes sure the given signal is not connected to the given callable and disconnects it otherwise. static func ensure_disconnected(_signal: Signal, callable: Callable): assert(callable.is_valid()) From 088a3ddc1819843a4203db91e746ab8679219d65 Mon Sep 17 00:00:00 2001 From: juliohq Date: Mon, 4 Sep 2023 06:53:36 -0300 Subject: [PATCH 8/8] Move `ensure_connected` and `ensure_disconnected` functions --- Assets/Player/PlayerCamera.gd | 4 ++-- Assets/UI/BookMenu/BookMenu.gd | 12 ++++++------ Assets/UI/Pages/OptionsUI/OptionsUI.gd | 12 ++++++------ Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd | 2 +- Assets/World/Utils.gd | 12 ++++++++++++ Utils/SignalUtils.gd | 13 ------------- 6 files changed, 27 insertions(+), 28 deletions(-) delete mode 100644 Utils/SignalUtils.gd diff --git a/Assets/Player/PlayerCamera.gd b/Assets/Player/PlayerCamera.gd index 7b1aef77..605c48f6 100644 --- a/Assets/Player/PlayerCamera.gd +++ b/Assets/Player/PlayerCamera.gd @@ -152,8 +152,8 @@ func switch_context(new_context: InteractionContext) -> void: if active_context: active_context._on_exit() active_context = new_context - SignalUtils.ensure_connected(active_context.switch_context, switch_context) - SignalUtils.ensure_connected(active_context.context_aborted, abort_context) + Utils.ensure_connected(active_context.switch_context, switch_context) + Utils.ensure_connected(active_context.context_aborted, abort_context) active_context._on_enter() func abort_context() -> void: diff --git a/Assets/UI/BookMenu/BookMenu.gd b/Assets/UI/BookMenu/BookMenu.gd index f42f14b1..77ff686e 100644 --- a/Assets/UI/BookMenu/BookMenu.gd +++ b/Assets/UI/BookMenu/BookMenu.gd @@ -19,12 +19,12 @@ func _ready() -> void: for page in %Pages.get_children(): if %Pages.get_tab_count() > 1: page_control = page.find_child("PageControl") - SignalUtils.ensure_connected(page_control.get_node("PrevButton").pressed, _on_PrevButton_pressed) - SignalUtils.ensure_connected(page_control.get_node("NextButton").pressed, _on_NextButton_pressed) + Utils.ensure_connected(page_control.get_node("PrevButton").pressed, _on_PrevButton_pressed) + Utils.ensure_connected(page_control.get_node("NextButton").pressed, _on_NextButton_pressed) page_control.visible = true - SignalUtils.ensure_connected(%Pages.tab_changed, _on_Pages_tab_changed) + Utils.ensure_connected(%Pages.tab_changed, _on_Pages_tab_changed) # Force-call to determine initial state of page controls (enabled/disabled) %Pages.tab_changed.emit(%Pages.current_tab) @@ -37,7 +37,7 @@ func set_has_delete_button(new_has_delete_button: bool) -> void: for page in %Pages.get_children(): var delete_button := page.find_child("DeleteButton") as TextureButton - SignalUtils.ensure_connected(delete_button.pressed, _on_DeleteButton_pressed) + Utils.ensure_connected(delete_button.pressed, _on_DeleteButton_pressed) delete_button.visible = has_delete_button func set_has_cancel_button(new_has_cancel_button: bool) -> void: @@ -55,7 +55,7 @@ func set_has_cancel_button(new_has_cancel_button: bool) -> void: cancel_button = page.find_child("RightPageControls").find_child("CancelButton") as TextureButton else: cancel_button = page.find_child("CancelButton") as TextureButton - SignalUtils.ensure_connected(cancel_button.pressed, _on_CancelButton_pressed) + Utils.ensure_connected(cancel_button.pressed, _on_CancelButton_pressed) cancel_button.visible = has_cancel_button @@ -67,7 +67,7 @@ func set_has_ok_button(new_has_ok_button: bool) -> void: for page in %Pages.get_children(): var ok_button := page.find_child("OKButton") as TextureButton - SignalUtils.ensure_connected(ok_button.pressed, _on_OKButton_pressed) + Utils.ensure_connected(ok_button.pressed, _on_OKButton_pressed) ok_button.visible = has_ok_button func _on_PrevButton_pressed() -> void: diff --git a/Assets/UI/Pages/OptionsUI/OptionsUI.gd b/Assets/UI/Pages/OptionsUI/OptionsUI.gd index 62952213..a2a0980a 100644 --- a/Assets/UI/Pages/OptionsUI/OptionsUI.gd +++ b/Assets/UI/Pages/OptionsUI/OptionsUI.gd @@ -73,7 +73,7 @@ func _ready() -> void: # Window mode settings["WindowMode"].options = Global.WINDOW_MODES.values() settings["WindowMode"].selected = Config.window_mode - SignalUtils.ensure_connected(settings["WindowMode"].item_selected, _on_WindowMode_item_selected) + Utils.ensure_connected(settings["WindowMode"].item_selected, _on_WindowMode_item_selected) settings["WindowMode"].item_selected.emit(Config.window_mode) # Populate with available resolutions @@ -81,20 +81,20 @@ func _ready() -> void: for screen_resolution_index in SCREEN_RESOLUTIONS.size(): if Config.screen_resolution == SCREEN_RESOLUTIONS[screen_resolution_index]: settings["ScreenResolution"].selected = screen_resolution_index - SignalUtils.ensure_connected(settings["ScreenResolution"].item_selected, _on_ScreenResolution_item_selected) + Utils.ensure_connected(settings["ScreenResolution"].item_selected, _on_ScreenResolution_item_selected) # Audio parameters settings["MasterVolume"].value = Config.master_volume - SignalUtils.ensure_connected(settings["MasterVolume"].value_changed, _on_MasterVolume_value_changed) + Utils.ensure_connected(settings["MasterVolume"].value_changed, _on_MasterVolume_value_changed) settings["MusicVolume"].value = Config.music_volume - SignalUtils.ensure_connected(settings["MusicVolume"].value_changed, _on_MusicVolume_value_changed) + Utils.ensure_connected(settings["MusicVolume"].value_changed, _on_MusicVolume_value_changed) settings["EffectsVolume"].value = Config.effects_volume - SignalUtils.ensure_connected(settings["EffectsVolume"].value_changed, _on_EffectsVolume_value_changed) + Utils.ensure_connected(settings["EffectsVolume"].value_changed, _on_EffectsVolume_value_changed) settings["VoiceVolume"].value = Config.voice_volume - SignalUtils.ensure_connected(settings["VoiceVolume"].value_changed, _on_VoiceVolume_value_changed) + Utils.ensure_connected(settings["VoiceVolume"].value_changed, _on_VoiceVolume_value_changed) func populate_dropdown(dropdown: OptionButton, items: Dictionary) -> void: for item in items.values(): diff --git a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd index 5501febf..2e3ea463 100644 --- a/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd +++ b/Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd @@ -35,7 +35,7 @@ func get_tab_container() -> TabContainer: for switch in get_parent().get_children(): switch.tab_container = tab_container - SignalUtils.ensure_connected(tab_container.tab_changed, _on_TabContainer_tab_changed) + Utils.ensure_connected(tab_container.tab_changed, _on_TabContainer_tab_changed) return tab_container diff --git a/Assets/World/Utils.gd b/Assets/World/Utils.gd index ff240457..d41052d6 100644 --- a/Assets/World/Utils.gd +++ b/Assets/World/Utils.gd @@ -11,3 +11,15 @@ static func map_3i_to_2i(vector_3: Vector3i) -> Vector2i: static func map_2i_to_3i(vector_2: Vector2i) -> Vector3i: return Vector3i(vector_2.x, 0, vector_2.y) + +## Makes sure the given signal is connected to the given callable and connects it otherwise. +static func ensure_connected(_signal: Signal, callable: Callable): + assert(callable.is_valid()) + if not _signal.is_connected(callable): + _signal.connect(callable) + +## Makes sure the given signal is not connected to the given callable and disconnects it otherwise. +static func ensure_disconnected(_signal: Signal, callable: Callable): + assert(callable.is_valid()) + if _signal.is_connected(callable): + _signal.disconnect(callable) diff --git a/Utils/SignalUtils.gd b/Utils/SignalUtils.gd deleted file mode 100644 index 61ba6c98..00000000 --- a/Utils/SignalUtils.gd +++ /dev/null @@ -1,13 +0,0 @@ -class_name SignalUtils - -## Makes sure the given signal is connected to the given callable and connects it otherwise. -static func ensure_connected(_signal: Signal, callable: Callable): - assert(callable.is_valid()) - if not _signal.is_connected(callable): - _signal.connect(callable) - -## Makes sure the given signal is not connected to the given callable and disconnects it otherwise. -static func ensure_disconnected(_signal: Signal, callable: Callable): - assert(callable.is_valid()) - if _signal.is_connected(callable): - _signal.disconnect(callable)