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-12-29 23:59:48 +00:00
|
|
|
signal swipe_left
|
|
|
|
|
|
|
|
signal swipe_right
|
|
|
|
|
|
|
|
signal swipe_up
|
|
|
|
|
|
|
|
signal swipe_down
|
|
|
|
|
2019-09-24 14:01:37 +00:00
|
|
|
var Accessible = preload("Accessible.gd")
|
2018-05-26 14:47:18 +00:00
|
|
|
|
2020-03-30 20:15:00 +00:00
|
|
|
export var enabled = true setget _set_enabled, _get_enabled
|
|
|
|
|
2019-12-30 00:18:09 +00:00
|
|
|
export var min_swipe_distance = 5
|
|
|
|
|
2019-12-30 00:23:51 +00:00
|
|
|
export var tap_execute_interval = 125
|
2019-12-30 00:18:09 +00:00
|
|
|
|
2019-12-30 00:23:51 +00:00
|
|
|
export var explore_by_touch_interval = 200
|
2019-12-29 23:59:48 +00:00
|
|
|
|
2019-12-27 18:50:33 +00:00
|
|
|
var focus_restore_timer
|
2019-10-03 14:34:22 +00:00
|
|
|
|
2020-03-30 20:15:00 +00:00
|
|
|
func _set_enabled(v):
|
|
|
|
if enabled:
|
|
|
|
augment_tree(get_tree().root)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
enabled = v
|
|
|
|
|
|
|
|
func _get_enabled():
|
|
|
|
return enabled
|
|
|
|
|
2019-10-03 14:34:22 +00:00
|
|
|
func focused(node):
|
2019-12-27 18:50:33 +00:00
|
|
|
focus_restore_timer = null
|
2019-10-03 14:34:22 +00:00
|
|
|
|
2019-12-30 14:25:57 +00:00
|
|
|
func click_focused(node):
|
|
|
|
pass
|
|
|
|
|
2019-10-03 14:34:22 +00:00
|
|
|
func unfocused(node):
|
2019-12-27 18:50:33 +00:00
|
|
|
focus_restore_timer = get_tree().create_timer(0.2)
|
2019-10-03 14:34:22 +00:00
|
|
|
|
2018-05-28 21:45:13 +00:00
|
|
|
func augment_node(node):
|
2020-03-30 20:15:00 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
2018-05-26 14:47:18 +00:00
|
|
|
if node is Control:
|
2020-03-18 13:56:45 +00:00
|
|
|
Accessible.new(node)
|
2019-10-03 14:34:22 +00:00
|
|
|
if not node.is_connected("focus_entered", self, "focused"):
|
|
|
|
node.connect("focus_entered", self, "focused", [node])
|
2019-12-30 14:25:57 +00:00
|
|
|
if not node.is_connected("mouse_entered", self, "click_focused"):
|
|
|
|
node.connect("mouse_entered", self, "click_focused", [node])
|
2019-10-03 14:34:22 +00:00
|
|
|
if not node.is_connected("focus_exited", self, "unfocused"):
|
|
|
|
node.connect("focus_exited", self, "unfocused", [node])
|
2019-12-30 14:25:57 +00:00
|
|
|
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):
|
2020-03-30 20:15:00 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
2019-12-27 00:25:26 +00:00
|
|
|
if node is Accessible:
|
|
|
|
return
|
2018-06-07 21:28:33 +00:00
|
|
|
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():
|
2020-03-30 20:15:00 +00:00
|
|
|
if enabled:
|
|
|
|
augment_tree(get_tree().root)
|
2020-03-18 13:56:45 +00:00
|
|
|
get_tree().connect("node_added", self, "augment_node")
|
2019-12-30 00:44:15 +00:00
|
|
|
connect("swipe_right", self, "swipe_right")
|
|
|
|
connect("swipe_left", self, "swipe_left")
|
|
|
|
connect("swipe_up", self, "swipe_up")
|
|
|
|
connect("swipe_down", self, "swipe_down")
|
|
|
|
|
2019-12-30 00:52:44 +00:00
|
|
|
func press_and_release(action):
|
2019-12-30 00:44:15 +00:00
|
|
|
var event = InputEventAction.new()
|
|
|
|
event.action = action
|
|
|
|
event.pressed = true
|
|
|
|
get_tree().input_event(event)
|
2019-12-30 00:52:44 +00:00
|
|
|
event.pressed = false
|
|
|
|
get_tree().input_event(event)
|
2019-12-30 00:44:15 +00:00
|
|
|
|
2020-01-14 22:41:35 +00:00
|
|
|
func _ui_focus_next():
|
|
|
|
for event in InputMap.get_action_list("ui_focus_next"):
|
|
|
|
if event is InputEventKey:
|
|
|
|
event.pressed = true
|
|
|
|
Input.action_press("ui_focus_next")
|
|
|
|
get_tree().input_event(event)
|
|
|
|
event.pressed = false
|
|
|
|
Input.action_release("ui_focus_next")
|
|
|
|
get_tree().input_event(event)
|
|
|
|
return
|
|
|
|
|
|
|
|
func _ui_focus_prev():
|
|
|
|
for event in InputMap.get_action_list("ui_focus_prev"):
|
|
|
|
if event is InputEventKey:
|
|
|
|
event.pressed = true
|
|
|
|
Input.action_press("ui_focus_prev")
|
|
|
|
get_tree().input_event(event)
|
|
|
|
event.pressed = false
|
|
|
|
Input.action_release("ui_focus_prev")
|
|
|
|
get_tree().input_event(event)
|
|
|
|
return
|
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
func swipe_right():
|
2020-01-14 22:41:35 +00:00
|
|
|
_ui_focus_next()
|
2019-12-30 00:44:15 +00:00
|
|
|
|
|
|
|
func swipe_left():
|
2020-01-14 22:41:35 +00:00
|
|
|
_ui_focus_prev()
|
2019-12-30 00:44:15 +00:00
|
|
|
|
|
|
|
func swipe_up():
|
|
|
|
TTS.speak("Swipe up")
|
|
|
|
|
|
|
|
func swipe_down():
|
|
|
|
TTS.speak("Swipe down")
|
2019-12-27 17:19:49 +00:00
|
|
|
|
2019-12-29 23:59:48 +00:00
|
|
|
var touch_index = null
|
|
|
|
|
|
|
|
var touch_position = null
|
|
|
|
|
2019-12-30 00:18:09 +00:00
|
|
|
var touch_start_time = null
|
|
|
|
|
|
|
|
var touch_stop_time = null
|
|
|
|
|
2019-12-29 23:59:48 +00:00
|
|
|
var explore_by_touch = false
|
|
|
|
|
2019-12-30 00:18:09 +00:00
|
|
|
var tap_count = 0
|
|
|
|
|
2019-12-27 17:19:49 +00:00
|
|
|
func _input(event):
|
2020-03-30 20:15:00 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
2019-12-27 17:19:49 +00:00
|
|
|
if event is InputEventScreenTouch:
|
2019-12-29 23:59:48 +00:00
|
|
|
get_tree().set_input_as_handled()
|
|
|
|
if touch_index and event.index != touch_index:
|
|
|
|
return
|
|
|
|
if event.pressed:
|
|
|
|
touch_index = event.index
|
|
|
|
touch_position = event.position
|
2019-12-30 00:18:09 +00:00
|
|
|
touch_start_time = OS.get_ticks_msec()
|
|
|
|
touch_stop_time = null
|
2019-12-29 23:59:48 +00:00
|
|
|
else:
|
|
|
|
touch_index = null
|
|
|
|
var relative = event.position - touch_position
|
2019-12-30 00:18:09 +00:00
|
|
|
if relative.length() < min_swipe_distance:
|
|
|
|
tap_count += 1
|
2019-12-30 00:23:51 +00:00
|
|
|
elif not explore_by_touch:
|
2019-12-30 00:18:09 +00:00
|
|
|
if abs(relative.x) > abs(relative.y):
|
|
|
|
if relative.x > 0:
|
|
|
|
emit_signal("swipe_right")
|
|
|
|
else:
|
|
|
|
emit_signal("swipe_left")
|
2019-12-29 23:59:48 +00:00
|
|
|
else:
|
2019-12-30 00:18:09 +00:00
|
|
|
if relative.y > 0:
|
|
|
|
emit_signal("swipe_down")
|
|
|
|
else:
|
|
|
|
emit_signal("swipe_up")
|
2019-12-29 23:59:48 +00:00
|
|
|
touch_position = null
|
2019-12-30 00:18:09 +00:00
|
|
|
touch_start_time = null
|
|
|
|
touch_stop_time = OS.get_ticks_msec()
|
2019-12-29 23:59:48 +00:00
|
|
|
explore_by_touch = false
|
|
|
|
elif event is InputEventScreenDrag:
|
|
|
|
if touch_index and event.index != touch_index:
|
|
|
|
return
|
2019-12-30 00:23:51 +00:00
|
|
|
if not explore_by_touch and OS.get_ticks_msec() - touch_start_time >= explore_by_touch_interval:
|
|
|
|
explore_by_touch = true
|
2019-12-30 14:27:20 +00:00
|
|
|
if event is InputEventMouseButton:
|
|
|
|
if event.device == -1 and not explore_by_touch:
|
|
|
|
get_tree().set_input_as_handled()
|
2019-12-27 18:54:27 +00:00
|
|
|
|
2019-12-27 18:50:33 +00:00
|
|
|
func _process(delta):
|
2020-03-30 20:15:00 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
2019-12-30 00:18:09 +00:00
|
|
|
if touch_stop_time and OS.get_ticks_msec() - touch_stop_time >= tap_execute_interval and tap_count != 0:
|
|
|
|
touch_stop_time = null
|
2019-12-30 00:52:44 +00:00
|
|
|
if tap_count == 2:
|
|
|
|
press_and_release("ui_accept")
|
2019-12-30 00:18:09 +00:00
|
|
|
tap_count = 0
|
2019-12-27 18:50:33 +00:00
|
|
|
if focus_restore_timer and focus_restore_timer.time_left <= 0:
|
|
|
|
var focus = find_focusable_control(get_tree().root)
|
|
|
|
if focus and not focus.get_focus_owner():
|
|
|
|
print("Restoring focus.")
|
|
|
|
focus.grab_focus()
|
|
|
|
focus.grab_click_focus()
|