From 8081c067a611b147b9bde00ce1b7aec92fabaa69 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 25 Feb 2014 13:09:37 +0100 Subject: [PATCH] moving the camera with W and S keys --- apps/opencs/view/render/scenewidget.cpp | 61 +++++++++++++++++++++++-- apps/opencs/view/render/scenewidget.hpp | 14 +++++- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index 88493fd76..280a85fd4 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -10,16 +11,18 @@ namespace CSVRender { - SceneWidget::SceneWidget(QWidget *parent) : QWidget(parent) , mWindow(NULL) , mCamera(NULL) - , mSceneMgr(NULL), mNavigationMode (NavigationMode_Free) + , mSceneMgr(NULL), mNavigationMode (NavigationMode_Free), mUpdate (false) + , mKeyForward (false), mKeyBackward (false) { setAttribute(Qt::WA_PaintOnScreen); setAttribute(Qt::WA_NoSystemBackground); + setFocusPolicy (Qt::StrongFocus); + mSceneMgr = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC); // Throw in a random color just to make sure multiple scenes work @@ -44,6 +47,11 @@ namespace CSVRender mCamera->lookAt(0,0,0); mCamera->setNearClipDistance(0.1); mCamera->setFarClipDistance(3000); + + QTimer *timer = new QTimer (this); + + connect (timer, SIGNAL (timeout()), this, SLOT (update())); + timer->start (20); /// \todo make this configurable } void SceneWidget::updateOgreWindow() @@ -93,7 +101,6 @@ namespace CSVRender e->accept(); } - QPaintEngine* SceneWidget::paintEngine() const { // We don't want another paint engine to get in the way. @@ -130,4 +137,52 @@ namespace CSVRender return QWidget::event(e); } + void SceneWidget::keyPressEvent (QKeyEvent *event) + { + switch (event->key()) + { + case Qt::Key_W: mKeyForward = true; break; + case Qt::Key_S: mKeyBackward = true; break; + default: QWidget::keyPressEvent (event); + } + } + + void SceneWidget::keyReleaseEvent (QKeyEvent *event) + { + switch (event->key()) + { + case Qt::Key_W: mKeyForward = false; break; + case Qt::Key_S: mKeyBackward = false; break; + default: QWidget::keyReleaseEvent (event); + } + } + + void SceneWidget::focusOutEvent (QFocusEvent *event) + { + mKeyForward = false; + mKeyBackward = false; + + QWidget::focusOutEvent (event); + } + + void SceneWidget::update() + { + if (mKeyForward && !mKeyBackward) + { + mCamera->move (mCamera->getDirection()); + mUpdate = true; + } + + if (!mKeyForward && mKeyBackward) + { + mCamera->move (-mCamera->getDirection()); + mUpdate = true; + } + + if (mUpdate) + { + mUpdate = false; + mWindow->update(); + } + } } diff --git a/apps/opencs/view/render/scenewidget.hpp b/apps/opencs/view/render/scenewidget.hpp index deba47fbb..777398353 100644 --- a/apps/opencs/view/render/scenewidget.hpp +++ b/apps/opencs/view/render/scenewidget.hpp @@ -12,7 +12,6 @@ namespace Ogre namespace CSVRender { - class SceneWidget : public QWidget { Q_OBJECT @@ -34,6 +33,12 @@ namespace CSVRender void resizeEvent(QResizeEvent* e); bool event(QEvent* e); + void keyPressEvent (QKeyEvent *event); + + void keyReleaseEvent (QKeyEvent *event); + + void focusOutEvent (QFocusEvent *event); + void updateOgreWindow(); Ogre::Camera* mCamera; @@ -41,6 +46,13 @@ namespace CSVRender Ogre::RenderWindow* mWindow; NavigationMode mNavigationMode; + bool mUpdate; + int mKeyForward; + int mKeyBackward; + + private slots: + + void update(); }; }