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"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CCBone *CCBone::create()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
CCBone *pBone = new CCBone();
|
|
|
|
if (pBone && pBone->init())
|
|
|
|
{
|
|
|
|
pBone->autorelease();
|
|
|
|
return pBone;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pBone);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
2013-06-07 10:52:32 +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-06-06 12:02:54 +08:00
|
|
|
CCBone *pBone = new CCBone();
|
|
|
|
if (pBone && pBone->init(name))
|
|
|
|
{
|
|
|
|
pBone->autorelease();
|
|
|
|
return pBone;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pBone);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCBone::CCBone()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tweenData = NULL;
|
|
|
|
_parent = NULL;
|
|
|
|
_armature = NULL;
|
|
|
|
_childArmature = NULL;
|
|
|
|
_boneData = NULL;
|
|
|
|
_tween = NULL;
|
|
|
|
_tween = NULL;
|
|
|
|
_children = NULL;
|
|
|
|
_displayManager = NULL;
|
|
|
|
_ignoreMovementBoneData = false;
|
|
|
|
_worldTransform = CCAffineTransformMake(1, 0, 0, 1, 0, 0);
|
|
|
|
_transformDirty = true;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CCBone::~CCBone(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_tweenData);
|
|
|
|
CC_SAFE_DELETE(_children);
|
|
|
|
CC_SAFE_DELETE(_tween);
|
|
|
|
CC_SAFE_DELETE(_displayManager);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_boneData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_boneData->release();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_childArmature);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCBone::init()
|
|
|
|
{
|
|
|
|
return CCBone::init(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCBone::init(const char *name)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
if(NULL != name)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = name;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_tweenData);
|
|
|
|
_tweenData = new CCFrameData();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_tween);
|
|
|
|
_tween = new CCTween();
|
|
|
|
_tween->init(this);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_displayManager);
|
|
|
|
_displayManager = new CCDisplayManager();
|
|
|
|
_displayManager->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-06-06 16:22:58 +08:00
|
|
|
void CCBone::setBoneData(CCBoneData *boneData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
CCAssert(NULL != boneData, "_boneData must not be NULL");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_boneData = boneData;
|
|
|
|
_boneData->retain();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = _boneData->name;
|
|
|
|
_ZOrder = _boneData->zOrder;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayManager->initDisplayList(boneData);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
CCBoneData *CCBone::getBoneData()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _boneData;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 22:08:22 +08:00
|
|
|
void CCBone::setArmature(CCArmature *armature)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_armature = armature;
|
|
|
|
_tween->setAnimation(_armature->getAnimation());
|
2013-06-08 22:08:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CCArmature *CCBone::getArmature()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _armature;
|
2013-06-08 22:08:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
void CCBone::update(float delta)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_parent)
|
|
|
|
_transformDirty = _transformDirty || _parent->isTransformDirty();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_transformDirty)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float cosX = cos(_tweenData->skewX);
|
|
|
|
float cosY = cos(_tweenData->skewY);
|
|
|
|
float sinX = sin(_tweenData->skewX);
|
|
|
|
float sinY = sin(_tweenData->skewY);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_worldTransform.a = _tweenData->scaleX * cosY;
|
|
|
|
_worldTransform.b = _tweenData->scaleX * sinY;
|
|
|
|
_worldTransform.c = _tweenData->scaleY * sinX;
|
|
|
|
_worldTransform.d = _tweenData->scaleY * cosX;
|
|
|
|
_worldTransform.tx = _tweenData->x;
|
|
|
|
_worldTransform.ty = _tweenData->y;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_worldTransform = CCAffineTransformConcat(nodeToParentTransform(), _worldTransform);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_parent)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_worldTransform = CCAffineTransformConcat(_worldTransform, _parent->_worldTransform);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCDisplayFactory::updateDisplay(this, _displayManager->getCurrentDecorativeDisplay(), delta, _transformDirty);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
CCObject *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
CCBone *childBone = (CCBone *)object;
|
|
|
|
childBone->update(delta);
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = false;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
void CCBone::updateDisplayedColor(const ccColor3B &parentColor)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCNodeRGBA::updateDisplayedColor(parentColor);
|
|
|
|
updateColor();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCNodeRGBA::updateDisplayedOpacity(parentOpacity);
|
|
|
|
updateColor();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::updateColor()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCNode *display = _displayManager->getDisplayRenderNode();
|
2013-06-07 10:52:32 +08:00
|
|
|
CCRGBAProtocol *protocol = dynamic_cast<CCRGBAProtocol *>(display);
|
|
|
|
if(protocol != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
protocol->setColor(ccc3(_displayedColor.r * _tweenData->r / 255, _displayedColor.g * _tweenData->g / 255, _displayedColor.b * _tweenData->b / 255));
|
|
|
|
protocol->setOpacity(_displayedOpacity * _tweenData->a / 255);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
void CCBone::addChildBone(CCBone *child)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCAssert( NULL != child, "Argument must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert( NULL == child->_parent, "child already added. It can't be added again");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(!_children)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
childrenAlloc();
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children->indexOfObject(child) == UINT_MAX)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_children->addObject(child);
|
2013-06-07 10:52:32 +08:00
|
|
|
child->setParentBone(this);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::removeChildBone(CCBone *bone, bool recursion)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( _children->indexOfObject(bone) != UINT_MAX )
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
if(recursion)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCArray *_ccbones = bone->_children;
|
2013-06-07 10:52:32 +08:00
|
|
|
CCObject *_object = NULL;
|
|
|
|
CCARRAY_FOREACH(_ccbones, _object)
|
|
|
|
{
|
|
|
|
CCBone *_ccBone = (CCBone *)_object;
|
|
|
|
bone->removeChildBone(_ccBone, recursion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bone->setParentBone(NULL);
|
|
|
|
|
|
|
|
bone->getDisplayManager()->setCurrentDecorativeDisplay(NULL);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_children->removeObject(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::removeFromParent(bool recursion)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (NULL != _parent)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->removeChildBone(this, recursion);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::setParentBone(CCBone *parent)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent = parent;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCBone *CCBone::getParentBone()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _parent;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::childrenAlloc(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_children);
|
|
|
|
_children = CCArray::createWithCapacity(4);
|
|
|
|
_children->retain();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCBone::setChildArmature(CCArmature *armature)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_childArmature != armature)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(armature);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_childArmature);
|
|
|
|
_childArmature = armature;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCArmature *CCBone::getChildArmature()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _childArmature;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCArray *CCBone::getChildren()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _children;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTween *CCBone::getTween()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _tween;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::setZOrder(int zOrder)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ZOrder != zOrder)
|
2013-06-07 10:52:32 +08:00
|
|
|
CCNode::setZOrder(zOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::setTransformDirty(bool dirty)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = dirty;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCBone::isTransformDirty()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _transformDirty;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCAffineTransform CCBone::nodeToArmatureTransform()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _worldTransform;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
void CCBone::addDisplay(CCDisplayData *_displayData, int _index)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayManager->addDisplay(_displayData, _index);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCBone::changeDisplayByIndex(int _index, bool _force)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_displayManager->changeDisplayByIndex(_index, _force);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
NS_CC_EXT_END
|