Skip to content

Commit 64b1cf0

Browse files
Subsystem Choices: Add helper methods to focus and select choices (#2499)
Choices subsystem now has these methods to focus and select choices from code: - focus_choice(index) - get_choice_button(index) - select_choice(index) - select_focused_choice() --------- Co-authored-by: Jowan-Spooner <raban-loeffler@posteo.de>
1 parent dcf0038 commit 64b1cf0

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

addons/dialogic/Modules/Choice/subsystem_choices.gd

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func post_install() -> void:
7070
func hide_all_choices() -> void:
7171
for node in get_tree().get_nodes_in_group('dialogic_choice_button'):
7272
node.hide()
73-
if node.is_connected('button_up', _on_choice_selected):
74-
node.disconnect('button_up', _on_choice_selected)
73+
if node.pressed.is_connected(_on_choice_selected):
74+
node.pressed.disconnect(_on_choice_selected)
7575

7676

7777
## Collects information on all the choices of the current question.
@@ -156,7 +156,7 @@ func show_current_question(instant:=true) -> void:
156156
var question_info := get_current_question_info()
157157

158158
for choice in question_info.choices:
159-
var node: DialogicNode_ChoiceButton = get_choice_button_node(choice.button_index)
159+
var node: DialogicNode_ChoiceButton = get_choice_button(choice.button_index)
160160

161161
if not node:
162162
missing_button = true
@@ -193,8 +193,24 @@ func show_current_question(instant:=true) -> void:
193193
printerr("[Dialogic] The layout you are using doesn't have enough Choice Buttons for the choices you are trying to display.")
194194

195195

196+
func focus_choice(button_index:int) -> void:
197+
var node: DialogicNode_ChoiceButton = get_choice_button(button_index)
198+
if node:
199+
node.grab_focus()
200+
201+
202+
func select_choice(button_index:int) -> void:
203+
var node: DialogicNode_ChoiceButton = get_choice_button(button_index)
204+
if node:
205+
node.pressed.emit()
206+
207+
208+
func select_focused_choice() -> void:
209+
if get_viewport().gui_get_focus_owner() is DialogicNode_ChoiceButton:
210+
(get_viewport().gui_get_focus_owner() as DialogicNode_ChoiceButton).pressed.emit()
211+
196212

197-
func get_choice_button_node(button_index:int) -> DialogicNode_ChoiceButton:
213+
func get_choice_button(button_index:int) -> DialogicNode_ChoiceButton:
198214
var idx := 1
199215
for node: DialogicNode_ChoiceButton in get_tree().get_nodes_in_group('dialogic_choice_button'):
200216
if !node.get_parent().is_visible_in_tree():
@@ -229,6 +245,7 @@ func _on_choice_selected(choice_info := {}) -> void:
229245

230246

231247

248+
## Returns the indexes of the choice events related to the current question.
232249
func get_current_choice_indexes() -> Array:
233250
var choices := []
234251
var evt_idx := dialogic.current_event_idx
@@ -252,9 +269,10 @@ func get_current_choice_indexes() -> Array:
252269
return choices
253270

254271

272+
## Forward the dialogic action to the focused button
255273
func _on_dialogic_action() -> void:
256-
if get_viewport().gui_get_focus_owner() is DialogicNode_ChoiceButton and use_input_action and not dialogic.Inputs.input_was_mouse_input:
257-
get_viewport().gui_get_focus_owner().pressed.emit()
274+
if use_input_action and not dialogic.Inputs.input_was_mouse_input:
275+
select_focused_choice()
258276

259277

260278
#endregion

0 commit comments

Comments
 (0)