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.
This commit is contained in:
Fredia Huya-Kouadio 2023-05-03 14:23:34 -07:00
parent 716fef79ef
commit 94d6c3dcc6
8 changed files with 74 additions and 65 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);
@ -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<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 +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<InputEvent> &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<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 +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<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 +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<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, "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

@ -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<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;
@ -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;

View file

@ -71,6 +71,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>
@ -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.
</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

@ -360,6 +360,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_
@ -426,6 +427,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());
if (st->is_pressed()) {

View file

@ -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<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);
@ -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: {

View file

@ -86,13 +86,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();