From 035d9e729684b66d25a2cf9cc76365555b9625fc Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Apr 2023 19:38:36 +0200 Subject: [PATCH] add scrolling --- Makefile | 2 +- bindings/action_manifest.json | 16 ++++++++-- bindings/index_controller.json | 18 +++++++++++ src/app.cpp | 18 +++++++---- src/app.h | 6 ++-- src/controller.cpp | 55 ++++++++++++++++++++++++---------- src/controller.h | 6 +++- src/main.cpp | 4 +-- 8 files changed, 95 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index f3fb366..3264bb6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.2.0 +VERSION=v0.2.1 # CC := g++ CC := clang++ LFLAGS := -lX11 -lXrandr -lXtst -lglfw -lGL diff --git a/bindings/action_manifest.json b/bindings/action_manifest.json index 049a317..d4621c1 100644 --- a/bindings/action_manifest.json +++ b/bindings/action_manifest.json @@ -45,6 +45,16 @@ "name": "/actions/cursor/in/mouse_right", "requirement": "suggested", "type": "boolean" + }, + { + "name": "/actions/cursor/in/mouse_middle", + "requirement": "optional", + "type": "boolean" + }, + { + "name": "/actions/cursor/in/scroll", + "requirement": "suggested", + "type": "vector2" } ], "action_sets": [ @@ -70,8 +80,10 @@ "/actions/edit/in/grab": "grab panel", "/actions/edit/in/distance": "push/pull overlay", "/actions/cursor/in/activate_cursor": "activate cursor", - "/actions/cursor/in/mouse_left": "mouse left", - "/actions/cursor/in/mouse_right": "mouse right" + "/actions/cursor/in/mouse_left": "left mouse button", + "/actions/cursor/in/mouse_right": "right mouse button", + "/actions/cursor/in/mouse_middle": "middle mouse button", + "/actions/cursor/in/scroll": "scroll" } ] } \ No newline at end of file diff --git a/bindings/index_controller.json b/bindings/index_controller.json index 35b1b40..de796c5 100644 --- a/bindings/index_controller.json +++ b/bindings/index_controller.json @@ -195,6 +195,24 @@ }, "mode" : "button", "path" : "/user/hand/right/input/trackpad" + }, + { + "inputs" : { + "position" : { + "output" : "/actions/cursor/in/scroll" + } + }, + "mode" : "joystick", + "path" : "/user/hand/left/input/thumbstick" + }, + { + "inputs" : { + "position" : { + "output" : "/actions/cursor/in/scroll" + } + }, + "mode" : "joystick", + "path" : "/user/hand/right/input/thumbstick" } ] }, diff --git a/src/app.cpp b/src/app.cpp index f2f56af..a2cdeb1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -9,6 +9,8 @@ const VRMat root_start_pose = {{{1, 0, 0, 0}, {0, 1, 0, 0.8f}, {0, 0, 1, 0}}}; // 0.8m above origin +const int FRAME_INTERVAL = 4; // number of update loops until the frame buffer is updated + App::App() { _tracking_origin = vr::TrackingUniverseStanding; @@ -69,6 +71,10 @@ App::App() assert(action_err == 0); action_err = vr_input->GetActionHandle("/actions/cursor/in/mouse_right", &_input_handles.cursor.mouse_right); assert(action_err == 0); + action_err = vr_input->GetActionHandle("/actions/cursor/in/mouse_middle", &_input_handles.cursor.mouse_middle); + assert(action_err == 0); + action_err = vr_input->GetActionHandle("/actions/cursor/in/scroll", &_input_handles.cursor.scroll); + assert(action_err == 0); action_err = vr_input->GetActionSetHandle("/actions/main", &_input_handles.main_set); assert(action_err == 0); action_err = vr_input->GetActionSetHandle("/actions/edit", &_input_handles.edit_set); @@ -138,9 +144,9 @@ void App::InitRootOverlay() _root_overlay.SetTextureToColor(110, 30, 190); } -void App::Update() +void App::Update(float dtime) { - UpdateInput(); + UpdateInput(dtime); if (!_hidden) { _root_overlay.Update(); @@ -153,7 +159,7 @@ void App::Update() _frames_since_framebuffer += 1; } -void App::UpdateInput() +void App::UpdateInput(float dtime) { vr::VRActiveActionSet_t active_sets[2]; active_sets[0].ulActionSet = _input_handles.main_set; @@ -206,8 +212,8 @@ void App::UpdateInput() } } } - _controllers[0]->Update(); - _controllers[1]->Update(); + _controllers[0]->Update(dtime); + _controllers[1]->Update(dtime); } void App::UpdateUIVisibility() @@ -218,7 +224,7 @@ void App::UpdateUIVisibility() void App::UpdateFramebuffer() { - if (_frames_since_framebuffer < 2) + if (_frames_since_framebuffer < FRAME_INTERVAL) { return; } diff --git a/src/app.h b/src/app.h index fc81339..232f74a 100644 --- a/src/app.h +++ b/src/app.h @@ -30,6 +30,8 @@ struct InputHandles vr::VRActionHandle_t activate; vr::VRActionHandle_t mouse_left; vr::VRActionHandle_t mouse_right; + vr::VRActionHandle_t mouse_middle; + vr::VRActionHandle_t scroll; } cursor; vr::VRActionSetHandle_t cursor_set; struct @@ -45,7 +47,7 @@ class App public: App(); ~App(); - void Update(); + void Update(float dtime); std::vector GetControllers(); glm::mat4 GetTrackerPose(TrackerID tracker); @@ -94,6 +96,6 @@ class App void InitRootOverlay(); void UpdateFramebuffer(); - void UpdateInput(); + void UpdateInput(float dtime); void UpdateUIVisibility(); }; \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index e4e8722..6f7891e 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -4,9 +4,10 @@ #include "util.h" #include -const float laser_width = 0.004f; -const Color edit_col{1, 0.6f, 1}; -const Color cursor_col{0.3f, 1, 1}; +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; Controller::Controller(App *app, ControllerSide side) { @@ -16,6 +17,7 @@ Controller::Controller(App *app, ControllerSide side) _is_connected = false; _side = side; _cursor_active = false; + _last_sent_scroll = 0; std::string laser_name = "controller_laser_"; if (side == ControllerSide::Left) @@ -70,7 +72,7 @@ glm::vec3 Controller::GetLastRot() return _last_rotation; } -void Controller::Update() +void Controller::Update(float dtime) { UpdateStatus(); if (!_is_connected) @@ -80,7 +82,7 @@ void Controller::Update() if (_app->_edit_mode) { - _laser.SetColor(edit_col); + _laser.SetColor(EDIT_COLOR); if (_last_ray.overlay != nullptr) { auto ray = _last_ray; @@ -100,7 +102,7 @@ void Controller::Update() if (_grabbed_overlay != nullptr) { - float move = _app->GetInputAnalog(_app->_input_handles.edit.distance, _input_handle).y * 0.1; // TODO use frame time + float move = _app->GetInputAnalog(_app->_input_handles.edit.distance, _input_handle).y * dtime * 8; if (move != 0.0f) { auto transform = _grabbed_overlay->GetTarget()->transform; @@ -120,7 +122,7 @@ void Controller::Update() } _cursor_active = !_cursor_active; _app->_active_cursor = this; - _laser.SetColor(cursor_col); + _laser.SetColor(CURSOR_COLOR); } if (_cursor_active) { @@ -139,20 +141,41 @@ void Controller::Update() pos *= _last_ray.hit_panel->Width(); _last_ray.hit_panel->SetCursor(pos.x, pos.y); } - auto mouse_left = _app->GetInputDigital(_app->_input_handles.cursor.mouse_left, _input_handle); - if (mouse_left.bChanged) + UpdateMouseButton(_app->_input_handles.cursor.mouse_left, 1); + UpdateMouseButton(_app->_input_handles.cursor.mouse_middle, 2); + UpdateMouseButton(_app->_input_handles.cursor.mouse_right, 3); + auto scroll_state = _app->GetInputAnalog(_app->_input_handles.cursor.scroll, _input_handle); + if (scroll_state.y != 0) { - _app->SendMouseInput(1, mouse_left.bState); - } - auto mouse_right = _app->GetInputDigital(_app->_input_handles.cursor.mouse_right, _input_handle); - if (mouse_right.bChanged) - { - _app->SendMouseInput(3, mouse_right.bState); + _last_sent_scroll += dtime * glm::abs(scroll_state.y) * SCROLL_SPEED; + if (_last_sent_scroll > 1) + { + _last_sent_scroll = 0; + if (scroll_state.y > 0) + { + _app->SendMouseInput(4, true); + _app->SendMouseInput(4, false); + } + else if (scroll_state.y < 0) + { + _app->SendMouseInput(5, true); + _app->SendMouseInput(5, false); + } + } } } } } +void Controller::UpdateMouseButton(vr::VRActionHandle_t binding, unsigned int button) +{ + auto state = _app->GetInputDigital(binding, _input_handle); + if (state.bChanged) + { + _app->SendMouseInput(button, state.bState); + } +} + void Controller::UpdateLaser() { auto controller_pose = _app->GetTrackerPose(_device_index); @@ -170,7 +193,7 @@ void Controller::UpdateLaser() hmd_local_pos.z = 0; auto hmd_dir = glm::normalize(hmd_local_pos); - VRMat transform = {{{laser_width * hmd_dir.y, 0, laser_width * hmd_dir.x, 0}, {laser_width * -hmd_dir.x, 0, laser_width * hmd_dir.y, 0}, {0, len, 0, len * -0.5f}}}; + VRMat transform = {{{LASER_WIDTH * hmd_dir.y, 0, LASER_WIDTH * hmd_dir.x, 0}, {LASER_WIDTH * -hmd_dir.x, 0, LASER_WIDTH * hmd_dir.y, 0}, {0, len, 0, len * -0.5f}}}; _laser.SetTransformTracker(_device_index, &transform); _laser.SetHidden(!_is_connected || _app->_hidden || (!_app->_edit_mode && !_cursor_active)); } diff --git a/src/controller.h b/src/controller.h index cf810d2..56531f5 100644 --- a/src/controller.h +++ b/src/controller.h @@ -26,7 +26,7 @@ class Controller void ReleaseOverlay(); - void Update(); + void Update(float dtime); bool _cursor_active; @@ -34,6 +34,8 @@ class Controller void UpdateStatus(); void UpdateLaser(); + void UpdateMouseButton(vr::VRActionHandle_t binding, unsigned int button); + App *_app; Overlay _laser; ControllerSide _side; @@ -47,4 +49,6 @@ class Controller Ray _last_ray; glm::vec3 _last_rotation; glm::vec3 _last_pos; + + float _last_sent_scroll; }; diff --git a/src/main.cpp b/src/main.cpp index 617bcc2..ebf2fec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include "app.h" #include -#define UPDATE_RATE 60 +#define UPDATE_RATE 120 bool should_exit = false; @@ -18,8 +18,8 @@ int main() while (!should_exit) { - app.Update(); usleep(1000000 / UPDATE_RATE); + app.Update(1.0 / UPDATE_RATE); } printf("\nShutting down\n"); return 0;