2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2020-10-17 16:32:16 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2021-10-11 12:15:41 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-10-11 12:15:41 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCArmature.h"
|
|
|
|
#include "CCArmatureDataManager.h"
|
|
|
|
#include "CCArmatureDefine.h"
|
|
|
|
#include "CCDataReaderHelper.h"
|
|
|
|
#include "CCDatas.h"
|
|
|
|
#include "CCSkin.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCGroupCommand.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "box2d/box2d.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "chipmunk/chipmunk.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
Armature* Armature::create()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Armature* armature = new Armature();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (armature->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
armature->autorelease();
|
|
|
|
return armature;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(armature);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Armature* Armature::create(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Armature* armature = new Armature();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (armature->init(name))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
armature->autorelease();
|
|
|
|
return armature;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(armature);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Armature* Armature::create(std::string_view name, Bone* parentBone)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Armature* armature = new Armature();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (armature->init(name, parentBone))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
armature->autorelease();
|
|
|
|
return armature;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(armature);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Armature::Armature()
|
|
|
|
: _armatureData(nullptr)
|
|
|
|
, _batchNode(nullptr)
|
|
|
|
, _parentBone(nullptr)
|
|
|
|
, _armatureTransformDirty(true)
|
|
|
|
, _animation(nullptr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-11-25 01:35:26 +08:00
|
|
|
Armature::~Armature()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_boneDic.clear();
|
|
|
|
_topBoneList.clear();
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(_animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Armature::init()
|
|
|
|
{
|
|
|
|
return init("");
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Armature::init(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
removeAllChildren();
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(_animation);
|
2021-12-08 00:11:53 +08:00
|
|
|
_animation = new ArmatureAnimation();
|
2019-11-23 20:27:39 +08:00
|
|
|
_animation->init(this);
|
|
|
|
|
|
|
|
_boneDic.clear();
|
|
|
|
_topBoneList.clear();
|
|
|
|
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
|
|
|
_name = name;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ArmatureDataManager* armatureDataManager = ArmatureDataManager::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_name.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationData* animationData = armatureDataManager->getAnimationData(name);
|
2019-11-23 20:27:39 +08:00
|
|
|
CCASSERT(animationData, "AnimationData not exist! ");
|
|
|
|
|
|
|
|
_animation->setAnimationData(animationData);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ArmatureData* armatureData = armatureDataManager->getArmatureData(name);
|
2019-11-23 20:27:39 +08:00
|
|
|
CCASSERT(armatureData, "armatureData doesn't exists!");
|
|
|
|
|
|
|
|
_armatureData = armatureData;
|
|
|
|
|
|
|
|
for (auto& element : armatureData->boneDataDic)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* bone = createBone(element.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
//! init bone's Tween to 1st movement's 1st frame
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
MovementData* movData = animationData->getMovement(animationData->movementNames.at(0));
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!movData);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MovementBoneData* movBoneData = movData->getMovementBoneData(bone->getName());
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!movBoneData || movBoneData->frameList.size() <= 0);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FrameData* frameData = movBoneData->getFrameData(0);
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!frameData);
|
|
|
|
|
|
|
|
bone->getTweenData()->copy(frameData);
|
|
|
|
bone->changeDisplayWithIndex(frameData->displayIndex, false);
|
2021-12-25 10:04:45 +08:00
|
|
|
} while (0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
update(0);
|
|
|
|
updateOffsetPoint();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_name = "new_armature";
|
|
|
|
_armatureData = ArmatureData::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
_armatureData->name = _name;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationData* animationData = AnimationData::create();
|
|
|
|
animationData->name = _name;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
armatureDataManager->addArmatureData(_name, _armatureData);
|
|
|
|
armatureDataManager->addAnimationData(_name, animationData);
|
|
|
|
|
|
|
|
_animation->setAnimationData(animationData);
|
|
|
|
}
|
|
|
|
|
|
|
|
setCascadeOpacityEnabled(true);
|
|
|
|
setCascadeColorEnabled(true);
|
|
|
|
|
|
|
|
bRet = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
} while (0);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Armature::init(std::string_view name, Bone* parentBone)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_parentBone = parentBone;
|
|
|
|
return init(name);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Bone* Armature::createBone(std::string_view boneName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* existedBone = getBone(boneName);
|
|
|
|
if (existedBone != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
return existedBone;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
BoneData* boneData = (BoneData*)_armatureData->getBoneData(boneName);
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string parentName = boneData->parentName;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* bone = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!parentName.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
createBone(parentName);
|
|
|
|
bone = Bone::create(boneName);
|
|
|
|
addBone(bone, parentName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bone = Bone::create(boneName);
|
|
|
|
addBone(bone, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
bone->setBoneData(boneData);
|
|
|
|
bone->getDisplayManager()->changeDisplayWithIndex(-1, false);
|
|
|
|
|
|
|
|
return bone;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Armature::addBone(Bone* bone, std::string_view parentName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCASSERT(bone != nullptr, "Argument must be non-nil");
|
2019-11-23 20:27:39 +08:00
|
|
|
CCASSERT(_boneDic.at(bone->getName()) == nullptr, "bone already added. It can't be added again");
|
|
|
|
|
|
|
|
if (!parentName.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* boneParent = _boneDic.at(parentName);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (boneParent)
|
|
|
|
{
|
|
|
|
boneParent->addChildBone(bone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_topBoneList.pushBack(bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_topBoneList.pushBack(bone);
|
|
|
|
}
|
|
|
|
|
|
|
|
bone->setArmature(this);
|
|
|
|
|
|
|
|
_boneDic.insert(bone->getName(), bone);
|
|
|
|
addChild(bone);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::removeBone(Bone* bone, bool recursion)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(bone != nullptr, "bone must be added to the bone dictionary!");
|
|
|
|
|
|
|
|
bone->setArmature(nullptr);
|
|
|
|
bone->removeFromParent(recursion);
|
|
|
|
|
|
|
|
if (_topBoneList.contains(bone))
|
|
|
|
{
|
|
|
|
_topBoneList.eraseObject(bone);
|
|
|
|
}
|
|
|
|
_boneDic.erase(bone->getName());
|
|
|
|
removeChild(bone, true);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Bone* Armature::getBone(std::string_view name) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _boneDic.at(name);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Armature::changeBoneParent(Bone* bone, std::string_view parentName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCASSERT(bone != nullptr, "bone must be added to the bone dictionary!");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone->getParentBone())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bone->getParentBone()->getChildren().eraseObject(bone);
|
|
|
|
bone->setParentBone(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentName.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* boneParent = _boneDic.at(parentName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (boneParent)
|
|
|
|
{
|
|
|
|
boneParent->addChildBone(bone);
|
|
|
|
if (_topBoneList.contains(bone))
|
|
|
|
{
|
|
|
|
_topBoneList.eraseObject(bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_topBoneList.pushBack(bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cocos2d::Map<std::string, Bone*>& Armature::getBoneDic() const
|
|
|
|
{
|
|
|
|
return _boneDic;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Mat4& Armature::getNodeToParentTransform() const
|
|
|
|
{
|
|
|
|
if (_transformDirty)
|
|
|
|
_armatureTransformDirty = true;
|
|
|
|
|
|
|
|
return Node::getNodeToParentTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Armature::updateOffsetPoint()
|
|
|
|
{
|
|
|
|
// Set contentsize and Calculate anchor point.
|
|
|
|
Rect rect = getBoundingBox();
|
|
|
|
setContentSize(rect.size);
|
|
|
|
_offsetPoint.set(-rect.origin.x, -rect.origin.y);
|
|
|
|
if (rect.size.width != 0 && rect.size.height != 0)
|
|
|
|
{
|
|
|
|
setAnchorPoint(Vec2(_offsetPoint.x / rect.size.width, _offsetPoint.y / rect.size.height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Armature::setAnchorPoint(const Vec2& point)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!point.equals(_anchorPoint))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_anchorPoint = point;
|
2021-12-25 10:04:45 +08:00
|
|
|
_anchorPointInPoints.set(_contentSize.width * _anchorPoint.x - _offsetPoint.x,
|
|
|
|
_contentSize.height * _anchorPoint.y - _offsetPoint.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
_realAnchorPointInPoints.set(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y);
|
|
|
|
_transformDirty = _inverseDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& Armature::getAnchorPointInPoints() const
|
|
|
|
{
|
|
|
|
return _realAnchorPointInPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vec2& Armature::getOffsetPoints() const
|
|
|
|
{
|
|
|
|
return _offsetPoint;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::setAnimation(ArmatureAnimation* animation)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_animation = animation;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ArmatureAnimation* Armature::getAnimation() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _animation;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Armature::getArmatureTransformDirty() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _armatureTransformDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Armature::update(float dt)
|
|
|
|
{
|
|
|
|
_animation->update(dt);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& bone : _topBoneList)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
bone->update(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
_armatureTransformDirty = false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::draw(cocos2d::Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_parentBone == nullptr && _batchNode == nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// CC_NODE_DRAW_SETUP();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& object : _children)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Bone* bone = dynamic_cast<Bone*>(object))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* node = bone->getDisplayRenderNode();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (nullptr == node)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (bone->getDisplayRenderNodeType())
|
|
|
|
{
|
|
|
|
case CS_DISPLAY_SPRITE:
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Skin* skin = static_cast<Skin*>(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
skin->updateTransform();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
BlendFunc func = bone->getBlendFunc();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (func.src != BlendFunc::ALPHA_PREMULTIPLIED.src || func.dst != BlendFunc::ALPHA_PREMULTIPLIED.dst)
|
|
|
|
{
|
|
|
|
skin->setBlendFunc(bone->getBlendFunc());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_blendFunc == BlendFunc::ALPHA_PREMULTIPLIED && !skin->getTexture()->hasPremultipliedAlpha())
|
|
|
|
{
|
|
|
|
skin->setBlendFunc(_blendFunc.ALPHA_NON_PREMULTIPLIED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skin->setBlendFunc(_blendFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skin->draw(renderer, transform, flags);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_ARMATURE:
|
|
|
|
{
|
|
|
|
node->draw(renderer, transform, flags);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
node->visit(renderer, transform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
// CC_NODE_DRAW_SETUP();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (Node* node = dynamic_cast<Node*>(object))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
node->visit(renderer, transform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
// CC_NODE_DRAW_SETUP();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Armature::onEnter()
|
|
|
|
{
|
|
|
|
Node::onEnter();
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Armature::onExit()
|
|
|
|
{
|
|
|
|
Node::onExit();
|
|
|
|
unscheduleUpdate();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::visit(cocos2d::Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
|
|
|
|
|
|
|
if (isVisitableByVisitingCamera())
|
|
|
|
{
|
|
|
|
// IMPORTANT:
|
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
|
|
|
// but it is deprecated and your code should not rely on it
|
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sortAllChildren();
|
|
|
|
draw(renderer, _modelViewTransform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// FIX ME: Why need to set _orderOfArrival to 0??
|
|
|
|
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
|
|
|
|
// setOrderOfArrival(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect Armature::getBoundingBox() const
|
|
|
|
{
|
|
|
|
float minx, miny, maxx, maxy = 0;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
Rect boundingBox = Rect(0, 0, 0, 0);
|
|
|
|
|
|
|
|
for (const auto& object : _children)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Bone* bone = dynamic_cast<Bone*>(object))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Rect r = bone->getDisplayManager()->getBoundingBox();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (r.equals(Rect::ZERO))
|
2019-11-23 20:27:39 +08:00
|
|
|
continue;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (first)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
minx = r.getMinX();
|
|
|
|
miny = r.getMinY();
|
|
|
|
maxx = r.getMaxX();
|
|
|
|
maxy = r.getMaxY();
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
minx = r.getMinX() < boundingBox.getMinX() ? r.getMinX() : boundingBox.getMinX();
|
|
|
|
miny = r.getMinY() < boundingBox.getMinY() ? r.getMinY() : boundingBox.getMinY();
|
|
|
|
maxx = r.getMaxX() > boundingBox.getMaxX() ? r.getMaxX() : boundingBox.getMaxX();
|
|
|
|
maxy = r.getMaxY() > boundingBox.getMaxY() ? r.getMaxY() : boundingBox.getMaxY();
|
|
|
|
}
|
|
|
|
|
|
|
|
boundingBox.setRect(minx, miny, maxx - minx, maxy - miny);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RectApplyTransform(boundingBox, getNodeToParentTransform());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* Armature::getBoneAtPoint(float x, float y) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
long length = _children.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (long i = length - 1; i >= 0; i--)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-11 12:15:41 +08:00
|
|
|
Bone* bs = static_cast<Bone*>(_children.at(i));
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bs->getDisplayManager()->containPoint(x, y))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::setParentBone(Bone* parentBone)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_parentBone = parentBone;
|
|
|
|
|
|
|
|
for (auto& element : _boneDic)
|
|
|
|
{
|
|
|
|
element.second->setArmature(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* Armature::getParentBone() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _parentBone;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::setColliderFilter(ColliderFilter* filter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (auto& element : _boneDic)
|
|
|
|
{
|
|
|
|
element.second->setColliderFilter(filter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
|
|
|
|
|
|
|
void Armature::drawContour()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& element : _boneDic)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Bone* bone = element.second;
|
|
|
|
ColliderDetector* detector = bone->getColliderDetector();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (!detector)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList();
|
|
|
|
|
|
|
|
for (auto& object : bodyList)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderBody* body = static_cast<ColliderBody*>(object);
|
|
|
|
const std::vector<Vec2>& vertexList = body->getCalculatedVertexList();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
unsigned long length = vertexList.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2* points = new Vec2[length];
|
|
|
|
for (unsigned long i = 0; i < length; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 p = vertexList.at(i);
|
2019-11-23 20:27:39 +08:00
|
|
|
points[i].x = p.x;
|
|
|
|
points[i].y = p.y;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
# if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
# elif _MSC_VER >= 1400 // vs 2005 or higher
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable : 4996)
|
|
|
|
# endif
|
2019-11-25 01:35:26 +08:00
|
|
|
cocos2d::log("TODO in %s %s %d", __FILE__, __FUNCTION__, __LINE__);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
# pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
# elif _MSC_VER >= 1400 // vs 2005 or higher
|
|
|
|
# pragma warning(pop)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
delete[] points;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Body* Armature::getBody() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _body;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::setBody(b2Body* body)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_body == body)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_body = body;
|
|
|
|
_body->SetUserData(this);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& object : _children)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Bone* bone = dynamic_cast<Bone*>(object))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto displayList = bone->getDisplayManager()->getDecorativeDisplayList();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto displayObject : displayList)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ColliderDetector* detector = static_cast<DecorativeDisplay*>(displayObject)->getColliderDetector();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (detector != nullptr)
|
|
|
|
{
|
|
|
|
detector->setBody(_body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
b2Fixture* Armature::getShapeList()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
return _body->GetFixtureList();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2021-12-25 10:04:45 +08:00
|
|
|
cpBody* Armature::getBody() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _body;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Armature::setBody(cpBody* body)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_body == body)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_body = body;
|
2019-11-23 20:27:39 +08:00
|
|
|
_body->data = this;
|
|
|
|
|
|
|
|
for (const auto& object : _children)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (Bone* bone = dynamic_cast<Bone*>(object))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto displayList = bone->getDisplayManager()->getDecorativeDisplayList();
|
|
|
|
|
|
|
|
for (const auto& displayObject : displayList)
|
|
|
|
{
|
|
|
|
auto detector = displayObject->getColliderDetector();
|
|
|
|
if (detector != nullptr)
|
|
|
|
{
|
|
|
|
detector->setBody(body);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cpShape* Armature::getShapeList()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
return _body->shapeList_private;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|