Skip to content

Commit

Permalink
Streamline InputEvent hierarchy to define position only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sauermann committed Mar 14, 2022
1 parent 95b6c6c commit 2ef2a18
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 106 deletions.
78 changes: 22 additions & 56 deletions core/input/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,21 +484,26 @@ void InputEventKey::_bind_methods() {

///////////////////////////////////

void InputEventMouse::set_button_mask(MouseButton p_mask) {
button_mask = p_mask;
emit_changed();
void InputEventWithPosition::set_position(const Vector2 &p_pos) {
pos = p_pos;
}

MouseButton InputEventMouse::get_button_mask() const {
return button_mask;
Vector2 InputEventWithPosition::get_position() const {
return pos;
}

void InputEventMouse::set_position(const Vector2 &p_pos) {
pos = p_pos;
void InputEventWithPosition::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);

ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
}

Vector2 InputEventMouse::get_position() const {
return pos;
///////////////////////////////////

void InputEventMouse::set_button_mask(MouseButton p_mask) {
button_mask = p_mask;
emit_changed();
}

void InputEventMouse::set_global_position(const Vector2 &p_global_pos) {
Expand All @@ -509,18 +514,18 @@ Vector2 InputEventMouse::get_global_position() const {
return global_pos;
}

MouseButton InputEventMouse::get_button_mask() const {
return button_mask;
}

void InputEventMouse::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_button_mask", "button_mask"), &InputEventMouse::set_button_mask);
ClassDB::bind_method(D_METHOD("get_button_mask"), &InputEventMouse::get_button_mask);

ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventMouse::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &InputEventMouse::get_position);

ClassDB::bind_method(D_METHOD("set_global_position", "global_position"), &InputEventMouse::set_global_position);
ClassDB::bind_method(D_METHOD("get_global_position"), &InputEventMouse::get_global_position);

ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask"), "set_button_mask", "get_button_mask");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position"), "set_global_position", "get_global_position");
}

Expand Down Expand Up @@ -1107,14 +1112,6 @@ int InputEventScreenTouch::get_index() const {
return index;
}

void InputEventScreenTouch::set_position(const Vector2 &p_pos) {
pos = p_pos;
}

Vector2 InputEventScreenTouch::get_position() const {
return pos;
}

void InputEventScreenTouch::set_pressed(bool p_pressed) {
pressed = p_pressed;
}
Expand All @@ -1128,8 +1125,9 @@ Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, co
st.instantiate();
st->set_device(get_device());
st->set_window_id(get_window_id());
st->set_modifiers_from_event(this);
st->set_index(index);
st->set_position(p_xform.xform(pos + p_local_ofs));
st->set_position(p_xform.xform(get_position() + p_local_ofs));
st->set_pressed(pressed);

return st;
Expand All @@ -1150,14 +1148,10 @@ void InputEventScreenTouch::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index);
ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index);

ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position);
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);

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, "pressed"), "set_pressed", "is_pressed");
}

Expand All @@ -1171,14 +1165,6 @@ int InputEventScreenDrag::get_index() const {
return index;
}

void InputEventScreenDrag::set_position(const Vector2 &p_pos) {
pos = p_pos;
}

Vector2 InputEventScreenDrag::get_position() const {
return pos;
}

void InputEventScreenDrag::set_relative(const Vector2 &p_relative) {
relative = p_relative;
}
Expand All @@ -1202,9 +1188,10 @@ Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, con

sd->set_device(get_device());
sd->set_window_id(get_window_id());
sd->set_modifiers_from_event(this);

sd->set_index(index);
sd->set_position(p_xform.xform(pos + p_local_ofs));
sd->set_position(p_xform.xform(get_position() + p_local_ofs));
sd->set_relative(p_xform.basis_xform(relative));
sd->set_velocity(p_xform.basis_xform(velocity));

Expand Down Expand Up @@ -1240,17 +1227,13 @@ void InputEventScreenDrag::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);

ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenDrag::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenDrag::get_position);

ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventScreenDrag::set_relative);
ClassDB::bind_method(D_METHOD("get_relative"), &InputEventScreenDrag::get_relative);

ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventScreenDrag::set_velocity);
ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventScreenDrag::get_velocity);

ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity"), "set_velocity", "get_velocity");
}
Expand Down Expand Up @@ -1344,23 +1327,6 @@ void InputEventAction::_bind_methods() {

///////////////////////////////////

void InputEventGesture::set_position(const Vector2 &p_pos) {
pos = p_pos;
}

void InputEventGesture::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position);
ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position);

ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
}

Vector2 InputEventGesture::get_position() const {
return pos;
}

///////////////////////////////////

void InputEventMagnifyGesture::set_factor(real_t p_factor) {
factor = p_factor;
}
Expand Down
53 changes: 23 additions & 30 deletions core/input/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,25 @@ class InputEventKey : public InputEventWithModifiers {
InputEventKey() {}
};

class InputEventMouse : public InputEventWithModifiers {
GDCLASS(InputEventMouse, InputEventWithModifiers);

MouseButton button_mask = MouseButton::NONE;
class InputEventWithPosition : public InputEventWithModifiers {
GDCLASS(InputEventWithPosition, InputEventWithModifiers);

Vector2 pos;

protected:
static void _bind_methods();

public:
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;

InputEventWithPosition() {}
};

class InputEventMouse : public InputEventWithPosition {
GDCLASS(InputEventMouse, InputEventWithPosition);

MouseButton button_mask = MouseButton::NONE;
Vector2 global_pos;

protected:
Expand All @@ -220,9 +233,6 @@ class InputEventMouse : public InputEventWithModifiers {
void set_button_mask(MouseButton p_mask);
MouseButton get_button_mask() const;

void set_position(const Vector2 &p_pos);
Vector2 get_position() const;

void set_global_position(const Vector2 &p_global_pos);
Vector2 get_global_position() const;

Expand Down Expand Up @@ -357,10 +367,9 @@ class InputEventJoypadButton : public InputEvent {
InputEventJoypadButton() {}
};

class InputEventScreenTouch : public InputEventFromWindow {
GDCLASS(InputEventScreenTouch, InputEventFromWindow);
class InputEventScreenTouch : public InputEventWithPosition {
GDCLASS(InputEventScreenTouch, InputEventWithPosition);
int index = 0;
Vector2 pos;
bool pressed = false;

protected:
Expand All @@ -370,9 +379,6 @@ class InputEventScreenTouch : public InputEventFromWindow {
void set_index(int p_index);
int get_index() const;

void set_position(const Vector2 &p_pos);
Vector2 get_position() const;

void set_pressed(bool p_pressed);
virtual bool is_pressed() const override;

Expand All @@ -383,10 +389,9 @@ class InputEventScreenTouch : public InputEventFromWindow {
InputEventScreenTouch() {}
};

class InputEventScreenDrag : public InputEventFromWindow {
GDCLASS(InputEventScreenDrag, InputEventFromWindow);
class InputEventScreenDrag : public InputEventWithPosition {
GDCLASS(InputEventScreenDrag, InputEventWithPosition);
int index = 0;
Vector2 pos;
Vector2 relative;
Vector2 velocity;

Expand All @@ -397,9 +402,6 @@ class InputEventScreenDrag : public InputEventFromWindow {
void set_index(int p_index);
int get_index() const;

void set_position(const Vector2 &p_pos);
Vector2 get_position() const;

void set_relative(const Vector2 &p_relative);
Vector2 get_relative() const;

Expand Down Expand Up @@ -448,17 +450,8 @@ class InputEventAction : public InputEvent {
InputEventAction() {}
};

class InputEventGesture : public InputEventWithModifiers {
GDCLASS(InputEventGesture, InputEventWithModifiers);

Vector2 pos;

protected:
static void _bind_methods();

public:
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
class InputEventGesture : public InputEventWithPosition {
GDCLASS(InputEventGesture, InputEventWithPosition);
};

class InputEventMagnifyGesture : public InputEventGesture {
Expand Down
1 change: 1 addition & 0 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void register_core_types() {
GDREGISTER_ABSTRACT_CLASS(InputEvent);
GDREGISTER_ABSTRACT_CLASS(InputEventWithModifiers);
GDREGISTER_ABSTRACT_CLASS(InputEventFromWindow);
GDREGISTER_ABSTRACT_CLASS(InputEventWithPosition);
GDREGISTER_CLASS(InputEventKey);
GDREGISTER_CLASS(InputEventShortcut);
GDREGISTER_ABSTRACT_CLASS(InputEventMouse);
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/InputEventFromWindow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<tutorials>
</tutorials>
<members>
<member name="window_id" type="int" setter="set_window_id" getter="get_window_id" default="0">
<member name="window_id" type="int" setter="set_window_id" getter="get_window_id">
</member>
</members>
</class>
7 changes: 1 addition & 6 deletions doc/classes/InputEventGesture.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="InputEventGesture" inherits="InputEventWithModifiers" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="InputEventGesture" inherits="InputEventWithPosition" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Base class for touch control gestures.
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<members>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
The local gesture position relative to the [Viewport]. If used in [method Control._gui_input], the position is relative to the current [Control] that received this gesture.
</member>
</members>
</class>
6 changes: 1 addition & 5 deletions doc/classes/InputEventMouse.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="InputEventMouse" inherits="InputEventWithModifiers" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="InputEventMouse" inherits="InputEventWithPosition" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Base input event type for mouse events.
</brief_description>
Expand All @@ -17,9 +17,5 @@
When received in [method Node._input] or [method Node._unhandled_input], returns the mouse's position in the root [Viewport] using the coordinate system of the root [Viewport].
When received in [method Control._gui_input], returns the mouse's position in the [CanvasLayer] that the [Control] is in using the coordinate system of the [CanvasLayer].
</member>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
When received in [method Node._input] or [method Node._unhandled_input], returns the mouse's position in the [Viewport] this [Node] is in using the coordinate system of this [Viewport].
When received in [method Control._gui_input], returns the mouse's position in the [Control] using the local coordinate system of the [Control].
</member>
</members>
</class>
5 changes: 1 addition & 4 deletions doc/classes/InputEventScreenDrag.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="InputEventScreenDrag" inherits="InputEventFromWindow" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="InputEventScreenDrag" inherits="InputEventWithPosition" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Input event type for screen drag events. Only available on mobile devices.
</brief_description>
Expand All @@ -13,9 +13,6 @@
<member name="index" type="int" setter="set_index" getter="get_index" default="0">
The drag event index in the case of a multi-drag event.
</member>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
The drag position.
</member>
<member name="relative" type="Vector2" setter="set_relative" getter="get_relative" default="Vector2(0, 0)">
The drag position relative to the previous position (position at the last frame).
</member>
Expand Down
5 changes: 1 addition & 4 deletions doc/classes/InputEventScreenTouch.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="InputEventScreenTouch" inherits="InputEventFromWindow" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="InputEventScreenTouch" inherits="InputEventWithPosition" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Input event type for screen touch events.
(only available on mobile devices)
Expand All @@ -14,9 +14,6 @@
<member name="index" type="int" setter="set_index" getter="get_index" default="0">
The touch index in the case of a multi-touch event. One index = one finger.
</member>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
The touch position, in screen (global) coordinates.
</member>
<member name="pressed" type="bool" setter="set_pressed" getter="is_pressed" default="false">
If [code]true[/code], the touch's state is pressed. If [code]false[/code], the touch's state is released.
</member>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/InputEventWithPosition.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="InputEventWithPosition" inherits="InputEventWithModifiers" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Base class for events that contain a position.
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<members>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
The local event position relative to the [Viewport]. If used in [method Control._gui_input], the position is relative to the current [Control] that received this event.
</member>
</members>
</class>

0 comments on commit 2ef2a18

Please sign in to comment.