From 4a078d7cf717c91694c96ee2a8d72135eeb9c95e Mon Sep 17 00:00:00 2001 From: floralrainfall Date: Fri, 21 Jul 2023 04:31:37 -0400 Subject: [PATCH] better camera mouse controls --- .../Client/Common/Header/QtInputManager.hpp | 4 +++ .../Client/Common/Source/QtInputManager.cpp | 29 ++++++++++++++++--- Projects/Engine/Header/App/InputManager.hpp | 2 ++ Projects/Engine/Source/App/InputManager.cpp | 4 +++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Projects/Client/Common/Header/QtInputManager.hpp b/Projects/Client/Common/Header/QtInputManager.hpp index ddf0d03..be774ad 100644 --- a/Projects/Client/Common/Header/QtInputManager.hpp +++ b/Projects/Client/Common/Header/QtInputManager.hpp @@ -12,6 +12,8 @@ namespace RNR { private: QPointF last_position; + QPoint grab_position; + bool m_grabbed; OgreWidget* widget; bool reset; public: @@ -21,5 +23,7 @@ namespace RNR void mouseEvent(QMouseEvent* e); virtual void resetMouse(); + virtual void grab(); + virtual void ungrab(); }; } diff --git a/Projects/Client/Common/Source/QtInputManager.cpp b/Projects/Client/Common/Source/QtInputManager.cpp index 01cbaf0..9398c2c 100644 --- a/Projects/Client/Common/Source/QtInputManager.cpp +++ b/Projects/Client/Common/Source/QtInputManager.cpp @@ -7,6 +7,7 @@ namespace RNR { last_position = QPointF(); this->widget = widget; + m_grabbed = false; } void QtInputManager::keyEvent(QKeyEvent* e) @@ -56,10 +57,30 @@ namespace RNR void QtInputManager::resetMouse() { widget->clearFocus(); - QPoint glob = widget->mapToGlobal(QPoint(widget->width()/2,widget->height()/2)); - QCursor::setPos(glob); - last_position = QPoint(widget->width()/2,widget->height()/2); - widget->setFocus(); + if(!m_grabbed) + { + QPoint glob = widget->mapToGlobal(QPoint(widget->width()/2,widget->height()/2)); + QCursor::setPos(glob); + last_position = QPoint(widget->width()/2,widget->height()/2); + } + else + { + QCursor::setPos(widget->mapToGlobal(grab_position)); + last_position = grab_position; + } reset = true; + widget->setFocus(); + } + + void QtInputManager::grab() + { + grab_position = widget->mapFromGlobal(QCursor::pos()); + last_position = grab_position; + m_grabbed = true; + } + + void QtInputManager::ungrab() + { + m_grabbed = false; } } \ No newline at end of file diff --git a/Projects/Engine/Header/App/InputManager.hpp b/Projects/Engine/Header/App/InputManager.hpp index d4dc866..02b6031 100644 --- a/Projects/Engine/Header/App/InputManager.hpp +++ b/Projects/Engine/Header/App/InputManager.hpp @@ -23,6 +23,8 @@ namespace RNR IInputManager(); virtual void resetMouse() {}; + virtual void grab() {}; + virtual void ungrab() {}; void setWorld(World* world) { m_world = world; world->setInputManager(this); } diff --git a/Projects/Engine/Source/App/InputManager.cpp b/Projects/Engine/Source/App/InputManager.cpp index 28c6b1b..9a4c5e8 100644 --- a/Projects/Engine/Source/App/InputManager.cpp +++ b/Projects/Engine/Source/App/InputManager.cpp @@ -91,6 +91,10 @@ namespace RNR void IInputManager::mouseSecondaryState(bool down) { + if(down) + grab(); + else if(!down) + ungrab(); state.mouse_secondary = down; }