2015-07-23 11:51:49 +08:00
|
|
|
/****************************************************************************
|
2015-07-24 01:30:34 +08:00
|
|
|
Copyright (c) 2015 Chukong Technologies Inc.
|
2015-08-04 14:13:13 +08:00
|
|
|
|
2015-07-23 11:51:49 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h"
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "math/TransformUtils.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/ccGLStateCache.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include <stack>
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
using namespace cocos2d::GL;
|
|
|
|
|
2015-07-23 11:51:49 +08:00
|
|
|
NS_TIMELINE_BEGIN
|
|
|
|
|
|
|
|
SkeletonNode* SkeletonNode::create()
|
|
|
|
{
|
|
|
|
SkeletonNode* skeletonNode = new (std::nothrow) SkeletonNode();
|
|
|
|
if (skeletonNode && skeletonNode->init())
|
|
|
|
{
|
|
|
|
skeletonNode->autorelease();
|
|
|
|
return skeletonNode;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skeletonNode);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SkeletonNode::init()
|
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
_rackLength = _rackWidth = 20;
|
2015-07-23 11:51:49 +08:00
|
|
|
updateVertices();
|
2015-07-24 10:28:10 +08:00
|
|
|
setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
|
2015-07-23 11:51:49 +08:00
|
|
|
_rootSkeleton = this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::Rect SkeletonNode::getBoundingBox() const
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
float minx, miny, maxx, maxy = 0;
|
|
|
|
minx = miny = maxx = maxy;
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::Rect boundingBox = getVisibleSkinsRect();
|
2015-07-23 11:51:49 +08:00
|
|
|
bool first = true;
|
2015-07-24 10:28:10 +08:00
|
|
|
if (!boundingBox.equals(cocos2d::Rect::ZERO))
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
minx = boundingBox.getMinX();
|
|
|
|
miny = boundingBox.getMinY();
|
|
|
|
maxx = boundingBox.getMaxX();
|
|
|
|
maxy = boundingBox.getMaxY();
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
auto allbones = getAllSubBones();
|
|
|
|
for (const auto& bone : allbones)
|
|
|
|
{
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::Rect r = RectApplyAffineTransform(bone->getVisibleSkinsRect(),
|
2015-08-04 14:13:13 +08:00
|
|
|
bone->getNodeToParentAffineTransform(bone->getRootSkeletonNode()));
|
2015-07-24 10:28:10 +08:00
|
|
|
if (r.equals(cocos2d::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boundingBox.setRect(minx, miny, maxx - minx, maxy - miny);
|
|
|
|
return RectApplyAffineTransform(boundingBox, this->getNodeToParentAffineTransform());;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonNode::SkeletonNode()
|
2015-08-04 14:13:13 +08:00
|
|
|
: BoneNode()
|
|
|
|
, _subBonesDirty(true)
|
|
|
|
, _subBonesOrderDirty(true)
|
|
|
|
, _batchedVeticesCount(0)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonNode::~SkeletonNode()
|
|
|
|
{
|
2015-08-07 17:09:11 +08:00
|
|
|
for (auto &bonepair : _subBonesMap)
|
|
|
|
{
|
|
|
|
setRootSkeleton(bonepair.second, nullptr);
|
|
|
|
}
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::updateVertices()
|
|
|
|
{
|
|
|
|
if (_rackLength != _squareVertices[6].x - _anchorPointInPoints.x || _rackWidth != _squareVertices[3].y - _anchorPointInPoints.y)
|
|
|
|
{
|
|
|
|
const float radiusl = _rackLength * .5f;
|
|
|
|
const float radiusw = _rackWidth * .5f;
|
|
|
|
const float radiusl_2 = radiusl * .25f;
|
|
|
|
const float radiusw_2 = radiusw * .25f;
|
|
|
|
_squareVertices[5].y = _squareVertices[2].y = _squareVertices[1].y = _squareVertices[6].y
|
|
|
|
= _squareVertices[0].x = _squareVertices[4].x = _squareVertices[7].x = _squareVertices[3].x = .0f;
|
|
|
|
_squareVertices[5].x = -radiusl; _squareVertices[0].y = -radiusw;
|
2015-08-04 14:13:13 +08:00
|
|
|
_squareVertices[6].x = radiusl; _squareVertices[3].y = radiusw;
|
|
|
|
_squareVertices[1].x = radiusl_2; _squareVertices[7].y = radiusw_2;
|
|
|
|
_squareVertices[2].x = -radiusl_2; _squareVertices[4].y = -radiusw_2;
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
_squareVertices[i] += _anchorPointInPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
_transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::updateColor()
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
_squareColors[i] = _rackColor;
|
|
|
|
}
|
|
|
|
_transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
void SkeletonNode::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-07-24 19:48:05 +08:00
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!_visible)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-07-24 19:48:05 +08:00
|
|
|
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(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
if (!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for (; i < _children.size(); i++)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-07-24 19:48:05 +08:00
|
|
|
auto node = _children.at(i);
|
|
|
|
|
|
|
|
if (node && node->getLocalZOrder() < 0)
|
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
|
|
|
else
|
|
|
|
break;
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 19:48:05 +08:00
|
|
|
for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
|
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:13:13 +08:00
|
|
|
checkSubBonesDirty();
|
|
|
|
for (const auto& bone : _subOrderedAllBones)
|
2015-07-24 19:48:05 +08:00
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
visitSkins(renderer, bone);
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
2015-08-04 14:13:13 +08:00
|
|
|
|
2015-08-07 17:09:11 +08:00
|
|
|
if (_isRackShow)
|
|
|
|
{
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
|
|
|
// batch draw all sub bones
|
|
|
|
_batchBoneCommand.init(_globalZOrder, _modelViewTransform, parentFlags);
|
|
|
|
_batchBoneCommand.func = CC_CALLBACK_0(SkeletonNode::batchDrawAllSubBones, this, _modelViewTransform);
|
|
|
|
renderer->addCommand(&_batchBoneCommand);
|
|
|
|
}
|
2015-07-24 19:48:05 +08:00
|
|
|
_director->popMatrix(cocos2d::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 SkeletonNode::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
|
|
|
_customCommand.func = CC_CALLBACK_0(SkeletonNode::onDraw, this, transform, flags);
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
cocos2d::Vec4 pos;
|
|
|
|
pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
|
|
|
|
pos.w = 1;
|
|
|
|
_modelViewTransform.transformVector(&pos);
|
|
|
|
_noMVPVertices[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w;
|
|
|
|
}
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
void SkeletonNode::batchDrawAllSubBones(const cocos2d::Mat4 &transform)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
checkSubBonesDirty();
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
_batchedVeticesCount = 0;
|
2015-08-04 14:13:13 +08:00
|
|
|
for (const auto& bone : _subOrderedAllBones)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-08-07 17:09:11 +08:00
|
|
|
batchBoneDrawToSkeleton(bone);
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
2015-08-07 17:09:11 +08:00
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::Vec3* vetices = _batchedBoneVetices.data();
|
|
|
|
cocos2d::Color4F* veticesColor = _batchedBoneColors.data();
|
2015-07-23 11:51:49 +08:00
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformsForBuiltins(transform);
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2015-07-24 10:28:10 +08:00
|
|
|
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, vetices);
|
|
|
|
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, veticesColor);
|
2015-07-23 11:51:49 +08:00
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
2015-08-04 14:13:13 +08:00
|
|
|
|
2015-07-23 11:51:49 +08:00
|
|
|
#ifdef CC_STUDIO_ENABLED_VIEW
|
|
|
|
glLineWidth(1);
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
|
2016-03-11 12:56:58 +08:00
|
|
|
for (int i = 0; i < _batchedVeticesCount; i += 4 )
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, i, 4);
|
2016-03-11 12:56:58 +08:00
|
|
|
glDrawArrays(GL_LINE_LOOP, i, 4);
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _batchedVeticesCount);
|
|
|
|
#else
|
|
|
|
for (int i = 0; i < _batchedVeticesCount; i += 4)
|
|
|
|
{
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, i, 4);
|
|
|
|
}
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _batchedVeticesCount);
|
|
|
|
#endif //CC_STUDIO_ENABLED_VIEW
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
void SkeletonNode::onDraw(const cocos2d::Mat4 &transform, uint32_t flags)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformsForBuiltins(transform);
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2015-07-24 10:28:10 +08:00
|
|
|
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
|
|
|
|
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
|
2015-07-23 11:51:49 +08:00
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
cocos2d::GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
2015-07-23 11:51:49 +08:00
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::changeSkins(const std::map<std::string, std::string>& boneSkinNameMap)
|
|
|
|
{
|
|
|
|
for (auto &boneskin : boneSkinNameMap)
|
|
|
|
{
|
|
|
|
auto bone = getBoneNode(boneskin.first);
|
2015-08-04 14:13:13 +08:00
|
|
|
if (nullptr != bone)
|
2015-07-23 11:51:49 +08:00
|
|
|
bone->displaySkin(boneskin.second, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::changeSkins(const std::string& skinGroupName)
|
|
|
|
{
|
|
|
|
auto suit = _skinGroupMap.find(skinGroupName);
|
|
|
|
if (suit != _skinGroupMap.end())
|
|
|
|
{
|
|
|
|
changeSkins(suit->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BoneNode* SkeletonNode::getBoneNode(const std::string& boneName)
|
|
|
|
{
|
|
|
|
auto iter = _subBonesMap.find(boneName);
|
|
|
|
if (iter != _subBonesMap.end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:28:10 +08:00
|
|
|
const cocos2d::Map<std::string, BoneNode*>& SkeletonNode::getAllSubBonesMap() const
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
|
|
|
return _subBonesMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::addSkinGroup(std::string groupName, std::map<std::string, std::string> boneSkinNameMap)
|
|
|
|
{
|
|
|
|
_skinGroupMap.insert(std::make_pair(groupName, boneSkinNameMap));
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:13:13 +08:00
|
|
|
void SkeletonNode::checkSubBonesDirty()
|
|
|
|
{
|
|
|
|
if (_subBonesDirty)
|
|
|
|
{
|
|
|
|
updateOrderedAllbones();
|
|
|
|
_subBonesDirty = false;
|
|
|
|
}
|
|
|
|
if (_subBonesOrderDirty)
|
|
|
|
{
|
|
|
|
sortOrderedAllBones();
|
|
|
|
_subBonesOrderDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonNode::updateOrderedAllbones()
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
_subOrderedAllBones.clear();
|
|
|
|
// update sub bones, get All Visible SubBones
|
2015-07-28 16:31:06 +08:00
|
|
|
// get all sub bones as visit with visible
|
|
|
|
std::stack<BoneNode*> boneStack;
|
|
|
|
for (const auto& bone : _childBones)
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
if (bone->isVisible())
|
2015-07-28 16:31:06 +08:00
|
|
|
boneStack.push(bone);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (boneStack.size() > 0)
|
|
|
|
{
|
|
|
|
auto top = boneStack.top();
|
2015-08-04 14:13:13 +08:00
|
|
|
_subOrderedAllBones.pushBack(top);
|
2015-07-28 16:31:06 +08:00
|
|
|
boneStack.pop();
|
|
|
|
auto topChildren = top->getChildBones();
|
|
|
|
for (const auto& childbone : topChildren)
|
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
if (childbone->isVisible())
|
2015-07-28 16:31:06 +08:00
|
|
|
boneStack.push(childbone);
|
|
|
|
}
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:13:13 +08:00
|
|
|
void SkeletonNode::sortOrderedAllBones()
|
2015-07-23 11:51:49 +08:00
|
|
|
{
|
2015-08-04 14:13:13 +08:00
|
|
|
std::sort(_subOrderedAllBones.begin(), _subOrderedAllBones.end(), cocos2d::nodeComparisonLess);
|
2015-07-23 11:51:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_TIMELINE_END
|