axmol/extensions/cocostudio/ActionTimeline/CCBoneNode.cpp

741 lines
21 KiB
C++
Raw Normal View History

2015-07-23 11:51:49 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2015-2017 Chukong Technologies Inc.
https://axis-project.github.io/
2015-07-23 11:51:49 +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 "base/CCDirector.h"
2019-11-25 01:35:26 +08:00
#include "base/ccUtils.h"
2015-07-23 11:51:49 +08:00
#include "renderer/CCRenderer.h"
2019-11-25 01:35:26 +08:00
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
2020-10-17 16:32:16 +08:00
#include "ActionTimeline/CCBoneNode.h"
#include "ActionTimeline/CCSkeletonNode.h"
2015-07-23 11:51:49 +08:00
NS_TIMELINE_BEGIN
BoneNode* BoneNode::create()
{
2021-12-08 00:11:53 +08:00
BoneNode* ret = new BoneNode();
if (ret->init())
2015-07-23 11:51:49 +08:00
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-07-23 11:51:49 +08:00
return nullptr;
}
2015-07-24 01:30:34 +08:00
BoneNode* BoneNode::create(int length)
2015-07-23 11:51:49 +08:00
{
2021-12-08 00:11:53 +08:00
BoneNode* ret = new BoneNode();
if (ret->init())
2015-07-23 11:51:49 +08:00
{
ret->setDebugDrawLength(length);
ret->autorelease();
}
else
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-07-23 11:51:49 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
BoneNode::~BoneNode() {}
2020-10-17 16:32:16 +08:00
bool BoneNode::init()
{
_rackLength = 50;
2021-12-25 10:04:45 +08:00
_rackWidth = 20;
2020-10-17 16:32:16 +08:00
updateVertices();
updateColor();
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
2021-12-25 10:04:45 +08:00
auto* program =
axis::backend::Program::getBuiltinProgram(axis::backend::ProgramType::POSITION_COLOR); // TODO: noMVP?
setProgramState(new axis::backend::ProgramState(program), false);
2020-10-17 16:32:16 +08:00
pipelineDescriptor.programState = _programState;
2021-12-27 13:52:08 +08:00
_mvpLocation = _programState->getUniformLocation("u_MVPMatrix"sv);
2020-10-17 16:32:16 +08:00
2021-12-25 10:04:45 +08:00
auto vertexLayout = _programState->getVertexLayout();
2020-10-17 16:32:16 +08:00
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
2021-12-27 13:52:08 +08:00
auto iter = attributeInfo.find("a_position"sv);
2020-10-17 16:32:16 +08:00
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position"sv, iter->second.location, axis::backend::VertexFormat::FLOAT3, 0,
2021-12-25 10:04:45 +08:00
false);
2020-10-17 16:32:16 +08:00
}
2021-12-27 13:52:08 +08:00
iter = attributeInfo.find("a_color"sv);
2020-10-17 16:32:16 +08:00
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color"sv, iter->second.location, axis::backend::VertexFormat::FLOAT4,
2021-12-25 10:04:45 +08:00
3 * sizeof(float), false);
2020-10-17 16:32:16 +08:00
}
vertexLayout->setLayout(7 * sizeof(float));
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), 4, axis::CustomCommand::BufferUsage::DYNAMIC);
_customCommand.createIndexBuffer(axis::CustomCommand::IndexFormat::U_SHORT, 6,
axis::CustomCommand::BufferUsage::STATIC);
2021-12-25 10:04:45 +08:00
unsigned short indices[6] = {0, 1, 2, 0, 2, 3};
2020-10-17 16:32:16 +08:00
_customCommand.updateIndexBuffer(indices, sizeof(indices));
return true;
}
void BoneNode::addChild(axis::Node* child, int localZOrder, int tag)
2015-07-23 11:51:49 +08:00
{
addToChildrenListHelper(child);
Node::addChild(child, localZOrder, tag);
2015-07-23 11:51:49 +08:00
}
2021-12-26 23:26:34 +08:00
void BoneNode::addChild(Node* child, int localZOrder, std::string_view name)
2015-07-23 11:51:49 +08:00
{
addToChildrenListHelper(child);
Node::addChild(child, localZOrder, name);
2015-07-23 11:51:49 +08:00
}
void BoneNode::addSkin(SkinNode* skin, bool isDisplay, bool hideOthers)
{
2022-07-16 10:43:05 +08:00
AXASSERT(skin != nullptr, "Argument must be non-nil");
2015-07-23 11:51:49 +08:00
if (hideOthers)
{
for (auto&& bonskin : _boneSkins)
2015-07-23 11:51:49 +08:00
{
bonskin->setVisible(false);
}
}
Node::addChild(skin);
skin->setVisible(isDisplay);
}
void BoneNode::addSkin(SkinNode* skin, bool display)
{
addSkin(skin, display, false);
}
void BoneNode::removeChild(Node* child, bool cleanup /* = true */)
{
ssize_t index = _children.getIndex(child);
2022-07-15 19:17:01 +08:00
if (index != axis::AX_INVALID_INDEX)
{
removeFromChildrenListHelper(child);
Node::removeChild(child, cleanup);
}
2015-07-23 11:51:49 +08:00
}
void BoneNode::removeFromBoneList(BoneNode* bone)
{
if (_rootSkeleton != nullptr)
2021-12-25 10:04:45 +08:00
{
auto skeletonNode = dynamic_cast<SkeletonNode*>(bone);
if (skeletonNode == nullptr) // is not a nested skeleton
2015-07-23 11:51:49 +08:00
{
auto subBones = bone->getAllSubBones();
subBones.pushBack(bone);
for (auto&& subBone : subBones)
{
if (subBone->_rootSkeleton == nullptr)
continue;
subBone->_rootSkeleton = nullptr;
auto toremoveIter = _rootSkeleton->_subBonesMap.find(subBone->getName());
if (toremoveIter != _rootSkeleton->_subBonesMap.end())
{
_rootSkeleton->_subBonesMap.erase(toremoveIter);
2021-12-25 10:04:45 +08:00
_rootSkeleton->_subBonesDirty = true;
_rootSkeleton->_subBonesOrderDirty = true;
}
}
2015-07-23 11:51:49 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
_rootSkeleton->_subBonesDirty = true;
_rootSkeleton->_subBonesOrderDirty = true;
}
}
_childBones.eraseObject(bone);
2015-07-23 11:51:49 +08:00
}
void BoneNode::addToBoneList(BoneNode* bone)
{
_childBones.pushBack(bone);
if (_rootSkeleton != nullptr)
2015-07-23 11:51:49 +08:00
{
auto skeletonNode = dynamic_cast<SkeletonNode*>(bone);
2021-12-25 10:04:45 +08:00
if (skeletonNode == nullptr && bone->_rootSkeleton == nullptr) // not nest skeleton
2015-07-23 11:51:49 +08:00
{
auto subBones = bone->getAllSubBones();
subBones.pushBack(bone);
for (auto&& subBone : subBones)
2015-07-23 11:51:49 +08:00
{
subBone->_rootSkeleton = _rootSkeleton;
2021-12-25 10:04:45 +08:00
auto bonename = subBone->getName();
if (_rootSkeleton->_subBonesMap.find(bonename) == _rootSkeleton->_subBonesMap.end())
2015-07-23 11:51:49 +08:00
{
_rootSkeleton->_subBonesMap.insert(subBone->getName(), subBone);
2021-12-25 10:04:45 +08:00
_rootSkeleton->_subBonesDirty = true;
_rootSkeleton->_subBonesOrderDirty = true;
2015-07-23 11:51:49 +08:00
}
else
2022-07-16 10:43:05 +08:00
AXLOG("already has a bone named %s in skeleton %s", bonename.data(),
2021-12-27 13:52:08 +08:00
_rootSkeleton->getName().data());
2015-07-23 11:51:49 +08:00
}
}
else
2015-07-23 11:51:49 +08:00
{
2021-12-25 10:04:45 +08:00
_rootSkeleton->_subBonesDirty = true;
_rootSkeleton->_subBonesOrderDirty = true;
2015-07-23 11:51:49 +08:00
}
}
}
void BoneNode::addToSkinList(SkinNode* skin)
{
_boneSkins.pushBack(skin);
auto blendSkin = dynamic_cast<BlendProtocol*>(skin);
if (nullptr != blendSkin && _blendFunc != blendSkin->getBlendFunc())
{
blendSkin->setBlendFunc(_blendFunc);
}
2015-07-23 11:51:49 +08:00
}
void BoneNode::removeFromSkinList(SkinNode* skin)
{
_boneSkins.eraseObject(skin);
}
void BoneNode::displaySkin(SkinNode* skin, bool hideOthers)
{
for (auto&& boneskin : _boneSkins)
2015-07-23 11:51:49 +08:00
{
if (boneskin == skin)
{
boneskin->setVisible(true);
}
else if (hideOthers)
{
boneskin->setVisible(false);
}
}
}
2021-12-26 23:26:34 +08:00
void BoneNode::displaySkin(std::string_view skinName, bool hideOthers)
2015-07-23 11:51:49 +08:00
{
for (auto&& skin : _boneSkins)
2015-07-23 11:51:49 +08:00
{
if (skinName == skin->getName())
{
skin->setVisible(true);
}
else if (hideOthers)
{
skin->setVisible(false);
}
}
}
axis::Vector<SkinNode*> BoneNode::getVisibleSkins() const
2015-07-23 11:51:49 +08:00
{
axis::Vector<SkinNode*> displayingSkins;
2021-12-25 10:04:45 +08:00
for (const auto& boneskin : _boneSkins)
2015-07-23 11:51:49 +08:00
{
if (boneskin->isVisible())
{
displayingSkins.pushBack(boneskin);
}
}
return displayingSkins;
}
axis::Rect BoneNode::getBoundingBox() const
2015-07-23 11:51:49 +08:00
{
axis::Rect boundingBox = getVisibleSkinsRect();
2015-07-23 11:51:49 +08:00
return RectApplyAffineTransform(boundingBox, this->getNodeToParentAffineTransform());
}
axis::Rect BoneNode::getVisibleSkinsRect() const
2015-07-23 11:51:49 +08:00
{
float minx, miny, maxx, maxy = 0;
minx = miny = maxx = maxy;
2021-12-25 10:04:45 +08:00
bool first = true;
2015-07-23 11:51:49 +08:00
axis::Rect displayRect = axis::Rect(0, 0, 0, 0);
2015-07-23 11:51:49 +08:00
if (_isRackShow && _rootSkeleton != nullptr && _rootSkeleton->_isRackShow)
{
2021-12-25 10:04:45 +08:00
maxx = _rackLength;
maxy = _rackWidth;
2015-07-23 11:51:49 +08:00
first = false;
}
for (const auto& skin : _boneSkins)
{
axis::Rect r = skin->getBoundingBox();
if (!skin->isVisible() || r.equals(axis::Rect::ZERO))
2015-07-23 11:51:49 +08:00
continue;
if (first)
{
minx = r.getMinX();
miny = r.getMinY();
maxx = r.getMaxX();
maxy = r.getMaxY();
first = false;
}
else
{
minx = MIN(r.getMinX(), minx);
miny = MIN(r.getMinY(), miny);
maxx = MAX(r.getMaxX(), maxx);
maxy = MAX(r.getMaxY(), maxy);
}
displayRect.setRect(minx, miny, maxx - minx, maxy - miny);
}
return displayRect;
}
void BoneNode::setBlendFunc(const axis::BlendFunc& blendFunc)
2015-07-23 11:51:49 +08:00
{
if (_blendFunc != blendFunc)
{
_blendFunc = blendFunc;
for (auto&& skin : _boneSkins)
{
auto blendSkin = dynamic_cast<BlendProtocol*>(skin);
if (nullptr != blendSkin)
{
blendSkin->setBlendFunc(_blendFunc);
}
}
}
2015-07-23 11:51:49 +08:00
}
void BoneNode::setDebugDrawLength(float length)
{
_rackLength = length;
updateVertices();
}
void BoneNode::setDebugDrawWidth(float width)
{
_rackWidth = width;
updateVertices();
}
void BoneNode::setDebugDrawEnabled(bool isDebugDraw)
{
if (_isRackShow == isDebugDraw)
return;
_isRackShow = isDebugDraw;
}
void BoneNode::setDebugDrawColor(const axis::Color4F& color)
2015-07-23 11:51:49 +08:00
{
_rackColor = color;
updateColor();
}
void BoneNode::visit(axis::Renderer* renderer, const axis::Mat4& parentTransform, uint32_t parentFlags)
{
// quick return if not visible. children won't be drawn.
if (!_visible)
{
return;
}
uint32_t flags = processParentFlags(parentTransform, parentFlags);
// 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->pushMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_director->loadMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
bool visibleByCamera = isVisitableByVisitingCamera();
2021-12-25 10:04:45 +08:00
bool isdebugdraw = visibleByCamera && _isRackShow && nullptr == _rootSkeleton;
int i = 0;
if (!_children.empty())
{
sortAllChildren();
// draw children zOrder < 0
for (; i < _children.size(); i++)
{
auto node = _children.at(i);
2021-12-25 10:04:45 +08:00
if (_rootSkeleton != nullptr && _boneSkins.contains(node)) // skip skin when bone is in a skeleton
continue;
if (node && node->getLocalZOrder() < 0)
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
// self draw
if (isdebugdraw)
this->draw(renderer, _modelViewTransform, flags);
for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
{
auto node = (*it);
2021-12-25 10:04:45 +08:00
if (_rootSkeleton != nullptr && _boneSkins.contains(node)) // skip skin when bone is in a skeleton
continue;
node->visit(renderer, _modelViewTransform, flags);
}
}
else if (isdebugdraw)
{
this->draw(renderer, _modelViewTransform, flags);
}
_director->popMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// reset for next frame
// _orderOfArrival = 0;
}
2015-07-23 11:51:49 +08:00
void BoneNode::draw(axis::Renderer* renderer, const axis::Mat4& transform, uint32_t flags)
2015-07-23 11:51:49 +08:00
{
2019-11-25 01:35:26 +08:00
_customCommand.init(_globalZOrder, _blendFunc);
2015-07-23 11:51:49 +08:00
renderer->addCommand(&_customCommand);
2022-07-15 19:17:01 +08:00
#ifdef AX_STUDIO_ENABLED_VIEW
2021-12-25 10:04:45 +08:00
// TODO
// glVertexAttribPointer(axis::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
// glVertexAttribPointer(axis::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
2019-11-25 01:35:26 +08:00
//
2021-12-25 10:04:45 +08:00
// glEnable(GL_LINE_SMOOTH);
// glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
// glDrawArrays(GL_LINE_LOOP, 0, 4);
2022-07-15 19:17:01 +08:00
// AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
#endif // AX_STUDIO_ENABLED_VIEW
2019-11-25 01:35:26 +08:00
2015-07-23 11:51:49 +08:00
for (int i = 0; i < 4; ++i)
{
axis::Vec4 pos;
2021-12-25 10:04:45 +08:00
pos.x = _squareVertices[i].x;
pos.y = _squareVertices[i].y;
pos.z = _positionZ;
2015-07-23 11:51:49 +08:00
pos.w = 1;
_modelViewTransform.transformVector(&pos);
_vertexData[i].noMVPVertices = axis::Vec3(pos.x, pos.y, pos.z) / pos.w;
2015-07-23 11:51:49 +08:00
}
2019-11-25 01:35:26 +08:00
_customCommand.updateVertexBuffer(_vertexData, sizeof(_vertexData));
2021-12-25 10:04:45 +08:00
_programState->setUniform(_mvpLocation, transform.m, sizeof(transform.m));
2015-07-23 11:51:49 +08:00
}
void BoneNode::updateVertices()
{
2021-12-25 10:04:45 +08:00
if (_rackLength != _squareVertices[2].x - _anchorPointInPoints.x ||
_squareVertices[3].y != _rackWidth / 2 - _anchorPointInPoints.y)
2015-07-23 11:51:49 +08:00
{
_squareVertices[1].x = _squareVertices[1].y = _squareVertices[3].y = .0f;
_squareVertices[0].x = _squareVertices[2].x = _rackLength * .1f;
2021-12-25 10:04:45 +08:00
_squareVertices[2].y = _rackWidth * .5f;
_squareVertices[0].y = -_squareVertices[2].y;
_squareVertices[3].x = _rackLength;
2015-07-23 11:51:49 +08:00
for (int i = 0; i < 4; i++)
{
_squareVertices[i] += _anchorPointInPoints;
}
_transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
}
}
void BoneNode::updateColor()
{
for (unsigned int i = 0; i < 4; i++)
{
2019-11-25 01:35:26 +08:00
_vertexData[i].squareColor = _rackColor;
2015-07-23 11:51:49 +08:00
}
_transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
}
void BoneNode::updateDisplayedColor(const axis::Color3B& /*parentColor*/)
{
if (_cascadeColorEnabled)
{
2021-12-25 10:04:45 +08:00
for (const auto& child : _boneSkins)
{
child->updateDisplayedColor(_displayedColor);
}
}
}
2019-11-25 01:35:26 +08:00
void BoneNode::updateDisplayedOpacity(uint8_t /*parentOpacity*/)
{
if (_cascadeOpacityEnabled)
{
for (const auto& child : _boneSkins)
{
child->updateDisplayedOpacity(_displayedOpacity);
}
}
}
void BoneNode::disableCascadeOpacity()
{
for (const auto& child : _boneSkins)
{
child->updateDisplayedOpacity(255);
}
}
void BoneNode::disableCascadeColor()
{
for (const auto& child : _boneSkins)
{
child->updateDisplayedColor(axis::Color3B::WHITE);
}
}
axis::Vector<BoneNode*> BoneNode::getAllSubBones() const
2015-07-23 11:51:49 +08:00
{
axis::Vector<BoneNode*> allBones;
2021-12-25 10:04:45 +08:00
std::stack<BoneNode*> boneStack; // for avoid recursive
2015-07-23 11:51:49 +08:00
for (const auto& bone : _childBones)
{
boneStack.push(bone);
}
while (boneStack.size() > 0)
{
auto top = boneStack.top();
allBones.pushBack(top);
boneStack.pop();
auto topchildren = top->getChildBones();
for (const auto& childbone : topchildren)
2015-07-23 11:51:49 +08:00
{
boneStack.push(childbone);
2015-07-23 11:51:49 +08:00
}
}
return allBones;
}
axis::Vector<SkinNode*> BoneNode::getAllSubSkins() const
2015-07-23 11:51:49 +08:00
{
auto allbones = getAllSubBones();
axis::Vector<SkinNode*> allskins;
2015-07-23 11:51:49 +08:00
for (const auto& bone : allbones)
{
for (const auto& skin : bone->getSkins())
{
allskins.pushBack(skin);
}
}
return allskins;
}
void BoneNode::sortAllChildren()
{
2015-07-28 23:49:25 +08:00
if (_reorderChildDirty)
{
sortNodes(_childBones);
sortNodes(_boneSkins);
2015-07-28 23:49:25 +08:00
Node::sortAllChildren();
}
2015-07-23 11:51:49 +08:00
}
SkeletonNode* BoneNode::getRootSkeletonNode() const
{
return _rootSkeleton;
}
2022-07-15 19:17:01 +08:00
#ifdef AX_STUDIO_ENABLED_VIEW
bool BoneNode::isPointOnRack(const axis::Vec2& bonePoint)
2015-07-23 11:51:49 +08:00
{
2015-07-23 17:41:37 +08:00
2021-12-25 10:04:45 +08:00
if (bonePoint.x >= 0.0f && bonePoint.y >= _squareVertices[0].y && bonePoint.x <= _rackLength &&
2015-07-23 11:51:49 +08:00
bonePoint.y <= _squareVertices[2].y)
{
if (_rackLength != 0.0f && _rackWidth != 0.0f)
{
float a1 = (_squareVertices[2].y - _squareVertices[3].y) / (_squareVertices[3].x - _squareVertices[0].x);
2016-09-12 09:45:34 +08:00
float a2 = (_squareVertices[2].y - _squareVertices[3].y) / (_squareVertices[0].x - _squareVertices[1].x);
2015-07-23 17:41:37 +08:00
float b1 = a1 * _squareVertices[3].x;
float y1 = bonePoint.y - _squareVertices[1].y;
2021-12-25 10:04:45 +08:00
if (y1 >= a1 * bonePoint.x - b1 && y1 <= a2 * bonePoint.x && y1 >= -a2 * bonePoint.x &&
2015-07-23 17:41:37 +08:00
y1 <= -a1 * bonePoint.x + b1)
2015-07-23 11:51:49 +08:00
return true;
}
}
return false;
}
2022-07-15 19:17:01 +08:00
#endif // AX_STUDIO_ENABLED_VIEW
2015-07-23 11:51:49 +08:00
void BoneNode::batchBoneDrawToSkeleton(BoneNode* bone) const
{
bool visibleByCamera = bone->isVisitableByVisitingCamera();
if (!visibleByCamera)
{
return;
}
axis::Vec3 vpos[4];
2015-07-23 11:51:49 +08:00
for (int i = 0; i < 4; i++)
{
axis::Vec4 pos;
2021-12-25 10:04:45 +08:00
pos.x = bone->_squareVertices[i].x;
pos.y = bone->_squareVertices[i].y;
pos.z = bone->_positionZ;
2015-07-23 11:51:49 +08:00
pos.w = 1;
bone->_modelViewTransform.transformVector(&pos); // call after visit
vpos[i] = axis::Vec3(pos.x, pos.y, pos.z) / pos.w;
2015-07-23 11:51:49 +08:00
}
int count = bone->_rootSkeleton->_batchedVeticesCount;
2021-12-25 10:04:45 +08:00
if (count + 8 > (int)(bone->_rootSkeleton->_batchedBoneVertexData.size()))
2015-07-23 11:51:49 +08:00
{
2019-11-25 01:35:26 +08:00
bone->_rootSkeleton->_batchedBoneVertexData.resize(count + 100);
2015-07-23 11:51:49 +08:00
}
for (int i = 0; i < 4; i++)
{
2019-11-25 01:35:26 +08:00
bone->_rootSkeleton->_batchedBoneVertexData[count + i].vertex = vpos[i];
2021-12-25 10:04:45 +08:00
bone->_rootSkeleton->_batchedBoneVertexData[count + i].color = bone->_vertexData[i].squareColor;
2015-07-23 11:51:49 +08:00
}
bone->_rootSkeleton->_batchedVeticesCount += 4;
}
// call after self visit
void BoneNode::visitSkins(axis::Renderer* renderer, BoneNode* bone) const
{
// quick return if not visible. children won't be drawn.
if (!bone->_visible)
{
return;
}
// 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->pushMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_director->loadMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, bone->_modelViewTransform);
if (!bone->_boneSkins.empty())
{
bone->sortAllChildren();
for (auto it = bone->_boneSkins.cbegin(); it != bone->_boneSkins.cend(); ++it)
(*it)->visit(renderer, bone->_modelViewTransform, true);
}
_director->popMatrix(axis::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// reset for next frame
// _orderOfArrival = 0;
}
void BoneNode::setRootSkeleton(BoneNode* bone, SkeletonNode* skeleton) const
{
bone->_rootSkeleton = skeleton;
}
2015-07-23 11:51:49 +08:00
void BoneNode::setLocalZOrder(int localZOrder)
{
Node::setLocalZOrder(localZOrder);
if (_rootSkeleton != nullptr)
_rootSkeleton->_subBonesOrderDirty = true;
2015-07-23 11:51:49 +08:00
}
2021-12-26 23:26:34 +08:00
void BoneNode::setName(std::string_view name)
2015-07-23 11:51:49 +08:00
{
auto oldname = getName();
Node::setName(name);
if (_rootSkeleton != nullptr)
{
auto oiter = _rootSkeleton->_subBonesMap.find(oldname);
auto niter = _rootSkeleton->_subBonesMap.find(name);
2021-12-25 10:04:45 +08:00
if (oiter != _rootSkeleton->_subBonesMap.end() && niter == _rootSkeleton->_subBonesMap.end())
2015-07-23 11:51:49 +08:00
{
auto node = oiter->second;
_rootSkeleton->_subBonesMap.erase(oiter);
_rootSkeleton->_subBonesMap.insert(name, node);
}
}
}
2021-12-25 10:04:45 +08:00
void BoneNode::addToChildrenListHelper(Node* child)
2015-07-23 11:51:49 +08:00
{
BoneNode* bone = dynamic_cast<BoneNode*>(child);
if (nullptr != bone)
{
addToBoneList(bone);
}
else
{
SkinNode* skin = dynamic_cast<SkinNode*>(child);
if (nullptr != skin)
{
addToSkinList(skin);
}
}
}
2021-12-25 10:04:45 +08:00
void BoneNode::removeFromChildrenListHelper(Node* child)
2015-07-23 11:51:49 +08:00
{
BoneNode* bone = dynamic_cast<BoneNode*>(child);
if (nullptr != bone)
{
removeFromBoneList(bone);
}
else
{
SkinNode* skin = dynamic_cast<SkinNode*>(child);
if (nullptr != skin)
{
removeFromSkinList(skin);
}
}
}
void BoneNode::setVisible(bool visible)
{
if (_visible == visible)
return;
Node::setVisible(visible);
if (_rootSkeleton != nullptr)
2015-07-23 11:51:49 +08:00
{
2021-12-25 10:04:45 +08:00
_rootSkeleton->_subBonesDirty = true;
_rootSkeleton->_subBonesOrderDirty = true;
2015-07-23 11:51:49 +08:00
}
}
void BoneNode::setContentSize(const axis::Size& contentSize)
2015-07-23 11:51:49 +08:00
{
Node::setContentSize(contentSize);
updateVertices();
}
void BoneNode::setAnchorPoint(const axis::Vec2& anchorPoint)
2015-07-23 11:51:49 +08:00
{
Node::setAnchorPoint(anchorPoint);
updateVertices();
}
NS_TIMELINE_END