From f968ed153b7e28220b3d90c2302fa79c67fe5551 Mon Sep 17 00:00:00 2001 From: "david.ongenaert" Date: Tue, 18 May 2021 22:06:26 +0200 Subject: [PATCH] Accessibility configurable from project settings - Move settings from separate .INI to project settings - Replace autoloaded TTS instance with dependency injection - Make loggin configurable Inspiration for how to do project settings: WAT-Sharp --- Accessible.gd | 31 ++++++++++++++++++++++--------- Plugin.gd | 47 +++++++++++++++++++++++++++++++++-------------- ScreenReader.gd | 13 ++++++++++--- 3 files changed, 65 insertions(+), 26 deletions(-) diff --git a/Accessible.gd b/Accessible.gd index 54ed7c7..a9ed9d5 100644 --- a/Accessible.gd +++ b/Accessible.gd @@ -1,7 +1,13 @@ extends Node +var TTS + var node +var should_stop_on_focus = true + +var logging + var position_in_children = 0 var column_in_row = 0 @@ -15,7 +21,8 @@ func get_siblings(): func click(item := node, button_index = BUTTON_LEFT): - print_debug("Click") + if logging: + print_debug("Click") var click = InputEventMouseButton.new() click.button_index = button_index click.pressed = true @@ -77,7 +84,7 @@ func _accept_dialog_focused(): func _accept_dialog_about_to_show(): _accept_dialog_speak() - ScreenReader.should_stop_on_focus = false + should_stop_on_focus = false func _basebutton_button_down(): @@ -181,7 +188,8 @@ func item_list_item_focused(idx): func item_list_focused(): var count = node.get_item_count() var selected = node.get_selected_items() - print_debug(selected) + if logging: + print_debug(selected) if len(selected) == 0: if node.get_item_count() == 0: return TTS.speak("list, 0 items", false) @@ -312,7 +320,8 @@ func popup_menu_focused(): func popup_menu_item_id_focused(index): - print_debug("item id focus %s" % index) + if logging: + print_debug("item id focus %s" % index) var tokens = PoolStringArray([]) var shortcut = node.get_item_shortcut(index) var name @@ -586,10 +595,11 @@ func tab_container_input(event): func focused(): - print_debug("Focus: %s" % node) - if ScreenReader.should_stop_on_focus: + if logging: + print_debug("Focus: %s" % node) + if should_stop_on_focus: TTS.stop() - ScreenReader.should_stop_on_focus = true + should_stop_on_focus = true if not node is Label: var label = _guess_label() if label: @@ -633,7 +643,8 @@ func focused(): tree_focused() else: TTS.speak(node.get_class(), true) - print_debug("No handler") + if logging: + print_debug("No handler") if node.hint_tooltip and not spoke_hint_tooltip: TTS.speak(node.hint_tooltip, false) spoke_hint_tooltip = false @@ -733,7 +744,9 @@ func editor_inspector_section_input(event): TTS.speak("collapsed", true) -func _init(node): +func _init(node, tts, console_logging: bool): + 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 8be5376..cbd4f9b 100644 --- a/Plugin.gd +++ b/Plugin.gd @@ -1,11 +1,36 @@ tool extends EditorPlugin +var TTS + +var TtsClass = preload("../godot-tts/TTS.gd") + var ScreenReader = preload("ScreenReader.gd") var screen_reader +func _initialise_settings() -> void: + _add_setting("In_Editor", TYPE_BOOL, true) + _add_setting("Logging", TYPE_BOOL, true) + _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): + return + ProjectSettings.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) + + func set_initial_screen_focus(screen): if not screen_reader.enabled: return @@ -22,19 +47,13 @@ func set_initial_screen_focus(screen): func _enter_tree(): - var editor_accessibility_enabled = true - add_autoload_singleton("TTS", "res://addons/godot-tts/TTS.gd") - var rate = TTS.normal_rate - var config = ConfigFile.new() - var err = config.load("res://.godot-accessibility-editor-settings.ini") - if not err: - editor_accessibility_enabled = config.get_value( - "global", "editor_accessibility_enabled", true - ) - rate = config.get_value("speech", "rate", 50) - if editor_accessibility_enabled: - TTS.call_deferred("_set_rate", rate) - screen_reader = ScreenReader.new() + _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) screen_reader.enable_focus_mode = true get_tree().root.call_deferred("add_child", screen_reader) add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null) @@ -48,7 +67,7 @@ var _focus_loss_interval = 0 func _process(delta): - if not screen_reader.enabled: + if not screen_reader or not screen_reader.enabled: return var focus = screen_reader.find_focusable_control(get_tree().root) focus = focus.get_focus_owner() diff --git a/ScreenReader.gd b/ScreenReader.gd index 6f17edc..ebfdb46 100644 --- a/ScreenReader.gd +++ b/ScreenReader.gd @@ -11,6 +11,10 @@ signal swipe_down var Accessible = preload("Accessible.gd") +var TTS + +var logging + export var enabled = true setget _set_enabled, _get_enabled export var min_swipe_distance = 5 @@ -21,8 +25,6 @@ export var explore_by_touch_interval = 200 export var enable_focus_mode = false -var should_stop_on_focus = true - func _set_enabled(v): if enabled: augment_tree(get_tree().root) @@ -40,7 +42,7 @@ func augment_node(node): if not enabled: return if node is Control: - Accessible.new(node) + Accessible.new(node, TTS, logging) func augment_tree(node): @@ -67,6 +69,11 @@ func find_focusable_control(node): return null +func _init(tts, console_logging: bool): + TTS = tts + logging = console_logging + + func _enter_tree(): pause_mode = Node.PAUSE_MODE_PROCESS if enabled: