axmol/extensions/CCArmature/CCArmature.cpp

579 lines
16 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 "CCArmature.h"
#include "utils/CCArmatureDataManager.h"
#include "utils/CCArmatureDefine.h"
#include "utils/CCDataReaderHelper.h"
#include "datas/CCDatas.h"
#include "display/CCSkin.h"
NS_CC_EXT_BEGIN
2013-06-07 10:52:32 +08:00
std::map<int, CCArmature *> CCArmature::m_sArmatureIndexDic;
2013-06-06 12:02:54 +08:00
CCArmature *CCArmature::create()
{
2013-06-07 10:52:32 +08:00
CCArmature *armature = new CCArmature();
2013-06-06 12:02:54 +08:00
if (armature && armature->init())
{
armature->autorelease();
return armature;
}
CC_SAFE_DELETE(armature);
return NULL;
}
2013-06-07 10:52:32 +08:00
CCArmature *CCArmature::create(const char *_name)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
CCArmature *armature = new CCArmature();
2013-06-06 12:02:54 +08:00
if (armature && armature->init(_name))
{
armature->autorelease();
return armature;
}
CC_SAFE_DELETE(armature);
return NULL;
}
2013-06-07 10:52:32 +08:00
CCArmature *CCArmature::create(const char *name, CCBone *parentBone)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
CCArmature *armature = new CCArmature();
if (armature && armature->init(name, parentBone))
{
armature->autorelease();
return armature;
}
CC_SAFE_DELETE(armature);
return NULL;
2013-06-06 12:02:54 +08:00
}
CCArmature::CCArmature()
2013-06-07 10:52:32 +08:00
: m_pAnimation(NULL)
2013-06-07 16:13:08 +08:00
, m_pArmatureData(NULL)
, m_pBatchNode(NULL)
2013-06-07 10:52:32 +08:00
, m_pAtlas(NULL)
2013-06-07 16:13:08 +08:00
, m_pParentBone(NULL)
, m_pBoneDic(NULL)
2013-06-07 10:52:32 +08:00
, m_pTopBoneList(NULL)
2013-06-06 12:02:54 +08:00
{
}
CCArmature::~CCArmature(void)
{
if(NULL != m_pBoneDic)
{
m_pBoneDic->removeAllObjects();
CC_SAFE_DELETE(m_pBoneDic);
}
2013-06-07 10:52:32 +08:00
if (NULL != m_pTopBoneList)
{
m_pTopBoneList->removeAllObjects();
CC_SAFE_DELETE(m_pTopBoneList);
}
2013-06-06 12:02:54 +08:00
CC_SAFE_DELETE(m_pAnimation);
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bool CCArmature::init()
{
return init(NULL);
}
bool CCArmature::init(const char *name)
{
bool bRet = false;
do
{
2013-06-07 10:52:32 +08:00
removeAllChildren();
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
CC_SAFE_DELETE(m_pAnimation);
m_pAnimation = new CCArmatureAnimation();
2013-06-07 10:52:32 +08:00
m_pAnimation->init(this);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
CC_SAFE_DELETE(m_pBoneDic);
2013-06-06 12:02:54 +08:00
m_pBoneDic = new CCDictionary();
2013-06-07 10:52:32 +08:00
CC_SAFE_DELETE(m_pTopBoneList);
m_pTopBoneList = new CCArray();
m_pTopBoneList->init();
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
m_sBlendFunc.src = CC_BLEND_SRC;
m_sBlendFunc.dst = CC_BLEND_DST;
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
m_strName = name == NULL ? "" : name;
2013-06-06 12:02:54 +08:00
CCArmatureDataManager *armatureDataManager = CCArmatureDataManager::sharedArmatureDataManager();
2013-06-07 10:52:32 +08:00
if(m_strName.length() != 0)
2013-06-06 12:02:54 +08:00
{
m_strName = name;
2013-06-07 10:52:32 +08:00
CCAnimationData *animationData = armatureDataManager->getAnimationData(name);
2013-06-06 16:22:58 +08:00
CCAssert(animationData, "CCAnimationData not exist! ");
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
m_pAnimation->setAnimationData(animationData);
2013-06-07 10:52:32 +08:00
2013-06-06 16:22:58 +08:00
CCArmatureData *armatureData = armatureDataManager->getArmatureData(name);
2013-06-06 12:02:54 +08:00
CCAssert(armatureData, "");
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
m_pArmatureData = armatureData;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
CCDictElement *_element = NULL;
2013-06-07 10:52:32 +08:00
CCDictionary *boneDataDic = &armatureData->boneDataDic;
2013-06-06 12:02:54 +08:00
CCDICT_FOREACH(boneDataDic, _element)
{
CCBone *bone = createBone(_element->getStrKey());
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
//! init bone's CCTween to 1st movement's 1st frame
2013-06-07 10:52:32 +08:00
do
{
2013-06-06 16:22:58 +08:00
CCMovementData *movData = animationData->getMovement(animationData->movementNames.at(0).c_str());
2013-06-06 12:02:54 +08:00
CC_BREAK_IF(!movData);
2013-06-07 10:52:32 +08:00
2013-06-06 16:22:58 +08:00
CCMovementBoneData *movBoneData = movData->getMovementBoneData(bone->getName().c_str());
2013-06-06 12:02:54 +08:00
CC_BREAK_IF(!movBoneData || movBoneData->frameList.count() <= 0);
2013-06-07 10:52:32 +08:00
2013-06-06 16:22:58 +08:00
CCFrameData *frameData = movBoneData->getFrameData(0);
2013-06-06 12:02:54 +08:00
CC_BREAK_IF(!frameData);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bone->getTweenData()->copy(frameData);
2013-06-07 10:52:32 +08:00
bone->changeDisplayByIndex(frameData->displayIndex, false);
}
while (0);
}
update(0);
updateOffsetPoint();
2013-06-06 12:02:54 +08:00
}
else
{
m_strName = "new_armature";
2013-06-06 16:22:58 +08:00
m_pArmatureData = CCArmatureData::create();
2013-06-06 12:02:54 +08:00
m_pArmatureData->name = m_strName;
2013-06-07 10:52:32 +08:00
2013-06-06 16:22:58 +08:00
CCAnimationData *animationData = CCAnimationData::create();
2013-06-07 10:52:32 +08:00
animationData->name = m_strName;
2013-06-06 12:02:54 +08:00
armatureDataManager->addArmatureData(m_strName.c_str(), m_pArmatureData);
armatureDataManager->addAnimationData(m_strName.c_str(), animationData);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
m_pAnimation->setAnimationData(animationData);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
2013-06-06 12:02:54 +08:00
unscheduleUpdate();
2013-06-07 10:52:32 +08:00
scheduleUpdate();
setCascadeOpacityEnabled(true);
setCascadeColorEnabled(true);
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
bool CCArmature::init(const char *name, CCBone *parentBone)
{
2013-06-07 10:52:32 +08:00
m_pParentBone = parentBone;
return init(name);
2013-06-06 12:02:54 +08:00
}
CCBone *CCArmature::createBone(const char *boneName)
{
2013-06-07 10:52:32 +08:00
CCBone *existedBone = getBone(boneName);
if(existedBone != NULL)
return existedBone;
CCBoneData *boneData = (CCBoneData *)m_pArmatureData->getBoneData(boneName);
std::string parentName = boneData->parentName;
2013-06-06 12:02:54 +08:00
CCBone *bone = NULL;
2013-06-07 10:52:32 +08:00
if( parentName.length() != 0 )
2013-06-06 12:02:54 +08:00
{
createBone(parentName.c_str());
bone = CCBone::create(boneName);
addBone(bone, parentName.c_str());
2013-06-07 10:52:32 +08:00
}
else
{
2013-06-06 12:02:54 +08:00
bone = CCBone::create(boneName);
addBone(bone, "");
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bone->setBoneData(boneData);
2013-06-07 10:52:32 +08:00
bone->getDisplayManager()->changeDisplayByIndex(-1, false);
2013-06-06 12:02:54 +08:00
return bone;
}
void CCArmature::addBone(CCBone *bone, const char *parentName)
{
CCAssert( bone != NULL, "Argument must be non-nil");
CCAssert(m_pBoneDic->objectForKey(bone->getName()) == NULL, "bone already added. It can't be added again");
if (NULL != parentName)
{
2013-06-07 10:52:32 +08:00
CCBone *boneParent = (CCBone *)m_pBoneDic->objectForKey(parentName);
2013-06-06 12:02:54 +08:00
if (boneParent)
2013-06-07 10:52:32 +08:00
{
boneParent->addChildBone(bone);
}
else
{
if (m_pParentBone)
m_pParentBone->addChildBone(bone);
else
m_pTopBoneList->addObject(bone);
}
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
else
{
if (m_pParentBone)
m_pParentBone->addChildBone(bone);
else
m_pTopBoneList->addObject(bone);
}
2013-06-06 12:02:54 +08:00
bone->setArmature(this);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
m_pBoneDic->setObject(bone, bone->getName());
addChild(bone);
}
void CCArmature::removeBone(CCBone *bone, bool recursion)
{
CCAssert(bone != NULL, "bone must be added to the bone dictionary!");
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bone->setArmature(NULL);
bone->removeFromParent(recursion);
2013-06-07 10:52:32 +08:00
if (m_pTopBoneList->containsObject(bone))
{
m_pTopBoneList->removeObject(bone);
}
2013-06-06 12:02:54 +08:00
m_pBoneDic->removeObjectForKey(bone->getName());
removeChild(bone, true);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
CCBone *CCArmature::getBone(const char *_name) const
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
return (CCBone *)m_pBoneDic->objectForKey(_name);
2013-06-06 12:02:54 +08:00
}
void CCArmature::changeBoneParent(CCBone *bone, const char *parentName)
{
2013-06-07 10:52:32 +08:00
CCAssert(bone != NULL, "bone must be added to the bone dictionary!");
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
bone->getParentBone()->getChildren()->removeObject(bone);
bone->setParentBone(NULL);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
if (parentName != NULL)
{
CCBone *boneParent = (CCBone *)m_pBoneDic->objectForKey(parentName);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
if (boneParent)
{
boneParent->addChildBone(bone);
}
}
2013-06-06 12:02:54 +08:00
}
CCDictionary *CCArmature::getBoneDic()
{
return m_pBoneDic;
}
CCAffineTransform CCArmature::nodeToParentTransform()
{
2013-06-07 10:52:32 +08:00
if (m_bTransformDirty)
{
// Translate values
float x = m_obPosition.x;
float y = m_obPosition.y;
if (m_bIgnoreAnchorPointForPosition)
{
x += m_obAnchorPointInPoints.x;
y += m_obAnchorPointInPoints.y;
}
// Rotation values
// Change rotation code to handle X and Y
// If we skew with the exact same value for both x and y then we're simply just rotating
float cx = 1, sx = 0, cy = 1, sy = 0;
if (m_fRotationX || m_fRotationY)
{
float radiansX = -CC_DEGREES_TO_RADIANS(m_fRotationX);
float radiansY = -CC_DEGREES_TO_RADIANS(m_fRotationY);
cx = cosf(radiansX);
sx = sinf(radiansX);
cy = cosf(radiansY);
sy = sinf(radiansY);
}
// Add offset point
x += cy * m_pOffsetPoint.x * m_fScaleX + -sx * m_pOffsetPoint.y * m_fScaleY;
y += sy * m_pOffsetPoint.x * m_fScaleX + cx * m_pOffsetPoint.y * m_fScaleY;
bool needsSkewMatrix = ( m_fSkewX || m_fSkewY );
// optimization:
// inline anchor point calculation if skew is not needed
// Adjusted transform calculation for rotational skew
if (! needsSkewMatrix && !m_obAnchorPointInPoints.equals(CCPointZero))
{
x += cy * -m_obAnchorPointInPoints.x * m_fScaleX + -sx * -m_obAnchorPointInPoints.y * m_fScaleY;
y += sy * -m_obAnchorPointInPoints.x * m_fScaleX + cx * -m_obAnchorPointInPoints.y * m_fScaleY;
}
// Build Transform Matrix
// Adjusted transform calculation for rotational skew
m_sTransform = CCAffineTransformMake( cy * m_fScaleX, sy * m_fScaleX,
-sx * m_fScaleY, cx * m_fScaleY,
x, y );
// XXX: Try to inline skew
// If skew is needed, apply skew and then anchor point
if (needsSkewMatrix)
{
CCAffineTransform skewMatrix = CCAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(m_fSkewY)),
tanf(CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,
0.0f, 0.0f );
m_sTransform = CCAffineTransformConcat(skewMatrix, m_sTransform);
// adjust anchor point
if (!m_obAnchorPointInPoints.equals(CCPointZero))
{
m_sTransform = CCAffineTransformTranslate(m_sTransform, -m_obAnchorPointInPoints.x, -m_obAnchorPointInPoints.y);
}
}
if (m_bAdditionalTransformDirty)
{
m_sTransform = CCAffineTransformConcat(m_sTransform, m_sAdditionalTransform);
m_bAdditionalTransformDirty = false;
}
m_bTransformDirty = false;
}
return m_sTransform;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
void CCArmature::updateOffsetPoint()
{
2013-06-07 10:52:32 +08:00
// Set contentsize and Calculate anchor point.
CCRect rect = boundingBox();
setContentSize(rect.size);
m_pOffsetPoint = ccp(-rect.origin.x, -rect.origin.y);
setAnchorPoint(ccp(m_pOffsetPoint.x / rect.size.width, m_pOffsetPoint.y / rect.size.height));
2013-06-06 12:02:54 +08:00
}
void CCArmature::update(float dt)
{
m_pAnimation->update(dt);
2013-06-07 10:52:32 +08:00
CCObject *object = NULL;
CCARRAY_FOREACH(m_pTopBoneList, object)
{
((CCBone *)object)->update(dt);
}
2013-06-06 12:02:54 +08:00
}
void CCArmature::draw()
{
2013-06-07 10:52:32 +08:00
if (m_pParentBone == NULL)
{
CC_NODE_DRAW_SETUP();
ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
}
CCObject *object = NULL;
CCARRAY_FOREACH(m_pChildren, object)
{
CCBone *bone = (CCBone *)object;
CCDisplayManager *displayManager = bone->getDisplayManager();
CCNode *node = displayManager->getDisplayRenderNode();
if (NULL == node)
continue;
if(CCSkin *skin = dynamic_cast<CCSkin *>(node))
{
CCTextureAtlas *textureAtlas = skin->getTextureAtlas();
if(m_pAtlas != textureAtlas)
{
if (m_pAtlas)
{
m_pAtlas->drawQuads();
m_pAtlas->removeAllQuads();
}
}
m_pAtlas = textureAtlas;
if (m_pAtlas->getCapacity() == m_pAtlas->getTotalQuads() && !m_pAtlas->resizeCapacity(m_pAtlas->getCapacity() * 2))
return;
skin->draw();
}
else if(CCArmature *armature = dynamic_cast<CCArmature *>(node))
{
CCTextureAtlas *textureAtlas = armature->getTextureAtlas();
if(m_pAtlas != textureAtlas)
{
if (m_pAtlas)
{
m_pAtlas->drawQuads();
m_pAtlas->removeAllQuads();
}
}
armature->draw();
}
else
{
if (m_pAtlas)
{
m_pAtlas->drawQuads();
m_pAtlas->removeAllQuads();
}
node->visit();
CC_NODE_DRAW_SETUP();
ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
}
}
if(m_pAtlas && !m_pBatchNode && m_pParentBone == NULL)
{
m_pAtlas->drawQuads();
m_pAtlas->removeAllQuads();
}
2013-06-06 12:02:54 +08:00
}
void CCArmature::visit()
{
2013-06-07 10:52:32 +08:00
// quick return if not visible. children won't be drawn.
if (!m_bVisible)
{
return;
}
kmGLPushMatrix();
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->beforeDraw();
}
transform();
sortAllChildren();
draw();
// reset for next frame
m_uOrderOfArrival = 0;
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->afterDraw(this);
}
kmGLPopMatrix();
2013-06-06 12:02:54 +08:00
}
CCRect CCArmature::boundingBox()
{
2013-06-07 10:52:32 +08:00
float minx, miny, maxx, maxy = 0;
bool first = true;
CCRect boundingBox = CCRectMake(0, 0, 0, 0);
CCObject *object = NULL;
CCARRAY_FOREACH(m_pChildren, object)
{
CCBone *bone = (CCBone *)object;
CCRect r = bone->getDisplayManager()->getBoundingBox();
if(first)
{
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 boundingBox;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
CCBone *CCArmature::getBoneAtPoint(float x, float y)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
int length = m_pChildren->data->num;
CCBone **bs = (CCBone **)m_pChildren->data->arr;
for(int i = length - 1; i >= 0; i--)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
if(bs[i]->getDisplayManager()->containPoint(x, y))
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
return bs[i];
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
return NULL;
}
2013-06-07 10:52:32 +08:00
NS_CC_EXT_END