Unify event generation to support both simple and keyfake strategies, using the latter on Android where necessary.

This commit is contained in:
Nolan Darilek 2020-05-28 13:00:42 -05:00
parent 4281e3777d
commit 95bbbff780

View File

@ -119,16 +119,8 @@ func _enter_tree():
connect("swipe_down", self, "swipe_down") connect("swipe_down", self, "swipe_down")
func _press_and_release(action): func _press_and_release(action, fake_via_keyboard = false):
var event = InputEventAction.new() if fake_via_keyboard:
event.action = action
event.pressed = true
get_tree().input_event(event)
event.pressed = false
get_tree().input_event(event)
func _send_via_keyboard(action):
for event in InputMap.get_action_list(action): for event in InputMap.get_action_list(action):
if event is InputEventKey: if event is InputEventKey:
event.pressed = true event.pressed = true
@ -138,38 +130,50 @@ func _send_via_keyboard(action):
Input.action_release(action) Input.action_release(action)
get_tree().input_event(event) get_tree().input_event(event)
return return
var event = InputEventAction.new()
event.action = action
event.pressed = true
get_tree().input_event(event)
event.pressed = false
get_tree().input_event(event)
func _ui_left(): func _ui_left(fake_via_keyboard = false):
_send_via_keyboard("ui_left") _press_and_release("ui_left", fake_via_keyboard)
func _ui_right(): func _ui_right(fake_via_keyboard = false):
_send_via_keyboard("ui_right") _press_and_release("ui_right", fake_via_keyboard)
func _ui_up(): func _ui_up(fake_via_keyboard = false):
_send_via_keyboard("ui_up") _press_and_release("ui_up", fake_via_keyboard)
func _ui_down(): func _ui_down(fake_via_keyboard = false):
_send_via_keyboard("ui_down") _press_and_release("ui_down", fake_via_keyboard)
func _ui_focus_next(): func _ui_focus_next(fake_via_keyboard = false):
_send_via_keyboard("ui_focus_next") _press_and_release("ui_focus_next", fake_via_keyboard)
func _ui_focus_prev(): func _ui_focus_prev(fake_via_keyboard = false):
_send_via_keyboard("ui_focus_prev") _press_and_release("ui_focus_prev", fake_via_keyboard)
func swipe_right(): func swipe_right():
_ui_focus_next() var fake_via_keyboard = false
if OS.get_name() == "Android":
fake_via_keyboard = true
_ui_focus_next(fake_via_keyboard)
func swipe_left(): func swipe_left():
_ui_focus_prev() var fake_via_keyboard = false
if OS.get_name() == "Android":
fake_via_keyboard = true
_ui_focus_prev(fake_via_keyboard)
func swipe_up(): func swipe_up():
@ -228,17 +232,17 @@ func _input(event):
return return
in_focus_mode_handler = true in_focus_mode_handler = true
if Input.is_action_just_pressed("ui_left"): if Input.is_action_just_pressed("ui_left"):
_press_and_release("ui_left") _ui_left()
elif Input.is_action_just_pressed("ui_right"): elif Input.is_action_just_pressed("ui_right"):
_press_and_release("ui_right") _ui_right()
elif Input.is_action_just_pressed("ui_up"): elif Input.is_action_just_pressed("ui_up"):
_press_and_release("ui_up") _ui_up()
elif Input.is_action_just_pressed("ui_down"): elif Input.is_action_just_pressed("ui_down"):
_press_and_release("ui_down") _ui_down()
elif Input.is_action_just_pressed("ui_focus_prev"): elif Input.is_action_just_pressed("ui_focus_prev"):
_press_and_release("ui_focus_prev") _ui_focus_prev()
elif Input.is_action_just_pressed("ui_focus_next"): elif Input.is_action_just_pressed("ui_focus_next"):
_press_and_release("ui_focus_next") _ui_focus_next()
get_tree().set_input_as_handled() get_tree().set_input_as_handled()
in_focus_mode_handler = false in_focus_mode_handler = false
return return