From 2ef2a18b1539d0a3cf96dad90ff6481e5d5cf872 Mon Sep 17 00:00:00 2001 From: Markus Sauermann <6299227+Sauermann@users.noreply.github.com> Date: Mon, 14 Mar 2022 16:31:02 +0100 Subject: [PATCH] Streamline InputEvent hierarchy to define position only once. --- core/input/input_event.cpp | 78 ++++++++------------------ core/input/input_event.h | 53 ++++++++--------- core/register_core_types.cpp | 1 + doc/classes/InputEventFromWindow.xml | 2 +- doc/classes/InputEventGesture.xml | 7 +-- doc/classes/InputEventMouse.xml | 6 +- doc/classes/InputEventScreenDrag.xml | 5 +- doc/classes/InputEventScreenTouch.xml | 5 +- doc/classes/InputEventWithPosition.xml | 15 +++++ 9 files changed, 66 insertions(+), 106 deletions(-) create mode 100644 doc/classes/InputEventWithPosition.xml diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index ab0f36132f14b..197abcaa268e1 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -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) { @@ -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"); } @@ -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; } @@ -1128,8 +1125,9 @@ Ref 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; @@ -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"); } @@ -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; } @@ -1202,9 +1188,10 @@ Ref 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)); @@ -1240,9 +1227,6 @@ 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); @@ -1250,7 +1234,6 @@ void InputEventScreenDrag::_bind_methods() { 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"); } @@ -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; } diff --git a/core/input/input_event.h b/core/input/input_event.h index 114db46623221..98bc24876d225 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -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: @@ -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; @@ -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: @@ -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; @@ -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; @@ -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; @@ -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 { diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 2aa47c6c965be..5b2acae967df1 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -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); diff --git a/doc/classes/InputEventFromWindow.xml b/doc/classes/InputEventFromWindow.xml index adbf8ccc088d5..ac153fdefc846 100644 --- a/doc/classes/InputEventFromWindow.xml +++ b/doc/classes/InputEventFromWindow.xml @@ -7,7 +7,7 @@ - + diff --git a/doc/classes/InputEventGesture.xml b/doc/classes/InputEventGesture.xml index 85d1a405b258a..23db6206a3513 100644 --- a/doc/classes/InputEventGesture.xml +++ b/doc/classes/InputEventGesture.xml @@ -1,5 +1,5 @@ - + Base class for touch control gestures. @@ -7,9 +7,4 @@ - - - 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. - - diff --git a/doc/classes/InputEventMouse.xml b/doc/classes/InputEventMouse.xml index 889329d97edb7..2cde1bb634cb3 100644 --- a/doc/classes/InputEventMouse.xml +++ b/doc/classes/InputEventMouse.xml @@ -1,5 +1,5 @@ - + Base input event type for mouse events. @@ -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]. - - 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]. - diff --git a/doc/classes/InputEventScreenDrag.xml b/doc/classes/InputEventScreenDrag.xml index e5cc522b21fb4..ed77b6688bbc5 100644 --- a/doc/classes/InputEventScreenDrag.xml +++ b/doc/classes/InputEventScreenDrag.xml @@ -1,5 +1,5 @@ - + Input event type for screen drag events. Only available on mobile devices. @@ -13,9 +13,6 @@ The drag event index in the case of a multi-drag event. - - The drag position. - The drag position relative to the previous position (position at the last frame). diff --git a/doc/classes/InputEventScreenTouch.xml b/doc/classes/InputEventScreenTouch.xml index fb50454917a44..65a908ed9056e 100644 --- a/doc/classes/InputEventScreenTouch.xml +++ b/doc/classes/InputEventScreenTouch.xml @@ -1,5 +1,5 @@ - + Input event type for screen touch events. (only available on mobile devices) @@ -14,9 +14,6 @@ The touch index in the case of a multi-touch event. One index = one finger. - - The touch position, in screen (global) coordinates. - If [code]true[/code], the touch's state is pressed. If [code]false[/code], the touch's state is released. diff --git a/doc/classes/InputEventWithPosition.xml b/doc/classes/InputEventWithPosition.xml new file mode 100644 index 0000000000000..c39dd65c34788 --- /dev/null +++ b/doc/classes/InputEventWithPosition.xml @@ -0,0 +1,15 @@ + + + + Base class for events that contain a position. + + + + + + + + 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. + + +