mirror of https://github.com/axmolengine/axmol.git
Add position rotation setters & getters override for custom behaviors.
This commit is contained in:
parent
e9ce8da4ef
commit
81a6c508d1
|
@ -99,7 +99,7 @@ Camera::Camera()
|
|||
, _farPlane(1024)
|
||||
, _zoomFactorNearPlane(10)
|
||||
, _zoomFactorFarPlane(1024)
|
||||
, _positionCenter({0, 0})
|
||||
, _originalPosition({0, 0})
|
||||
, _isCameraInitialized(false)
|
||||
{
|
||||
// minggo comment
|
||||
|
@ -206,7 +206,7 @@ bool Camera::initDefault()
|
|||
case Director::Projection::_2D:
|
||||
{
|
||||
initOrthographic(size.width, size.height, -1024, 1024);
|
||||
setPosition3D(Vec3(0.f, 0.f, 0.f));
|
||||
setPosition3D(Vec3(size.width / 2, size.height / 2, 0.f));
|
||||
setRotation3D(Vec3(0.f, 0.f, 0.f));
|
||||
break;
|
||||
}
|
||||
|
@ -226,11 +226,9 @@ bool Camera::initDefault()
|
|||
break;
|
||||
}
|
||||
if (_zoomFactor != 1.0F)
|
||||
{
|
||||
applyZoom();
|
||||
if (_projectionType == Director::Projection::_2D)
|
||||
setPositionCenter(_positionCenter);
|
||||
}
|
||||
setPosition(getPosition());
|
||||
setRotation3D(getRotation3D());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -293,7 +291,7 @@ Vec2 Camera::projectGL(const Vec3& src) const
|
|||
Vec4 clipPos;
|
||||
getViewProjectionMatrix().transformVector(Vec4(src.x, src.y, src.z, 1.0f), &clipPos);
|
||||
|
||||
CCASSERT(clipPos.w != 0.0f, "clipPos.w can't be 0.0f!");
|
||||
//CCASSERT(clipPos.w != 0.0f, "clipPos.w can't be 0.0f!");
|
||||
float ndcX = clipPos.x / clipPos.w;
|
||||
float ndcY = clipPos.y / clipPos.w;
|
||||
|
||||
|
@ -410,20 +408,7 @@ void Camera::setZoom(float factor)
|
|||
_zoomFactor = factor;
|
||||
applyZoom();
|
||||
if (_projectionType == Director::Projection::_2D)
|
||||
setPositionCenter(_positionCenter);
|
||||
}
|
||||
|
||||
void Camera::setPositionCenter(const Vec2& position)
|
||||
{
|
||||
setPositionCenter(position.x, position.y);
|
||||
}
|
||||
|
||||
void Camera::setPositionCenter(float x, float y)
|
||||
{
|
||||
Vec2 pos = {x, y};
|
||||
_positionCenter = pos;
|
||||
auto v = Director::getInstance()->getVisibleSize();
|
||||
setPosition(pos.x + v.width / 2 * (1.0F - _zoomFactor), pos.y + v.height / 2 * (1.0F - _zoomFactor));
|
||||
setPosition(getPosition());
|
||||
}
|
||||
|
||||
void Camera::applyZoom()
|
||||
|
@ -477,11 +462,9 @@ void Camera::applyCustomProperties()
|
|||
}
|
||||
}
|
||||
if (_zoomFactor != 1.0F)
|
||||
{
|
||||
applyZoom();
|
||||
if (_projectionType == Director::Projection::_2D)
|
||||
setPositionCenter(_positionCenter);
|
||||
}
|
||||
setPosition(getPosition());
|
||||
setRotation3D(getRotation3D());
|
||||
}
|
||||
|
||||
void Camera::onEnter()
|
||||
|
@ -504,6 +487,90 @@ void Camera::onExit()
|
|||
Node::onExit();
|
||||
}
|
||||
|
||||
inline const Vec2& getPositionCenter(const Vec2& pos,
|
||||
Director::Projection projectionType,
|
||||
float zoomFactor,
|
||||
float angleOfRotation)
|
||||
{
|
||||
auto director = Director::getInstance();
|
||||
if (projectionType == Director::Projection::_2D)
|
||||
{
|
||||
auto v = director->getVisibleSize();
|
||||
auto rpos = Vec2(pos.x, pos.y);
|
||||
rpos -= v / 2;
|
||||
rpos = Vec2(rpos.x + v.width / 2 * (1.0F - zoomFactor), rpos.y + v.height / 2 * (1.0F - zoomFactor));
|
||||
return rpos.rotateByAngle(rpos + v / 2 * zoomFactor, -CC_DEGREES_TO_RADIANS(angleOfRotation));
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
const Vec2& Camera::getPosition() const
|
||||
{
|
||||
return _originalPosition;
|
||||
}
|
||||
|
||||
void Camera::setPosition(const Vec2& position)
|
||||
{
|
||||
_originalPosition = getPositionCenter(position, _projectionType, getZoom(), getRotation());
|
||||
Node::setPosition(_originalPosition.x, _originalPosition.y);
|
||||
_originalPosition = position;
|
||||
}
|
||||
|
||||
void Camera::getPosition(float* x, float* y) const
|
||||
{
|
||||
*x = _originalPosition.x;
|
||||
*y = _originalPosition.y;
|
||||
}
|
||||
|
||||
void Camera::setPosition(float x, float y)
|
||||
{
|
||||
_originalPosition = getPositionCenter({x, y}, _projectionType, getZoom(), getRotation());
|
||||
Node::setPosition(_originalPosition.x, _originalPosition.y);
|
||||
_originalPosition = {x, y};
|
||||
}
|
||||
|
||||
void Camera::setPosition3D(const Vec3& position)
|
||||
{
|
||||
_originalPosition = getPositionCenter({position.x, position.y}, _projectionType, getZoom(), getRotation());
|
||||
Node::setPosition3D({_originalPosition.x, _originalPosition.y, position.z});
|
||||
_originalPosition = {position.x, position.y};
|
||||
}
|
||||
|
||||
Vec3 Camera::getPosition3D() const
|
||||
{
|
||||
return {_originalPosition.x, _originalPosition.y, getPositionZ()};
|
||||
}
|
||||
|
||||
float Camera::getPositionX() const
|
||||
{
|
||||
return _originalPosition.x;
|
||||
}
|
||||
|
||||
void Camera::setPositionX(float x)
|
||||
{
|
||||
setPosition(x, _originalPosition.y);
|
||||
}
|
||||
|
||||
float Camera::getPositionY() const
|
||||
{
|
||||
return _originalPosition.y;
|
||||
}
|
||||
|
||||
void Camera::setPositionY(float y)
|
||||
{
|
||||
setPosition(_originalPosition.x, y);
|
||||
}
|
||||
|
||||
float Camera::getPositionZ() const
|
||||
{
|
||||
return Node::getPositionZ();
|
||||
}
|
||||
|
||||
void Camera::setPositionZ(float positionZ)
|
||||
{
|
||||
Node::setPositionZ(positionZ);
|
||||
}
|
||||
|
||||
void Camera::setScene(Scene* scene)
|
||||
{
|
||||
if (_scene != scene)
|
||||
|
|
|
@ -290,35 +290,98 @@ public:
|
|||
*/
|
||||
void setZoom(float factor);
|
||||
|
||||
/**
|
||||
* Gets the position of the camera before any zoom transformations.
|
||||
* Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
*/
|
||||
const Vec2& getPositionCenter() { return _positionCenter; }
|
||||
///**
|
||||
// * Gets the position of the camera before any zoom transformations.
|
||||
// * Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
// */
|
||||
//const Vec2& getPositionCenter() { return _positionCenter; }
|
||||
|
||||
/**
|
||||
* Sets the position of the camera with respect to the zoom factor.
|
||||
* Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
*/
|
||||
void setPositionCenter(const Vec2& position);
|
||||
///**
|
||||
// * Sets the position of the camera with respect to the zoom factor.
|
||||
// * Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
// */
|
||||
//void setPositionCenter(const Vec2& position);
|
||||
|
||||
/**
|
||||
* Sets the position of the camera with respect to the zoom factor.
|
||||
* Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
*/
|
||||
void setPositionCenter(float x, float y);
|
||||
///**
|
||||
// * Sets the position of the camera with respect to the zoom factor.
|
||||
// * Should only be used If you're zooming in and out while 2D projection mode is set in the director.
|
||||
// */
|
||||
//void setPositionCenter(float x, float y);
|
||||
|
||||
/**
|
||||
Apply the zoom factor.
|
||||
*/
|
||||
void applyZoom();
|
||||
|
||||
/**
|
||||
Apply the fov, near far planes and aspect values non-destructively.
|
||||
*/
|
||||
void applyCustomProperties();
|
||||
|
||||
// override
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
|
||||
/**
|
||||
* Override of getPosition() in Node class for custom behaviours
|
||||
*/
|
||||
virtual const Vec2& getPosition() const override;
|
||||
|
||||
/**
|
||||
* Override of getPosition() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPosition(const Vec2& position) override;
|
||||
|
||||
/**
|
||||
* Override of getPosition() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void getPosition(float* x, float* y) const override;
|
||||
|
||||
/**
|
||||
* Override of setPosition() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPosition(float x, float y) override;
|
||||
|
||||
/**
|
||||
* Override of setPosition3D() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPosition3D(const Vec3& position) override;
|
||||
|
||||
/**
|
||||
* Override of getPosition3D() in Node class for custom behaviours
|
||||
*/
|
||||
virtual Vec3 getPosition3D() const override;
|
||||
|
||||
/**
|
||||
* Override of getPositionX() in Node class for custom behaviours
|
||||
*/
|
||||
virtual float getPositionX() const override;
|
||||
|
||||
/**
|
||||
* Override of setPositionX() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPositionX(float x) override;
|
||||
|
||||
/**
|
||||
* Override of getPositionY() in Node class for custom behaviours
|
||||
*/
|
||||
virtual float getPositionY() const override;
|
||||
|
||||
/**
|
||||
* Override of setPositionY() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPositionY(float y) override;
|
||||
|
||||
/**
|
||||
* Override of getPositionZ() in Node class for custom behaviours
|
||||
*/
|
||||
virtual float getPositionZ() const override;
|
||||
|
||||
/**
|
||||
* Override of setPositionZ() in Node class for custom behaviours
|
||||
*/
|
||||
virtual void setPositionZ(float positionZ) override;
|
||||
|
||||
/**
|
||||
Before rendering the scene with this camera, the background needs to be cleared.
|
||||
It will clear the depth buffer with max depth by default.
|
||||
|
@ -399,8 +462,9 @@ protected:
|
|||
float _zoomFactor; // The zoom factor of the camera. 3D = (cameraZDistance * _zoomFactor), 2D = (cameraScale * _zoomFactor)
|
||||
float _zoomFactorFarPlane;
|
||||
float _zoomFactorNearPlane;
|
||||
Vec2 _positionCenter;
|
||||
bool _isCameraInitialized;
|
||||
Vec2 _originalPosition;
|
||||
|
||||
bool _isCameraInitialized;
|
||||
|
||||
CameraBackgroundBrush* _clearBrush = nullptr; // brush used to clear the back ground
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue