3
0
Fork 0
forked from suyu/suyu

input_common: Implement dedicated motion from mouse

This commit is contained in:
Narr the Reg 2023-02-21 21:46:42 -06:00
parent 8f3e2a1b48
commit 673accd630
3 changed files with 85 additions and 24 deletions

View file

@ -10,17 +10,25 @@
#include "input_common/drivers/mouse.h" #include "input_common/drivers/mouse.h"
namespace InputCommon { namespace InputCommon {
constexpr int update_time = 10;
constexpr float default_stick_sensitivity = 0.022f;
constexpr float default_motion_sensitivity = 0.008f;
constexpr int mouse_axis_x = 0; constexpr int mouse_axis_x = 0;
constexpr int mouse_axis_y = 1; constexpr int mouse_axis_y = 1;
constexpr int wheel_axis_x = 2; constexpr int wheel_axis_x = 2;
constexpr int wheel_axis_y = 3; constexpr int wheel_axis_y = 3;
constexpr int motion_wheel_y = 4;
constexpr PadIdentifier identifier = { constexpr PadIdentifier identifier = {
.guid = Common::UUID{}, .guid = Common::UUID{},
.port = 0, .port = 0,
.pad = 0, .pad = 0,
}; };
constexpr PadIdentifier motion_identifier = {
.guid = Common::UUID{},
.port = 0,
.pad = 1,
};
constexpr PadIdentifier real_mouse_identifier = { constexpr PadIdentifier real_mouse_identifier = {
.guid = Common::UUID{}, .guid = Common::UUID{},
.port = 1, .port = 1,
@ -37,47 +45,87 @@ Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_))
PreSetController(identifier); PreSetController(identifier);
PreSetController(real_mouse_identifier); PreSetController(real_mouse_identifier);
PreSetController(touch_identifier); PreSetController(touch_identifier);
PreSetController(motion_identifier);
// Initialize all mouse axis // Initialize all mouse axis
PreSetAxis(identifier, mouse_axis_x); PreSetAxis(identifier, mouse_axis_x);
PreSetAxis(identifier, mouse_axis_y); PreSetAxis(identifier, mouse_axis_y);
PreSetAxis(identifier, wheel_axis_x); PreSetAxis(identifier, wheel_axis_x);
PreSetAxis(identifier, wheel_axis_y); PreSetAxis(identifier, wheel_axis_y);
PreSetAxis(identifier, motion_wheel_y);
PreSetAxis(real_mouse_identifier, mouse_axis_x); PreSetAxis(real_mouse_identifier, mouse_axis_x);
PreSetAxis(real_mouse_identifier, mouse_axis_y); PreSetAxis(real_mouse_identifier, mouse_axis_y);
PreSetAxis(touch_identifier, mouse_axis_x); PreSetAxis(touch_identifier, mouse_axis_x);
PreSetAxis(touch_identifier, mouse_axis_y); PreSetAxis(touch_identifier, mouse_axis_y);
// Initialize variables
mouse_origin = {};
last_mouse_position = {};
wheel_position = {};
last_mouse_change = {};
last_motion_change = {};
update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); }); update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); });
} }
void Mouse::UpdateThread(std::stop_token stop_token) { void Mouse::UpdateThread(std::stop_token stop_token) {
Common::SetCurrentThreadName("Mouse"); Common::SetCurrentThreadName("Mouse");
constexpr int update_time = 10;
while (!stop_token.stop_requested()) { while (!stop_token.stop_requested()) {
if (Settings::values.mouse_panning) { UpdateStickInput();
// Slow movement by 4% UpdateMotionInput();
last_mouse_change *= 0.96f;
const float sensitivity =
Settings::values.mouse_panning_sensitivity.GetValue() * 0.022f;
SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity);
SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity);
}
SetAxis(identifier, motion_wheel_y, 0.0f); if (mouse_panning_timeout++ > 20) {
if (mouse_panning_timout++ > 20) {
StopPanning(); StopPanning();
} }
std::this_thread::sleep_for(std::chrono::milliseconds(update_time)); std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
} }
} }
void Mouse::UpdateStickInput() {
if (!Settings::values.mouse_panning) {
return;
}
const float sensitivity =
Settings::values.mouse_panning_sensitivity.GetValue() * default_stick_sensitivity;
// Slow movement by 4%
last_mouse_change *= 0.96f;
SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity);
SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity);
}
void Mouse::UpdateMotionInput() {
const float sensitivity =
Settings::values.mouse_panning_sensitivity.GetValue() * default_motion_sensitivity;
// Slow movement by 7%
if (Settings::values.mouse_panning) {
last_motion_change *= 0.93f;
} else {
last_motion_change.z *= 0.93f;
}
const BasicMotion motion_data{
.gyro_x = last_motion_change.x * sensitivity,
.gyro_y = last_motion_change.y * sensitivity,
.gyro_z = last_motion_change.z * sensitivity,
.accel_x = 0,
.accel_y = 0,
.accel_z = 0,
.delta_timestamp = update_time * 1000,
};
SetMotion(motion_identifier, 0, motion_data);
}
void Mouse::Move(int x, int y, int center_x, int center_y) { void Mouse::Move(int x, int y, int center_x, int center_y) {
if (Settings::values.mouse_panning) { if (Settings::values.mouse_panning) {
mouse_panning_timeout = 0;
auto mouse_change = auto mouse_change =
(Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>(); (Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>();
mouse_panning_timout = 0; Common::Vec3<float> motion_change{-mouse_change.y, -mouse_change.x, last_motion_change.z};
const auto move_distance = mouse_change.Length(); const auto move_distance = mouse_change.Length();
if (move_distance == 0) { if (move_distance == 0) {
@ -93,6 +141,7 @@ void Mouse::Move(int x, int y, int center_x, int center_y) {
// Average mouse movements // Average mouse movements
last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f); last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f);
last_motion_change = (last_motion_change * 0.69f) + (motion_change * 0.31f);
const auto last_move_distance = last_mouse_change.Length(); const auto last_move_distance = last_mouse_change.Length();
@ -116,6 +165,12 @@ void Mouse::Move(int x, int y, int center_x, int center_y) {
const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f; const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f;
SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity); SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity);
SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity); SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity);
last_motion_change = {
static_cast<float>(-mouse_move.y) / 50.0f,
static_cast<float>(-mouse_move.x) / 50.0f,
last_motion_change.z,
};
} }
} }
@ -157,15 +212,19 @@ void Mouse::ReleaseButton(MouseButton button) {
SetAxis(identifier, mouse_axis_x, 0); SetAxis(identifier, mouse_axis_x, 0);
SetAxis(identifier, mouse_axis_y, 0); SetAxis(identifier, mouse_axis_y, 0);
} }
last_motion_change.x = 0;
last_motion_change.y = 0;
button_pressed = false; button_pressed = false;
} }
void Mouse::MouseWheelChange(int x, int y) { void Mouse::MouseWheelChange(int x, int y) {
wheel_position.x += x; wheel_position.x += x;
wheel_position.y += y; wheel_position.y += y;
last_motion_change.z += static_cast<f32>(y) / 100.0f;
SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x)); SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x));
SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y)); SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y));
SetAxis(identifier, motion_wheel_y, static_cast<f32>(y) / 100.0f);
} }
void Mouse::ReleaseAllButtons() { void Mouse::ReleaseAllButtons() {
@ -234,6 +293,9 @@ Common::Input::ButtonNames Mouse::GetUIName(const Common::ParamPackage& params)
if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) { if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
return Common::Input::ButtonNames::Engine; return Common::Input::ButtonNames::Engine;
} }
if (params.Has("motion")) {
return Common::Input::ButtonNames::Engine;
}
return Common::Input::ButtonNames::Invalid; return Common::Input::ButtonNames::Invalid;
} }

View file

@ -96,6 +96,8 @@ public:
private: private:
void UpdateThread(std::stop_token stop_token); void UpdateThread(std::stop_token stop_token);
void UpdateStickInput();
void UpdateMotionInput();
void StopPanning(); void StopPanning();
Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const; Common::Input::ButtonNames GetUIButtonName(const Common::ParamPackage& params) const;
@ -103,9 +105,10 @@ private:
Common::Vec2<int> mouse_origin; Common::Vec2<int> mouse_origin;
Common::Vec2<int> last_mouse_position; Common::Vec2<int> last_mouse_position;
Common::Vec2<float> last_mouse_change; Common::Vec2<float> last_mouse_change;
Common::Vec3<float> last_motion_change;
Common::Vec2<int> wheel_position; Common::Vec2<int> wheel_position;
bool button_pressed; bool button_pressed;
int mouse_panning_timout{}; int mouse_panning_timeout{};
std::jthread update_thread; std::jthread update_thread;
}; };

View file

@ -142,14 +142,10 @@ void MappingFactory::RegisterMotion(const MappingData& data) {
new_input.Set("port", static_cast<int>(data.pad.port)); new_input.Set("port", static_cast<int>(data.pad.port));
new_input.Set("pad", static_cast<int>(data.pad.pad)); new_input.Set("pad", static_cast<int>(data.pad.pad));
// If engine is mouse map the mouse position as 3 axis motion // If engine is mouse map it automatically to mouse motion
if (data.engine == "mouse") { if (data.engine == "mouse") {
new_input.Set("axis_x", 1); new_input.Set("motion", 0);
new_input.Set("invert_x", "-"); new_input.Set("pad", 1);
new_input.Set("axis_y", 0);
new_input.Set("axis_z", 4);
new_input.Set("range", 1.0f);
new_input.Set("deadzone", 0.0f);
input_queue.Push(new_input); input_queue.Push(new_input);
return; return;
} }