axmol/core/2d/ParallaxNode.cpp

165 lines
5.0 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "2d/ParallaxNode.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
class PointObject : public Ref
{
public:
2021-12-25 10:04:45 +08:00
static PointObject* create(Vec2 ratio, Vec2 offset)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PointObject* ret = new PointObject();
2019-11-23 20:27:39 +08:00
ret->initWithPoint(ratio, offset);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool initWithPoint(Vec2 ratio, Vec2 offset)
{
2021-12-25 10:04:45 +08:00
_ratio = ratio;
2019-11-23 20:27:39 +08:00
_offset = offset;
2021-12-25 10:04:45 +08:00
_child = nullptr;
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const Vec2& getRatio() const { return _ratio; }
void setRatio(const Vec2& ratio) { _ratio = ratio; }
const Vec2& getOffset() const { return _offset; }
void setOffset(const Vec2& offset) { _offset = offset; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Node* getChild() const { return _child; }
void setChild(Node* child) { _child = child; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
Vec2 _ratio;
Vec2 _offset;
2021-12-25 10:04:45 +08:00
Node* _child; // weak ref
2019-11-23 20:27:39 +08:00
};
ParallaxNode::ParallaxNode()
{
_parallaxArray.reserve(5);
2019-11-23 20:27:39 +08:00
_lastPosition.set(-100.0f, -100.0f);
}
ParallaxNode::~ParallaxNode()
{
}
2021-12-25 10:04:45 +08:00
ParallaxNode* ParallaxNode::create()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ParallaxNode* ret = new ParallaxNode();
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, int /*tag*/)
{
2022-07-16 10:43:05 +08:00
AXASSERT(0, "ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
2019-11-23 20:27:39 +08:00
}
void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, std::string_view /*name*/)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(0, "ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void ParallaxNode::addChild(Node* child, int z, const Vec2& ratio, const Vec2& offset)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(child != nullptr, "Argument must be non-nil");
2021-12-25 10:04:45 +08:00
PointObject* obj = PointObject::create(ratio, offset);
2019-11-23 20:27:39 +08:00
obj->setChild(child);
_parallaxArray.pushBack(obj);
2019-11-23 20:27:39 +08:00
Vec2 pos = this->absolutePosition();
2021-12-25 10:04:45 +08:00
pos.x = -pos.x + pos.x * ratio.x + offset.x;
pos.y = -pos.y + pos.y * ratio.y + offset.y;
2019-11-23 20:27:39 +08:00
child->setPosition(pos);
Node::addChild(child, z, child->getName());
}
void ParallaxNode::removeChild(Node* child, bool cleanup)
{
for (auto iter = _parallaxArray.begin(); iter != _parallaxArray.end(); ++iter)
2019-11-23 20:27:39 +08:00
{
PointObject* point = (PointObject*)*iter;
2019-11-23 20:27:39 +08:00
if (point->getChild() == child)
{
_parallaxArray.erase(iter);
2019-11-23 20:27:39 +08:00
break;
}
}
Node::removeChild(child, cleanup);
}
void ParallaxNode::removeAllChildrenWithCleanup(bool cleanup)
{
_parallaxArray.clear();
2019-11-23 20:27:39 +08:00
Node::removeAllChildrenWithCleanup(cleanup);
}
Vec2 ParallaxNode::absolutePosition()
{
Vec2 ret = _position;
2021-12-25 10:04:45 +08:00
Node* cn = this;
2019-11-23 20:27:39 +08:00
while (cn->getParent() != nullptr)
{
2021-12-25 10:04:45 +08:00
cn = cn->getParent();
2019-11-23 20:27:39 +08:00
ret = ret + cn->getPosition();
}
return ret;
}
/*
The positions are updated at visit because:
- using a timer is not guaranteed that it will called after all the positions were updated
- overriding "draw" will only precise if the children have a z > 0
*/
2021-12-25 10:04:45 +08:00
void ParallaxNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
// Vec2 pos = position_;
// Vec2 pos = [self convertToWorldSpace:Vec2::ZERO];
Vec2 pos = this->absolutePosition();
2021-12-25 10:04:45 +08:00
if (!pos.equals(_lastPosition))
2019-11-23 20:27:39 +08:00
{
for (auto item : _parallaxArray)
2019-11-23 20:27:39 +08:00
{
auto point = static_cast<PointObject*>(item);
2021-12-25 10:04:45 +08:00
float x = -pos.x + pos.x * point->getRatio().x + point->getOffset().x;
float y = -pos.y + pos.y * point->getRatio().y + point->getOffset().y;
point->getChild()->setPosition(x, y);
2019-11-23 20:27:39 +08:00
}
_lastPosition = pos;
}
Node::visit(renderer, parentTransform, parentFlags);
}
NS_AX_END