From cf788ae684966b07cd15cacad86043a4c4d14c4c Mon Sep 17 00:00:00 2001 From: Univeous Date: Tue, 30 Nov 2021 23:18:59 +0800 Subject: [PATCH] Fix UI navigation with joysticks --- scene/main/viewport.cpp | 63 ++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 9225d644ee04..ec621dad465e 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -2427,28 +2427,57 @@ void Viewport::_gui_input_event(Ref p_event) { if (from && p_event->is_pressed()) { Control *next = nullptr; - if (p_event->is_action_pressed("ui_focus_next", true)) { - next = from->find_next_valid_focus(); - } + Ref joypadmotion_event = p_event; + if (joypadmotion_event.is_valid()) { + Input *input = Input::get_singleton(); - if (p_event->is_action_pressed("ui_focus_prev", true)) { - next = from->find_prev_valid_focus(); - } + if (p_event->is_action_pressed("ui_focus_next") && input->is_action_just_pressed("ui_focus_next")) { + next = from->find_next_valid_focus(); + } - if (!mods && p_event->is_action_pressed("ui_up", true)) { - next = from->_get_focus_neighbour(MARGIN_TOP); - } + if (p_event->is_action_pressed("ui_focus_prev") && input->is_action_just_pressed("ui_focus_prev")) { + next = from->find_prev_valid_focus(); + } - if (!mods && p_event->is_action_pressed("ui_left", true)) { - next = from->_get_focus_neighbour(MARGIN_LEFT); - } + if (!mods && p_event->is_action_pressed("ui_up") && input->is_action_just_pressed("ui_up")) { + next = from->_get_focus_neighbour(MARGIN_TOP); + } - if (!mods && p_event->is_action_pressed("ui_right", true)) { - next = from->_get_focus_neighbour(MARGIN_RIGHT); - } + if (!mods && p_event->is_action_pressed("ui_left") && input->is_action_just_pressed("ui_left")) { + next = from->_get_focus_neighbour(MARGIN_LEFT); + } + + if (!mods && p_event->is_action_pressed("ui_right") && input->is_action_just_pressed("ui_right")) { + next = from->_get_focus_neighbour(MARGIN_RIGHT); + } + + if (!mods && p_event->is_action_pressed("ui_down") && input->is_action_just_pressed("ui_down")) { + next = from->_get_focus_neighbour(MARGIN_BOTTOM); + } + } else { + if (p_event->is_action_pressed("ui_focus_next", true)) { + next = from->find_next_valid_focus(); + } + + if (p_event->is_action_pressed("ui_focus_prev", true)) { + next = from->find_prev_valid_focus(); + } + + if (!mods && p_event->is_action_pressed("ui_up", true)) { + next = from->_get_focus_neighbour(MARGIN_TOP); + } + + if (!mods && p_event->is_action_pressed("ui_left", true)) { + next = from->_get_focus_neighbour(MARGIN_LEFT); + } - if (!mods && p_event->is_action_pressed("ui_down", true)) { - next = from->_get_focus_neighbour(MARGIN_BOTTOM); + if (!mods && p_event->is_action_pressed("ui_right", true)) { + next = from->_get_focus_neighbour(MARGIN_RIGHT); + } + + if (!mods && p_event->is_action_pressed("ui_down", true)) { + next = from->_get_focus_neighbour(MARGIN_BOTTOM); + } } if (next) {