Reformat.

This commit is contained in:
Nolan Darilek 2020-05-19 12:03:59 -05:00
parent 6279f1a3c9
commit 9e2e99349c
3 changed files with 811 additions and 687 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,22 +5,26 @@ var ScreenReader = preload("ScreenReader.gd")
var screen_reader var screen_reader
func _enter_tree(): func _enter_tree():
var editor_accessibility_enabled = true var editor_accessibility_enabled = true
var rate = 50 var rate = 50
var config = ConfigFile.new() var config = ConfigFile.new()
var err = config.load("res://.godot-accessibility-editor-settings.ini") var err = config.load("res://.godot-accessibility-editor-settings.ini")
if not err: if not err:
editor_accessibility_enabled = config.get_value("global", "editor_accessibility_enabled", true) editor_accessibility_enabled = config.get_value(
rate = config.get_value("speech", "rate", 50) "global", "editor_accessibility_enabled", true
add_autoload_singleton("TTS", "res://addons/godot-tts/TTS.gd") )
if editor_accessibility_enabled: rate = config.get_value("speech", "rate", 50)
TTS.call_deferred("set_rate", rate) add_autoload_singleton("TTS", "res://addons/godot-tts/TTS.gd")
screen_reader = ScreenReader.new() if editor_accessibility_enabled:
get_tree().root.call_deferred("add_child", screen_reader) TTS.call_deferred("set_rate", rate)
call_deferred("connect", "scene_changed", screen_reader, "set_initial_scene_focus") screen_reader = ScreenReader.new()
call_deferred("connect", "main_screen_changed", screen_reader, "set_initial_screen_focus") get_tree().root.call_deferred("add_child", screen_reader)
add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null) call_deferred("connect", "scene_changed", screen_reader, "set_initial_scene_focus")
call_deferred("connect", "main_screen_changed", screen_reader, "set_initial_screen_focus")
add_custom_type("ScreenReader", "Node", preload("ScreenReader.gd"), null)
func _exit_tree(): func _exit_tree():
remove_custom_type("ScreenReader") remove_custom_type("ScreenReader")

View File

