diff options
Diffstat (limited to 'core/input/input.cpp')
-rw-r--r-- | core/input/input.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp index 8f976cbaa3..d87a8824f7 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); @@ -198,9 +199,10 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S r_options->push_back(name.quote()); } } + Object::get_argument_options(p_function, p_idx, r_options); } -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; @@ -209,12 +211,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) { @@ -223,6 +228,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; } @@ -593,7 +599,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; @@ -601,10 +608,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_ @@ -668,8 +677,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; @@ -682,7 +692,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); @@ -826,10 +838,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(); } @@ -864,6 +881,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]; @@ -878,6 +897,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; @@ -1518,7 +1539,7 @@ void Input::add_joy_mapping(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) { |