1 extends DialogicSubsystem
3 ## Subsystem that manages showing and activating of choices.
5 ## Emitted when a choice button was pressed. Info includes the keys 'button_index', 'text', 'event_index'.
6 signal choice_selected(info:Dictionary)
7 ## Emitted when a set of choices is reached and shown.
8 ## Info includes the keys 'choices' (an array of dictionaries with infos on all the choices).
9 signal question_shown(info:Dictionary)
11 ## Contains information on the latest question.
12 var last_question_info := {}
14 ## The delay between the text finishing revealing and the choices appearing
15 var reveal_delay := 0.0
16 ## If true the player has to click to reveal choices when they are reached
17 var reveal_by_input := false
18 ## The delay between the choices becoming visible and being clickable. Can prevent accidental selection.
19 var block_delay := 0.2
20 ## If true, the first (top-most) choice will be focused
21 var autofocus_first_choice := true
22 ## If true the dialogic input action is used to trigger choices.
23 ## However mouse events will be ignored no matter what.
24 var use_input_action := false
26 enum FalseBehaviour {HIDE=0, DISABLE=1}
27 ## The behaviour of choices with a false condition and else_action set to DEFAULT.
28 var default_false_behaviour := FalseBehaviour.HIDE
30 enum HotkeyBehaviour {NONE, NUMBERS}
31 ## Will add some hotkeys to the choices if different then HotkeyBehaviour.NONE.
32 var hotkey_behaviour := HotkeyBehaviour.NONE
37 ## Used to block choices from being clicked for a couple of seconds (if delay is set in settings).
38 var _choice_blocker := Timer.new()
41 ####################################################################################################
43 func clear_game_state(_clear_flag:=DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
47 func _ready() -> void:
48 _choice_blocker.one_shot = true
49 DialogicUtil.update_timer_process_callback(_choice_blocker)
50 add_child(_choice_blocker)
52 reveal_delay = float(ProjectSettings.get_setting('dialogic/choices/reveal_delay', reveal_delay))
53 reveal_by_input = ProjectSettings.get_setting('dialogic/choices/reveal_by_input', reveal_by_input)
54 block_delay = ProjectSettings.get_setting('dialogic/choices/delay', block_delay)
55 autofocus_first_choice = ProjectSettings.get_setting('dialogic/choices/autofocus_first', autofocus_first_choice)
56 hotkey_behaviour = ProjectSettings.get_setting('dialogic/choices/hotkey_behaviour', hotkey_behaviour)
57 default_false_behaviour = ProjectSettings.get_setting('dialogic/choices/def_false_behaviour', default_false_behaviour)
60 func post_install() -> void:
61 dialogic.Inputs.dialogic_action.connect(_on_dialogic_action)
67 ####################################################################################################
69 ## Hides all choice buttons.
70 func hide_all_choices() -> void:
71 for node in get_tree().get_nodes_in_group('dialogic_choice_button'):
73 if node.is_connected('button_up', _on_choice_selected):
74 node.disconnect('button_up', _on_choice_selected)
77 ## Collects information on all the choices of the current question.
78 ## The result is a dictionary like this:
81 ## {'event_index':10, 'button_index':1, 'disabled':false, 'text':"My Choice", 'visible':true},
82 ## {'event_index':15, 'button_index':2, 'disabled':false, 'text':"My Choice2", 'visible':true},
84 func get_current_question_info() -> Dictionary:
85 var question_info := {'choices':[]}
88 last_question_info = {'choices':[]}
90 for choice_index in get_current_choice_indexes():
91 var event: DialogicEvent = dialogic.current_timeline_events[choice_index]
93 if not event is DialogicChoiceEvent:
96 var choice_event: DialogicChoiceEvent = event
98 choice_info['event_index'] = choice_index
99 choice_info['button_index'] = button_idx
102 var condition: String = choice_event.condition
104 if condition.is_empty() or dialogic.Expressions.execute_condition(choice_event.condition):
105 choice_info['disabled'] = false
106 choice_info['text'] = choice_event.get_property_translated('text')
107 choice_info['visible'] = true
110 choice_info['disabled'] = true
111 if not choice_event.disabled_text.is_empty():
112 choice_info['text'] = choice_event.get_property_translated('disabled_text')
114 choice_info['text'] = choice_event.get_property_translated('text')
116 var hide := choice_event.else_action == DialogicChoiceEvent.ElseActions.HIDE
117 hide = hide or choice_event.else_action == DialogicChoiceEvent.ElseActions.DEFAULT and default_false_behaviour == DialogicChoiceEvent.ElseActions.HIDE
118 choice_info['visible'] = not hide
123 choice_info.text = dialogic.Text.parse_text(choice_info.text, true, true, false, true, false, false)
125 choice_info.merge(choice_event.extra_data)
127 if dialogic.has_subsystem('History'):
128 choice_info['visited_before'] = dialogic.History.has_event_been_visited(choice_index)
130 question_info['choices'].append(choice_info)
135 ## Lists all current choices and shows buttons.
136 func show_current_question(instant:=true) -> void:
138 _choice_blocker.stop()
140 if !instant and (reveal_delay != 0 or reveal_by_input):
141 if reveal_delay != 0:
142 _choice_blocker.start(reveal_delay)
143 _choice_blocker.timeout.connect(show_current_question)
145 dialogic.Inputs.dialogic_action.connect(show_current_question)
148 if _choice_blocker.timeout.is_connected(show_current_question):
149 _choice_blocker.timeout.disconnect(show_current_question)
150 if dialogic.Inputs.dialogic_action.is_connected(show_current_question):
151 dialogic.Inputs.dialogic_action.disconnect(show_current_question)
153 var missing_button := false
155 var question_info := get_current_question_info()
157 for choice in question_info.choices:
158 var node: DialogicNode_ChoiceButton = get_choice_button_node(choice.button_index)
161 missing_button = true
164 node._load_info(choice)
166 if choice.button_index == 1 and autofocus_first_choice:
169 match hotkey_behaviour:
170 ## Add 1 to 9 as shortcuts if it's enabled
171 HotkeyBehaviour.NUMBERS:
172 if choice.button_index > 0 or choice.button_index < 10:
173 var shortcut: Shortcut
174 if node.shortcut != null:
175 shortcut = node.shortcut
177 shortcut = Shortcut.new()
179 var input_key := InputEventKey.new()
180 input_key.keycode = OS.find_keycode_from_string(str(choice.button_index))
181 shortcut.events.append(input_key)
182 node.shortcut = shortcut
184 if node.pressed.is_connected(_on_choice_selected):
185 node.pressed.disconnect(_on_choice_selected)
186 node.pressed.connect(_on_choice_selected.bind(choice))
188 _choice_blocker.start(block_delay)
189 question_shown.emit(question_info)
192 printerr("[Dialogic] The layout you are using doesn't have enough Choice Buttons for the choices you are trying to display.")
196 func get_choice_button_node(button_index:int) -> DialogicNode_ChoiceButton:
198 for node: DialogicNode_ChoiceButton in get_tree().get_nodes_in_group('dialogic_choice_button'):
199 if !node.get_parent().is_visible_in_tree():
201 if node.choice_index == button_index or (node.choice_index == -1 and idx == button_index):
204 if node.choice_index > 0:
205 idx = node.choice_index
211 func _on_choice_selected(choice_info := {}) -> void:
212 if dialogic.paused or not _choice_blocker.is_stopped():
215 if dialogic.has_subsystem('History'):
216 var all_choices: Array = dialogic.Choices.last_question_info['choices']
217 if dialogic.has_subsystem('VAR'):
218 dialogic.History.store_simple_history_entry(dialogic.VAR.parse_variables(choice_info.text), "Choice", {'all_choices': all_choices})
220 dialogic.History.store_simple_history_entry(choice_info.text, "Choice", {'all_choices': all_choices})
221 if dialogic.has_subsystem("History"):
222 dialogic.History.mark_event_as_visited(choice_info.event_index)
224 choice_selected.emit(choice_info)
226 dialogic.current_state = dialogic.States.IDLE
227 dialogic.handle_event(choice_info.event_index + 1)
231 func get_current_choice_indexes() -> Array:
233 var evt_idx := dialogic.current_event_idx
236 if evt_idx >= len(dialogic.current_timeline_events):
238 if dialogic.current_timeline_events[evt_idx] is DialogicChoiceEvent:
240 choices.append(evt_idx)
242 elif dialogic.current_timeline_events[evt_idx].can_contain_events:
248 if dialogic.current_timeline_events[evt_idx] is DialogicEndBranchEvent:
254 func _on_dialogic_action() -> void:
255 if get_viewport().gui_get_focus_owner() is DialogicNode_ChoiceButton and use_input_action and not dialogic.Inputs.input_was_mouse_input:
256 get_viewport().gui_get_focus_owner().pressed.emit()
263 ####################################################################################################
265 func is_question(index:int) -> bool:
266 if dialogic.current_timeline_events[index] is DialogicTextEvent:
267 if len(dialogic.current_timeline_events)-1 != index:
268 if dialogic.current_timeline_events[index+1] is DialogicChoiceEvent:
271 if dialogic.current_timeline_events[index] is DialogicChoiceEvent:
272 if index != 0 and dialogic.current_timeline_events[index-1] is DialogicEndBranchEvent:
273 if dialogic.current_timeline_events[dialogic.current_timeline_events[index-1].find_opening_index(index-1)] is DialogicChoiceEvent: