Use global TTS singleton to avoid passing around instances.

This commit is contained in:
Nolan Darilek 2019-10-02 16:20:25 -05:00
parent 8ef356b9bb
commit 0e8b6ae9e8
3 changed files with 46 additions and 53 deletions

View File

@ -1,7 +1,5 @@
extends Object extends Object
var tts
var node var node
var position_in_children = 0 var position_in_children = 0
@ -50,14 +48,14 @@ func checkbox_focus():
else: else:
tokens.append("unchecked") tokens.append("unchecked")
tokens.append(" checkbox") tokens.append(" checkbox")
tts.speak(tokens.join(" "), false) TTS.speak(tokens.join(" "), false)
func checkbox_toggled(checked): func checkbox_toggled(checked):
if node.has_focus(): if node.has_focus():
if checked: if checked:
tts.speak("checked", true) TTS.speak("checked", true)
else: else:
tts.speak("unchecked", true) TTS.speak("unchecked", true)
var spoke_hint_tooltip var spoke_hint_tooltip
@ -69,10 +67,10 @@ func button_focus():
spoke_hint_tooltip = true spoke_hint_tooltip = true
tokens.append(node.hint_tooltip) tokens.append(node.hint_tooltip)
tokens.append("button") tokens.append("button")
tts.speak(tokens.join(": "), false) TTS.speak(tokens.join(": "), false)
func texturebutton_focus(): func texturebutton_focus():
tts.speak("button", false) TTS.speak("button", false)
func item_list_item_focused(idx): func item_list_item_focused(idx):
var tokens = PoolStringArray([]) var tokens = PoolStringArray([])
@ -83,23 +81,23 @@ func item_list_item_focused(idx):
if text: if text:
tokens.append(text) tokens.append(text)
tokens.append("%s of %s" % [idx + 1, node.get_item_count()]) tokens.append("%s of %s" % [idx + 1, node.get_item_count()])
tts.speak(tokens.join(": ")) TTS.speak(tokens.join(": "))
func item_list_focus(): func item_list_focus():
var count = node.get_item_count() var count = node.get_item_count()
var selected = node.get_selected_items()[0] var selected = node.get_selected_items()[0]
position_in_children = selected position_in_children = selected
tts.speak("list, %s %s" % [count, tts.singular_or_plural(count, "item", "items")], false) TTS.speak("list, %s %s" % [count, TTS.singular_or_plural(count, "item", "items")], false)
item_list_item_focused(selected) item_list_item_focused(selected)
func item_list_item_selected(index): func item_list_item_selected(index):
tts.speak("Selected", true) TTS.speak("Selected", true)
func item_list_multi_selected(index, selected): func item_list_multi_selected(index, selected):
tts.speak("Multiselect", false) TTS.speak("Multiselect", false)
func item_list_nothing_selected(): func item_list_nothing_selected():
tts.speak("Nothing selected", true) TTS.speak("Nothing selected", true)
func item_list_input(event): func item_list_input(event):
var old_pos = position_in_children var old_pos = position_in_children
@ -128,7 +126,7 @@ func label_focus():
var text = node.text var text = node.text
if text == "": if text == "":
text = "blank" text = "blank"
tts.speak(text, false) TTS.speak(text, false)
func line_edit_focus(): func line_edit_focus():
var text = "blank" var text = "blank"
@ -141,13 +139,13 @@ func line_edit_focus():
var type = "editable text" var type = "editable text"
if not node.editable: if not node.editable:
type = "text" type = "text"
tts.speak("%s: %s" % [text, type], false) TTS.speak("%s: %s" % [text, type], false)
func text_deleted(text): func text_deleted(text):
tts.speak("%s deleted" % text, true) TTS.speak("%s deleted" % text, true)
func text_inserted(text): func text_inserted(text):
tts.speak(text, true) TTS.speak(text, true)
var old_pos var old_pos
@ -156,9 +154,9 @@ func line_edit_input(event):
if old_pos != null and old_pos != pos: if old_pos != null and old_pos != pos:
var text = node.text var text = node.text
if pos > len(text)-1: if pos > len(text)-1:
tts.speak("blank", true) TTS.speak("blank", true)
else: else:
tts.speak(text[pos], true) TTS.speak(text[pos], true)
old_pos = pos old_pos = pos
elif old_pos == null: elif old_pos == null:
old_pos = pos old_pos = pos
@ -171,13 +169,13 @@ func menu_button_focus():
tokens.append(node.hint_tooltip) tokens.append(node.hint_tooltip)
spoke_hint_tooltip = true spoke_hint_tooltip = true
tokens.append("menu") tokens.append("menu")
tts.speak(tokens.join(": "), false) TTS.speak(tokens.join(": "), false)
func panel_focus(): func panel_focus():
tts.speak("panel", true) TTS.speak("panel", true)
func popup_menu_focus(): func popup_menu_focus():
tts.speak("menu", false) TTS.speak("menu", false)
func popup_menu_item_id_focus(index): func popup_menu_item_id_focus(index):
print("item id focus %s" % index) print("item id focus %s" % index)
@ -209,14 +207,14 @@ func popup_menu_item_id_focus(index):
if disabled: if disabled:
tokens.append("disabled") tokens.append("disabled")
tokens.append(str(index + 1) + " of " + str(node.get_item_count())) tokens.append(str(index + 1) + " of " + str(node.get_item_count()))
tts.speak(tokens.join(": "), true) TTS.speak(tokens.join(": "), true)
func popup_menu_item_id_pressed(index): func popup_menu_item_id_pressed(index):
if node.is_item_checkable(index): if node.is_item_checkable(index):
if node.is_item_checked(index): if node.is_item_checked(index):
tts.speak("checked", true) TTS.speak("checked", true)
else: else:
tts.speak("unchecked", true) TTS.speak("unchecked", true)
func tree_item_render(): func tree_item_render():
@ -232,7 +230,7 @@ func tree_item_render():
tokens.append("tree item") tokens.append("tree item")
if focused_tree_item.is_selected(0): if focused_tree_item.is_selected(0):
tokens.append("selected") tokens.append("selected")
tts.speak(tokens.join(": "), true) TTS.speak(tokens.join(": "), true)
func tree_item_deselect_all(item: TreeItem): func tree_item_deselect_all(item: TreeItem):
for i in range(node.columns): for i in range(node.columns):
@ -270,7 +268,7 @@ func tree_item_selected():
var button_count = cell.get_button_count(i) var button_count = cell.get_button_count(i)
if button_count != 0: if button_count != 0:
button_index = 0 button_index = 0
tokens.append(str(button_count) + " " + tts.singular_or_plural(button_count, "button", "buttons")) tokens.append(str(button_count) + " " + TTS.singular_or_plural(button_count, "button", "buttons"))
if cell.has_method("get_button_tooltip"): if cell.has_method("get_button_tooltip"):
var button_tooltip = cell.get_button_tooltip(i, button_index) var button_tooltip = cell.get_button_tooltip(i, button_index)
if button_tooltip: if button_tooltip:
@ -278,13 +276,13 @@ func tree_item_selected():
tokens.append("button") tokens.append("button")
if button_count > 1: if button_count > 1:
tokens.append("Use Home and End to switch focus.") tokens.append("Use Home and End to switch focus.")
tts.speak(tokens.join(": "), true) TTS.speak(tokens.join(": "), true)
func tree_item_multi_select(item, column, selected): func tree_item_multi_select(item, column, selected):
if selected: if selected:
tts.speak("selected", true) TTS.speak("selected", true)
else: else:
tts.speak("unselected", true) TTS.speak("unselected", true)
func tree_input(event): func tree_input(event):
var item = node.get_selected() var item = node.get_selected()
@ -324,29 +322,29 @@ func tree_input(event):
if tooltip: if tooltip:
tokens.append(tooltip) tokens.append(tooltip)
tokens.append("button") tokens.append("button")
tts.speak(tokens.join(": "), true) TTS.speak(tokens.join(": "), true)
func tree_focus(): func tree_focus():
if node.get_selected(): if node.get_selected():
tree_item_render() tree_item_render()
else: else:
tts.speak("tree", true) TTS.speak("tree", true)
func tree_item_collapse(item): func tree_item_collapse(item):
if node.has_focus(): if node.has_focus():
if item.collapsed: if item.collapsed:
tts.speak("collapsed", true) TTS.speak("collapsed", true)
else: else:
tts.speak("expanded", true) TTS.speak("expanded", true)
func tab_container_focus(): func tab_container_focus():
var text = node.get_tab_title(node.current_tab) var text = node.get_tab_title(node.current_tab)
text += ": tab: " + str(node.current_tab + 1) + " of " + str(node.get_tab_count()) text += ": tab: " + str(node.current_tab + 1) + " of " + str(node.get_tab_count())
tts.speak(text, false) TTS.speak(text, false)
func tab_container_tab_changed(tab): func tab_container_tab_changed(tab):
if node.has_focus(): if node.has_focus():
tts.stop() TTS.stop()
tab_container_focus() tab_container_focus()
func tab_container_input(event): func tab_container_input(event):
@ -366,10 +364,10 @@ func tab_container_input(event):
func focus(): func focus():
print("Focus: %s" % node) print("Focus: %s" % node)
tts.stop() TTS.stop()
var label = guess_label() var label = guess_label()
if label: if label:
tts.speak(label, false) TTS.speak(label, false)
if node is MenuButton: if node is MenuButton:
menu_button_focus() menu_button_focus()
elif node is AcceptDialog: elif node is AcceptDialog:
@ -397,10 +395,10 @@ func focus():
elif node is Tree: elif node is Tree:
tree_focus() tree_focus()
else: else:
tts.speak(node.get_class(), true) TTS.speak(node.get_class(), true)
print("No handler") print("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
func unfocus(): func unfocus():
@ -418,7 +416,7 @@ func click_focus():
func gui_input(event): func gui_input(event):
if event is InputEventKey and Input.is_action_just_pressed("ui_accept") and event.control and event.alt: if event is InputEventKey and Input.is_action_just_pressed("ui_accept") and event.control and event.alt:
tts.speak("click", false) TTS.speak("click", false)
click() click()
elif event is InputEventKey and event.pressed and not event.echo and event.scancode == KEY_MENU: elif event is InputEventKey and event.pressed and not event.echo and event.scancode == KEY_MENU:
node.get_tree().root.warp_mouse(node.rect_global_position) node.get_tree().root.warp_mouse(node.rect_global_position)
@ -482,7 +480,7 @@ func editor_inspector_section_focus():
tokens.append("expanded") tokens.append("expanded")
else: else:
tokens.append("collapsed") tokens.append("collapsed")
tts.speak(tokens.join(": "), false) TTS.speak(tokens.join(": "), false)
func editor_inspector_section_input(event): func editor_inspector_section_input(event):
if event.is_action_pressed("ui_accept"): if event.is_action_pressed("ui_accept"):
@ -490,15 +488,14 @@ func editor_inspector_section_input(event):
var child = node.get_children()[0] var child = node.get_children()[0]
var expanded = child.is_visible_in_tree() var expanded = child.is_visible_in_tree()
if expanded: if expanded:
tts.speak("expanded", true) TTS.speak("expanded", true)
else: else:
tts.speak("collapsed", true) TTS.speak("collapsed", true)
func _init(tts, node): func _init(node):
if node.is_in_group("accessible"): if node.is_in_group("accessible"):
return return
node.add_to_group("accessible") node.add_to_group("accessible")
self.tts = tts
self.node = node self.node = node
if is_focusable(node): if is_focusable(node):
node.set_focus_mode(Control.FOCUS_ALL) node.set_focus_mode(Control.FOCUS_ALL)

View File

@ -6,6 +6,7 @@ var ScreenReader = preload("ScreenReader.gd")
var screen_reader var screen_reader
func _enter_tree(): func _enter_tree():
add_autoload_singleton("TTS", "res://addons/godot-tts/TTS.gd")
screen_reader = ScreenReader.new() screen_reader = ScreenReader.new()
get_tree().root.call_deferred("add_child", screen_reader) get_tree().root.call_deferred("add_child", screen_reader)
call_deferred("connect", "scene_changed", screen_reader, "set_initial_scene_focus") call_deferred("connect", "scene_changed", screen_reader, "set_initial_scene_focus")

View File

@ -3,13 +3,9 @@ extends Node
var Accessible = preload("Accessible.gd") var Accessible = preload("Accessible.gd")
var TTS = preload("../godot-tts/TTS.gd")
var tts
func augment_node(node): func augment_node(node):
if node is Control: if node is Control:
Accessible.new(tts, node) Accessible.new(node)
func augment_tree(node): func augment_tree(node):
augment_node(node) augment_node(node)
@ -17,7 +13,7 @@ func augment_tree(node):
augment_tree(child) augment_tree(child)
func set_initial_screen_focus(screen): func set_initial_screen_focus(screen):
tts.speak("%s: screen" % screen, false) TTS.speak("%s: screen" % screen, false)
var control = find_focusable_control(get_tree().root) var control = find_focusable_control(get_tree().root)
if control.get_focus_owner() != null: if control.get_focus_owner() != null:
return return
@ -47,6 +43,5 @@ func set_initial_scene_focus(scene):
focus.grab_focus() focus.grab_focus()
func _enter_tree(): func _enter_tree():
tts = TTS.new() TTS.rate = 255
tts.rate = 255
get_tree().connect("node_added", self, "augment_tree") get_tree().connect("node_added", self, "augment_tree")