diff --git a/cpp/modmesh/view/R3DWidget.hpp b/cpp/modmesh/view/R3DWidget.hpp index 01e346a0..af63941a 100644 --- a/cpp/modmesh/view/R3DWidget.hpp +++ b/cpp/modmesh/view/R3DWidget.hpp @@ -60,6 +60,45 @@ class ROrbitCameraController using Qt3DExtras::QOrbitCameraController::QOrbitCameraController; }; /* end class ROrbitCameraController */ +class RCustomCameraController + : public Qt3DExtras::QAbstractCameraController +{ + Q_OBJECT +public: + explicit RCustomCameraController(Qt3DCore::QNode * parent = nullptr) + : QAbstractCameraController(parent) + { + } + ~RCustomCameraController() {} + +private: + void moveCamera(const QAbstractCameraController::InputState & state, float dt) override + { + Qt3DRender::QCamera * theCamera = camera(); + + if (theCamera == nullptr) + return; + + theCamera->translate(QVector3D(state.txAxisValue * linearSpeed(), + state.tyAxisValue * linearSpeed(), + state.tzAxisValue * linearSpeed()) * + dt); + if (state.leftMouseButtonActive) + { + float theLookSpeed = lookSpeed(); + if (state.shiftKeyActive) + { + theLookSpeed *= 0.2f; + } + + const QVector3D upVector(0.0f, 1.0f, 0.0f); + + theCamera->pan(state.rxAxisValue * theLookSpeed * dt, upVector); + theCamera->tilt(state.ryAxisValue * theLookSpeed * dt); + } + }; +}; /* end class RCustomCameraController */ + class RScene : public Qt3DCore::QEntity { @@ -85,6 +124,8 @@ class RScene void setFirstPersonCameraController() { setCameraController(new RFirstPersonCameraController(this)); } + void setCustomCameraController() { setCameraController(new RCustomCameraController(this)); } + private: Qt3DExtras::QAbstractCameraController * m_controller = nullptr; diff --git a/cpp/modmesh/view/RManager.cpp b/cpp/modmesh/view/RManager.cpp index 2783ab8e..90f9f49a 100644 --- a/cpp/modmesh/view/RManager.cpp +++ b/cpp/modmesh/view/RManager.cpp @@ -193,15 +193,39 @@ void RManager::setUpMenu() } }); + auto * use_custom_camera = new RAction( + QString("Use Custom Camera Controller"), + QString("Use Custom Camera Controller"), + [this]() + { + qDebug() << "Use Custom Camera Controller (menu demo)"; + for (auto subwin : m_mdiArea->subWindowList()) + { + try + { + R3DWidget * viewer = dynamic_cast(subwin->widget()); + viewer->scene()->setCustomCameraController(); + viewer->scene()->controller()->setCamera(viewer->camera()); + } + catch (std::bad_cast & e) + { + std::cerr << e.what() << std::endl; + } + } + }); + auto * cameraGroup = new QActionGroup(m_mainWindow); cameraGroup->addAction(use_orbit_camera); cameraGroup->addAction(use_fps_camera); + cameraGroup->addAction(use_custom_camera); use_orbit_camera->setCheckable(true); use_fps_camera->setCheckable(true); + use_custom_camera->setCheckable(true); use_orbit_camera->setChecked(true); m_cameraMenu->addAction(use_orbit_camera); m_cameraMenu->addAction(use_fps_camera); + m_cameraMenu->addAction(use_custom_camera); } {