From 1b07f905afc3b612be20ad2f83fdda4f91fa1f48 Mon Sep 17 00:00:00 2001 From: "david.ongenaert" Date: Wed, 19 May 2021 13:17:30 +0200 Subject: [PATCH] Move settings to Editor Settings and other fixes - Make logging an exported property of ScreenReader - Move should_stop_on_focus back to ScreenReader, where it was originally - Initialise TTS in ScreenReader instead of Plugin, so that it works in games too (that wasn't particularly smart of me) - Prevent connect() errors when ScreenReader._enter_tree() is called more than once (for example when moving the node in the tree). --- Accessible.gd | 30 ++++++++++++++---------------- Plugin.gd | 39 +++++++++++++++++++++------------------ ScreenReader.gd | 43 +++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Accessible.gd b/Accessible.gd index a9ed9d5..b70f189 100644 --- a/Accessible.gd +++ b/Accessible.gd @@ -1,17 +1,15 @@ extends Node -var TTS - var node -var should_stop_on_focus = true - -var logging - var position_in_children = 0 var column_in_row = 0 +var screen_reader + +var TTS + func get_siblings(): var parent = node.get_parent() @@ -21,7 +19,7 @@ func get_siblings(): func click(item := node, button_index = BUTTON_LEFT): - if logging: + if screen_reader.logging: print_debug("Click") var click = InputEventMouseButton.new() click.button_index = button_index @@ -84,7 +82,7 @@ func _accept_dialog_focused(): func _accept_dialog_about_to_show(): _accept_dialog_speak() - should_stop_on_focus = false + screen_reader.should_stop_on_focus = false func _basebutton_button_down(): @@ -188,7 +186,7 @@ func item_list_item_focused(idx): func item_list_focused(): var count = node.get_item_count() var selected = node.get_selected_items() - if logging: + if screen_reader.logging: print_debug(selected) if len(selected) == 0: if node.get_item_count() == 0: @@ -320,7 +318,7 @@ func popup_menu_focused(): func popup_menu_item_id_focused(index): - if logging: + if screen_reader.logging: print_debug("item id focus %s" % index) var tokens = PoolStringArray([]) var shortcut = node.get_item_shortcut(index) @@ -595,11 +593,11 @@ func tab_container_input(event): func focused(): - if logging: + if screen_reader.logging: print_debug("Focus: %s" % node) - if should_stop_on_focus: + if screen_reader.should_stop_on_focus: TTS.stop() - should_stop_on_focus = true + screen_reader.should_stop_on_focus = true if not node is Label: var label = _guess_label() if label: @@ -643,7 +641,7 @@ func focused(): tree_focused() else: TTS.speak(node.get_class(), true) - if logging: + if screen_reader.logging: print_debug("No handler") if node.hint_tooltip and not spoke_hint_tooltip: TTS.speak(node.hint_tooltip, false) @@ -744,9 +742,9 @@ func editor_inspector_section_input(event): TTS.speak("collapsed", true) -func _init(node, tts, console_logging: bool): +func _init(node, reader, tts): + screen_reader = reader TTS = tts - logging = console_logging name = "Accessible for " + node.name if node.is_in_group("accessible"): return diff --git a/Plugin.gd b/Plugin.gd index cbd4f9b..cc0dbb8 100644 --- a/Plugin.gd +++ b/Plugin.gd @@ -1,38 +1,40 @@ tool extends EditorPlugin -var TTS - -var TtsClass = preload("../godot-tts/TTS.gd") - var ScreenReader = preload("ScreenReader.gd") var screen_reader +var TTS + +var editor_settings + +const settings_path = "interface/accessibility/" + func _initialise_settings() -> void: - _add_setting("In_Editor", TYPE_BOOL, true) - _add_setting("Logging", TYPE_BOOL, true) + editor_settings = get_editor_interface().get_editor_settings() + _add_setting("In_Editor", TYPE_BOOL, false) + _add_setting("Logging", TYPE_BOOL, false) _add_setting("Speech_Rate", TYPE_INT, 50) - ProjectSettings.save() func _add_setting(title: String, type: int, value, hint_type: int = -1, hint_string = "") -> void: - title = title.insert(0, "Accessibility/") - if ProjectSettings.has_setting(title): + title = title.insert(0, settings_path) + if editor_settings.has_setting(title): return - ProjectSettings.set(title, value) + editor_settings.set(title, value) var prop: Dictionary = {} prop["name"] = title prop["type"] = type if hint_type > -1: prop["hint"] = hint_type prop["hint_string"] = hint_string - ProjectSettings.add_property_info(prop) + editor_settings.add_property_info(prop) func set_initial_screen_focus(screen): - if not screen_reader.enabled: + if not screen_reader or not screen_reader.enabled: return TTS.speak("%s: screen" % screen, false) var control = screen_reader.find_focusable_control(get_tree().root) @@ -48,13 +50,14 @@ func set_initial_screen_focus(screen): func _enter_tree(): _initialise_settings() - TTS = TtsClass.new() - var rate = ProjectSettings.get_setting("Accessibility/Speech_Rate") - TTS.call_deferred("_set_rate", rate) - var logging = ProjectSettings.get_setting("Accessibility/Logging") - if ProjectSettings.get_setting("Accessibility/In_Editor"): - screen_reader = ScreenReader.new(TTS, logging) + var rate = editor_settings.get_setting(settings_path + "Speech_Rate") + var logging = editor_settings.get_setting(settings_path + "Logging") + if editor_settings.get_setting(settings_path + "In_Editor"): + screen_reader = ScreenReader.new() screen_reader.enable_focus_mode = true + screen_reader.logging = logging + TTS = screen_reader.TTS + TTS.call_deferred("_set_rate", rate) get_tree().root.call_deferred("add_child", screen_reader) add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null) diff --git a/ScreenReader.gd b/ScreenReader.gd index ebfdb46..2910170 100644 --- a/ScreenReader.gd +++ b/ScreenReader.gd @@ -11,11 +11,11 @@ signal swipe_down var Accessible = preload("Accessible.gd") +var TtsClass = preload("../godot-tts/TTS.gd") + var TTS -var logging - -export var enabled = true setget _set_enabled, _get_enabled +export var enabled = true export var min_swipe_distance = 5 @@ -25,24 +25,16 @@ export var explore_by_touch_interval = 200 export var enable_focus_mode = false -func _set_enabled(v): - if enabled: - augment_tree(get_tree().root) - else: - for accessible in get_tree().get_nodes_in_group("accessibles"): - accessible.queue_free() - enabled = v +export var logging = true - -func _get_enabled(): - return enabled +var should_stop_on_focus = true func augment_node(node): if not enabled: return if node is Control: - Accessible.new(node, TTS, logging) + Accessible.new(node, self, TTS) func augment_tree(node): @@ -69,20 +61,27 @@ func find_focusable_control(node): return null -func _init(tts, console_logging: bool): - TTS = tts - logging = console_logging +func _init(): + TTS = TtsClass.new() func _enter_tree(): pause_mode = Node.PAUSE_MODE_PROCESS if enabled: augment_tree(get_tree().root) - get_tree().connect("node_added", self, "augment_node") - connect("swipe_right", self, "swipe_right") - connect("swipe_left", self, "swipe_left") - connect("swipe_up", self, "swipe_up") - connect("swipe_down", self, "swipe_down") + else: + for accessible in get_tree().get_nodes_in_group("accessibles"): + accessible.queue_free() + _safe_connect(get_tree(), "node_added", self, "augment_node") + _safe_connect(self, "swipe_right", self, "swipe_right") + _safe_connect(self, "swipe_left", self, "swipe_left") + _safe_connect(self, "swipe_up", self, "swipe_up") + _safe_connect(self, "swipe_down", self, "swipe_down") + + +func _safe_connect(to_object, signal_name, object, method, binds = [], flags = 0): + if not to_object.is_connected(signal_name, object, method): + to_object.connect(signal_name, object, method, binds, flags) func _press_and_release(action, fake_via_keyboard = false):