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).
This commit is contained in:
david.ongenaert 2021-05-19 13:17:30 +02:00
parent f968ed153b
commit 1b07f905af
3 changed files with 56 additions and 56 deletions

View File

@ -1,17 +1,15 @@
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
var screen_reader
var TTS
func get_siblings(): func get_siblings():
var parent = node.get_parent() var parent = node.get_parent()
@ -21,7 +19,7 @@ func get_siblings():
func click(item := node, button_index = BUTTON_LEFT): func click(item := node, button_index = BUTTON_LEFT):
if logging: if screen_reader.logging:
print_debug("Click") print_debug("Click")
var click = InputEventMouseButton.new() var click = InputEventMouseButton.new()
click.button_index = button_index click.button_index = button_index
@ -84,7 +82,7 @@ func _accept_dialog_focused():
func _accept_dialog_about_to_show(): func _accept_dialog_about_to_show():
_accept_dialog_speak() _accept_dialog_speak()
should_stop_on_focus = false screen_reader.should_stop_on_focus = false
func _basebutton_button_down(): func _basebutton_button_down():
@ -188,7 +186,7 @@ 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()
if logging: if screen_reader.logging:
print_debug(selected) print_debug(selected)
if len(selected) == 0: if len(selected) == 0:
if node.get_item_count() == 0: if node.get_item_count() == 0:
@ -320,7 +318,7 @@ func popup_menu_focused():
func popup_menu_item_id_focused(index): func popup_menu_item_id_focused(index):
if logging: if screen_reader.logging:
print_debug("item id focus %s" % index) 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)
@ -595,11 +593,11 @@ func tab_container_input(event):
func focused(): func focused():
if logging: if screen_reader.logging:
print_debug("Focus: %s" % node) print_debug("Focus: %s" % node)
if should_stop_on_focus: if screen_reader.should_stop_on_focus:
TTS.stop() TTS.stop()
should_stop_on_focus = true screen_reader.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:
@ -643,7 +641,7 @@ func focused():
tree_focused() tree_focused()
else: else:
TTS.speak(node.get_class(), true) TTS.speak(node.get_class(), true)
if logging: if screen_reader.logging:
print_debug("No handler") 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)
@ -744,9 +742,9 @@ func editor_inspector_section_input(event):
TTS.speak("collapsed", true) TTS.speak("collapsed", true)
func _init(node, tts, console_logging: bool): func _init(node, reader, tts):
screen_reader = reader
TTS = tts 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,38 +1,40 @@
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
var TTS
var editor_settings
const settings_path = "interface/accessibility/"
func _initialise_settings() -> void: func _initialise_settings() -> void:
_add_setting("In_Editor", TYPE_BOOL, true) editor_settings = get_editor_interface().get_editor_settings()
_add_setting("Logging", TYPE_BOOL, true) _add_setting("In_Editor", TYPE_BOOL, false)
_add_setting("Logging", TYPE_BOOL, false)
_add_setting("Speech_Rate", TYPE_INT, 50) _add_setting("Speech_Rate", TYPE_INT, 50)
ProjectSettings.save()
func _add_setting(title: String, type: int, value, hint_type: int = -1, hint_string = "") -> void: func _add_setting(title: String, type: int, value, hint_type: int = -1, hint_string = "") -> void:
title = title.insert(0, "Accessibility/") title = title.insert(0, settings_path)
if ProjectSettings.has_setting(title): if editor_settings.has_setting(title):
return return
ProjectSettings.set(title, value) editor_settings.set(title, value)
var prop: Dictionary = {} var prop: Dictionary = {}
prop["name"] = title prop["name"] = title
prop["type"] = type prop["type"] = type
if hint_type > -1: if hint_type > -1:
prop["hint"] = hint_type prop["hint"] = hint_type
prop["hint_string"] = hint_string prop["hint_string"] = hint_string
ProjectSettings.add_property_info(prop) editor_settings.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 or not screen_reader.enabled:
return return
TTS.speak("%s: screen" % screen, false) TTS.speak("%s: screen" % screen, false)
var control = screen_reader.find_focusable_control(get_tree().root) var control = screen_reader.find_focusable_control(get_tree().root)
@ -48,13 +50,14 @@ func set_initial_screen_focus(screen):
func _enter_tree(): func _enter_tree():
_initialise_settings() _initialise_settings()
TTS = TtsClass.new() var rate = editor_settings.get_setting(settings_path + "Speech_Rate")
var rate = ProjectSettings.get_setting("Accessibility/Speech_Rate") var logging = editor_settings.get_setting(settings_path + "Logging")
TTS.call_deferred("_set_rate", rate) if editor_settings.get_setting(settings_path + "In_Editor"):
var logging = ProjectSettings.get_setting("Accessibility/Logging") screen_reader = ScreenReader.new()
if ProjectSettings.get_setting("Accessibility/In_Editor"):
screen_reader = ScreenReader.new(TTS, logging)
screen_reader.enable_focus_mode = true 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) 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)

View File

@ -11,11 +11,11 @@ signal swipe_down
var Accessible = preload("Accessible.gd") var Accessible = preload("Accessible.gd")
var TtsClass = preload("../godot-tts/TTS.gd")
var TTS var TTS
var logging export var enabled = true
export var enabled = true setget _set_enabled, _get_enabled
export var min_swipe_distance = 5 export var min_swipe_distance = 5
@ -25,24 +25,16 @@ export var explore_by_touch_interval = 200
export var enable_focus_mode = false export var enable_focus_mode = false
func _set_enabled(v): export var logging = true
if enabled:
augment_tree(get_tree().root)
else:
for accessible in get_tree().get_nodes_in_group("accessibles"):
accessible.queue_free()
enabled = v
var should_stop_on_focus = true
func _get_enabled():
return enabled
func augment_node(node): func augment_node(node):
if not enabled: if not enabled:
return return
if node is Control: if node is Control:
Accessible.new(node, TTS, logging) Accessible.new(node, self, TTS)
func augment_tree(node): func augment_tree(node):
@ -69,20 +61,27 @@ func find_focusable_control(node):
return null return null
func _init(tts, console_logging: bool): func _init():
TTS = tts TTS = TtsClass.new()
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:
augment_tree(get_tree().root) augment_tree(get_tree().root)
get_tree().connect("node_added", self, "augment_node") else:
connect("swipe_right", self, "swipe_right") for accessible in get_tree().get_nodes_in_group("accessibles"):
connect("swipe_left", self, "swipe_left") accessible.queue_free()
connect("swipe_up", self, "swipe_up") _safe_connect(get_tree(), "node_added", self, "augment_node")
connect("swipe_down", self, "swipe_down") _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): func _press_and_release(action, fake_via_keyboard = false):