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
This commit is contained in:
david.ongenaert 2021-05-18 22:06:26 +02:00
parent 783fcf3a3c
commit f968ed153b
3 changed files with 65 additions and 26 deletions

View File

@ -1,7 +1,13 @@
extends Node extends Node
var TTS
var node var node
var should_stop_on_focus = true
var logging
var position_in_children = 0 var position_in_children = 0
var column_in_row = 0 var column_in_row = 0
@ -15,7 +21,8 @@ func get_siblings():
func click(item := node, button_index = BUTTON_LEFT): func click(item := node, button_index = BUTTON_LEFT):
print_debug("Click") if logging:
print_debug("Click")
var click = InputEventMouseButton.new() var click = InputEventMouseButton.new()
click.button_index = button_index click.button_index = button_index
click.pressed = true click.pressed = true
@ -77,7 +84,7 @@ func _accept_dialog_focused():
func _accept_dialog_about_to_show(): func _accept_dialog_about_to_show():
_accept_dialog_speak() _accept_dialog_speak()
ScreenReader.should_stop_on_focus = false should_stop_on_focus = false
func _basebutton_button_down(): func _basebutton_button_down():
@ -181,7 +188,8 @@ func item_list_item_focused(idx):
func item_list_focused(): func item_list_focused():
var count = node.get_item_count() var count = node.get_item_count()
var selected = node.get_selected_items() var selected = node.get_selected_items()
print_debug(selected) if logging:
print_debug(selected)
if len(selected) == 0: if len(selected) == 0:
if node.get_item_count() == 0: if node.get_item_count() == 0:
return TTS.speak("list, 0 items", false) return TTS.speak("list, 0 items", false)
@ -312,7 +320,8 @@ func popup_menu_focused():
func popup_menu_item_id_focused(index): 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 tokens = PoolStringArray([])
var shortcut = node.get_item_shortcut(index) var shortcut = node.get_item_shortcut(index)
var name var name
@ -586,10 +595,11 @@ func tab_container_input(event):
func focused(): func focused():
print_debug("Focus: %s" % node) if logging:
if ScreenReader.should_stop_on_focus: print_debug("Focus: %s" % node)
if should_stop_on_focus:
TTS.stop() TTS.stop()
ScreenReader.should_stop_on_focus = true should_stop_on_focus = true
if not node is Label: if not node is Label:
var label = _guess_label() var label = _guess_label()
if label: if label:
@ -633,7 +643,8 @@ func focused():
tree_focused() tree_focused()
else: else:
TTS.speak(node.get_class(), true) 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: if node.hint_tooltip and not spoke_hint_tooltip:
TTS.speak(node.hint_tooltip, false) TTS.speak(node.hint_tooltip, false)
spoke_hint_tooltip = false spoke_hint_tooltip = false
@ -733,7 +744,9 @@ func editor_inspector_section_input(event):
TTS.speak("collapsed", true) TTS.speak("collapsed", true)
func _init(node): func _init(node, tts, console_logging: bool):
TTS = tts
logging = console_logging
name = "Accessible for " + node.name name = "Accessible for " + node.name
if node.is_in_group("accessible"): if node.is_in_group("accessible"):
return return

View File

@ -1,11 +1,36 @@
tool tool
extends EditorPlugin extends EditorPlugin
var TTS
var TtsClass = preload("../godot-tts/TTS.gd")
var ScreenReader = preload("ScreenReader.gd") var ScreenReader = preload("ScreenReader.gd")
var screen_reader 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): func set_initial_screen_focus(screen):
if not screen_reader.enabled: if not screen_reader.enabled:
return return
@ -22,19 +47,13 @@ func set_initial_screen_focus(screen):
func _enter_tree(): func _enter_tree():
var editor_accessibility_enabled = true _initialise_settings()
add_autoload_singleton("TTS", "res://addons/godot-tts/TTS.gd") TTS = TtsClass.new()
var rate = TTS.normal_rate var rate = ProjectSettings.get_setting("Accessibility/Speech_Rate")
var config = ConfigFile.new() TTS.call_deferred("_set_rate", rate)
var err = config.load("res://.godot-accessibility-editor-settings.ini") var logging = ProjectSettings.get_setting("Accessibility/Logging")
if not err: if ProjectSettings.get_setting("Accessibility/In_Editor"):
editor_accessibility_enabled = config.get_value( screen_reader = ScreenReader.new(TTS, logging)
"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()
screen_reader.enable_focus_mode = true screen_reader.enable_focus_mode = true
get_tree().root.call_deferred("add_child", screen_reader) get_tree().root.call_deferred("add_child", screen_reader)
add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null) add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null)
@ -48,7 +67,7 @@ var _focus_loss_interval = 0
func _process(delta): func _process(delta):
if not screen_reader.enabled: if not screen_reader or not screen_reader.enabled:
return return
var focus = screen_reader.find_focusable_control(get_tree().root) var focus = screen_reader.find_focusable_control(get_tree().root)
focus = focus.get_focus_owner() focus = focus.get_focus_owner()

View File

@ -11,6 +11,10 @@ signal swipe_down
var Accessible = preload("Accessible.gd") var Accessible = preload("Accessible.gd")
var TTS
var logging
export var enabled = true setget _set_enabled, _get_enabled export var enabled = true setget _set_enabled, _get_enabled
export var min_swipe_distance = 5 export var min_swipe_distance = 5
@ -21,8 +25,6 @@ export var explore_by_touch_interval = 200
export var enable_focus_mode = false export var enable_focus_mode = false
var should_stop_on_focus = true
func _set_enabled(v): func _set_enabled(v):
if enabled: if enabled:
augment_tree(get_tree().root) augment_tree(get_tree().root)
@ -40,7 +42,7 @@ func augment_node(node):
if not enabled: if not enabled:
return return
if node is Control: if node is Control:
Accessible.new(node) Accessible.new(node, TTS, logging)
func augment_tree(node): func augment_tree(node):
@ -67,6 +69,11 @@ func find_focusable_control(node):
return null return null
func _init(tts, console_logging: bool):
TTS = tts
logging = console_logging
func _enter_tree(): func _enter_tree():
pause_mode = Node.PAUSE_MODE_PROCESS pause_mode = Node.PAUSE_MODE_PROCESS
if enabled: if enabled: