Ported: 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.
- m4gr3d
94d6c3dcc6
This commit is contained in:
Relintai 2023-06-11 10:26:23 +02:00
parent 7e64510427
commit 3331a7ae04
8 changed files with 76 additions and 68 deletions

View File

@ -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);
@ -127,6 +137,8 @@ void InputEvent::_bind_methods() {
InputEvent::InputEvent() {
device = 0;
canceled = false;
pressed = false;
}
//////////////////
@ -227,10 +239,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 +379,6 @@ void InputEventKey::_bind_methods() {
}
InputEventKey::InputEventKey() {
pressed = false;
scancode = 0;
physical_scancode = 0;
unicode = 0; ///unicode
@ -440,8 +447,8 @@ 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 +474,7 @@ Ref<InputEvent> 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 +558,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,7 +569,7 @@ 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);
@ -569,13 +577,13 @@ void InputEventMouseButton::_bind_methods() {
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, "pressed"), "set_pressed", "is_pressed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "canceled"), "set_canceled", "is_canceled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick");
}
InputEventMouseButton::InputEventMouseButton() {
factor = 1;
button_index = 0;
pressed = false;
doubleclick = false;
}
@ -678,6 +686,10 @@ bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
return false;
}
if (is_canceled() != motion->is_canceled()) {
return false;
}
if (is_pressed() != motion->is_pressed()) {
return false;
}
@ -750,16 +762,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<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventJoypadMotion> jm = p_event;
if (jm.is_null()) {
@ -842,9 +851,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 +893,7 @@ bool InputEventJoypadButton::shortcut_match(const Ref<InputEvent> &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 +904,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 +913,6 @@ void InputEventJoypadButton::_bind_methods() {
InputEventJoypadButton::InputEventJoypadButton() {
button_index = 0;
pressure = 0;
pressed = false;
}
//////////////////////////////////////////////
@ -930,8 +934,8 @@ 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<InputEvent> 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<InputEvent> &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<InputEvent> &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_NAME, "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;
}
/////////////////////////////

View File

@ -32,8 +32,8 @@
#include "core/math/transform_2d.h"
#include "core/object/resource.h"
#include "core/typedefs.h"
#include "core/string/ustring.h"
#include "core/typedefs.h"
/**
* Input Event classes. These are used in the main loop.
@ -199,6 +199,9 @@ class InputEvent : public Resource {
int device;
protected:
bool canceled;
bool pressed;
static void _bind_methods();
public:
@ -214,8 +217,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;
// ...-.
@ -281,8 +285,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
@ -294,7 +296,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;
@ -350,7 +351,6 @@ class InputEventMouseButton : public InputEventMouse {
float factor;
int button_index;
bool pressed; //otherwise released
bool doubleclick; //last even less than doubleclick time
protected:
@ -364,7 +364,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;
@ -417,6 +417,7 @@ public:
class InputEventJoypadMotion : public InputEvent {
GDCLASS(InputEventJoypadMotion, InputEvent);
int axis; ///< Joypad axis
float axis_value; ///< -1 to 1
@ -430,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<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;
@ -445,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();
@ -455,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;
@ -473,7 +470,6 @@ class InputEventScreenTouch : public InputEvent {
GDCLASS(InputEventScreenTouch, InputEvent);
int index;
Vector2 pos;
bool pressed;
bool double_tap;
protected:
@ -487,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;
@ -533,7 +529,6 @@ class InputEventAction : public InputEvent {
GDCLASS(InputEventAction, InputEvent);
StringName action;
bool pressed;
float strength;
protected:
@ -544,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;

View File

@ -62,6 +62,12 @@
Returns [code]true[/code] if this input event's type is one that can be assigned to an input action.
</description>
</method>
<method name="is_canceled" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if this input event has been canceled.
</description>
</method>
<method name="is_echo" qualifiers="const">
<return type="bool" />
<description>
@ -75,6 +81,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.
</description>
</method>
<method name="is_released" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if this input event is released. Not relevant for events of type [InputEventMouseMotion] or [InputEventScreenDrag].
</description>
</method>
<method name="shortcut_match" qualifiers="const">
<return type="bool" />
<argument index="0" name="event" type="InputEvent" />

View File

@ -15,6 +15,9 @@
<member name="button_index" type="int" setter="set_button_index" getter="get_button_index" default="0">
The mouse button identifier, one of the [enum ButtonList] button or button wheel constants.
</member>
<member name="canceled" type="bool" setter="set_canceled" getter="is_canceled" default="false">
If [code]true[/code], the mouse button event has been canceled.
</member>
<member name="doubleclick" type="bool" setter="set_doubleclick" getter="is_doubleclick" default="false">
If [code]true[/code], the mouse button's state is a double-click.
</member>

View File

@ -13,6 +13,9 @@
<methods>
</methods>
<members>
<member name="canceled" type="bool" setter="set_canceled" getter="is_canceled" default="false">
If [code]true[/code], the touch event has been canceled.
</member>
<member name="double_tap" type="bool" setter="set_double_tap" getter="is_double_tap" default="false">
If [code]true[/code], the touch's state is a double tap.
</member>

View File

@ -364,6 +364,7 @@ void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool
Ref<InputEventScreenTouch> 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_
@ -430,6 +431,7 @@ void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &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());

View File

@ -101,24 +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<InputEventScreenTouch> 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);
@ -127,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();
}
@ -145,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
@ -221,11 +216,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;
}
@ -244,6 +239,7 @@ void AndroidInputHandler::_parse_mouse_event_info(int buttons_mask, bool p_press
}
ev->set_pressed(p_pressed);
ev->set_canceled(p_canceled);
int changed_button_mask = buttons_state ^ buttons_mask;
buttons_state = buttons_mask;
@ -255,7 +251,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;
}
@ -283,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: {

View File

@ -85,13 +85,13 @@ private:
void _wheel_button_click(int event_buttons_mask, const Ref<InputEventMouseButton> &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();