Rename speed to velocity when it's a directional Vector

This commit is contained in:
Marcel Admiraal 2021-12-29 13:22:22 +00:00
parent 9f058674ac
commit 3105d9b1f3
13 changed files with 73 additions and 78 deletions

View File

@ -121,7 +121,7 @@ void Input::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_accelerometer", "value"), &Input::set_accelerometer);
ClassDB::bind_method(D_METHOD("set_magnetometer", "value"), &Input::set_magnetometer);
ClassDB::bind_method(D_METHOD("set_gyroscope", "value"), &Input::set_gyroscope);
ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed);
ClassDB::bind_method(D_METHOD("get_last_mouse_velocity"), &Input::get_last_mouse_velocity);
ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask);
ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode);
ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode);
@ -183,7 +183,7 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S
}
}
void Input::SpeedTrack::update(const Vector2 &p_delta_p) {
void Input::VelocityTrack::update(const Vector2 &p_delta_p) {
uint64_t tick = OS::get_singleton()->get_ticks_usec();
uint32_t tdiff = tick - last_tick;
float delta_t = tdiff / 1000000.0;
@ -202,17 +202,17 @@ void Input::SpeedTrack::update(const Vector2 &p_delta_p) {
accum = accum - slice;
accum_t -= min_ref_frame;
speed = (slice / min_ref_frame).lerp(speed, min_ref_frame / max_ref_frame);
velocity = (slice / min_ref_frame).lerp(velocity, min_ref_frame / max_ref_frame);
}
}
void Input::SpeedTrack::reset() {
void Input::VelocityTrack::reset() {
last_tick = OS::get_singleton()->get_ticks_usec();
speed = Vector2();
velocity = Vector2();
accum_t = 0;
}
Input::SpeedTrack::SpeedTrack() {
Input::VelocityTrack::VelocityTrack() {
min_ref_frame = 0.1;
max_ref_frame = 0.3;
reset();
@ -515,7 +515,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
drag_event->set_position(mm->get_position());
drag_event->set_relative(mm->get_relative());
drag_event->set_speed(mm->get_speed());
drag_event->set_velocity(mm->get_velocity());
event_dispatch_function(drag_event);
}
@ -525,12 +525,12 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
if (st.is_valid()) {
if (st->is_pressed()) {
SpeedTrack &track = touch_speed_track[st->get_index()];
VelocityTrack &track = touch_velocity_track[st->get_index()];
track.reset();
} else {
// Since a pointer index may not occur again (OSs may or may not reuse them),
// imperatively remove it from the map to keep no fossil entries in it
touch_speed_track.erase(st->get_index());
touch_velocity_track.erase(st->get_index());
}
if (emulate_mouse_from_touch) {
@ -570,9 +570,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
Ref<InputEventScreenDrag> sd = p_event;
if (sd.is_valid()) {
SpeedTrack &track = touch_speed_track[sd->get_index()];
VelocityTrack &track = touch_velocity_track[sd->get_index()];
track.update(sd->get_relative());
sd->set_speed(track.speed);
sd->set_velocity(track.velocity);
if (emulate_mouse_from_touch && sd->get_index() == mouse_from_touch_index) {
Ref<InputEventMouseMotion> motion_event;
@ -582,7 +582,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
motion_event->set_position(sd->get_position());
motion_event->set_global_position(sd->get_position());
motion_event->set_relative(sd->get_relative());
motion_event->set_speed(sd->get_speed());
motion_event->set_velocity(sd->get_velocity());
motion_event->set_button_mask(mouse_button_mask);
_parse_input_event_impl(motion_event, true);
@ -696,7 +696,7 @@ void Input::set_gyroscope(const Vector3 &p_gyroscope) {
}
void Input::set_mouse_position(const Point2 &p_posf) {
mouse_speed_track.update(p_posf - mouse_pos);
mouse_velocity_track.update(p_posf - mouse_pos);
mouse_pos = p_posf;
}
@ -704,8 +704,8 @@ Point2 Input::get_mouse_position() const {
return mouse_pos;
}
Point2 Input::get_last_mouse_speed() const {
return mouse_speed_track.speed;
Point2 Input::get_last_mouse_velocity() const {
return mouse_velocity_track.velocity;
}
MouseButton Input::get_mouse_button_mask() const {

View File

@ -117,9 +117,9 @@ private:
int mouse_from_touch_index = -1;
struct SpeedTrack {
struct VelocityTrack {
uint64_t last_tick;
Vector2 speed;
Vector2 velocity;
Vector2 accum;
float accum_t;
float min_ref_frame;
@ -127,7 +127,7 @@ private:
void update(const Vector2 &p_delta_p);
void reset();
SpeedTrack();
VelocityTrack();
};
struct Joypad {
@ -141,8 +141,8 @@ private:
int hat_current = 0;
};
SpeedTrack mouse_speed_track;
Map<int, SpeedTrack> touch_speed_track;
VelocityTrack mouse_velocity_track;
Map<int, VelocityTrack> touch_velocity_track;
Map<int, Joypad> joy_names;
int fallback_mapping = -1;
@ -274,7 +274,7 @@ public:
Vector3 get_gyroscope() const;
Point2 get_mouse_position() const;
Point2 get_last_mouse_speed() const;
Vector2 get_last_mouse_velocity() const;
MouseButton get_mouse_button_mask() const;
void warp_mouse_position(const Vector2 &p_to);

View File

@ -739,20 +739,15 @@ Vector2 InputEventMouseMotion::get_relative() const {
return relative;
}
void InputEventMouseMotion::set_speed(const Vector2 &p_speed) {
speed = p_speed;
void InputEventMouseMotion::set_velocity(const Vector2 &p_velocity) {
velocity = p_velocity;
}
Vector2 InputEventMouseMotion::get_speed() const {
return speed;
Vector2 InputEventMouseMotion::get_velocity() const {
return velocity;
}
Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
Vector2 g = get_global_position();
Vector2 l = p_xform.xform(get_position() + p_local_ofs);
Vector2 r = p_xform.basis_xform(get_relative());
Vector2 s = p_xform.basis_xform(get_speed());
Ref<InputEventMouseMotion> mm;
mm.instantiate();
@ -761,20 +756,20 @@ Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, co
mm->set_modifiers_from_event(this);
mm->set_position(l);
mm->set_position(p_xform.xform(get_position() + p_local_ofs));
mm->set_pressure(get_pressure());
mm->set_tilt(get_tilt());
mm->set_global_position(g);
mm->set_global_position(get_global_position());
mm->set_button_mask(get_button_mask());
mm->set_relative(r);
mm->set_speed(s);
mm->set_relative(p_xform.basis_xform(get_relative()));
mm->set_velocity(p_xform.basis_xform(get_velocity()));
return mm;
}
String InputEventMouseMotion::as_text() const {
return vformat(RTR("Mouse motion at position (%s) with speed (%s)"), String(get_position()), String(get_speed()));
return vformat(RTR("Mouse motion at position (%s) with velocity (%s)"), String(get_position()), String(get_velocity()));
}
String InputEventMouseMotion::to_string() {
@ -802,7 +797,7 @@ String InputEventMouseMotion::to_string() {
// Work around the fact vformat can only take 5 substitutions but 6 need to be passed.
String mask_and_position = vformat("button_mask=%s, position=(%s)", button_mask_string, String(get_position()));
return vformat("InputEventMouseMotion: %s, relative=(%s), speed=(%s), pressure=%.2f, tilt=(%s)", mask_and_position, String(get_relative()), String(get_speed()), get_pressure(), String(get_tilt()));
return vformat("InputEventMouseMotion: %s, relative=(%s), velocity=(%s), pressure=%.2f, tilt=(%s)", mask_and_position, String(get_relative()), String(get_velocity()), get_pressure(), String(get_tilt()));
}
bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
@ -841,7 +836,7 @@ bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) {
set_position(motion->get_position());
set_global_position(motion->get_global_position());
set_speed(motion->get_speed());
set_velocity(motion->get_velocity());
relative += motion->get_relative();
return true;
@ -857,13 +852,13 @@ void InputEventMouseMotion::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_relative", "relative"), &InputEventMouseMotion::set_relative);
ClassDB::bind_method(D_METHOD("get_relative"), &InputEventMouseMotion::get_relative);
ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InputEventMouseMotion::set_speed);
ClassDB::bind_method(D_METHOD("get_speed"), &InputEventMouseMotion::get_speed);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &InputEventMouseMotion::set_velocity);
ClassDB::bind_method(D_METHOD("get_velocity"), &InputEventMouseMotion::get_velocity);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "tilt"), "set_tilt", "get_tilt");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pressure"), "set_pressure", "get_pressure");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "relative"), "set_relative", "get_relative");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "speed"), "set_speed", "get_speed");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity"), "set_velocity", "get_velocity");
}
///////////////////////////////////
@ -1188,12 +1183,12 @@ Vector2 InputEventScreenDrag::get_relative() const {
return relative;
}
void InputEventScreenDrag::set_speed(const Vector2 &p_speed) {
speed = p_speed;
void InputEventScreenDrag::set_velocity(const Vector2 &p_velocity) {
velocity = p_velocity;
}
Vector2 InputEventScreenDrag::get_speed() const {
return speed;
Vector2 InputEventScreenDrag::get_velocity() const {
return velocity;
}
Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {
@ -1207,17 +1202,17 @@ Ref<InputEvent> InputEventScreenDrag::xformed_by(const Transform2D &p_xform, con
sd->set_index(index);
sd->set_position(p_xform.xform(pos + p_local_ofs));
sd->set_relative(p_xform.basis_xform(relative));
sd->set_speed(p_xform.basis_xform(speed));
sd->set_velocity(p_xform.basis_xform(velocity));
return sd;
}
String InputEventScreenDrag::as_text() const {
return vformat(RTR("Screen dragged with %s touch points at position (%s) with speed of (%s)"), itos(index), String(get_position()), String(get_speed()));
return vformat(RTR("Screen dragged with %s touch points at position (%s) with velocity of (%s)"), itos(index), String(get_position()), String(get_velocity()));
}
String InputEventScreenDrag::to_string() {
return vformat("InputEventScreenDrag: index=%d, position=(%s), relative=(%s), speed=(%s)", index, String(get_position()), String(get_relative()), String(get_speed()));
return vformat("InputEventScreenDrag: index=%d, position=(%s), relative=(%s), velocity=(%s)", index, String(get_position()), String(get_relative()), String(get_velocity()));
}
bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
@ -1230,7 +1225,7 @@ bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
}
set_position(drag->get_position());
set_speed(drag->get_speed());
set_velocity(drag->get_velocity());
relative += drag->get_relative();
return true;
@ -1246,13 +1241,13 @@ void InputEventScreenDrag::_bind_methods() {
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_speed", "speed"), &InputEventScreenDrag::set_speed);
ClassDB::bind_method(D_METHOD("get_speed"), &InputEventScreenDrag::get_speed);
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, "speed"), "set_speed", "get_speed");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity"), "set_velocity", "get_velocity");
}
///////////////////////////////////

View File

@ -271,7 +271,7 @@ class InputEventMouseMotion : public InputEventMouse {
Vector2 tilt;
float pressure = 0;
Vector2 relative;
Vector2 speed;
Vector2 velocity;
protected:
static void _bind_methods();
@ -286,8 +286,8 @@ public:
void set_relative(const Vector2 &p_relative);
Vector2 get_relative() const;
void set_speed(const Vector2 &p_speed);
Vector2 get_speed() const;
void set_velocity(const Vector2 &p_velocity);
Vector2 get_velocity() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
virtual String as_text() const override;
@ -388,7 +388,7 @@ class InputEventScreenDrag : public InputEventFromWindow {
int index = 0;
Vector2 pos;
Vector2 relative;
Vector2 speed;
Vector2 velocity;
protected:
static void _bind_methods();
@ -403,8 +403,8 @@ public:
void set_relative(const Vector2 &p_relative);
Vector2 get_relative() const;
void set_speed(const Vector2 &p_speed);
Vector2 get_speed() const;
void set_velocity(const Vector2 &p_velocity);
Vector2 get_velocity() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
virtual String as_text() const override;

View File

@ -141,10 +141,10 @@
Returns the strength of the joypad vibration: x is the strength of the weak motor, and y is the strength of the strong motor.
</description>
</method>
<method name="get_last_mouse_speed" qualifiers="const">
<method name="get_last_mouse_velocity" qualifiers="const">
<return type="Vector2" />
<description>
Returns the mouse speed for the last time the cursor was moved, and this until the next frame where the mouse moves. This means that even if the mouse is not moving, this function will still return the value of the last motion.
Returns the mouse velocity for the last time the cursor was moved, and this until the next frame where the mouse moves. This means that even if the mouse is not moving, this function will still return the value of the last motion.
</description>
</method>
<method name="get_magnetometer" qualifiers="const">

View File

@ -4,7 +4,7 @@
Input event type for mouse motion events.
</brief_description>
<description>
Contains mouse and pen motion information. Supports relative, absolute positions and speed. See [method Node._input].
Contains mouse and pen motion information. Supports relative, absolute positions and velocity. See [method Node._input].
[b]Note:[/b] By default, this event is only emitted once per frame rendered at most. If you need more precise input reporting, call [method Input.set_use_accumulated_input] with [code]false[/code] to make events emitted as often as possible. If you use InputEventMouseMotion to draw lines, consider implementing [url=https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm]Bresenham's line algorithm[/url] as well to avoid visible gaps in lines if the user is moving the mouse quickly.
</description>
<tutorials>
@ -19,11 +19,11 @@
The mouse position relative to the previous position (position at the last frame).
[b]Note:[/b] Since [InputEventMouseMotion] is only emitted when the mouse moves, the last event won't have a relative position of [code]Vector2(0, 0)[/code] when the user stops moving the mouse.
</member>
<member name="speed" type="Vector2" setter="set_speed" getter="get_speed" default="Vector2(0, 0)">
The mouse speed in pixels per second.
</member>
<member name="tilt" type="Vector2" setter="set_tilt" getter="get_tilt" default="Vector2(0, 0)">
Represents the angles of tilt of the pen. Positive X-coordinate value indicates a tilt to the right. Positive Y-coordinate value indicates a tilt toward the user. Ranges from [code]-1.0[/code] to [code]1.0[/code] for both axes.
</member>
<member name="velocity" type="Vector2" setter="set_velocity" getter="get_velocity" default="Vector2(0, 0)">
The mouse velocity in pixels per second.
</member>
</members>
</class>

View File

@ -19,8 +19,8 @@
<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>
<member name="speed" type="Vector2" setter="set_speed" getter="get_speed" default="Vector2(0, 0)">
The drag speed.
<member name="velocity" type="Vector2" setter="set_velocity" getter="get_velocity" default="Vector2(0, 0)">
The drag velocity.
</member>
</members>
</class>

View File

@ -230,7 +230,7 @@ void DisplayServerJavaScript::mouse_move_callback(double p_x, double p_y, double
ev->set_relative(Vector2(p_rel_x, p_rel_y));
Input::get_singleton()->set_mouse_position(ev->get_position());
ev->set_speed(Input::get_singleton()->get_last_mouse_speed());
ev->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
Input::get_singleton()->parse_input_event(ev);
}

View File

@ -3644,7 +3644,7 @@ void DisplayServerX11::process_events() {
mm->set_position(pos);
mm->set_global_position(pos);
Input::get_singleton()->set_mouse_position(pos);
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
mm->set_relative(rel);
@ -3674,7 +3674,7 @@ void DisplayServerX11::process_events() {
mm->set_window_id(E.key);
mm->set_position(pos_focused);
mm->set_global_position(pos_focused);
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
Input::get_singleton()->parse_input_event(mm);
break;

View File

@ -787,7 +787,7 @@ static void _mouseDownEvent(DisplayServer::WindowID window_id, NSEvent *event, M
mm->set_tilt(Vector2(p.x, p.y));
}
mm->set_global_position(pos);
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
const Vector2i relativeMotion = Vector2i(delta.x, delta.y) * DS_OSX->screen_get_max_scale();
mm->set_relative(relativeMotion);
_get_key_modifier_state([event modifierFlags], mm);

View File

@ -2067,7 +2067,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_position(c);
mm->set_global_position(c);
Input::get_singleton()->set_mouse_position(c);
mm->set_speed(Vector2(0, 0));
mm->set_velocity(Vector2(0, 0));
if (raw->data.mouse.usFlags == MOUSE_MOVE_RELATIVE) {
mm->set_relative(Vector2(raw->data.mouse.lLastX, raw->data.mouse.lLastY));
@ -2172,7 +2172,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
}
Input::get_singleton()->set_mouse_position(mm->get_position());
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
if (old_invalid) {
old_x = mm->get_position().x;
@ -2319,7 +2319,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
}
Input::get_singleton()->set_mouse_position(mm->get_position());
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
if (old_invalid) {
old_x = mm->get_position().x;
@ -2425,7 +2425,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
}
Input::get_singleton()->set_mouse_position(mm->get_position());
mm->set_speed(Input::get_singleton()->get_last_mouse_speed());
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
if (old_invalid) {
old_x = mm->get_position().x;

View File

@ -3172,7 +3172,7 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {
if (drag_touching && !drag_touching_deaccel) {
drag_accum -= mm->get_relative().y;
v_scroll->set_value(drag_from + drag_accum);
drag_speed = -mm->get_speed().y;
drag_speed = -mm->get_velocity().y;
}
}

View File

@ -1700,13 +1700,13 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
if (over) {
Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
Size2 pos = localizer.xform(mpos);
Vector2 speed = localizer.basis_xform(mm->get_speed());
Vector2 velocity = localizer.basis_xform(mm->get_velocity());
Vector2 rel = localizer.basis_xform(mm->get_relative());
mm = mm->xformed_by(Transform2D()); // Make a copy.
mm->set_global_position(mpos);
mm->set_speed(speed);
mm->set_velocity(velocity);
mm->set_relative(rel);
if (mm->get_button_mask() == MouseButton::NONE) {
@ -1956,12 +1956,12 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
if (over->can_process()) {
Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
Size2 pos = localizer.xform(drag_event->get_position());
Vector2 speed = localizer.basis_xform(drag_event->get_speed());
Vector2 velocity = localizer.basis_xform(drag_event->get_velocity());
Vector2 rel = localizer.basis_xform(drag_event->get_relative());
drag_event = drag_event->xformed_by(Transform2D()); // Make a copy.
drag_event->set_speed(speed);
drag_event->set_velocity(velocity);
drag_event->set_relative(rel);
drag_event->set_position(pos);