From 8b7a3e0067ea82b452b2112e2fda41ecc9760dd9 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 1 May 2023 18:21:28 +0200 Subject: [PATCH] prevent accidental dragging by locking cursor in place while mouse movement is small --- src/app.cpp | 1 + src/controller.cpp | 17 ++++++++++++----- src/controller.h | 6 ++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index a2cdeb1..caa4500 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -142,6 +142,7 @@ void App::InitRootOverlay() _root_overlay.SetWidth(0.25f); _root_overlay.SetTransformWorld(&root_start_pose); _root_overlay.SetTextureToColor(110, 30, 190); + _root_overlay.SetHidden(true); } void App::Update(float dtime) diff --git a/src/controller.cpp b/src/controller.cpp index 6f7891e..9597303 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -7,7 +7,8 @@ const float LASER_WIDTH = 0.004f; const Color EDIT_COLOR{1, 0.6f, 1}; const Color CURSOR_COLOR{0.3f, 1, 1}; -const float SCROLL_SPEED = 48.0f; +const float SCROLL_SPEED = 48; +const float MOUSE_DRAG_THRESHOLD = 48; Controller::Controller(App *app, ControllerSide side) { @@ -16,8 +17,6 @@ Controller::Controller(App *app, ControllerSide side) _input_handle = 0; _is_connected = false; _side = side; - _cursor_active = false; - _last_sent_scroll = 0; std::string laser_name = "controller_laser_"; if (side == ControllerSide::Left) @@ -126,7 +125,6 @@ void Controller::Update(float dtime) } if (_cursor_active) { - // printf("update cursor on hand %d\n", _side); if (_last_ray.overlay != nullptr && _last_ray.hit_panel != nullptr) { auto pos = glm::vec2(_last_ray.local_pos.x, _last_ray.local_pos.y); @@ -139,7 +137,15 @@ void Controller::Update(float dtime) pos.y += 0.5f * _last_ray.overlay->Ratio(); pos *= _last_ray.hit_panel->Width(); - _last_ray.hit_panel->SetCursor(pos.x, pos.y); + if (glm::length(pos - _last_set_mouse_pos) > MOUSE_DRAG_THRESHOLD) + { + _mouse_drag_lock = false; + } + if (!_mouse_drag_lock) + { + _last_ray.hit_panel->SetCursor(pos.x, pos.y); + _last_set_mouse_pos = pos; + } } UpdateMouseButton(_app->_input_handles.cursor.mouse_left, 1); UpdateMouseButton(_app->_input_handles.cursor.mouse_middle, 2); @@ -173,6 +179,7 @@ void Controller::UpdateMouseButton(vr::VRActionHandle_t binding, unsigned int bu if (state.bChanged) { _app->SendMouseInput(button, state.bState); + _mouse_drag_lock = state.bState; } } diff --git a/src/controller.h b/src/controller.h index 56531f5..e055a80 100644 --- a/src/controller.h +++ b/src/controller.h @@ -28,7 +28,7 @@ class Controller void Update(float dtime); - bool _cursor_active; + bool _cursor_active = false; private: void UpdateStatus(); @@ -50,5 +50,7 @@ class Controller glm::vec3 _last_rotation; glm::vec3 _last_pos; - float _last_sent_scroll; + float _last_sent_scroll = 0; + bool _mouse_drag_lock = false; + glm::vec2 _last_set_mouse_pos; };