Skip to content

Commit

Permalink
Merge pull request #76715 from m4gr3d/add_input_event_cancelled_state_3x
Browse files Browse the repository at this point in the history
[3.x] Augment the `InputEvent` class with a `CANCELED` state
  • Loading branch information
akien-mga committed May 17, 2023
2 parents e5e73e7 + 94d6c3d commit 1538b87
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 65 deletions.
68 changes: 33 additions & 35 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -371,7 +377,6 @@ void InputEventKey::_bind_methods() {
}

InputEventKey::InputEventKey() {
pressed = false;
scancode = 0;
physical_scancode = 0;
unicode = 0; ///unicode
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -561,21 +568,21 @@ 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");
}

InputEventMouseButton::InputEventMouseButton() {
factor = 1;
button_index = 0;
pressed = false;
doubleclick = false;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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");
Expand All @@ -908,7 +912,6 @@ void InputEventJoypadButton::_bind_methods() {
InputEventJoypadButton::InputEventJoypadButton() {
button_index = 0;
pressure = 0;
pressed = false;
}

//////////////////////////////////////////////
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -1130,28 +1132,24 @@ 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() {
ClassDB::bind_method(D_METHOD("set_action", "action"), &InputEventAction::set_action);
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;
}
/////////////////////////////
Expand Down
23 changes: 8 additions & 15 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class InputEvent : public Resource {
int device;

protected:
bool canceled = false;
bool pressed = false;

static void _bind_methods();

public:
Expand All @@ -215,8 +218,9 @@ class InputEvent : public Resource {
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;
// ...-.

Expand Down Expand Up @@ -282,8 +286,6 @@ class InputEventWithModifiers : public InputEvent {
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
Expand All @@ -295,7 +297,6 @@ class InputEventKey : public InputEventWithModifiers {

public:
void set_pressed(bool p_pressed);
virtual bool is_pressed() const;

void set_scancode(uint32_t p_scancode);
uint32_t get_scancode() const;
Expand Down Expand Up @@ -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:
Expand All @@ -365,7 +365,7 @@ class InputEventMouseButton : public InputEventMouse {
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;
Expand Down Expand Up @@ -431,8 +431,6 @@ class InputEventJoypadMotion : public InputEvent {
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;

Expand All @@ -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();
Expand All @@ -456,7 +453,6 @@ class InputEventJoypadButton : public InputEvent {
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;
Expand All @@ -474,7 +470,6 @@ class InputEventScreenTouch : public InputEvent {
GDCLASS(InputEventScreenTouch, InputEvent);
int index;
Vector2 pos;
bool pressed;
bool double_tap;

protected:
Expand All @@ -488,7 +483,7 @@ class InputEventScreenTouch : public InputEvent {
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;
Expand Down Expand Up @@ -534,7 +529,6 @@ class InputEventAction : public InputEvent {
GDCLASS(InputEventAction, InputEvent);

StringName action;
bool pressed;
float strength;

protected:
Expand All @@ -545,7 +539,6 @@ class InputEventAction : public InputEvent {
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;
Expand Down
Loading

0 comments on commit 1538b87

Please sign in to comment.