2019-09-24 14:20:45 +00:00
|
|
|
tool
|
2019-09-24 14:01:37 +00:00
|
|
|
extends Node
|
2018-05-26 11:34:15 +00:00
|
|
|
|
2019-09-24 14:01:37 +00:00
|
|
|
var Accessible = preload("Accessible.gd")
|
2018-05-26 14:47:18 +00:00
|
|
|
|
2019-12-23 03:01:47 +00:00
|
|
|
var accessibles = []
|
|
|
|
|
2019-10-03 14:34:22 +00:00
|
|
|
var focus_restore_timer = Timer.new()
|
|
|
|
|
|
|
|
func focused(node):
|
|
|
|
focus_restore_timer.stop()
|
|
|
|
|
|
|
|
func click_focused(node):
|
|
|
|
pass
|
|
|
|
|
|
|
|
func restore_focus():
|
|
|
|
var focus = find_focusable_control(get_tree().root)
|
2019-10-03 14:52:41 +00:00
|
|
|
if focus and not focus.get_focus_owner():
|
|
|
|
print("Restoring focus.")
|
2019-10-03 14:34:22 +00:00
|
|
|
focus.grab_focus()
|
|
|
|
focus.grab_click_focus()
|
|
|
|
|
|
|
|
func unfocused(node):
|
|
|
|
focus_restore_timer.start(0.2)
|
|
|
|
|
2018-05-28 21:45:13 +00:00
|
|
|
func augment_node(node):
|
2018-05-26 14:47:18 +00:00
|
|
|
if node is Control:
|
2019-12-23 00:55:45 +00:00
|
|
|
var accessible = Accessible.new(node)
|
2019-12-23 03:01:47 +00:00
|
|
|
accessibles.append(accessible)
|
2019-10-03 14:34:22 +00:00
|
|
|
if not node.is_connected("focus_entered", self, "focused"):
|
|
|
|
node.connect("focus_entered", self, "focused", [node])
|
|
|
|
if not node.is_connected("mouse_entered", self, "click_focused"):
|
|
|
|
node.connect("mouse_entered", self, "click_focused", [node])
|
|
|
|
if not node.is_connected("focus_exited", self, "unfocused"):
|
|
|
|
node.connect("focus_exited", self, "unfocused", [node])
|
|
|
|
if not node.is_connected("mouse_exited", self, "unfocused"):
|
|
|
|
node.connect("mouse_exited", self, "unfocused", [node])
|
2018-05-26 14:47:18 +00:00
|
|
|
|
2018-06-07 21:28:33 +00:00
|
|
|
func augment_tree(node):
|
|
|
|
augment_node(node)
|
|
|
|
for child in node.get_children():
|
2018-06-10 19:42:58 +00:00
|
|
|
augment_tree(child)
|
2018-06-07 21:28:33 +00:00
|
|
|
|
2018-05-28 21:45:13 +00:00
|
|
|
func set_initial_screen_focus(screen):
|
2019-10-02 21:20:25 +00:00
|
|
|
TTS.speak("%s: screen" % screen, false)
|
2019-09-05 14:01:20 +00:00
|
|
|
var control = find_focusable_control(get_tree().root)
|
|
|
|
if control.get_focus_owner() != null:
|
|
|
|
return
|
2018-06-10 19:42:58 +00:00
|
|
|
self.augment_tree(get_tree().root)
|
|
|
|
var focus = find_focusable_control(get_tree().root)
|
|
|
|
if not focus:
|
|
|
|
return
|
|
|
|
focus.grab_click_focus()
|
|
|
|
focus.grab_focus()
|
|
|
|
|
|
|
|
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):
|
|
|
|
return node
|
|
|
|
for child in node.get_children():
|
|
|
|
var result = find_focusable_control(child)
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
return null
|
2018-05-28 19:22:09 +00:00
|
|
|
|
2018-05-28 21:45:13 +00:00
|
|
|
func set_initial_scene_focus(scene):
|
2019-09-05 14:01:20 +00:00
|
|
|
self.augment_tree(get_tree().root)
|
|
|
|
var focus = find_focusable_control(get_tree().root)
|
|
|
|
if not focus:
|
|
|
|
return
|
|
|
|
focus.grab_click_focus()
|
|
|
|
focus.grab_focus()
|
2018-05-28 19:22:09 +00:00
|
|
|
|
2018-05-26 11:34:15 +00:00
|
|
|
func _enter_tree():
|
2019-10-03 14:34:22 +00:00
|
|
|
focus_restore_timer.one_shot = true
|
|
|
|
focus_restore_timer.connect("timeout", self, "restore_focus")
|
|
|
|
add_child(focus_restore_timer)
|
2018-06-10 19:56:58 +00:00
|
|
|
get_tree().connect("node_added", self, "augment_tree")
|
2019-12-23 03:01:47 +00:00
|
|
|
|
|
|
|
func _exit_tree():
|
|
|
|
for accessible in accessibles:
|
2019-12-23 15:59:12 +00:00
|
|
|
if accessible.timer != null:
|
|
|
|
accessible.timer.unreference()
|
|
|
|
accessible.timer = null
|
2019-12-23 03:01:47 +00:00
|
|
|
accessible.free()
|