Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor signal connections and set theme override to null #118

Merged
merged 8 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Assets/Player/CameraControls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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(Callable(self, "_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:
Expand Down
2 changes: 1 addition & 1 deletion Assets/Player/Minimap/MinimapShipLayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions Assets/Player/PlayerCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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(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"))
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:
Expand Down
5 changes: 2 additions & 3 deletions Assets/UI/BasicControls/OptionButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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:
Expand Down
23 changes: 6 additions & 17 deletions Assets/UI/BookMenu/BookMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
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(Callable(self, "_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)
Expand All @@ -35,12 +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
if not delete_button.pressed.is_connected(Callable(self, callback_function)):
delete_button.pressed.connect(Callable(self, 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:
Expand All @@ -49,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
Expand All @@ -61,8 +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
if not cancel_button.is_connected(pressed_signal, Callable(self, callback_function)):
cancel_button.connect(pressed_signal, Callable(self, callback_function))
SignalUtils.ensure_connected(cancel_button.pressed, _on_CancelButton_pressed)
cancel_button.visible = has_cancel_button


Expand All @@ -72,13 +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
if not ok_button.is_connected(pressed_signal, Callable(self, callback_function)):
ok_button.connect("pressed", Callable(self, "_on_OKButton_pressed"))
SignalUtils.ensure_connected(ok_button.pressed, _on_OKButton_pressed)
ok_button.visible = has_ok_button

func _on_PrevButton_pressed() -> void:
Expand Down
8 changes: 2 additions & 6 deletions Assets/UI/Pages/HelpUI/HelpUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Assets/UI/Pages/NewGameUI/ColorSelection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Assets/UI/Pages/OptionsUI/OptionsUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,28 @@ 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"))
SignalUtils.ensure_connected(settings["WindowMode"].item_selected, _on_WindowMode_item_selected)
settings["WindowMode"].item_selected.emit(Config.window_mode)

# Populate with available resolutions
settings["ScreenResolution"].options = SCREEN_RESOLUTIONS
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"))
SignalUtils.ensure_connected(settings["ScreenResolution"].item_selected, _on_ScreenResolution_item_selected)

# Audio parameters
settings["MasterVolume"].value = Config.master_volume
settings["MasterVolume"].value_changed.connect(Callable(self, "_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(Callable(self, "_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(Callable(self, "_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(Callable(self, "_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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions Assets/UI/TabWidgets/Switches/SwitchTabWidget.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions Assets/UI/TabWidgets/TabWidget.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion Assets/UI/TabWidgets/WidgetDetail.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion Assets/World/Buildings/Building.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions Utils/SignalUtils.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class_name SignalUtils


juliohq marked this conversation as resolved.
Show resolved Hide resolved
## 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)


juliohq marked this conversation as resolved.
Show resolved Hide resolved
## 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)