From 94d6c3dcc68f9de4b7c004e3a18513edb1d5dda2 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Wed, 3 May 2023 14:23:34 -0700 Subject: [PATCH] Augment the `InputEvent` class with a `CANCELED` state The `InputEvent` class currently supports the `pressed` and `released` states, which given the binary nature, is represented by a `bool` field. This commit introduced the `CANCELED` state, which signals that an ongoing input event has been canceled. To represent all the states, the `InputEventState` enum is added and the `InputEvent` logic is refactored accordingly. --- core/os/input_event.cpp | 68 +++++++++++----------- core/os/input_event.h | 23 +++----- doc/classes/InputEvent.xml | 12 ++++ doc/classes/InputEventMouseButton.xml | 3 + doc/classes/InputEventScreenTouch.xml | 3 + main/input_default.cpp | 2 + platform/android/android_input_handler.cpp | 24 ++++---- platform/android/android_input_handler.h | 4 +- 8 files changed, 74 insertions(+), 65 deletions(-) diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 4e127c442fd..b8c83eec65e 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -72,8 +72,16 @@ float InputEvent::get_action_raw_strength(const StringName &p_action, bool p_exa return valid ? raw_strength : 0.0f; } +bool InputEvent::is_canceled() const { + return canceled; +} + bool InputEvent::is_pressed() const { - return false; + return pressed && !canceled; +} + +bool InputEvent::is_released() const { + return !pressed && !canceled; } bool InputEvent::is_echo() const { @@ -109,7 +117,9 @@ void InputEvent::_bind_methods() { ClassDB::bind_method(D_METHOD("is_action_released", "action", "exact_match"), &InputEvent::is_action_released, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_action_strength", "action", "exact_match"), &InputEvent::get_action_strength, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("is_canceled"), &InputEvent::is_canceled); ClassDB::bind_method(D_METHOD("is_pressed"), &InputEvent::is_pressed); + ClassDB::bind_method(D_METHOD("is_released"), &InputEvent::is_released); ClassDB::bind_method(D_METHOD("is_echo"), &InputEvent::is_echo); ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text); @@ -227,10 +237,6 @@ void InputEventKey::set_pressed(bool p_pressed) { pressed = p_pressed; } -bool InputEventKey::is_pressed() const { - return pressed; -} - void InputEventKey::set_scancode(uint32_t p_scancode) { scancode = p_scancode; } @@ -371,7 +377,6 @@ void InputEventKey::_bind_methods() { } InputEventKey::InputEventKey() { - pressed = false; scancode = 0; physical_scancode = 0; unicode = 0; ///unicode @@ -440,8 +445,9 @@ int InputEventMouseButton::get_button_index() const { void InputEventMouseButton::set_pressed(bool p_pressed) { pressed = p_pressed; } -bool InputEventMouseButton::is_pressed() const { - return pressed; + +void InputEventMouseButton::set_canceled(bool p_canceled) { + canceled = p_canceled; } void InputEventMouseButton::set_doubleclick(bool p_doubleclick) { @@ -467,6 +473,7 @@ Ref InputEventMouseButton::xformed_by(const Transform2D &p_xform, co mb->set_button_mask(get_button_mask()); mb->set_pressed(pressed); + mb->set_canceled(canceled); mb->set_doubleclick(doubleclick); mb->set_factor(factor); mb->set_button_index(button_index); @@ -550,7 +557,7 @@ String InputEventMouseButton::as_text() const { button_index_string = itos(get_button_index()); break; } - return "InputEventMouseButton : button_index=" + button_index_string + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + "), button_mask=" + itos(get_button_mask()) + ", doubleclick=" + (doubleclick ? "true" : "false"); + return "InputEventMouseButton : button_index=" + button_index_string + ", pressed=" + (pressed ? "true" : "false") + ", canceled=" + (canceled ? "true" : "false") + ", position=(" + String(get_position()) + "), button_mask=" + itos(get_button_mask()) + ", doubleclick=" + (doubleclick ? "true" : "false"); } void InputEventMouseButton::_bind_methods() { @@ -561,13 +568,14 @@ void InputEventMouseButton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_button_index"), &InputEventMouseButton::get_button_index); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed); - // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed); + ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventMouseButton::set_canceled); ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick); ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick); ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor"); ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick"); } @@ -575,7 +583,6 @@ void InputEventMouseButton::_bind_methods() { InputEventMouseButton::InputEventMouseButton() { factor = 1; button_index = 0; - pressed = false; doubleclick = false; } @@ -678,6 +685,10 @@ bool InputEventMouseMotion::accumulate(const Ref &p_event) { return false; } + if (is_canceled() != motion->is_canceled()) { + return false; + } + if (is_pressed() != motion->is_pressed()) { return false; } @@ -750,16 +761,13 @@ int InputEventJoypadMotion::get_axis() const { void InputEventJoypadMotion::set_axis_value(float p_value) { axis_value = p_value; + pressed = Math::abs(axis_value) >= 0.5f; } float InputEventJoypadMotion::get_axis_value() const { return axis_value; } -bool InputEventJoypadMotion::is_pressed() const { - return Math::abs(axis_value) >= 0.5f; -} - bool InputEventJoypadMotion::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref jm = p_event; if (jm.is_null()) { @@ -842,9 +850,6 @@ int InputEventJoypadButton::get_button_index() const { void InputEventJoypadButton::set_pressed(bool p_pressed) { pressed = p_pressed; } -bool InputEventJoypadButton::is_pressed() const { - return pressed; -} void InputEventJoypadButton::set_pressure(float p_pressure) { pressure = p_pressure; @@ -887,7 +892,7 @@ bool InputEventJoypadButton::shortcut_match(const Ref &p_event, bool } String InputEventJoypadButton::as_text() const { - return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (pressed ? "true" : "false") + ", pressure=" + String(Variant(pressure)); + return "InputEventJoypadButton : button_index=" + itos(button_index) + ", pressed=" + (is_pressed() ? "true" : "false") + ", pressure=" + String(Variant(pressure)); } void InputEventJoypadButton::_bind_methods() { @@ -898,7 +903,6 @@ void InputEventJoypadButton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_pressure"), &InputEventJoypadButton::get_pressure); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventJoypadButton::set_pressed); - // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventJoypadButton::is_pressed); ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "pressure"), "set_pressure", "get_pressure"); @@ -908,7 +912,6 @@ void InputEventJoypadButton::_bind_methods() { InputEventJoypadButton::InputEventJoypadButton() { button_index = 0; pressure = 0; - pressed = false; } ////////////////////////////////////////////// @@ -930,8 +933,9 @@ Vector2 InputEventScreenTouch::get_position() const { void InputEventScreenTouch::set_pressed(bool p_pressed) { pressed = p_pressed; } -bool InputEventScreenTouch::is_pressed() const { - return pressed; + +void InputEventScreenTouch::set_canceled(bool p_canceled) { + canceled = p_canceled; } void InputEventScreenTouch::set_double_tap(bool p_double_tap) { @@ -948,13 +952,14 @@ Ref InputEventScreenTouch::xformed_by(const Transform2D &p_xform, co st->set_index(index); st->set_position(p_xform.xform(pos + p_local_ofs)); st->set_pressed(pressed); + st->set_canceled(canceled); st->set_double_tap(double_tap); return st; } String InputEventScreenTouch::as_text() const { - return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", position=(" + String(get_position()) + "), double_tap=" + (double_tap ? "true" : "false"); + return "InputEventScreenTouch : index=" + itos(index) + ", pressed=" + (pressed ? "true" : "false") + ", canceled=" + (canceled ? "true" : "false") + ", position=(" + String(get_position()) + "), double_tap=" + (double_tap ? "true" : "false"); } void InputEventScreenTouch::_bind_methods() { @@ -965,20 +970,20 @@ void InputEventScreenTouch::_bind_methods() { ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed); - //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed); + ClassDB::bind_method(D_METHOD("set_canceled", "canceled"), &InputEventScreenTouch::set_canceled); ClassDB::bind_method(D_METHOD("set_double_tap", "double_tap"), &InputEventScreenTouch::set_double_tap); ClassDB::bind_method(D_METHOD("is_double_tap"), &InputEventScreenTouch::is_double_tap); ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_tap"), "set_double_tap", "is_double_tap"); } InputEventScreenTouch::InputEventScreenTouch() { index = 0; - pressed = false; double_tap = false; } @@ -1082,9 +1087,6 @@ StringName InputEventAction::get_action() const { void InputEventAction::set_pressed(bool p_pressed) { pressed = p_pressed; } -bool InputEventAction::is_pressed() const { - return pressed; -} void InputEventAction::set_strength(float p_strength) { strength = CLAMP(p_strength, 0.0f, 1.0f); @@ -1114,7 +1116,7 @@ bool InputEventAction::action_match(const Ref &p_event, bool p_exact bool match = action == act->action; if (match) { - bool pressed = act->pressed; + bool pressed = act->is_pressed(); if (p_pressed != nullptr) { *p_pressed = pressed; } @@ -1130,7 +1132,7 @@ bool InputEventAction::action_match(const Ref &p_event, bool p_exact } String InputEventAction::as_text() const { - return "InputEventAction : action=" + action + ", pressed=(" + (pressed ? "true" : "false"); + return "InputEventAction : action=" + action + ", pressed=(" + (is_pressed() ? "true" : "false"); } void InputEventAction::_bind_methods() { @@ -1138,20 +1140,16 @@ void InputEventAction::_bind_methods() { ClassDB::bind_method(D_METHOD("get_action"), &InputEventAction::get_action); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventAction::set_pressed); - //ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventAction::is_pressed); ClassDB::bind_method(D_METHOD("set_strength", "strength"), &InputEventAction::set_strength); ClassDB::bind_method(D_METHOD("get_strength"), &InputEventAction::get_strength); - // ClassDB::bind_method(D_METHOD("is_action", "name"), &InputEventAction::is_action); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_strength", "get_strength"); } InputEventAction::InputEventAction() { - pressed = false; strength = 1.0f; } ///////////////////////////// diff --git a/core/os/input_event.h b/core/os/input_event.h index e1f8b2d325b..434ab3ac4f1 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -200,6 +200,9 @@ class InputEvent : public Resource { int device; protected: + bool canceled = false; + bool pressed = false; + static void _bind_methods(); public: @@ -215,8 +218,9 @@ public: float get_action_strength(const StringName &p_action, bool p_exact_match = false) const; float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const; - // To be removed someday, since they do not make sense for all events - virtual bool is_pressed() const; + bool is_canceled() const; + bool is_pressed() const; + bool is_released() const; virtual bool is_echo() const; // ...-. @@ -282,8 +286,6 @@ public: class InputEventKey : public InputEventWithModifiers { GDCLASS(InputEventKey, InputEventWithModifiers); - bool pressed; /// otherwise release - uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks uint32_t physical_scancode; uint32_t unicode; ///unicode @@ -295,7 +297,6 @@ protected: public: void set_pressed(bool p_pressed); - virtual bool is_pressed() const; void set_scancode(uint32_t p_scancode); uint32_t get_scancode() const; @@ -351,7 +352,6 @@ class InputEventMouseButton : public InputEventMouse { float factor; int button_index; - bool pressed; //otherwise released bool doubleclick; //last even less than doubleclick time protected: @@ -365,7 +365,7 @@ public: int get_button_index() const; void set_pressed(bool p_pressed); - virtual bool is_pressed() const; + void set_canceled(bool p_canceled); void set_doubleclick(bool p_doubleclick); bool is_doubleclick() const; @@ -431,8 +431,6 @@ public: void set_axis_value(float p_value); float get_axis_value() const; - virtual bool is_pressed() const; - virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; @@ -446,7 +444,6 @@ class InputEventJoypadButton : public InputEvent { GDCLASS(InputEventJoypadButton, InputEvent); int button_index; - bool pressed; float pressure; //0 to 1 protected: static void _bind_methods(); @@ -456,7 +453,6 @@ public: int get_button_index() const; void set_pressed(bool p_pressed); - virtual bool is_pressed() const; void set_pressure(float p_pressure); float get_pressure() const; @@ -474,7 +470,6 @@ class InputEventScreenTouch : public InputEvent { GDCLASS(InputEventScreenTouch, InputEvent); int index; Vector2 pos; - bool pressed; bool double_tap; protected: @@ -488,7 +483,7 @@ public: Vector2 get_position() const; void set_pressed(bool p_pressed); - virtual bool is_pressed() const; + void set_canceled(bool p_canceled); void set_double_tap(bool p_double_tap); bool is_double_tap() const; @@ -534,7 +529,6 @@ class InputEventAction : public InputEvent { GDCLASS(InputEventAction, InputEvent); StringName action; - bool pressed; float strength; protected: @@ -545,7 +539,6 @@ public: StringName get_action() const; void set_pressed(bool p_pressed); - virtual bool is_pressed() const; void set_strength(float p_strength); float get_strength() const; diff --git a/doc/classes/InputEvent.xml b/doc/classes/InputEvent.xml index 02631c23ba2..4357b78cfb5 100644 --- a/doc/classes/InputEvent.xml +++ b/doc/classes/InputEvent.xml @@ -71,6 +71,12 @@ Returns [code]true[/code] if this input event's type is one that can be assigned to an input action. + + + + Returns [code]true[/code] if this input event has been canceled. + + @@ -84,6 +90,12 @@ [b]Note:[/b] Due to keyboard ghosting, [method is_action_pressed] may return [code]false[/code] even if one of the action's keys is pressed. See [url=$DOCS_URL/tutorials/inputs/input_examples.html#keyboard-events]Input examples[/url] in the documentation for more information. + + + + Returns [code]true[/code] if this input event is released. Not relevant for events of type [InputEventMouseMotion] or [InputEventScreenDrag]. + + diff --git a/doc/classes/InputEventMouseButton.xml b/doc/classes/InputEventMouseButton.xml index 35896ff93c4..f48aca2e3aa 100644 --- a/doc/classes/InputEventMouseButton.xml +++ b/doc/classes/InputEventMouseButton.xml @@ -15,6 +15,9 @@ The mouse button identifier, one of the [enum ButtonList] button or button wheel constants. + + If [code]true[/code], the mouse button event has been canceled. + If [code]true[/code], the mouse button's state is a double-click. diff --git a/doc/classes/InputEventScreenTouch.xml b/doc/classes/InputEventScreenTouch.xml index fc1256acc75..b2762888f85 100644 --- a/doc/classes/InputEventScreenTouch.xml +++ b/doc/classes/InputEventScreenTouch.xml @@ -13,6 +13,9 @@ + + If [code]true[/code], the touch event has been canceled. + If [code]true[/code], the touch's state is a double tap. diff --git a/main/input_default.cpp b/main/input_default.cpp index 3a3ee3a09ef..5d98746327d 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -360,6 +360,7 @@ void InputDefault::_parse_input_event_impl(const Ref &p_event, bool Ref touch_event; touch_event.instance(); touch_event->set_pressed(mb->is_pressed()); + touch_event->set_canceled(mb->is_canceled()); touch_event->set_position(mb->get_position()); touch_event->set_double_tap(mb->is_doubleclick()); _THREAD_SAFE_UNLOCK_ @@ -426,6 +427,7 @@ void InputDefault::_parse_input_event_impl(const Ref &p_event, bool button_event->set_position(st->get_position()); button_event->set_global_position(st->get_position()); button_event->set_pressed(st->is_pressed()); + button_event->set_canceled(st->is_canceled()); button_event->set_button_index(BUTTON_LEFT); button_event->set_doubleclick(st->is_double_tap()); if (st->is_pressed()) { diff --git a/platform/android/android_input_handler.cpp b/platform/android/android_input_handler.cpp index 8d8317c9729..9609af76e8d 100644 --- a/platform/android/android_input_handler.cpp +++ b/platform/android/android_input_handler.cpp @@ -101,22 +101,19 @@ void AndroidInputHandler::process_key_event(int p_scancode, int p_physical_scanc } void AndroidInputHandler::_cancel_all_touch() { - _parse_all_touch(false, false, true); + _parse_all_touch(false, true); touch.clear(); } -void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_double_tap, bool reset_index) { +void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_canceled, bool p_double_tap) { if (touch.size()) { //end all if exist for (int i = 0; i < touch.size(); i++) { Ref ev; ev.instance(); - if (reset_index) { - ev->set_index(-1); - } else { - ev->set_index(touch[i].id); - } + ev->set_index(touch[i].id); ev->set_pressed(p_pressed); + ev->set_canceled(p_canceled); ev->set_position(touch[i].pos); ev->set_double_tap(p_double_tap); input->parse_input_event(ev); @@ -125,7 +122,7 @@ void AndroidInputHandler::_parse_all_touch(bool p_pressed, bool p_double_tap, bo } void AndroidInputHandler::_release_all_touch() { - _parse_all_touch(false, false); + _parse_all_touch(false); touch.clear(); } @@ -143,7 +140,7 @@ void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const } //send touch - _parse_all_touch(true, p_double_tap); + _parse_all_touch(true, false, p_double_tap); } break; case AMOTION_EVENT_ACTION_MOVE: { //motion @@ -220,11 +217,11 @@ void AndroidInputHandler::process_touch_event(int p_event, int p_pointer, const void AndroidInputHandler::_cancel_mouse_event_info(bool p_source_mouse_relative) { buttons_state = 0; - _parse_mouse_event_info(0, false, false, p_source_mouse_relative); + _parse_mouse_event_info(0, false, true, false, p_source_mouse_relative); mouse_event_info.valid = false; } -void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_double_click, bool p_source_mouse_relative) { +void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_canceled, bool p_double_click, bool p_source_mouse_relative) { if (!mouse_event_info.valid) { return; } @@ -241,6 +238,7 @@ void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_press hover_prev_pos = mouse_event_info.pos; } ev->set_pressed(p_pressed); + ev->set_canceled(p_canceled); int changed_button_mask = buttons_state ^ buttons_mask; buttons_state = buttons_mask; @@ -252,7 +250,7 @@ void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_press } void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative) { - _parse_mouse_event_info(0, false, false, p_source_mouse_relative); + _parse_mouse_event_info(0, false, false, false, p_source_mouse_relative); mouse_event_info.valid = false; } @@ -281,7 +279,7 @@ void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_an mouse_event_info.valid = true; mouse_event_info.pos = p_event_pos; - _parse_mouse_event_info(event_buttons_mask, true, p_double_click, p_source_mouse_relative); + _parse_mouse_event_info(event_buttons_mask, true, false, p_double_click, p_source_mouse_relative); } break; case AMOTION_EVENT_ACTION_CANCEL: { diff --git a/platform/android/android_input_handler.h b/platform/android/android_input_handler.h index a8c3d956f46..5567107f803 100644 --- a/platform/android/android_input_handler.h +++ b/platform/android/android_input_handler.h @@ -86,13 +86,13 @@ private: void _wheel_button_click(int event_buttons_mask, const Ref &ev, int wheel_button, float factor); - void _parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_double_click, bool p_source_mouse_relative); + void _parse_mouse_event_info(int buttons_mask, bool p_pressed, bool p_canceled, bool p_double_click, bool p_source_mouse_relative); void _release_mouse_event_info(bool p_source_mouse_relative = false); void _cancel_mouse_event_info(bool p_source_mouse_relative = false); - void _parse_all_touch(bool p_pressed, bool p_double_tap, bool reset_index = false); + void _parse_all_touch(bool p_pressed, bool p_canceled = false, bool p_double_tap = false); void _release_all_touch();