1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00

moved implementation of the 1st person camera into a separate file

This commit is contained in:
Marc Zinnschlag 2014-02-27 15:23:14 +01:00
parent 14c3bfcf62
commit c977b2a756
7 changed files with 158 additions and 80 deletions

View File

@ -68,7 +68,7 @@ opencs_units (view/render
) )
opencs_units_noqt (view/render opencs_units_noqt (view/render
navigation navigation navigation1st
) )
opencs_units_noqt (view/world opencs_units_noqt (view/world

View File

@ -0,0 +1,60 @@
#include "navigation1st.hpp"
#include <OgreCamera.h>
#include <QPoint>
CSVRender::Navigation1st::Navigation1st() : mCamera (0) {}
bool CSVRender::Navigation1st::activate (Ogre::Camera *camera)
{
mCamera = camera;
mCamera->setFixedYawAxis (true);
return false;
}
bool CSVRender::Navigation1st::wheelMoved (int delta)
{
mCamera->move (getFactor (true) * mCamera->getDirection() * delta);
return true;
}
bool CSVRender::Navigation1st::mouseMoved (const QPoint& delta, int mode)
{
if (mode==0)
{
// turn camera
if (delta.x())
mCamera->yaw (Ogre::Degree (getFactor (true) * delta.x()));
if (delta.y())
mCamera->pitch (Ogre::Degree (getFactor (true) * delta.y()));
return true;
}
else if (mode==1)
{
// pan camera
if (delta.x())
mCamera->move (getFactor (true) * mCamera->getDerivedRight() * delta.x());
if (delta.y())
mCamera->move (getFactor (true) * -mCamera->getDerivedUp() * delta.y());
return true;
}
return false;
}
bool CSVRender::Navigation1st::handleMovementKeys (int vertical, int horizontal)
{
if (vertical)
mCamera->move (getFactor (false) * mCamera->getDirection() * vertical);
if (horizontal)
mCamera->move (getFactor (true) * mCamera->getDerivedRight() * horizontal);
return true;
}

View File

@ -0,0 +1,32 @@
#ifndef OPENCS_VIEW_NAVIGATION1ST_H
#define OPENCS_VIEW_NAVIGATION1ST_H
#include "navigation.hpp"
namespace CSVRender
{
/// \brief First person-like camera controls
class Navigation1st : public Navigation
{
Ogre::Camera *mCamera;
public:
Navigation1st();
virtual bool activate (Ogre::Camera *camera);
///< \return Update required?
virtual bool wheelMoved (int delta);
///< \return Update required?
virtual bool mouseMoved (const QPoint& delta, int mode);
///< \param mode: 0: default mouse key, 1: default mouse key and modifier key 1
/// \return Update required?
virtual bool handleMovementKeys (int vertical, int horizontal);
///< \return Update required?
};
}
#endif

View File

@ -9,16 +9,18 @@
#include <OgreEntity.h> #include <OgreEntity.h>
#include <OgreCamera.h> #include <OgreCamera.h>
#include "navigation.hpp"
namespace CSVRender namespace CSVRender
{ {
SceneWidget::SceneWidget(QWidget *parent) SceneWidget::SceneWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, mWindow(NULL) , mWindow(NULL)
, mCamera(NULL) , mCamera(NULL)
, mSceneMgr(NULL), mNavigationMode (NavigationMode_1stPerson), mUpdate (false) , mSceneMgr(NULL), mNavigation (0), mUpdate (false)
, mKeyForward (false), mKeyBackward (false), mKeyLeft (false), mKeyRight (false) , mKeyForward (false), mKeyBackward (false), mKeyLeft (false), mKeyRight (false)
, mFast (false), mDragging (false), mMod1 (false) , mFast (false), mDragging (false), mMod1 (false)
, mMouseSensitivity (2), mFastFactor (4) /// \todo make these configurable , mFastFactor (4) /// \todo make this configurable
{ {
setAttribute(Qt::WA_PaintOnScreen); setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground); setAttribute(Qt::WA_NoSystemBackground);
@ -94,12 +96,12 @@ namespace CSVRender
Ogre::Root::getSingleton().destroyRenderTarget(mWindow); Ogre::Root::getSingleton().destroyRenderTarget(mWindow);
} }
void SceneWidget::setNavigationMode (NavigationMode mode) void SceneWidget::setNavigation (Navigation *navigation)
{ {
if (mode!=mNavigationMode) if ((mNavigation = navigation))
{ {
mNavigationMode = mode; mNavigation->setFastModeFactor (mFast ? mFastFactor : 1);
mNavigation->activate (mCamera);
} }
} }
@ -156,8 +158,17 @@ namespace CSVRender
case Qt::Key_S: mKeyBackward = true; break; case Qt::Key_S: mKeyBackward = true; break;
case Qt::Key_A: mKeyLeft = true; break; case Qt::Key_A: mKeyLeft = true; break;
case Qt::Key_D: mKeyRight = true; break; case Qt::Key_D: mKeyRight = true; break;
case Qt::Key_Shift: mFast = true; break;
case Qt::Key_Control: mMod1 = true; break; case Qt::Key_Control: mMod1 = true; break;
case Qt::Key_Shift:
mFast = true;
if (mNavigation)
mNavigation->setFastModeFactor (mFastFactor);
break;
default: QWidget::keyPressEvent (event); default: QWidget::keyPressEvent (event);
} }
} }
@ -170,19 +181,27 @@ namespace CSVRender
case Qt::Key_S: mKeyBackward = false; break; case Qt::Key_S: mKeyBackward = false; break;
case Qt::Key_A: mKeyLeft = false; break; case Qt::Key_A: mKeyLeft = false; break;
case Qt::Key_D: mKeyRight = false; break; case Qt::Key_D: mKeyRight = false; break;
case Qt::Key_Shift: mFast = false; break;
case Qt::Key_Control: mMod1 = false; break; case Qt::Key_Control: mMod1 = false; break;
case Qt::Key_Shift:
mFast = false;
if (mNavigation)
mNavigation->setFastModeFactor (1);
break;
default: QWidget::keyReleaseEvent (event); default: QWidget::keyReleaseEvent (event);
} }
} }
void SceneWidget::wheelEvent (QWheelEvent *event) void SceneWidget::wheelEvent (QWheelEvent *event)
{ {
if (int delta = event->delta()) if (mNavigation)
{ if (event->delta())
mCamera->move ((getFastFactor() * mCamera->getDirection() * delta)/mMouseSensitivity); if (mNavigation->wheelMoved (event->delta()))
mUpdate = true; mUpdate = true;
}
} }
void SceneWidget::leaveEvent (QEvent *event) void SceneWidget::leaveEvent (QEvent *event)
@ -199,40 +218,9 @@ namespace CSVRender
QPoint diff = mOldPos-event->pos(); QPoint diff = mOldPos-event->pos();
mOldPos = event->pos(); mOldPos = event->pos();
if (!mMod1) if (mNavigation)
{ if (mNavigation->mouseMoved (diff, mMod1 ? 1 : 0))
// turn camera
if (diff.x())
{
mCamera->yaw (
Ogre::Degree ((getFastFactor() * diff.x())/mMouseSensitivity));
mUpdate = true; mUpdate = true;
}
if (diff.y())
{
mCamera->pitch (
Ogre::Degree ((getFastFactor() * diff.y())/mMouseSensitivity));
mUpdate = true;
}
}
else
{
// pan camera
if (diff.x())
{
Ogre::Vector3 direction = mCamera->getDerivedRight();
mCamera->move ((getFastFactor() * direction * diff.x())/mMouseSensitivity);
mUpdate = true;
}
if (diff.y())
{
Ogre::Vector3 direction = mCamera->getDerivedUp();
mCamera->move ((getFastFactor() * -direction * diff.y())/mMouseSensitivity);
mUpdate = true;
}
}
} }
else else
{ {
@ -262,30 +250,24 @@ namespace CSVRender
void SceneWidget::update() void SceneWidget::update()
{ {
if (mKeyForward && !mKeyBackward) if (mNavigation)
{ {
mCamera->move (getFastFactor() * mCamera->getDirection()); int horizontal = 0;
mUpdate = true; int vertical = 0;
}
if (!mKeyForward && mKeyBackward) if (mKeyForward && !mKeyBackward)
{ vertical = 1;
mCamera->move (getFastFactor() * -mCamera->getDirection()); else if (!mKeyForward && mKeyBackward)
mUpdate = true; vertical = -1;
}
if (mKeyLeft && !mKeyRight) if (mKeyLeft && !mKeyRight)
{ horizontal = -1;
Ogre::Vector3 direction = mCamera->getDerivedRight(); else if (!mKeyLeft && mKeyRight)
mCamera->move (getFastFactor() * -direction); horizontal = 1;
mUpdate = true;
}
if (!mKeyLeft && mKeyRight) if (horizontal || vertical)
{ if (mNavigation->handleMovementKeys (vertical, horizontal))
Ogre::Vector3 direction = mCamera->getDerivedRight(); mUpdate = true;
mCamera->move (getFastFactor() * direction);
mUpdate = true;
} }
if (mUpdate) if (mUpdate)

View File

@ -12,25 +12,21 @@ namespace Ogre
namespace CSVRender namespace CSVRender
{ {
class Navigation;
class SceneWidget : public QWidget class SceneWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
enum NavigationMode
{
NavigationMode_1stPerson,
NavigationMode_Free,
NavigationMode_Orbit
};
SceneWidget(QWidget *parent); SceneWidget(QWidget *parent);
virtual ~SceneWidget(); virtual ~SceneWidget();
QPaintEngine* paintEngine() const; QPaintEngine* paintEngine() const;
void setNavigationMode (NavigationMode mode); void setNavigation (Navigation *navigation);
///< \attention The ownership of \a navigation is not transferred to *this.
private: private:
void paintEvent(QPaintEvent* e); void paintEvent(QPaintEvent* e);
@ -59,7 +55,7 @@ namespace CSVRender
Ogre::SceneManager* mSceneMgr; Ogre::SceneManager* mSceneMgr;
Ogre::RenderWindow* mWindow; Ogre::RenderWindow* mWindow;
NavigationMode mNavigationMode; Navigation *mNavigation;
bool mUpdate; bool mUpdate;
int mKeyForward; int mKeyForward;
int mKeyBackward; int mKeyBackward;
@ -69,7 +65,6 @@ namespace CSVRender
bool mDragging; bool mDragging;
bool mMod1; bool mMod1;
QPoint mOldPos; QPoint mOldPos;
int mMouseSensitivity;
int mFastFactor; int mFastFactor;
private slots: private slots:

View File

@ -59,6 +59,8 @@ CSVWorld::SceneSubView::SceneSubView (const CSMWorld::UniversalId& id, CSMDoc::D
widget->setLayout (layout); widget->setLayout (layout);
setWidget (widget); setWidget (widget);
mScene->setNavigation (&m1st);
} }
void CSVWorld::SceneSubView::setEditLock (bool locked) void CSVWorld::SceneSubView::setEditLock (bool locked)
@ -81,9 +83,13 @@ void CSVWorld::SceneSubView::setStatusBar (bool show)
void CSVWorld::SceneSubView::selectNavigationMode (const std::string& mode) void CSVWorld::SceneSubView::selectNavigationMode (const std::string& mode)
{ {
if (mode=="1st") if (mode=="1st")
mScene->setNavigationMode (CSVRender::SceneWidget::NavigationMode_1stPerson); mScene->setNavigation (&m1st);
else if (mode=="free") else if (mode=="free")
mScene->setNavigationMode (CSVRender::SceneWidget::NavigationMode_Free); {
}
else if (mode=="orbit") else if (mode=="orbit")
mScene->setNavigationMode (CSVRender::SceneWidget::NavigationMode_Orbit); {
}
} }

View File

@ -3,6 +3,8 @@
#include "../doc/subview.hpp" #include "../doc/subview.hpp"
#include "../render/navigation1st.hpp"
class QModelIndex; class QModelIndex;
namespace CSMDoc namespace CSMDoc
@ -27,6 +29,7 @@ namespace CSVWorld
TableBottomBox *mBottom; TableBottomBox *mBottom;
CSVRender::SceneWidget *mScene; CSVRender::SceneWidget *mScene;
CSVRender::Navigation1st m1st;
public: public: