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
|
|
|
|
2020-05-28 15:58:36 +00:00
|
|
|
export var enable_focus_mode = false
|
|
|
|
|
2020-08-10 18:26:57 +00:00
|
|
|
var should_stop_on_focus = true
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2020-03-30 20:15:00 +00:00
|
|
|
func _set_enabled(v):
|
2020-05-19 17:03:59 +00:00
|
|
|
if enabled:
|
|
|
|
augment_tree(get_tree().root)
|
|
|
|
else:
|
2020-05-28 18:21:31 +00:00
|
|
|
for accessible in get_tree().get_nodes_in_group("accessibles"):
|
|
|
|
accessible.queue_free()
|
2020-05-19 17:03:59 +00:00
|
|
|
enabled = v
|
|
|
|
|
2020-03-30 20:15:00 +00:00
|
|
|
|
|
|
|
func _get_enabled():
|
2020-05-19 17:03:59 +00:00
|
|
|
return enabled
|
|
|
|
|
2020-03-30 20:15:00 +00:00
|
|
|
|
2018-05-28 21:45:13 +00:00
|
|
|
func augment_node(node):
|
2020-05-19 17:03:59 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
|
|
|
if node is Control:
|
|
|
|
Accessible.new(node)
|
|
|
|
|
2018-05-26 14:47:18 +00:00
|
|
|
|
2018-06-07 21:28:33 +00:00
|
|
|
func augment_tree(node):
|
2020-05-19 17:03:59 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
|
|
|
if node is Accessible:
|
|
|
|
return
|
|
|
|
augment_node(node)
|
|
|
|
for child in node.get_children():
|
|
|
|
augment_tree(child)
|
|
|
|
|
2018-06-07 21:28:33 +00:00
|
|
|
|
2020-06-16 23:03:13 +00:00
|
|
|
func find_focusable_control(node):
|
2020-05-19 17:03:59 +00:00
|
|
|
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():
|
2020-06-16 23:03:13 +00:00
|
|
|
var result = find_focusable_control(child)
|
2020-05-19 17:03:59 +00:00
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
return null
|
|
|
|
|
2018-05-28 19:22:09 +00:00
|
|
|
|
2018-05-26 11:34:15 +00:00
|
|
|
func _enter_tree():
|
2020-06-08 15:45:38 +00:00
|
|
|
pause_mode = Node.PAUSE_MODE_PROCESS
|
2020-05-19 17:03:59 +00:00
|
|
|
if enabled:
|
|
|
|
augment_tree(get_tree().root)
|
|
|
|
get_tree().connect("node_added", self, "augment_node")
|
|
|
|
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:44:15 +00:00
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _press_and_release(action, fake_via_keyboard = false):
|
|
|
|
if fake_via_keyboard:
|
|
|
|
for event in InputMap.get_action_list(action):
|
|
|
|
if event is InputEventKey:
|
|
|
|
event.pressed = true
|
|
|
|
Input.action_press(action)
|
|
|
|
get_tree().input_event(event)
|
|
|
|
event.pressed = false
|
|
|
|
Input.action_release(action)
|
|
|
|
get_tree().input_event(event)
|
|
|
|
return
|
2020-05-19 17:03:59 +00:00
|
|
|
var event = InputEventAction.new()
|
|
|
|
event.action = action
|
|
|
|
event.pressed = true
|
2020-06-09 18:45:00 +00:00
|
|
|
Input.action_press(action)
|
2020-05-19 17:03:59 +00:00
|
|
|
get_tree().input_event(event)
|
|
|
|
event.pressed = false
|
2020-06-09 18:45:00 +00:00
|
|
|
Input.action_release(action)
|
2020-05-19 17:03:59 +00:00
|
|
|
get_tree().input_event(event)
|
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_left(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_left", fake_via_keyboard)
|
2020-05-28 15:58:36 +00:00
|
|
|
|
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_right(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_right", fake_via_keyboard)
|
2020-05-28 15:58:36 +00:00
|
|
|
|
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_up(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_up", fake_via_keyboard)
|
2020-05-28 15:58:36 +00:00
|
|
|
|
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_down(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_down", fake_via_keyboard)
|
2020-05-28 15:58:36 +00:00
|
|
|
|
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_focus_next(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_focus_next", fake_via_keyboard)
|
2020-05-28 15:58:36 +00:00
|
|
|
|
|
|
|
|
2020-05-28 18:00:42 +00:00
|
|
|
func _ui_focus_prev(fake_via_keyboard = false):
|
|
|
|
_press_and_release("ui_focus_prev", fake_via_keyboard)
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2020-01-14 22:41:35 +00:00
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
func swipe_right():
|
2020-05-28 18:00:42 +00:00
|
|
|
var fake_via_keyboard = false
|
|
|
|
if OS.get_name() == "Android":
|
|
|
|
fake_via_keyboard = true
|
|
|
|
_ui_focus_next(fake_via_keyboard)
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
|
|
|
|
func swipe_left():
|
2020-05-28 18:00:42 +00:00
|
|
|
var fake_via_keyboard = false
|
|
|
|
if OS.get_name() == "Android":
|
|
|
|
fake_via_keyboard = true
|
|
|
|
_ui_focus_prev(fake_via_keyboard)
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
|
|
|
|
func swipe_up():
|
2020-06-16 23:03:13 +00:00
|
|
|
var focus = find_focusable_control(get_tree().root)
|
2020-06-09 18:23:58 +00:00
|
|
|
if focus:
|
|
|
|
focus = focus.get_focus_owner()
|
|
|
|
if focus:
|
|
|
|
if focus is Range:
|
|
|
|
_press_and_release("ui_right")
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2019-12-30 00:44:15 +00:00
|
|
|
|
|
|
|
func swipe_down():
|
2020-06-16 23:03:13 +00:00
|
|
|
var focus = find_focusable_control(get_tree().root)
|
2020-06-09 18:23:58 +00:00
|
|
|
if focus:
|
|
|
|
focus = focus.get_focus_owner()
|
|
|
|
if focus:
|
|
|
|
if focus is Range:
|
|
|
|
_press_and_release("ui_left")
|
2020-05-19 17:03:59 +00:00
|
|
|
|
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
|
|
|
|
|
2020-05-28 15:58:36 +00:00
|
|
|
var focus_mode = false
|
|
|
|
var in_focus_mode_handler = false
|
|
|
|
|
2020-05-19 17:03:59 +00:00
|
|
|
|
2019-12-27 17:19:49 +00:00
|
|
|
func _input(event):
|
2020-05-19 17:03:59 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
2020-07-22 17:13:19 +00:00
|
|
|
var focus = find_focusable_control(get_tree().root)
|
|
|
|
if focus:
|
|
|
|
focus = focus.get_focus_owner()
|
|
|
|
if focus is Tree and Input.is_action_just_pressed("ui_accept"):
|
|
|
|
var accessible
|
|
|
|
for n in focus.get_children():
|
|
|
|
if n is Accessible:
|
|
|
|
accessible = n
|
|
|
|
break
|
|
|
|
if accessible and accessible.button_index != null:
|
|
|
|
accessible._tree_input(event)
|
|
|
|
get_tree().set_input_as_handled()
|
2020-05-28 15:58:36 +00:00
|
|
|
if enable_focus_mode:
|
|
|
|
if (
|
|
|
|
event is InputEventKey
|
|
|
|
and Input.is_key_pressed(KEY_ESCAPE)
|
2020-05-29 16:07:56 +00:00
|
|
|
and Input.is_key_pressed(KEY_SHIFT)
|
2020-05-28 15:58:36 +00:00
|
|
|
and not event.echo
|
|
|
|
):
|
|
|
|
get_tree().set_input_as_handled()
|
|
|
|
if focus_mode:
|
|
|
|
focus_mode = false
|
|
|
|
TTS.speak("UI mode", true)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
focus_mode = true
|
|
|
|
TTS.speak("Focus mode", true)
|
|
|
|
return
|
|
|
|
if focus_mode:
|
|
|
|
if (
|
|
|
|
Input.is_action_just_pressed("ui_left")
|
|
|
|
or Input.is_action_just_pressed("ui_right")
|
|
|
|
or Input.is_action_just_pressed("ui_up")
|
|
|
|
or Input.is_action_just_pressed("ui_down")
|
|
|
|
or Input.is_action_just_pressed("ui_focus_next")
|
|
|
|
or Input.is_action_just_pressed("ui_focus_prev")
|
|
|
|
):
|
|
|
|
if in_focus_mode_handler:
|
|
|
|
return
|
|
|
|
in_focus_mode_handler = true
|
|
|
|
if Input.is_action_just_pressed("ui_left"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_left()
|
2020-05-28 15:58:36 +00:00
|
|
|
elif Input.is_action_just_pressed("ui_right"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_right()
|
2020-05-28 15:58:36 +00:00
|
|
|
elif Input.is_action_just_pressed("ui_up"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_up()
|
2020-05-28 15:58:36 +00:00
|
|
|
elif Input.is_action_just_pressed("ui_down"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_down()
|
2020-05-28 15:58:36 +00:00
|
|
|
elif Input.is_action_just_pressed("ui_focus_prev"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_focus_prev()
|
2020-05-28 15:58:36 +00:00
|
|
|
elif Input.is_action_just_pressed("ui_focus_next"):
|
2020-05-28 18:00:42 +00:00
|
|
|
_ui_focus_next()
|
2020-05-28 15:58:36 +00:00
|
|
|
get_tree().set_input_as_handled()
|
|
|
|
in_focus_mode_handler = false
|
|
|
|
return
|
2020-05-19 17:03:59 +00:00
|
|
|
if event is InputEventScreenTouch:
|
|
|
|
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
|
|
|
|
touch_start_time = OS.get_ticks_msec()
|
|
|
|
touch_stop_time = null
|
2020-06-09 19:29:57 +00:00
|
|
|
elif touch_position:
|
2020-05-19 17:03:59 +00:00
|
|
|
touch_index = null
|
|
|
|
var relative = event.position - touch_position
|
|
|
|
if relative.length() < min_swipe_distance:
|
|
|
|
tap_count += 1
|
|
|
|
elif not explore_by_touch:
|
|
|
|
if abs(relative.x) > abs(relative.y):
|
|
|
|
if relative.x > 0:
|
|
|
|
emit_signal("swipe_right")
|
|
|
|
else:
|
|
|
|
emit_signal("swipe_left")
|
|
|
|
else:
|
|
|
|
if relative.y > 0:
|
|
|
|
emit_signal("swipe_down")
|
|
|
|
else:
|
|
|
|
emit_signal("swipe_up")
|
|
|
|
touch_position = null
|
|
|
|
touch_start_time = null
|
|
|
|
touch_stop_time = OS.get_ticks_msec()
|
|
|
|
explore_by_touch = false
|
|
|
|
elif event is InputEventScreenDrag:
|
|
|
|
if touch_index and event.index != touch_index:
|
|
|
|
return
|
|
|
|
if (
|
|
|
|
not explore_by_touch
|
|
|
|
and OS.get_ticks_msec() - touch_start_time >= explore_by_touch_interval
|
|
|
|
):
|
|
|
|
explore_by_touch = true
|
|
|
|
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-05-19 17:03:59 +00:00
|
|
|
if not enabled:
|
|
|
|
return
|
|
|
|
if (
|
|
|
|
touch_stop_time
|
|
|
|
and OS.get_ticks_msec() - touch_stop_time >= tap_execute_interval
|
|
|
|
and tap_count != 0
|
|
|
|
):
|
|
|
|
touch_stop_time = null
|
|
|
|
if tap_count == 2:
|
2020-05-28 15:58:36 +00:00
|
|
|
_press_and_release("ui_accept")
|
2020-05-19 17:03:59 +00:00
|
|
|
tap_count = 0
|