godot-accessibility/addons/accessibility/accessible.gd

235 lines
6.7 KiB
GDScript3
Raw Normal View History

extends Object
2019-09-02 12:04:45 +00:00
var tts
var node
2018-07-19 20:12:01 +00:00
var position_in_children = 0
2018-06-10 21:14:43 +00:00
func item_or_items(count):
if count == 1:
return "item"
else:
return "items"
2018-07-03 17:01:00 +00:00
func focus_button():
2019-09-02 12:04:45 +00:00
var text
2018-06-10 20:32:27 +00:00
if node.text:
text = node.text
if text:
tts.speak("%s: button" % text, false)
else:
tts.speak("button", false)
2018-06-10 20:32:27 +00:00
2018-07-03 17:01:00 +00:00
func focus_item_list():
var count = node.get_item_count()
var selected = node.get_selected_items()
tts.speak("list, %s %s" % [count, item_or_items(count)], false)
2019-09-02 12:04:45 +00:00
tts.speak(selected, false)
2018-07-03 17:01:00 +00:00
2019-09-02 12:04:45 +00:00
func item_list_item_selected(index):
tts.speak("Selected", false)
2018-07-03 17:01:00 +00:00
2019-09-02 12:04:45 +00:00
func item_list_multi_selected(index, selected):
tts.speak("Multiselect", false)
2018-07-03 17:01:00 +00:00
2019-09-02 12:04:45 +00:00
func item_list_nothing_selected():
tts.speak("Nothing selected", false)
2018-07-03 17:01:00 +00:00
func input_item_list(event):
2018-07-19 20:12:01 +00:00
var old_pos = position_in_children
if event.echo or not event.pressed:
return
if event.scancode == KEY_UP:
node.get_tree().set_input_as_handled()
2018-07-19 20:12:01 +00:00
if position_in_children == 0:
return
2018-07-19 20:12:01 +00:00
position_in_children -= 1
elif event.scancode == KEY_DOWN:
node.get_tree().set_input_as_handled()
2018-07-19 20:12:01 +00:00
if position_in_children >= node.get_item_count()-1:
return
2018-07-19 20:12:01 +00:00
position_in_children += 1
elif event.scancode == KEY_HOME:
2018-07-19 20:12:01 +00:00
position_in_children = 0
elif event.scancode == KEY_END:
2018-07-19 20:12:01 +00:00
position_in_children = node.get_item_count()-1
if old_pos != position_in_children:
var text = node.get_item_text(position_in_children)
tts.speak("%s: %s of %s" % [text, position_in_children+1, node.get_item_count()], false)
2018-06-10 21:14:43 +00:00
func focus_label():
2019-09-02 12:04:45 +00:00
var text = node.text
if text == "":
text = "blank"
tts.speak(text, false)
2018-07-03 17:01:00 +00:00
func focus_line_edit():
2018-06-10 20:32:27 +00:00
var text = "blank"
if node.secret:
text = "password"
elif node.text != "":
text = node.text
elif node.placeholder_text != "":
text = node.placeholder_text
var type = "editable text"
if not node.editable:
type = "text"
tts.speak("%s: %s" % [text, type], false)
2018-06-10 20:32:27 +00:00
func text_deleted(text):
2019-09-02 12:04:45 +00:00
tts.speak("%s deleted" % text, true)
func text_inserted(text):
2019-09-02 12:04:45 +00:00
tts.speak(text, true)
2018-06-18 20:33:03 +00:00
var old_pos
func check_caret_moved():
var pos = node.caret_position
if old_pos != null and old_pos != pos:
2018-06-18 20:33:03 +00:00
var text = node.text
if pos > len(text)-1:
2019-09-02 12:04:45 +00:00
tts.speak("blank", true)
2018-06-18 20:33:03 +00:00
else:
2019-09-02 12:04:45 +00:00
tts.speak(text[pos], true)
2018-06-18 20:33:03 +00:00
old_pos = pos
elif old_pos == null:
old_pos = pos
2018-07-19 20:12:01 +00:00
func focus_menu_button():
tts.speak(node.text + ": menu", false)
2019-09-02 12:04:45 +00:00
func focus_popup_menu():
tts.speak("menu", false)
2018-07-19 20:12:01 +00:00
2019-09-02 12:04:45 +00:00
func focus_popup_menu_item(id):
print("id: %s" % id)
print("count: %s" % node.get_item_count())
2018-07-19 20:12:01 +00:00
var item = node.get_item_text(id)
2019-09-02 12:04:45 +00:00
var submenu = node.get_item_submenu(id)
2018-07-19 20:12:01 +00:00
var tooltip = node.get_item_tooltip(position_in_children)
2019-09-02 12:04:45 +00:00
print("Tooltip: %s" % tooltip)
var disabled = node.is_item_disabled(id)
2018-07-19 20:12:01 +00:00
if item and tooltip:
2019-09-02 12:04:45 +00:00
print("Got item and tooltip")
2018-07-19 20:12:01 +00:00
item += ": "
item += tooltip
elif tooltip:
2019-09-02 12:04:45 +00:00
print("Got tooltip only")
2018-07-19 20:12:01 +00:00
item = tooltip
var shortcut = node.get_item_shortcut(position_in_children)
2019-09-02 12:04:45 +00:00
if shortcut != null and shortcut.get_as_text() != "None":
print("Got shortcut: %s" % shortcut.get_as_text())
2018-07-19 20:12:01 +00:00
item += ": "+shortcut.get_as_text()
2019-09-02 12:04:45 +00:00
if item == "":
item = "Unlabelled"
if submenu:
item += ": menu"
if disabled:
item += ": disabled"
item += ": " + str(id + 1) + " of " + str(node.get_item_count())
tts.speak(item, true)
2018-07-19 20:12:01 +00:00
func render_tree_item():
2018-07-19 17:02:40 +00:00
var item = node.get_selected()
var result = item.get_text(0)
return result
2019-09-02 14:55:51 +00:00
func focus_tab_container():
var text = node.get_tab_title(node.current_tab)
text += ": tab: " + str(node.current_tab + 1) + " of " + str(node.get_tab_count())
tts.speak(text, false)
func input_tab_container(event):
tts.stop()
var new_tab = node.current_tab
if event.echo or not event.pressed:
return
if event.scancode == KEY_RIGHT:
new_tab += 1
elif event.scancode == KEY_LEFT:
new_tab -= 1
if new_tab < 0:
new_tab = node.get_tab_count() - 1
elif new_tab >= node.get_tab_count():
new_tab = 0
node.current_tab = new_tab
focus_tab_container()
2018-07-03 17:01:00 +00:00
func focus_tree():
if node.get_selected():
2019-09-02 12:04:45 +00:00
tts.speak(render_tree_item(), true)
else:
2019-09-02 12:04:45 +00:00
tts.speak("tree", true)
2018-07-19 17:02:40 +00:00
func select_tree():
if node.has_focus():
2019-09-02 12:04:45 +00:00
tts.speak(render_tree_item(), true)
2018-06-10 21:02:45 +00:00
2018-05-28 21:45:13 +00:00
func focused():
2019-09-02 12:04:45 +00:00
print("Focus: %s" % node)
tts.stop()
var parent = node.get_parent()
if parent is EditorProperty and parent.label:
tts.speak(parent.label, false)
2018-07-19 20:12:01 +00:00
if node is MenuButton:
focus_menu_button()
elif node is Button:
2018-07-03 17:01:00 +00:00
focus_button()
2018-06-10 21:14:43 +00:00
elif node is ItemList:
2018-07-03 17:01:00 +00:00
focus_item_list()
elif node is Label:
focus_label()
2018-06-10 20:32:27 +00:00
elif node is LineEdit:
2018-07-03 17:01:00 +00:00
focus_line_edit()
2018-07-19 20:12:01 +00:00
elif node is PopupMenu:
focus_popup_menu()
2019-09-02 14:55:51 +00:00
elif node is TabContainer:
focus_tab_container()
2018-06-10 21:02:45 +00:00
elif node is Tree:
2018-07-03 17:01:00 +00:00
focus_tree()
2018-06-10 20:18:00 +00:00
else:
2019-09-02 12:04:45 +00:00
print("No handler")
if node.hint_tooltip:
2019-09-02 12:04:45 +00:00
tts.speak(node.hint_tooltip, false)
2018-05-28 21:45:13 +00:00
func unfocused():
2018-07-19 20:12:01 +00:00
position_in_children = 0
func gui_input(event):
2019-09-02 14:55:51 +00:00
if node is TabContainer:
return input_tab_container(event)
if node is ItemList:
return input_item_list(event)
2018-07-19 20:12:01 +00:00
elif node is LineEdit:
return check_caret_moved()
2019-09-02 12:04:45 +00:00
func _init(tts, node):
if node.is_in_group("accessible"):
return
node.add_to_group("accessible")
2019-09-02 12:04:45 +00:00
self.tts = tts
self.node = node
if not node is Container and not node is Panel and not node is Separator and not node is ScrollBar and not node is Popup and node.get_class() != "Control":
2018-07-19 18:30:38 +00:00
node.set_focus_mode(Control.FOCUS_ALL)
2019-09-02 14:55:51 +00:00
elif node is TabContainer:
node.set_focus_mode(Control.FOCUS_ALL)
2018-07-03 16:42:00 +00:00
node.connect("focus_entered", self, "focused")
node.connect("mouse_entered", self, "focused")
node.connect("focus_exited", self, "unfocused")
node.connect("mouse_exited", self, "unfocused")
node.connect("gui_input", self, "gui_input")
if node is ItemList:
2019-09-02 12:04:45 +00:00
node.connect("item_selected", self, "item_list_item_selected")
node.connect("multi_selected", self, "item_list_multi_selected")
node.connect("nothing_selected", self, "item_list_nothing_selected")
# elif node is LineEdit:
# node.connect("text_deleted", self, "text_deleted")
# node.connect("text_inserted", self, "text_inserted")
2018-07-19 20:12:01 +00:00
elif node is PopupMenu:
node.connect("id_focused", self, "focus_popup_menu_item")
2018-07-19 17:02:40 +00:00
elif node is Tree:
node.connect("item_selected", self, "select_tree")
2018-07-03 16:42:00 +00:00
node.connect("tree_exiting", self, "free")