@ -21,127 +21,150 @@ export var explore_by_touch_interval = 200
var focus_restore_timer var focus_restore_timer
func _set_enabled(v): func _set_enabled(v):
if enabled: if enabled:
augment_tree(get_tree().root) augment_tree(get_tree().root)
else: else:
pass pass
enabled = v enabled = v
func _get_enabled(): func _get_enabled():
return enabled return enabled
func focused(node): func focused(node):
focus_restore_timer = null focus_restore_timer = null
func click_focused(node): func click_focused(node):
pass pass
func unfocused(node): func unfocused(node):
focus_restore_timer = get_tree().create_timer(0.2) focus_restore_timer = get_tree().create_timer(0.2)
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) Accessible.new(node)
if not node.is_connected("focus_entered", self, "focused"): if not node.is_connected("focus_entered", self, "focused"):
node.connect("focus_entered", self, "focused", [node]) node.connect("focus_entered", self, "focused", [node])
if not node.is_connected("mouse_entered", self, "click_focused"): if not node.is_connected("mouse_entered", self, "click_focused"):
node.connect("mouse_entered", self, "click_focused", [node]) node.connect("mouse_entered", self, "click_focused", [node])
if not node.is_connected("focus_exited", self, "unfocused"): if not node.is_connected("focus_exited", self, "unfocused"):
node.connect("focus_exited", self, "unfocused", [node]) node.connect("focus_exited", self, "unfocused", [node])
if not node.is_connected("mouse_exited", self, "unfocused"): if not node.is_connected("mouse_exited", self, "unfocused"):
node.connect("mouse_exited", self, "unfocused", [node]) node.connect("mouse_exited", self, "unfocused", [node])
func augment_tree(node): func augment_tree(node):
if not enabled: if not enabled:
return return
if node is Accessible: if node is Accessible:
return return
augment_node(node) augment_node(node)
for child in node.get_children(): for child in node.get_children():
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
self.augment_tree(get_tree().root) self.augment_tree(get_tree().root)
var focus = find_focusable_control(get_tree().root) var focus = find_focusable_control(get_tree().root)
if not focus: if not focus:
return return
focus.grab_click_focus() focus.grab_click_focus()
focus.grab_focus() focus.grab_focus()
func find_focusable_control(node): func find_focusable_control(node):
if node is Control and node.is_visible_in_tree() and (node.focus_mode == Control.FOCUS_CLICK or node.focus_mode == Control.FOCUS_ALL): if (
return node node is Control
for child in node.get_children(): and node.is_visible_in_tree()
var result = find_focusable_control(child) and (node.focus_mode == Control.FOCUS_CLICK or node.focus_mode == Control.FOCUS_ALL)
if result: ):
return result return node
return null for child in node.get_children():
var result = find_focusable_control(child)
if result:
return result
return null
func set_initial_scene_focus(scene): func set_initial_scene_focus(scene):
self.augment_tree(get_tree().root) self.augment_tree(get_tree().root)
var focus = find_focusable_control(get_tree().root) var focus = find_focusable_control(get_tree().root)
if not focus: if not focus:
return return
focus.grab_click_focus() focus.grab_click_focus()
focus.grab_focus() focus.grab_focus()
func _enter_tree(): func _enter_tree():
if enabled: if enabled:
augment_tree(get_tree().root) augment_tree(get_tree().root)
get_tree().connect("node_added", self, "augment_node") get_tree().connect("node_added", self, "augment_node")
connect("swipe_right", self, "swipe_right") connect("swipe_right", self, "swipe_right")
connect("swipe_left", self, "swipe_left") connect("swipe_left", self, "swipe_left")
connect("swipe_up", self, "swipe_up") connect("swipe_up", self, "swipe_up")
connect("swipe_down", self, "swipe_down") connect("swipe_down", self, "swipe_down")
func press_and_release(action): func press_and_release(action):
var event = InputEventAction.new() var event = InputEventAction.new()
event.action = action event.action = action
event.pressed = true event.pressed = true
get_tree().input_event(event) get_tree().input_event(event)
event.pressed = false event.pressed = false
get_tree().input_event(event) get_tree().input_event(event)
func _ui_focus_next(): func _ui_focus_next():
for event in InputMap.get_action_list("ui_focus_next"): for event in InputMap.get_action_list("ui_focus_next"):
if event is InputEventKey: if event is InputEventKey:
event.pressed = true event.pressed = true
Input.action_press("ui_focus_next") Input.action_press("ui_focus_next")
get_tree().input_event(event) get_tree().input_event(event)
event.pressed = false event.pressed = false
Input.action_release("ui_focus_next") Input.action_release("ui_focus_next")
get_tree().input_event(event) get_tree().input_event(event)
return return
func _ui_focus_prev(): func _ui_focus_prev():
for event in InputMap.get_action_list("ui_focus_prev"): for event in InputMap.get_action_list("ui_focus_prev"):
if event is InputEventKey: if event is InputEventKey:
event.pressed = true event.pressed = true
Input.action_press("ui_focus_prev") Input.action_press("ui_focus_prev")
get_tree().input_event(event) get_tree().input_event(event)
event.pressed = false event.pressed = false
Input.action_release("ui_focus_prev") Input.action_release("ui_focus_prev")
get_tree().input_event(event) get_tree().input_event(event)
return return
func swipe_right(): func swipe_right():
_ui_focus_next() _ui_focus_next()
func swipe_left(): func swipe_left():
_ui_focus_prev() _ui_focus_prev()
func swipe_up(): func swipe_up():
TTS.speak("Swipe up") TTS.speak("Swipe up")
func swipe_down(): func swipe_down():
TTS.speak("Swipe down") TTS.speak("Swipe down")
var touch_index = null var touch_index = null
@ -155,58 +178,67 @@ var explore_by_touch = false
var tap_count = 0 var tap_count = 0
func _input(event): func _input(event):
if not enabled: if not enabled:
return return
if event is InputEventScreenTouch: if event is InputEventScreenTouch:
get_tree().set_input_as_handled() get_tree().set_input_as_handled()
if touch_index and event.index != touch_index: if touch_index and event.index != touch_index:
return return
if event.pressed: if event.pressed:
touch_index = event.index touch_index = event.index
touch_position = event.position touch_position = event.position
touch_start_time = OS.get_ticks_msec() touch_start_time = OS.get_ticks_msec()
touch_stop_time = null touch_stop_time = null
else: else:
touch_index = null touch_index = null
var relative = event.position - touch_position var relative = event.position - touch_position
if relative.length() < min_swipe_distance: if relative.length() < min_swipe_distance:
tap_count += 1 tap_count += 1
elif not explore_by_touch: elif not explore_by_touch:
if abs(relative.x) > abs(relative.y): if abs(relative.x) > abs(relative.y):
if relative.x > 0: if relative.x > 0:
emit_signal("swipe_right") emit_signal("swipe_right")
else: else:
emit_signal("swipe_left") emit_signal("swipe_left")
else: else:
if relative.y > 0: if relative.y > 0:
emit_signal("swipe_down") emit_signal("swipe_down")
else: else:
emit_signal("swipe_up") emit_signal("swipe_up")
touch_position = null touch_position = null
touch_start_time = null touch_start_time = null
touch_stop_time = OS.get_ticks_msec() touch_stop_time = OS.get_ticks_msec()
explore_by_touch = false explore_by_touch = false
elif event is InputEventScreenDrag: elif event is InputEventScreenDrag:
if touch_index and event.index != touch_index: if touch_index and event.index != touch_index:
return return
if not explore_by_touch and OS.get_ticks_msec() - touch_start_time >= explore_by_touch_interval: if (
explore_by_touch = true not explore_by_touch
if event is InputEventMouseButton: and OS.get_ticks_msec() - touch_start_time >= explore_by_touch_interval
if event.device == -1 and not explore_by_touch: ):
get_tree().set_input_as_handled() explore_by_touch = true
if event is InputEventMouseButton:
if event.device == -1 and not explore_by_touch:
get_tree().set_input_as_handled()
func _process(delta): func _process(delta):
if not enabled: if not enabled:
return return
if touch_stop_time and OS.get_ticks_msec() - touch_stop_time >= tap_execute_interval and tap_count != 0: if (
touch_stop_time = null touch_stop_time
if tap_count == 2: and OS.get_ticks_msec() - touch_stop_time >= tap_execute_interval
press_and_release("ui_accept") and tap_count != 0
tap_count = 0 ):
if focus_restore_timer and focus_restore_timer.time_left <= 0: touch_stop_time = null
var focus = find_focusable_control(get_tree().root) if tap_count == 2:
if focus and not focus.get_focus_owner(): press_and_release("ui_accept")
print_debug("Restoring focus.") tap_count = 0
focus.grab_focus() if focus_restore_timer and focus_restore_timer.time_left <= 0:
focus.grab_click_focus() var focus = find_focusable_control(get_tree().root)
if focus and not focus.get_focus_owner():
print_debug("Restoring focus.")
focus.grab_focus()
focus.grab_click_focus()