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"
|
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
namespace cocos2d { namespace extension { namespace armature {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
std::map<int, Armature *> Armature::_armatureIndexDic;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *Armature::create()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = new Armature();
|
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
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *Armature::create(const char *_name)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = new Armature();
|
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-20 14:15:53 +08:00
|
|
|
Armature *Armature::create(const char *name, Bone *parentBone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = new Armature();
|
2013-06-07 10:52:32 +08:00
|
|
|
if (armature && armature->init(name, parentBone))
|
|
|
|
{
|
|
|
|
armature->autorelease();
|
|
|
|
return armature;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(armature);
|
|
|
|
return NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature::Armature()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _animation(NULL)
|
|
|
|
, _armatureData(NULL)
|
|
|
|
, _batchNode(NULL)
|
|
|
|
, _atlas(NULL)
|
|
|
|
, _parentBone(NULL)
|
|
|
|
, _boneDic(NULL)
|
|
|
|
, _topBoneList(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature::~Armature(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(NULL != _boneDic)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_boneDic->removeAllObjects();
|
|
|
|
CC_SAFE_DELETE(_boneDic);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (NULL != _topBoneList)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoneList->removeAllObjects();
|
|
|
|
CC_SAFE_DELETE(_topBoneList);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_animation);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Armature::init()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
return init(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Armature::init(const char *name)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
removeAllChildren();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_animation);
|
2013-06-20 14:15:53 +08:00
|
|
|
_animation = new ArmatureAnimation();
|
2013-06-15 14:03:30 +08:00
|
|
|
_animation->init(this);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_boneDic);
|
2013-06-20 14:15:53 +08:00
|
|
|
_boneDic = new Dictionary();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_topBoneList);
|
2013-06-20 14:15:53 +08:00
|
|
|
_topBoneList = new Array();
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoneList->init();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = CC_BLEND_SRC;
|
|
|
|
_blendFunc.dst = CC_BLEND_DST;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = name == NULL ? "" : name;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureDataManager *armatureDataManager = ArmatureDataManager::sharedArmatureDataManager();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_name.length() != 0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = name;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
AnimationData *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-15 14:03:30 +08:00
|
|
|
_animation->setAnimationData(animationData);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureData *armatureData = armatureDataManager->getArmatureData(name);
|
2013-06-06 12:02:54 +08:00
|
|
|
CCAssert(armatureData, "");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_armatureData = armatureData;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
DictElement *_element = NULL;
|
|
|
|
Dictionary *boneDataDic = &armatureData->boneDataDic;
|
2013-06-06 12:02:54 +08:00
|
|
|
CCDICT_FOREACH(boneDataDic, _element)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *bone = createBone(_element->getStrKey());
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
//! init bone's Tween to 1st movement's 1st frame
|
2013-06-07 10:52:32 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
MovementData *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-20 14:15:53 +08:00
|
|
|
MovementBoneData *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-20 14:15:53 +08:00
|
|
|
FrameData *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
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_name = "new_armature";
|
2013-06-20 14:15:53 +08:00
|
|
|
_armatureData = ArmatureData::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_armatureData->name = _name;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
AnimationData *animationData = AnimationData::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
animationData->name = _name;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
armatureDataManager->addArmatureData(_name.c_str(), _armatureData);
|
|
|
|
armatureDataManager->addAnimationData(_name.c_str(), animationData);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_animation->setAnimationData(animationData);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kShader_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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Armature::init(const char *name, Bone *parentBone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parentBone = parentBone;
|
2013-06-07 10:52:32 +08:00
|
|
|
return init(name);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *Armature::createBone(const char *boneName)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *existedBone = getBone(boneName);
|
2013-06-07 10:52:32 +08:00
|
|
|
if(existedBone != NULL)
|
|
|
|
return existedBone;
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
BoneData *boneData = (BoneData *)_armatureData->getBoneData(boneName);
|
2013-06-07 10:52:32 +08:00
|
|
|
std::string parentName = boneData->parentName;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *bone = NULL;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-07 14:01:03 +08:00
|
|
|
if( parentName.length() != 0 )
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
createBone(parentName.c_str());
|
2013-06-20 14:15:53 +08:00
|
|
|
bone = Bone::create(boneName);
|
2013-06-06 12:02:54 +08:00
|
|
|
addBone(bone, parentName.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
bone = Bone::create(boneName);
|
2013-06-06 12:02:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::addBone(Bone *bone, const char *parentName)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
CCAssert( bone != NULL, "Argument must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(_boneDic->objectForKey(bone->getName()) == NULL, "bone already added. It can't be added again");
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
if (NULL != parentName)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *boneParent = (Bone *)_boneDic->objectForKey(parentName);
|
2013-06-06 12:02:54 +08:00
|
|
|
if (boneParent)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
boneParent->addChildBone(bone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_parentBone)
|
|
|
|
_parentBone->addChildBone(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoneList->addObject(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_parentBone)
|
|
|
|
_parentBone->addChildBone(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoneList->addObject(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
bone->setArmature(this);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_boneDic->setObject(bone, bone->getName());
|
2013-06-06 12:02:54 +08:00
|
|
|
addChild(bone);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::removeBone(Bone *bone, bool recursion)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_topBoneList->containsObject(bone))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_topBoneList->removeObject(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_boneDic->removeObjectForKey(bone->getName());
|
2013-06-08 22:08:22 +08:00
|
|
|
removeChild(bone, true);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *Armature::getBone(const char *_name) const
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
return (Bone *)_boneDic->objectForKey(_name);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::changeBoneParent(Bone *bone, const char *parentName)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
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)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *boneParent = (Bone *)_boneDic->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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Dictionary *Armature::getBoneDic()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _boneDic;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
AffineTransform Armature::nodeToParentTransform()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_transformDirty)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
// Translate values
|
2013-06-15 14:03:30 +08:00
|
|
|
float x = _position.x;
|
|
|
|
float y = _position.y;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_rotationX || _rotationY)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float radiansX = -CC_DEGREES_TO_RADIANS(_rotationX);
|
|
|
|
float radiansY = -CC_DEGREES_TO_RADIANS(_rotationY);
|
2013-06-07 10:52:32 +08:00
|
|
|
cx = cosf(radiansX);
|
|
|
|
sx = sinf(radiansX);
|
|
|
|
cy = cosf(radiansY);
|
|
|
|
sy = sinf(radiansY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add offset point
|
2013-06-15 14:03:30 +08:00
|
|
|
x += cy * _offsetPoint.x * _scaleX + -sx * _offsetPoint.y * _scaleY;
|
|
|
|
y += sy * _offsetPoint.x * _scaleX + cx * _offsetPoint.y * _scaleY;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool needsSkewMatrix = ( _skewX || _skewY );
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
// optimization:
|
|
|
|
// inline anchor point calculation if skew is not needed
|
|
|
|
// Adjusted transform calculation for rotational skew
|
2013-06-20 14:15:53 +08:00
|
|
|
if (! needsSkewMatrix && !_anchorPointInPoints.equals(PointZero))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += cy * -_anchorPointInPoints.x * _scaleX + -sx * -_anchorPointInPoints.y * _scaleY;
|
|
|
|
y += sy * -_anchorPointInPoints.x * _scaleX + cx * -_anchorPointInPoints.y * _scaleY;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build Transform Matrix
|
|
|
|
// Adjusted transform calculation for rotational skew
|
2013-06-20 14:15:53 +08:00
|
|
|
_transform = AffineTransformMake( cy * _scaleX, sy * _scaleX,
|
2013-06-15 14:03:30 +08:00
|
|
|
-sx * _scaleY, cx * _scaleY,
|
2013-06-07 10:52:32 +08:00
|
|
|
x, y );
|
|
|
|
|
|
|
|
// XXX: Try to inline skew
|
|
|
|
// If skew is needed, apply skew and then anchor point
|
|
|
|
if (needsSkewMatrix)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
AffineTransform skewMatrix = AffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(_skewY)),
|
2013-06-15 14:03:30 +08:00
|
|
|
tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1.0f,
|
2013-06-07 10:52:32 +08:00
|
|
|
0.0f, 0.0f );
|
2013-06-20 14:15:53 +08:00
|
|
|
_transform = AffineTransformConcat(skewMatrix, _transform);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
// adjust anchor point
|
2013-06-20 14:15:53 +08:00
|
|
|
if (!_anchorPointInPoints.equals(PointZero))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_transform = AffineTransformTranslate(_transform, -_anchorPointInPoints.x, -_anchorPointInPoints.y);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_additionalTransformDirty)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_transform = AffineTransformConcat(_transform, _additionalTransform);
|
2013-06-15 14:03:30 +08:00
|
|
|
_additionalTransformDirty = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _transform;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::updateOffsetPoint()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
// Set contentsize and Calculate anchor point.
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect rect = boundingBox();
|
2013-06-07 10:52:32 +08:00
|
|
|
setContentSize(rect.size);
|
2013-07-12 14:11:55 +08:00
|
|
|
_offsetPoint = Point(-rect.origin.x, -rect.origin.y);
|
|
|
|
setAnchorPoint(Point(_offsetPoint.x / rect.size.width, _offsetPoint.y / rect.size.height));
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::update(float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_animation->update(dt);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_topBoneList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
static_cast<Bone*>(object)->update(dt);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::draw()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_parentBone == NULL)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
CC_NODE_DRAW_SETUP();
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBlendFunc(_blendFunc.src, _blendFunc.dst);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Bone *bone = static_cast<Bone *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
DisplayManager *displayManager = bone->getDisplayManager();
|
|
|
|
Node *node = displayManager->getDisplayRenderNode();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
if (NULL == node)
|
|
|
|
continue;
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
if(Skin *skin = dynamic_cast<Skin *>(node))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TextureAtlas *textureAtlas = skin->getTextureAtlas();
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_atlas != textureAtlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas->drawQuads();
|
|
|
|
_atlas->removeAllQuads();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas = textureAtlas;
|
|
|
|
if (_atlas->getCapacity() == _atlas->getTotalQuads() && !_atlas->resizeCapacity(_atlas->getCapacity() * 2))
|
2013-06-07 10:52:32 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
skin->draw();
|
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
else if(Armature *armature = dynamic_cast<Armature *>(node))
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TextureAtlas *textureAtlas = armature->getTextureAtlas();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_atlas != textureAtlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas->drawQuads();
|
|
|
|
_atlas->removeAllQuads();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
armature->draw();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas->drawQuads();
|
|
|
|
_atlas->removeAllQuads();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
node->visit();
|
|
|
|
|
|
|
|
CC_NODE_DRAW_SETUP();
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBlendFunc(_blendFunc.src, _blendFunc.dst);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_atlas && !_batchNode && _parentBone == NULL)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas->drawQuads();
|
|
|
|
_atlas->removeAllQuads();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Armature::visit()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
// quick return if not visible. children won't be drawn.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_visible)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->beforeDraw();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
transform();
|
|
|
|
sortAllChildren();
|
|
|
|
draw();
|
|
|
|
|
|
|
|
// reset for next frame
|
2013-06-15 14:03:30 +08:00
|
|
|
_orderOfArrival = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->afterDraw(this);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect Armature::boundingBox()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
float minx, miny, maxx, maxy = 0;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect boundingBox = Rect(0, 0, 0, 0);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Bone *bone = static_cast<Bone *>(object);
|
2013-06-20 14:15:53 +08:00
|
|
|
Rect r = bone->getDisplayManager()->getBoundingBox();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
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-20 14:15:53 +08:00
|
|
|
Bone *Armature::getBoneAtPoint(float x, float y)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int length = _children->data->num;
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone **bs = (Bone **)_children->data->arr;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
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-17 21:32:15 +08:00
|
|
|
}}} // namespace cocos2d { namespace extension { namespace armature {
|