moving the camera with W and S keys

actorid
Marc Zinnschlag 11 years ago
parent 4ec4599be3
commit 8081c067a6

@ -2,6 +2,7 @@
#include <QEvent>
#include <QResizeEvent>
#include <QTimer>
#include <OgreRoot.h>
#include <OgreRenderWindow.h>
@ -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();
}
}
}

@ -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();
};
}

Loading…
Cancel
Save