axmol/extensions/CocoStudio/Armature/CCBone.cpp

377 lines
8.7 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "CCBone.h"
#include "CCArmature.h"
#include "utils/CCUtilMath.h"
#include "utils/CCArmatureDataManager.h"
#include "utils/CCTransformHelp.h"
#include "display/CCDisplayManager.h"
2013-09-13 18:07:37 +08:00
NS_CC_EXT_ARMATURE_BEGIN
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
CCBone *CCBone::create()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
CCBone *pBone = new CCBone();
2013-06-06 12:02:54 +08:00
if (pBone && pBone->init())
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return NULL;
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CCBone *CCBone::create(const char *name)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CCBone *pBone = new CCBone();
2013-06-06 12:02:54 +08:00
if (pBone && pBone->init(name))
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return NULL;
}
2013-09-13 18:07:37 +08:00
CCBone::CCBone()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_pTweenData = NULL;
m_pParentBone = NULL;
m_pArmature = NULL;
m_pChildArmature = NULL;
m_pBoneData = NULL;
m_pTween = NULL;
m_pTween = NULL;
_children = NULL;
2013-09-13 18:07:37 +08:00
m_pDisplayManager = NULL;
m_bIgnoreMovementBoneData = false;
m_tWorldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0);
m_bBoneTransformDirty = true;
m_eBlendType = BLEND_NORMAL;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
CCBone::~CCBone(void)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(m_pTweenData);
CC_SAFE_DELETE(_children);
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(m_pTween);
CC_SAFE_DELETE(m_pDisplayManager);
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
if(m_pBoneData)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_pBoneData->release();
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CC_SAFE_RELEASE(m_pChildArmature);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
bool CCBone::init()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return CCBone::init(NULL);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
bool CCBone::init(const char *name)
2013-06-06 12:02:54 +08:00
{
bool bRet = false;
do
{
2013-06-07 10:52:32 +08:00
if(NULL != name)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_strName = name;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(m_pTweenData);
m_pTweenData = new CCFrameData();
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(m_pTween);
m_pTween = new CCTween();
m_pTween->init(this);
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CC_SAFE_DELETE(m_pDisplayManager);
m_pDisplayManager = new CCDisplayManager();
m_pDisplayManager->init(this);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
2013-09-13 18:07:37 +08:00
void CCBone::setBoneData(CCBoneData *boneData)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
CCAssert(NULL != boneData, "_boneData must not be NULL");
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
m_pBoneData = boneData;
m_pBoneData->retain();
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
m_strName = m_pBoneData->name;
_ZOrder = m_pBoneData->zOrder;
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
m_pDisplayManager->initDisplayList(boneData);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CCBoneData *CCBone::getBoneData()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return m_pBoneData;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
void CCBone::setArmature(CCArmature *armature)
{
2013-09-13 18:07:37 +08:00
m_pArmature = armature;
if (m_pArmature)
{
m_pTween->setAnimation(m_pArmature->getAnimation());
}
}
2013-09-13 18:07:37 +08:00
CCArmature *CCBone::getArmature()
{
2013-09-13 18:07:37 +08:00
return m_pArmature;
}
2013-09-13 18:07:37 +08:00
void CCBone::update(float delta)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
if (m_pParentBone)
m_bBoneTransformDirty = m_bBoneTransformDirty || m_pParentBone->isTransformDirty();
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
if (m_bBoneTransformDirty)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
if (m_pArmature->getArmatureData()->dataVersion >= VERSION_COMBINED)
{
CCTransformHelp::nodeConcat(*m_pTweenData, *m_pBoneData);
m_pTweenData->scaleX -= 1;
m_pTweenData->scaleY -= 1;
}
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
CCTransformHelp::nodeToMatrix(*m_pTweenData, m_tWorldTransform);
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
m_tWorldTransform = AffineTransformConcat(getNodeToParentTransform(), m_tWorldTransform);
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
if(m_pParentBone)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
m_tWorldTransform = AffineTransformConcat(m_tWorldTransform, m_pParentBone->m_tWorldTransform);
2013-06-07 10:52:32 +08:00
}
}
2013-09-13 18:07:37 +08:00
CCDisplayFactory::updateDisplay(this, m_pDisplayManager->getCurrentDecorativeDisplay(), delta, m_bBoneTransformDirty || m_pArmature->getArmatureTransformDirty());
2013-06-07 10:52:32 +08:00
Object *object = NULL;
CCARRAY_FOREACH(_children, object)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
CCBone *childBone = (CCBone *)object;
2013-06-07 10:52:32 +08:00
childBone->update(delta);
}
2013-09-13 18:07:37 +08:00
m_bBoneTransformDirty = false;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::updateDisplayedColor(const Color3B &parentColor)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
_realColor = Color3B(255, 255, 255);
NodeRGBA::updateDisplayedColor(parentColor);
2013-06-07 10:52:32 +08:00
updateColor();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::updateDisplayedOpacity(GLubyte parentOpacity)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
_realOpacity = 255;
NodeRGBA::updateDisplayedOpacity(parentOpacity);
2013-06-07 10:52:32 +08:00
updateColor();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::updateColor()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
Node *display = m_pDisplayManager->getDisplayRenderNode();
RGBAProtocol *protocol = dynamic_cast<RGBAProtocol *>(display);
2013-06-07 10:52:32 +08:00
if(protocol != NULL)
{
2013-09-13 18:07:37 +08:00
protocol->setColor(Color3B(_displayedColor.r * m_pTweenData->r / 255, _displayedColor.g * m_pTweenData->g / 255, _displayedColor.b * m_pTweenData->b / 255));
protocol->setOpacity(_displayedOpacity * m_pTweenData->a / 255);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::updateZOrder()
{
if (m_pArmature->getArmatureData()->dataVersion >= VERSION_COMBINED)
{
int zorder = m_pTweenData->zOrder + m_pBoneData->zOrder;
setZOrder(zorder);
}
else
{
setZOrder(m_pTweenData->zOrder);
}
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
void CCBone::addChildBone(CCBone *child)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
CCAssert( NULL != child, "Argument must be non-nil");
CCAssert( NULL == child->m_pParentBone, "child already added. It can't be added again");
2013-06-07 10:52:32 +08:00
if(!_children)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
_children = Array::createWithCapacity(4);
_children->retain();
2013-06-07 10:52:32 +08:00
}
if (_children->getIndexOfObject(child) == UINT_MAX)
2013-06-07 10:52:32 +08:00
{
_children->addObject(child);
2013-06-07 10:52:32 +08:00
child->setParentBone(this);
2013-06-06 12:02:54 +08:00
}
}
2013-09-13 18:07:37 +08:00
void CCBone::removeChildBone(CCBone *bone, bool recursion)
2013-06-06 12:02:54 +08:00
{
if ( _children->getIndexOfObject(bone) != UINT_MAX )
2013-06-07 10:52:32 +08:00
{
if(recursion)
{
Array *_ccbones = bone->_children;
Object *_object = NULL;
2013-06-07 10:52:32 +08:00
CCARRAY_FOREACH(_ccbones, _object)
{
2013-09-13 18:07:37 +08:00
CCBone *_ccBone = (CCBone *)_object;
2013-06-07 10:52:32 +08:00
bone->removeChildBone(_ccBone, recursion);
}
}
bone->setParentBone(NULL);
bone->getDisplayManager()->setCurrentDecorativeDisplay(NULL);
_children->removeObject(bone);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::removeFromParent(bool recursion)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
if (NULL != m_pParentBone)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
m_pParentBone->removeChildBone(this, recursion);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::setParentBone(CCBone *parent)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_pParentBone = parent;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
CCBone *CCBone::getParentBone()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return m_pParentBone;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::setChildArmature(CCArmature *armature)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
if (m_pChildArmature != armature)
{
CC_SAFE_RETAIN(armature);
CC_SAFE_RELEASE(m_pChildArmature);
m_pChildArmature = armature;
}
}
CCArmature *CCBone::getChildArmature()
{
return m_pChildArmature;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
CCTween *CCBone::getTween()
{
return m_pTween;
}
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
void CCBone::setZOrder(int zOrder)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
if (_ZOrder != zOrder)
Node::setZOrder(zOrder);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::setTransformDirty(bool dirty)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_bBoneTransformDirty = dirty;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
bool CCBone::isTransformDirty()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return m_bBoneTransformDirty;
2013-06-06 12:02:54 +08:00
}
AffineTransform CCBone::getNodeToArmatureTransform() const
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return m_tWorldTransform;
2013-06-06 12:02:54 +08:00
}
AffineTransform CCBone::getNodeToWorldTransform() const
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
return AffineTransformConcat(m_tWorldTransform, m_pArmature->getNodeToWorldTransform());
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
Node *CCBone::getDisplayRenderNode()
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
return m_pDisplayManager->getDisplayRenderNode();
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::addDisplay(CCDisplayData *displayData, int index)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
m_pDisplayManager->addDisplay(displayData, index);
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::addDisplay(Node *display, int index)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
m_pDisplayManager->addDisplay(display, index);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
void CCBone::changeDisplayByIndex(int index, bool force)
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
m_pDisplayManager->changeDisplayByIndex(index, force);
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
Array *CCBone::getColliderBodyList()
2013-06-06 12:02:54 +08:00
{
2013-09-13 18:07:37 +08:00
if (CCDecorativeDisplay *decoDisplay = m_pDisplayManager->getCurrentDecorativeDisplay())
{
if (CCColliderDetector *detector = decoDisplay->getColliderDetector())
{
return detector->getColliderBodyList();
}
}
return NULL;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
NS_CC_EXT_ARMATURE_END