Merge pull request #63168 from Levrault/master

Fix: InputEventJoypadMotion should trigger only once on a vslider
This commit is contained in:
Rémi Verschelde 2023-06-05 18:04:39 +02:00
commit f00c971b81
No known key found for this signature in database
GPG Key ID: C3336907360768E1
7 changed files with 178 additions and 1 deletions

View File

@ -1109,6 +1109,15 @@ String InputEventJoypadMotion::to_string() {
return vformat("InputEventJoypadMotion: axis=%d, axis_value=%.2f", axis, axis_value); return vformat("InputEventJoypadMotion: axis=%d, axis_value=%.2f", axis, axis_value);
} }
Ref<InputEventJoypadMotion> InputEventJoypadMotion::create_reference(JoyAxis p_axis, float p_value) {
Ref<InputEventJoypadMotion> ie;
ie.instantiate();
ie->set_axis(p_axis);
ie->set_axis_value(p_value);
return ie;
}
void InputEventJoypadMotion::_bind_methods() { void InputEventJoypadMotion::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis); ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis); ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);

View File

@ -319,6 +319,8 @@ public:
virtual String as_text() const override; virtual String as_text() const override;
virtual String to_string() override; virtual String to_string() override;
static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
InputEventJoypadMotion() {} InputEventJoypadMotion() {}
}; };

View File

@ -399,21 +399,25 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
inputs = List<Ref<InputEvent>>(); inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::LEFT)); inputs.push_back(InputEventKey::create_reference(Key::LEFT));
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT)); inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT));
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, -1.0));
default_builtin_cache.insert("ui_left", inputs); default_builtin_cache.insert("ui_left", inputs);
inputs = List<Ref<InputEvent>>(); inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::RIGHT)); inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT)); inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT));
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, 1.0));
default_builtin_cache.insert("ui_right", inputs); default_builtin_cache.insert("ui_right", inputs);
inputs = List<Ref<InputEvent>>(); inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::UP)); inputs.push_back(InputEventKey::create_reference(Key::UP));
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP)); inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP));
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, -1.0));
default_builtin_cache.insert("ui_up", inputs); default_builtin_cache.insert("ui_up", inputs);
inputs = List<Ref<InputEvent>>(); inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::DOWN)); inputs.push_back(InputEventKey::create_reference(Key::DOWN));
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN)); inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN));
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, 1.0));
default_builtin_cache.insert("ui_down", inputs); default_builtin_cache.insert("ui_down", inputs);
inputs = List<Ref<InputEvent>>(); inputs = List<Ref<InputEvent>>();

View File

@ -295,7 +295,18 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null()); ERR_FAIL_COND(p_event.is_null());
if (!items.is_empty()) { if (!items.is_empty()) {
Input *input = Input::get_singleton();
Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
Ref<InputEventJoypadButton> joypadbutton_event = p_event;
bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
if (p_event->is_action("ui_down", true) && p_event->is_pressed()) { if (p_event->is_action("ui_down", true) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_down", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over + 1; int search_from = mouse_over + 1;
if (search_from >= items.size()) { if (search_from >= items.size()) {
search_from = 0; search_from = 0;
@ -328,6 +339,12 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) {
} }
} }
} else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) { } else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_up", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over - 1; int search_from = mouse_over - 1;
if (search_from < 0) { if (search_from < 0) {
search_from = items.size() - 1; search_from = items.size() - 1;
@ -907,6 +924,82 @@ void PopupMenu::_notification(int p_what) {
} break; } break;
case NOTIFICATION_INTERNAL_PROCESS: { case NOTIFICATION_INTERNAL_PROCESS: {
Input *input = Input::get_singleton();
if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
set_process_internal(false);
return;
}
gamepad_event_delay_ms -= get_process_delta_time();
if (gamepad_event_delay_ms <= 0) {
if (input->is_action_pressed("ui_down")) {
gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
int search_from = mouse_over + 1;
if (search_from >= items.size()) {
search_from = 0;
}
bool match_found = false;
for (int i = search_from; i < items.size(); i++) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), i);
scroll_to_item(i);
control->queue_redraw();
match_found = true;
break;
}
}
if (!match_found) {
// If the last item is not selectable, try re-searching from the start.
for (int i = 0; i < search_from; i++) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), i);
scroll_to_item(i);
control->queue_redraw();
break;
}
}
}
}
if (input->is_action_pressed("ui_up")) {
gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
}
bool match_found = false;
for (int i = search_from; i >= 0; i--) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), i);
scroll_to_item(i);
control->queue_redraw();
match_found = true;
break;
}
}
if (!match_found) {
// If the first item is not selectable, try re-searching from the end.
for (int i = items.size() - 1; i >= search_from; i--) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), i);
scroll_to_item(i);
control->queue_redraw();
break;
}
}
}
}
}
// Only used when using operating system windows. // Only used when using operating system windows.
if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) { if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) {
Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position(); Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();

View File

@ -134,6 +134,10 @@ class PopupMenu : public Popup {
ScrollContainer *scroll_container = nullptr; ScrollContainer *scroll_container = nullptr;
Control *control = nullptr; Control *control = nullptr;
const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
struct ThemeCache { struct ThemeCache {
Ref<StyleBox> panel_style; Ref<StyleBox> panel_style;
Ref<StyleBox> hover_style; Ref<StyleBox> hover_style;

View File

@ -112,30 +112,58 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
} }
} }
Input *input = Input::get_singleton();
Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
Ref<InputEventJoypadButton> joypadbutton_event = p_event;
bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
if (!mm.is_valid() && !mb.is_valid()) { if (!mm.is_valid() && !mb.is_valid()) {
if (p_event->is_action_pressed("ui_left", true)) { if (p_event->is_action_pressed("ui_left", true)) {
if (orientation != HORIZONTAL) { if (orientation != HORIZONTAL) {
return; return;
} }
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_left", true)) {
return;
}
set_process_internal(true);
}
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
accept_event(); accept_event();
} else if (p_event->is_action_pressed("ui_right", true)) { } else if (p_event->is_action_pressed("ui_right", true)) {
if (orientation != HORIZONTAL) { if (orientation != HORIZONTAL) {
return; return;
} }
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_right", true)) {
return;
}
set_process_internal(true);
}
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
accept_event(); accept_event();
} else if (p_event->is_action_pressed("ui_up", true)) { } else if (p_event->is_action_pressed("ui_up", true)) {
if (orientation != VERTICAL) { if (orientation != VERTICAL) {
return; return;
} }
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_up", true)) {
return;
}
set_process_internal(true);
}
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
accept_event(); accept_event();
} else if (p_event->is_action_pressed("ui_down", true)) { } else if (p_event->is_action_pressed("ui_down", true)) {
if (orientation != VERTICAL) { if (orientation != VERTICAL) {
return; return;
} }
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_down", true)) {
return;
}
set_process_internal(true);
}
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
accept_event(); accept_event();
} else if (p_event->is_action("ui_home", true) && p_event->is_pressed()) { } else if (p_event->is_action("ui_home", true) && p_event->is_pressed()) {
@ -166,6 +194,39 @@ void Slider::_update_theme_item_cache() {
void Slider::_notification(int p_what) { void Slider::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_INTERNAL_PROCESS: {
Input *input = Input::get_singleton();
if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right") || input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
set_process_internal(false);
return;
}
gamepad_event_delay_ms -= get_process_delta_time();
if (gamepad_event_delay_ms <= 0) {
gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
if (orientation == HORIZONTAL) {
if (input->is_action_pressed("ui_left")) {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
}
if (input->is_action_pressed("ui_right")) {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
}
} else if (orientation == VERTICAL) {
if (input->is_action_pressed("ui_down")) {
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
}
if (input->is_action_pressed("ui_up")) {
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
}
}
}
} break;
case NOTIFICATION_THEME_CHANGED: { case NOTIFICATION_THEME_CHANGED: {
update_minimum_size(); update_minimum_size();
queue_redraw(); queue_redraw();

View File

@ -49,6 +49,10 @@ class Slider : public Range {
bool editable = true; bool editable = true;
bool scrollable = true; bool scrollable = true;
const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
struct ThemeCache { struct ThemeCache {
Ref<StyleBox> slider_style; Ref<StyleBox> slider_style;
Ref<StyleBox> grabber_area_style; Ref<StyleBox> grabber_area_style;