axmol/extensions/cocostudio/CCBone.cpp

474 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2021-07-16 11:04:38 +08:00
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2021-07-16 11:04:38 +08:00
https://adxe.org
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 "CCBone.h"
#include "CCArmature.h"
#include "CCArmatureDataManager.h"
#include "CCTransformHelp.h"
#include "CCDisplayManager.h"
2019-11-23 20:27:39 +08:00
using namespace cocos2d;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Bone* Bone::create()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Bone* pBone = new Bone();
2021-12-08 00:11:53 +08:00
if (pBone->init())
2019-11-23 20:27:39 +08:00
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return nullptr;
}
2021-12-25 10:04:45 +08:00
Bone* Bone::create(const std::string& name)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Bone* pBone = new Bone();
2021-12-08 00:11:53 +08:00
if (pBone->init(name))
2019-11-23 20:27:39 +08:00
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return nullptr;
}
Bone::Bone()
2021-07-16 11:04:38 +08:00
: _tweenData(nullptr)
, _parentBone(nullptr)
, _armature(nullptr)
, _childArmature(nullptr)
, _boneData(nullptr)
, _tween(nullptr)
, _displayManager(nullptr)
, _ignoreMovementBoneData(false)
, _worldTransform(Mat4::IDENTITY)
, _boneTransformDirty(true)
, _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED)
, _blendDirty(false)
, _worldInfo(nullptr)
, _armatureParentBone(nullptr)
, _dataVersion(0)
{}
2019-11-23 20:27:39 +08:00
2020-10-17 16:32:16 +08:00
Bone::~Bone(void)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_DELETE(_tweenData);
CC_SAFE_DELETE(_tween);
CC_SAFE_DELETE(_displayManager);
CC_SAFE_DELETE(_worldInfo);
CC_SAFE_RELEASE_NULL(_boneData);
CC_SAFE_RELEASE(_childArmature);
}
bool Bone::init()
{
2020-10-17 16:32:16 +08:00
return Bone::init(nullptr);
2019-11-23 20:27:39 +08:00
}
bool Bone::init(const std::string& name)
{
bool bRet = false;
do
{
_name = name;
CC_SAFE_DELETE(_tweenData);
2021-12-08 00:11:53 +08:00
_tweenData = new FrameData();
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(_tween);
2021-12-08 00:11:53 +08:00
_tween = new Tween();
2019-11-23 20:27:39 +08:00
_tween->init(this);
CC_SAFE_DELETE(_displayManager);
2021-12-08 00:11:53 +08:00
_displayManager = new DisplayManager();
2019-11-23 20:27:39 +08:00
_displayManager->init(this);
CC_SAFE_DELETE(_worldInfo);
2021-12-08 00:11:53 +08:00
_worldInfo = new BaseData();
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(_boneData);
2021-12-25 10:04:45 +08:00
_boneData = new BoneData();
2019-11-23 20:27:39 +08:00
bRet = true;
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return bRet;
}
2021-12-25 10:04:45 +08:00
void Bone::setBoneData(BoneData* boneData)
2019-11-23 20:27:39 +08:00
{
CCASSERT(nullptr != boneData, "_boneData must not be nullptr");
if (_boneData != boneData)
{
CC_SAFE_RETAIN(boneData);
CC_SAFE_RELEASE(_boneData);
_boneData = boneData;
}
_name = _boneData->name;
_setLocalZOrder(_boneData->zOrder);
_displayManager->initDisplayList(boneData);
}
2021-12-25 10:04:45 +08:00
BoneData* Bone::getBoneData() const
2019-11-23 20:27:39 +08:00
{
return _boneData;
}
2021-12-25 10:04:45 +08:00
void Bone::setArmature(Armature* armature)
2019-11-23 20:27:39 +08:00
{
_armature = armature;
if (_armature)
{
_tween->setAnimation(_armature->getAnimation());
2021-12-25 10:04:45 +08:00
_dataVersion = _armature->getArmatureData()->dataVersion;
2019-11-23 20:27:39 +08:00
_armatureParentBone = _armature->getParentBone();
}
else
{
_armatureParentBone = nullptr;
}
}
2021-12-25 10:04:45 +08:00
Armature* Bone::getArmature() const
2019-11-23 20:27:39 +08:00
{
return _armature;
}
void Bone::update(float delta)
{
if (_parentBone)
_boneTransformDirty = _boneTransformDirty || _parentBone->isTransformDirty();
if (_armatureParentBone && !_boneTransformDirty)
{
_boneTransformDirty = _armatureParentBone->isTransformDirty();
}
if (_boneTransformDirty)
{
_worldInfo->copy(_tweenData);
if (_dataVersion >= VERSION_COMBINED)
{
TransformHelp::nodeConcat(*_worldInfo, *_boneData);
_worldInfo->scaleX -= 1;
_worldInfo->scaleY -= 1;
}
2021-12-25 10:04:45 +08:00
_worldInfo->x = _worldInfo->x + _position.x;
_worldInfo->y = _worldInfo->y + _position.y;
2019-11-23 20:27:39 +08:00
_worldInfo->scaleX = _worldInfo->scaleX * _scaleX;
_worldInfo->scaleY = _worldInfo->scaleY * _scaleY;
2021-12-25 10:04:45 +08:00
_worldInfo->skewX = _worldInfo->skewX + _skewX + CC_DEGREES_TO_RADIANS(_rotationZ_X);
_worldInfo->skewY = _worldInfo->skewY + _skewY - CC_DEGREES_TO_RADIANS(_rotationZ_Y);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (_parentBone)
2019-11-23 20:27:39 +08:00
{
applyParentTransform(_parentBone);
}
else
{
if (_armatureParentBone)
{
applyParentTransform(_armatureParentBone);
}
}
TransformHelp::nodeToMatrix(*_worldInfo, _worldTransform);
if (_armatureParentBone)
{
_worldTransform = TransformConcat(_worldTransform, _armature->getNodeToParentTransform());
}
}
DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty());
2021-12-25 10:04:45 +08:00
for (const auto& obj : _children)
{
Bone* childBone = static_cast<Bone*>(obj);
2019-11-23 20:27:39 +08:00
childBone->update(delta);
}
_boneTransformDirty = false;
}
2021-12-25 10:04:45 +08:00
void Bone::applyParentTransform(Bone* parent)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float x = _worldInfo->x;
float y = _worldInfo->y;
_worldInfo->x = x * parent->_worldTransform.m[0] + y * parent->_worldTransform.m[4] + parent->_worldInfo->x;
_worldInfo->y = x * parent->_worldTransform.m[1] + y * parent->_worldTransform.m[5] + parent->_worldInfo->y;
2019-11-23 20:27:39 +08:00
_worldInfo->scaleX = _worldInfo->scaleX * parent->_worldInfo->scaleX;
_worldInfo->scaleY = _worldInfo->scaleY * parent->_worldInfo->scaleY;
2021-12-25 10:04:45 +08:00
_worldInfo->skewX = _worldInfo->skewX + parent->_worldInfo->skewX;
_worldInfo->skewY = _worldInfo->skewY + parent->_worldInfo->skewY;
2019-11-23 20:27:39 +08:00
}
void Bone::setBlendFunc(const BlendFunc& blendFunc)
{
if (_blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst)
{
2021-12-25 10:04:45 +08:00
_blendFunc = blendFunc;
2019-11-23 20:27:39 +08:00
_blendDirty = true;
}
}
2021-12-25 10:04:45 +08:00
void Bone::updateDisplayedColor(const Color3B& parentColor)
2019-11-23 20:27:39 +08:00
{
#ifdef CC_STUDIO_ENABLED_VIEW
_realColor = Color3B(255, 255, 255);
2021-12-25 10:04:45 +08:00
#endif // CC_STUDIO_ENABLED_VIEW
2019-11-23 20:27:39 +08:00
Node::updateDisplayedColor(parentColor);
}
2019-11-25 01:35:26 +08:00
void Bone::updateDisplayedOpacity(uint8_t parentOpacity)
2019-11-23 20:27:39 +08:00
{
#ifdef CC_STUDIO_ENABLED_VIEW
_realOpacity = 255;
2021-12-25 10:04:45 +08:00
#endif // CC_STUDIO_ENABLED_VIEW
2019-11-23 20:27:39 +08:00
Node::updateDisplayedOpacity(parentOpacity);
}
void Bone::updateColor()
{
2021-12-25 10:04:45 +08:00
Node* display = _displayManager->getDisplayRenderNode();
if (display != nullptr)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
display->setColor(Color3B(_displayedColor.r * _tweenData->r / 255, _displayedColor.g * _tweenData->g / 255,
_displayedColor.b * _tweenData->b / 255));
2019-11-23 20:27:39 +08:00
display->setOpacity(_displayedOpacity * _tweenData->a / 255);
}
}
void Bone::updateZOrder()
{
if (_dataVersion >= VERSION_COMBINED)
{
int zorder = _tweenData->zOrder + _boneData->zOrder;
setLocalZOrder(zorder);
}
else
{
setLocalZOrder(_tweenData->zOrder);
}
}
2021-12-25 10:04:45 +08:00
void Bone::addChildBone(Bone* child)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CCASSERT(nullptr != child, "Argument must be non-nil");
CCASSERT(nullptr == child->_parentBone, "child already added. It can't be added again");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (_children.empty())
2019-11-23 20:27:39 +08:00
{
_children.reserve(4);
}
if (_children.getIndex(child) == CC_INVALID_INDEX)
{
_children.pushBack(child);
child->setParentBone(this);
}
}
2021-12-25 10:04:45 +08:00
void Bone::removeChildBone(Bone* bone, bool recursion)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_children.empty() && _children.getIndex(bone) != CC_INVALID_INDEX)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (recursion)
2019-11-23 20:27:39 +08:00
{
auto ccbones = bone->_children;
2021-12-25 10:04:45 +08:00
for (auto& object : ccbones)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Bone* ccBone = static_cast<Bone*>(object);
2019-11-23 20:27:39 +08:00
bone->removeChildBone(ccBone, recursion);
}
}
bone->setParentBone(nullptr);
bone->getDisplayManager()->setCurrentDecorativeDisplay(nullptr);
_children.eraseObject(bone);
}
}
void Bone::removeFromParent(bool recursion)
{
if (nullptr != _parentBone)
{
_parentBone->removeChildBone(this, recursion);
}
}
2021-12-25 10:04:45 +08:00
void Bone::setParentBone(Bone* parent)
2019-11-23 20:27:39 +08:00
{
_parentBone = parent;
}
2021-12-25 10:04:45 +08:00
Bone* Bone::getParentBone()
2019-11-23 20:27:39 +08:00
{
return _parentBone;
}
2021-12-25 10:04:45 +08:00
void Bone::setChildArmature(Armature* armature)
2019-11-23 20:27:39 +08:00
{
if (_childArmature != armature)
{
if (armature == nullptr && _childArmature)
{
_childArmature->setParentBone(nullptr);
}
CC_SAFE_RETAIN(armature);
CC_SAFE_RELEASE(_childArmature);
_childArmature = armature;
}
}
2021-12-25 10:04:45 +08:00
Armature* Bone::getChildArmature() const
2019-11-23 20:27:39 +08:00
{
return _childArmature;
}
2021-12-25 10:04:45 +08:00
Tween* Bone::getTween()
2019-11-23 20:27:39 +08:00
{
return _tween;
}
void Bone::setLocalZOrder(int zOrder)
{
if (getLocalZOrder() != zOrder)
Node::setLocalZOrder(zOrder);
}
Mat4 Bone::getNodeToArmatureTransform() const
{
return _worldTransform;
}
Mat4 Bone::getNodeToWorldTransform() const
{
return TransformConcat(_worldTransform, _armature->getNodeToWorldTransform());
}
2021-12-25 10:04:45 +08:00
Node* Bone::getDisplayRenderNode()
2019-11-23 20:27:39 +08:00
{
return _displayManager->getDisplayRenderNode();
}
DisplayType Bone::getDisplayRenderNodeType()
{
return _displayManager->getDisplayRenderNodeType();
}
2021-12-25 10:04:45 +08:00
void Bone::addDisplay(DisplayData* displayData, int index)
2019-11-23 20:27:39 +08:00
{
_displayManager->addDisplay(displayData, index);
}
2021-12-25 10:04:45 +08:00
void Bone::addDisplay(Node* display, int index)
2019-11-23 20:27:39 +08:00
{
_displayManager->addDisplay(display, index);
}
void Bone::removeDisplay(int index)
{
_displayManager->removeDisplay(index);
}
void Bone::changeDisplayByIndex(int index, bool force)
{
changeDisplayWithIndex(index, force);
}
2021-12-25 10:04:45 +08:00
void Bone::changeDisplayByName(const std::string& name, bool force)
2019-11-23 20:27:39 +08:00
{
changeDisplayWithName(name, force);
}
void Bone::changeDisplayWithIndex(int index, bool force)
{
_displayManager->changeDisplayWithIndex(index, force);
}
void Bone::changeDisplayWithName(const std::string& name, bool force)
{
_displayManager->changeDisplayWithName(name, force);
}
ColliderDetector* Bone::getColliderDetector() const
{
2021-12-25 10:04:45 +08:00
if (DecorativeDisplay* decoDisplay = _displayManager->getCurrentDecorativeDisplay())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (ColliderDetector* detector = decoDisplay->getColliderDetector())
2019-11-23 20:27:39 +08:00
{
return detector;
}
}
return nullptr;
}
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
2021-12-25 10:04:45 +08:00
void Bone::setColliderFilter(ColliderFilter* filter)
2019-11-23 20:27:39 +08:00
{
auto array = _displayManager->getDecorativeDisplayList();
2021-12-25 10:04:45 +08:00
for (auto& object : array)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DecorativeDisplay* decoDisplay = static_cast<DecorativeDisplay*>(object);
if (ColliderDetector* detector = decoDisplay->getColliderDetector())
2019-11-23 20:27:39 +08:00
{
detector->setColliderFilter(filter);
}
}
}
2021-12-25 10:04:45 +08:00
ColliderFilter* Bone::getColliderFilter()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (DecorativeDisplay* decoDisplay = _displayManager->getCurrentDecorativeDisplay())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (ColliderDetector* detector = decoDisplay->getColliderDetector())
2019-11-23 20:27:39 +08:00
{
return detector->getColliderFilter();
}
}
return nullptr;
}
#endif
2021-12-25 10:04:45 +08:00
} // namespace cocostudio