summaryrefslogtreecommitdiffstats
path: root/core/input/input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/input/input.cpp')
-rw-r--r--core/input/input.cpp48
1 files changed, 35 insertions, 13 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp
index 7fe850069a..3de0ed39ec 100644
--- a/core/input/input.cpp
+++ b/core/input/input.cpp
@@ -130,6 +130,7 @@ void Input::_bind_methods() {
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_velocity"), &Input::get_last_mouse_velocity);
+ ClassDB::bind_method(D_METHOD("get_last_mouse_screen_velocity"), &Input::get_last_mouse_screen_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);
@@ -180,8 +181,9 @@ void Input::_bind_methods() {
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
}
+#ifdef TOOLS_ENABLED
void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
- String pf = p_function;
+ const String pf = p_function;
if ((p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || pf == "is_action_just_pressed" || pf == "is_action_just_released" || pf == "get_action_strength" || pf == "get_action_raw_strength")) ||
(p_idx < 2 && pf == "get_axis") ||
@@ -200,8 +202,9 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S
}
Object::get_argument_options(p_function, p_idx, r_options);
}
+#endif
-void Input::VelocityTrack::update(const Vector2 &p_delta_p) {
+void Input::VelocityTrack::update(const Vector2 &p_delta_p, const Vector2 &p_screen_delta_p) {
uint64_t tick = OS::get_singleton()->get_ticks_usec();
uint32_t tdiff = tick - last_tick;
float delta_t = tdiff / 1000000.0;
@@ -210,12 +213,15 @@ void Input::VelocityTrack::update(const Vector2 &p_delta_p) {
if (delta_t > max_ref_frame) {
// First movement in a long time, reset and start again.
velocity = Vector2();
+ screen_velocity = Vector2();
accum = p_delta_p;
+ screen_accum = p_screen_delta_p;
accum_t = 0;
return;
}
accum += p_delta_p;
+ screen_accum += p_screen_delta_p;
accum_t += delta_t;
if (accum_t < min_ref_frame) {
@@ -224,6 +230,7 @@ void Input::VelocityTrack::update(const Vector2 &p_delta_p) {
}
velocity = accum / accum_t;
+ screen_velocity = screen_accum / accum_t;
accum = Vector2();
accum_t = 0;
}
@@ -450,7 +457,7 @@ static String _hex_str(uint8_t p_byte) {
return ret;
}
-void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid, Dictionary p_joypad_info) {
+void Input::joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid, const Dictionary &p_joypad_info) {
_THREAD_SAFE_METHOD_
// Clear the pressed status if a Joypad gets disconnected.
@@ -594,7 +601,8 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
set_mouse_position(position);
}
Vector2 relative = mm->get_relative();
- mouse_velocity_track.update(relative);
+ Vector2 screen_relative = mm->get_relative_screen_position();
+ mouse_velocity_track.update(relative, screen_relative);
if (event_dispatch_function && emulate_touch_from_mouse && !p_is_emulated && mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
Ref<InputEventScreenDrag> drag_event;
@@ -602,10 +610,12 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
drag_event->set_position(position);
drag_event->set_relative(relative);
+ drag_event->set_relative_screen_position(screen_relative);
drag_event->set_tilt(mm->get_tilt());
drag_event->set_pen_inverted(mm->get_pen_inverted());
drag_event->set_pressure(mm->get_pressure());
drag_event->set_velocity(get_last_mouse_velocity());
+ drag_event->set_screen_velocity(get_last_mouse_screen_velocity());
drag_event->set_device(InputEvent::DEVICE_ID_EMULATION);
_THREAD_SAFE_UNLOCK_
@@ -669,8 +679,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
if (sd.is_valid()) {
VelocityTrack &track = touch_velocity_track[sd->get_index()];
- track.update(sd->get_relative());
+ track.update(sd->get_relative(), sd->get_relative_screen_position());
sd->set_velocity(track.velocity);
+ sd->set_screen_velocity(track.screen_velocity);
if (emulate_mouse_from_touch && sd->get_index() == mouse_from_touch_index) {
Ref<InputEventMouseMotion> motion_event;
@@ -683,7 +694,9 @@ 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_relative_screen_position(sd->get_relative_screen_position());
motion_event->set_velocity(sd->get_velocity());
+ motion_event->set_screen_velocity(sd->get_screen_velocity());
motion_event->set_button_mask(mouse_button_mask);
_parse_input_event_impl(motion_event, true);
@@ -827,10 +840,15 @@ Point2 Input::get_mouse_position() const {
}
Point2 Input::get_last_mouse_velocity() {
- mouse_velocity_track.update(Vector2());
+ mouse_velocity_track.update(Vector2(), Vector2());
return mouse_velocity_track.velocity;
}
+Point2 Input::get_last_mouse_screen_velocity() {
+ mouse_velocity_track.update(Vector2(), Vector2());
+ return mouse_velocity_track.screen_velocity;
+}
+
BitField<MouseButtonMask> Input::get_mouse_button_mask() const {
return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state();
}
@@ -865,6 +883,8 @@ Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, con
}
void Input::action_press(const StringName &p_action, float p_strength) {
+ ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action));
+
// Create or retrieve existing action.
ActionState &action_state = action_states[p_action];
@@ -879,6 +899,8 @@ void Input::action_press(const StringName &p_action, float p_strength) {
}
void Input::action_release(const StringName &p_action) {
+ ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action));
+
// Create or retrieve existing action.
ActionState &action_state = action_states[p_action];
action_state.cache.pressed = 0;
@@ -1394,7 +1416,7 @@ void Input::_get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat
}
}
-JoyButton Input::_get_output_button(String output) {
+JoyButton Input::_get_output_button(const String &output) {
for (int i = 0; i < (int)JoyButton::SDL_MAX; i++) {
if (output == _joy_buttons[i]) {
return JoyButton(i);
@@ -1403,7 +1425,7 @@ JoyButton Input::_get_output_button(String output) {
return JoyButton::INVALID;
}
-JoyAxis Input::_get_output_axis(String output) {
+JoyAxis Input::_get_output_axis(const String &output) {
for (int i = 0; i < (int)JoyAxis::SDL_MAX; i++) {
if (output == _joy_axes[i]) {
return JoyAxis(i);
@@ -1412,7 +1434,7 @@ JoyAxis Input::_get_output_axis(String output) {
return JoyAxis::INVALID;
}
-void Input::parse_mapping(String p_mapping) {
+void Input::parse_mapping(const String &p_mapping) {
_THREAD_SAFE_METHOD_;
JoyDeviceMapping mapping;
@@ -1515,11 +1537,11 @@ void Input::parse_mapping(String p_mapping) {
map_db.push_back(mapping);
}
-void Input::add_joy_mapping(String p_mapping, bool p_update_existing) {
+void Input::add_joy_mapping(const String &p_mapping, bool p_update_existing) {
parse_mapping(p_mapping);
if (p_update_existing) {
Vector<String> entry = p_mapping.split(",");
- String uid = entry[0];
+ const String &uid = entry[0];
for (KeyValue<int, Joypad> &E : joy_names) {
Joypad &joy = E.value;
if (joy.uid == uid) {
@@ -1529,7 +1551,7 @@ void Input::add_joy_mapping(String p_mapping, bool p_update_existing) {
}
}
-void Input::remove_joy_mapping(String p_guid) {
+void Input::remove_joy_mapping(const String &p_guid) {
for (int i = map_db.size() - 1; i >= 0; i--) {
if (p_guid == map_db[i].uid) {
map_db.remove_at(i);
@@ -1543,7 +1565,7 @@ void Input::remove_joy_mapping(String p_guid) {
}
}
-void Input::set_fallback_mapping(String p_guid) {
+void Input::set_fallback_mapping(const String &p_guid) {
for (int i = 0; i < map_db.size(); i++) {
if (map_db[i].uid == p_guid) {
fallback_mapping = i;