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

Refactoring in mwrender::Camera. enum for normal/preview/vanity mode.

This commit is contained in:
Petr Mikheev 2020-07-18 10:27:13 +02:00
parent 8ac7ffc32b
commit 63cab4052d
3 changed files with 34 additions and 50 deletions

View File

@ -52,7 +52,8 @@ namespace MWRender
mCamera(camera),
mAnimation(nullptr),
mFirstPersonView(true),
mPreviewMode(false),
mMode(Mode::Normal),
mVanityAllowed(true),
mNearest(30.f),
mFurthest(800.f),
mIsNearest(false),
@ -73,9 +74,6 @@ namespace MWRender
mDynamicCameraDistanceEnabled(false),
mShowCrosshairInThirdPersonMode(false)
{
mVanity.enabled = false;
mVanity.allowed = true;
mCameraDistance = mBaseCameraDistance;
mUpdateCallback = new UpdateRenderCameraCallback(this);
@ -133,9 +131,6 @@ namespace MWRender
void Camera::updateCamera(osg::Camera *cam)
{
if (mTrackingPtr.isEmpty())
return;
osg::Vec3d focal, position;
getPosition(focal, position);
@ -165,11 +160,6 @@ namespace MWRender
setPitch(pitch);
}
void Camera::attachTo(const MWWorld::Ptr &ptr)
{
mTrackingPtr = ptr;
}
void Camera::update(float duration, bool paused)
{
if (mAnimation->upperBodyReady())
@ -193,13 +183,11 @@ namespace MWRender
// only show the crosshair in game mode
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
wm->showCrosshair(!wm->isGuiMode() && !mVanity.enabled && !mPreviewMode
wm->showCrosshair(!wm->isGuiMode() && mMode != Mode::Preview && mMode != Mode::Vanity
&& (mFirstPersonView || mShowCrosshairInThirdPersonMode));
if(mVanity.enabled)
{
if(mMode == Mode::Vanity)
rotateCamera(0.f, osg::DegreesToRadians(3.f * duration), true);
}
updateFocalPointOffset(duration);
@ -280,9 +268,9 @@ namespace MWRender
void Camera::allowVanityMode(bool allow)
{
if (!allow && mVanity.enabled)
if (!allow && mMode == Mode::Vanity)
toggleVanityMode(false);
mVanity.allowed = allow;
mVanityAllowed = allow;
}
bool Camera::toggleVanityMode(bool enable)
@ -296,12 +284,12 @@ namespace MWRender
return false;
}
if(!mVanity.allowed && enable)
if (!mVanityAllowed && enable)
return false;
if(mVanity.enabled == enable)
if ((mMode == Mode::Vanity) == enable)
return true;
mVanity.enabled = enable;
mMode = enable ? Mode::Vanity : Mode::Normal;
processViewChange();
return true;
@ -312,10 +300,10 @@ namespace MWRender
if (mFirstPersonView && !mAnimation->upperBodyReady())
return;
if(mPreviewMode == enable)
if((mMode == Mode::Preview) == enable)
return;
mPreviewMode = enable;
mMode = enable ? Mode::Preview : Mode::Normal;
processViewChange();
}
@ -350,26 +338,21 @@ namespace MWRender
void Camera::updateBaseCameraDistance(float dist, bool adjust)
{
if(mFirstPersonView && !mPreviewMode && !mVanity.enabled)
if (isFirstPerson())
return;
if (adjust)
dist += std::min(mCameraDistance - getCameraDistanceCorrection(), mBaseCameraDistance);
mIsNearest = dist <= mNearest;
dist = osg::clampBetween(dist, mNearest, mFurthest);
if (!mFirstPersonView)
{
mBaseCameraDistance = dist;
Settings::Manager::setFloat("third person camera distance", "Camera", dist);
}
mBaseCameraDistance = osg::clampBetween(dist, mNearest, mFurthest);
Settings::Manager::setFloat("third person camera distance", "Camera", mBaseCameraDistance);
setCameraDistance();
}
void Camera::setCameraDistance(float dist, bool adjust)
{
if(mFirstPersonView && !mPreviewMode && !mVanity.enabled)
if (isFirstPerson())
return;
if (adjust)
dist += mCameraDistance;
@ -392,7 +375,7 @@ namespace MWRender
void Camera::setCameraDistance()
{
mFocalPointAdjustment = osg::Vec3d();
if (mFirstPersonView)
if (isFirstPerson())
return;
mCameraDistance = mBaseCameraDistance + getCameraDistanceCorrection();
if (mDynamicCameraDistanceEnabled)

View File

@ -23,6 +23,9 @@ namespace MWRender
/// \brief Camera control
class Camera
{
public:
enum class Mode { Normal, Vanity, Preview };
private:
MWWorld::Ptr mTrackingPtr;
osg::ref_ptr<const osg::Node> mTrackingNode;
@ -33,15 +36,13 @@ namespace MWRender
NpcAnimation *mAnimation;
bool mFirstPersonView;
bool mPreviewMode;
Mode mMode;
bool mVanityAllowed;
float mNearest;
float mFurthest;
bool mIsNearest;
struct {
bool enabled, allowed;
} mVanity;
float mHeight, mBaseCameraDistance;
float mPitch, mYaw;
@ -78,6 +79,8 @@ namespace MWRender
Camera(osg::Camera* camera);
~Camera();
/// Attach camera to object
void attachTo(const MWWorld::Ptr &ptr) { mTrackingPtr = ptr; }
MWWorld::Ptr getTrackingPtr() const { return mTrackingPtr; }
void setFocalPointTransitionSpeed(float v) { mFocalPointTransitionSpeedCoef = v; }
@ -102,9 +105,6 @@ namespace MWRender
float getPitch() const { return mPitch; }
void setPitch(float angle);
/// Attach camera to object
void attachTo(const MWWorld::Ptr &);
/// @param Force view mode switch, even if currently not allowed by the animation.
void toggleViewMode(bool force=false);
@ -117,8 +117,7 @@ namespace MWRender
/// \brief Lowers the camera for sneak.
void setSneakOffset(float offset);
bool isFirstPerson() const
{ return !(mVanity.enabled || mPreviewMode || !mFirstPersonView); }
bool isFirstPerson() const { return mFirstPersonView && mMode == Mode::Normal; }
void processViewChange();
@ -147,8 +146,8 @@ namespace MWRender
/// Stores focal and camera world positions in passed arguments
void getPosition(osg::Vec3d &focal, osg::Vec3d &camera) const;
bool isVanityOrPreviewModeEnabled() const { return mPreviewMode || mVanity.enabled; }
bool isVanityModeEnabled() const { return mVanity.enabled; }
bool isVanityOrPreviewModeEnabled() const { return mMode != Mode::Normal; }
Mode getMode() const { return mMode; }
bool isNearest() const { return mIsNearest; }
};

View File

@ -51,12 +51,14 @@ namespace MWRender
if (oldMode == mMode)
return;
if (mCamera->isVanityOrPreviewModeEnabled())
mCamera->setFocalPointTransitionSpeed(mCamera->isVanityModeEnabled() ? 0.2 : 1);
else if (oldMode == Mode::Combat || mMode == Mode::Combat)
if (mCamera->getMode() == Camera::Mode::Vanity)
// Player doesn't touch controls for a long time. Transition should be very slow.
mCamera->setFocalPointTransitionSpeed(0.2f);
else if ((oldMode == Mode::Combat || mMode == Mode::Combat) && mCamera->getMode() == Camera::Mode::Normal)
// Transition to/from combat mode and we are not it preview mode. Should be fast.
mCamera->setFocalPointTransitionSpeed(5.f);
else
mCamera->setFocalPointTransitionSpeed(1.f);
mCamera->setFocalPointTransitionSpeed(1.f); // Default transition speed.
switch (mMode)
{
@ -98,4 +100,4 @@ namespace MWRender
mMode = mDefaultShoulderIsRight ? Mode::RightShoulder : Mode::LeftShoulder;
}
}
}