Skip to content

Commit

Permalink
Revert "feat(gamepad): improve gamepad behavior with slider and popup…
Browse files Browse the repository at this point in the history
…_menu"

This reverts commit 166ca77.
  • Loading branch information
rsubtil committed Nov 1, 2023
1 parent 0f7b9b4 commit a277a4a
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 178 deletions.
9 changes: 0 additions & 9 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,15 +1109,6 @@ String InputEventJoypadMotion::to_string() {
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() {
ClassDB::bind_method(D_METHOD("set_axis", "axis"), &InputEventJoypadMotion::set_axis);
ClassDB::bind_method(D_METHOD("get_axis"), &InputEventJoypadMotion::get_axis);
Expand Down
2 changes: 0 additions & 2 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ class InputEventJoypadMotion : public InputEvent {
virtual String as_text() const override;
virtual String to_string() override;

static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);

InputEventJoypadMotion() {}
};

Expand Down
4 changes: 0 additions & 4 deletions core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,25 +399,21 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::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);

inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::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);

inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::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);

inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::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);

inputs = List<Ref<InputEvent>>();
Expand Down
93 changes: 0 additions & 93 deletions scene/gui/popup_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());

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_action("ui_focus_next", true)) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_down", true) && !input->is_action_just_pressed("ui_focus_next", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over + 1;
if (search_from >= items.size()) {
search_from = 0;
Expand Down Expand Up @@ -345,12 +334,6 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) {
}
}
} else if ((p_event->is_action("ui_up", true) || p_event->is_action("ui_focus_prev", true)) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_up", true) && !input->is_action_just_pressed("ui_focus_prev", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
Expand Down Expand Up @@ -930,82 +913,6 @@ void PopupMenu::_notification(int p_what) {
} break;

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.
if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) {
Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
Expand Down
4 changes: 0 additions & 4 deletions scene/gui/popup_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ class PopupMenu : public Popup {
ScrollContainer *scroll_container = 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 {
Ref<StyleBox> panel_style;
Ref<StyleBox> hover_style;
Expand Down
63 changes: 1 addition & 62 deletions scene/gui/slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,58 +112,30 @@ 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 (p_event->is_action_pressed("ui_left", true)) {
if (orientation != HORIZONTAL) {
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()));
accept_event();
} else if (p_event->is_action_pressed("ui_right", true)) {
if (orientation != HORIZONTAL) {
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()));
accept_event();
} else if (p_event->is_action_pressed("ui_up", true)) {
if (orientation != VERTICAL) {
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()));
accept_event();
} else if (p_event->is_action_pressed("ui_down", true)) {
if (orientation != VERTICAL) {
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()));
accept_event();
} else if (p_event->is_action("ui_home", true) && p_event->is_pressed()) {
Expand Down Expand Up @@ -194,39 +166,6 @@ void Slider::_update_theme_item_cache() {

void Slider::_notification(int 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: {
update_minimum_size();
queue_redraw();
Expand Down
4 changes: 0 additions & 4 deletions scene/gui/slider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ class Slider : public Range {
bool editable = 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 {
Ref<StyleBox> slider_style;
Ref<StyleBox> grabber_area_style;
Expand Down

0 comments on commit a277a4a

Please sign in to comment.