axmol/cocos/editor-support/cocostudio/CCBone.cpp

479 lines
11 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 "cocostudio/CCBone.h"
#include "cocostudio/CCArmature.h"
#include "cocostudio/CCUtilMath.h"
#include "cocostudio/CCArmatureDataManager.h"
#include "cocostudio/CCTransformHelp.h"
#include "cocostudio/CCDisplayManager.h"
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
namespace cocostudio {
2013-06-06 12:02:54 +08:00
Bone *Bone::create()
2013-06-06 12:02:54 +08:00
{
Bone *pBone = new Bone();
2013-06-06 12:02:54 +08:00
if (pBone && pBone->init())
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return nullptr;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
Bone *Bone::create(const char *name)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
Bone *pBone = new Bone();
2013-06-06 12:02:54 +08:00
if (pBone && pBone->init(name))
{
pBone->autorelease();
return pBone;
}
CC_SAFE_DELETE(pBone);
return nullptr;
2013-06-06 12:02:54 +08:00
}
Bone::Bone()
2013-06-06 12:02:54 +08:00
{
_tweenData = nullptr;
_parentBone = nullptr;
_armature = nullptr;
_childArmature = nullptr;
_boneData = nullptr;
_tween = nullptr;
_tween = nullptr;
_children = nullptr;
_displayManager = nullptr;
2013-09-16 12:21:18 +08:00
_ignoreMovementBoneData = false;
_worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0);
_boneTransformDirty = true;
_blendType = BLEND_NORMAL;
_worldInfo = nullptr;
2013-10-30 09:41:40 +08:00
_armatureParentBone = nullptr;
2013-10-30 09:41:40 +08:00
_dataVersion = 0;
2013-06-06 12:02:54 +08:00
}
Bone::~Bone(void)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
CC_SAFE_DELETE(_tweenData);
CC_SAFE_DELETE(_children);
2013-09-16 14:56:59 +08:00
CC_SAFE_DELETE(_tween);
2013-09-16 12:21:18 +08:00
CC_SAFE_DELETE(_displayManager);
2013-10-30 09:41:40 +08:00
CC_SAFE_DELETE(_worldInfo);
2013-06-07 10:52:32 +08:00
CC_SAFE_RELEASE_NULL(_boneData);
2013-06-07 10:52:32 +08:00
2013-09-16 12:21:18 +08:00
CC_SAFE_RELEASE(_childArmature);
2013-06-06 12:02:54 +08:00
}
bool Bone::init()
2013-06-06 12:02:54 +08:00
{
return Bone::init(nullptr);
2013-06-06 12:02:54 +08:00
}
bool Bone::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(nullptr != name)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_name = name;
2013-06-06 12:02:54 +08:00
}
2013-09-15 20:24:25 +08:00
CC_SAFE_DELETE(_tweenData);
_tweenData = new FrameData();
2013-06-06 12:02:54 +08:00
2013-09-16 14:56:59 +08:00
CC_SAFE_DELETE(_tween);
_tween = new Tween();
_tween->init(this);
2013-06-07 10:52:32 +08:00
2013-09-16 12:21:18 +08:00
CC_SAFE_DELETE(_displayManager);
_displayManager = new DisplayManager();
_displayManager->init(this);
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
CC_SAFE_DELETE(_worldInfo);
_worldInfo = new BaseData();
2013-06-07 10:52:32 +08:00
CC_SAFE_DELETE(_boneData);
2013-11-05 19:53:38 +08:00
_boneData = new BoneData();
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
void Bone::setBoneData(BoneData *boneData)
2013-06-06 12:02:54 +08:00
{
CCASSERT(nullptr != boneData, "_boneData must not be nullptr");
2013-06-07 10:52:32 +08:00
if (_boneData != boneData)
{
CC_SAFE_RETAIN(boneData);
CC_SAFE_RELEASE(_boneData);
_boneData = boneData;
}
2013-06-07 10:52:32 +08:00
2013-09-16 12:21:18 +08:00
_name = _boneData->name;
_ZOrder = _boneData->zOrder;
2013-06-07 10:52:32 +08:00
2013-09-16 12:21:18 +08:00
_displayManager->initDisplayList(boneData);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-11-05 19:53:38 +08:00
BoneData *Bone::getBoneData() const
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
return _boneData;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void Bone::setArmature(Armature *armature)
{
2013-09-15 20:24:25 +08:00
_armature = armature;
if (_armature)
2013-09-13 18:07:37 +08:00
{
2013-09-16 14:56:59 +08:00
_tween->setAnimation(_armature->getAnimation());
2013-10-30 09:41:40 +08:00
_dataVersion = _armature->getArmatureData()->dataVersion;
_armatureParentBone = _armature->getParentBone();
}
else
{
_armatureParentBone = nullptr;
2013-09-13 18:07:37 +08:00
}
}
2013-11-05 19:53:38 +08:00
Armature *Bone::getArmature() const
{
2013-09-15 20:24:25 +08:00
return _armature;
}
void Bone::update(float delta)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
if (_parentBone)
2013-09-16 12:21:18 +08:00
_boneTransformDirty = _boneTransformDirty || _parentBone->isTransformDirty();
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
if (_armatureParentBone && !_boneTransformDirty)
2013-10-29 16:57:06 +08:00
{
2013-10-30 09:41:40 +08:00
_boneTransformDirty = _armatureParentBone->isTransformDirty();
2013-10-29 16:57:06 +08:00
}
2013-09-16 12:21:18 +08:00
if (_boneTransformDirty)
2013-06-07 10:52:32 +08:00
{
2013-10-30 09:41:40 +08:00
if (_dataVersion >= VERSION_COMBINED)
2013-09-13 18:07:37 +08:00
{
2013-09-16 12:21:18 +08:00
TransformHelp::nodeConcat(*_tweenData, *_boneData);
2013-09-15 20:24:25 +08:00
_tweenData->scaleX -= 1;
_tweenData->scaleY -= 1;
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
_worldInfo->copy(_tweenData);
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
_worldInfo->x = _tweenData->x + _position.x;
_worldInfo->y = _tweenData->y + _position.y;
_worldInfo->scaleX = _tweenData->scaleX * _scaleX;
_worldInfo->scaleY = _tweenData->scaleY * _scaleY;
_worldInfo->skewX = _tweenData->skewX + _skewX + _rotationX;
_worldInfo->skewY = _tweenData->skewY + _skewY - _rotationY;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if(_parentBone)
2013-06-07 10:52:32 +08:00
{
2013-10-30 09:41:40 +08:00
applyParentTransform(_parentBone);
2013-06-07 10:52:32 +08:00
}
2013-10-29 16:57:06 +08:00
else
{
2013-10-30 09:41:40 +08:00
if (_armatureParentBone)
2013-10-29 16:57:06 +08:00
{
2013-10-30 09:41:40 +08:00
applyParentTransform(_armatureParentBone);
2013-10-29 16:57:06 +08:00
}
}
2013-10-30 09:41:40 +08:00
TransformHelp::nodeToMatrix(*_worldInfo, _worldTransform);
if (_armatureParentBone)
{
_worldTransform = AffineTransformConcat(_worldTransform, _armature->getNodeToParentTransform());
}
2013-06-07 10:52:32 +08:00
}
2013-10-30 09:41:40 +08:00
DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty());
2013-06-07 10:52:32 +08:00
if (_children)
2013-06-07 10:52:32 +08:00
{
for(auto object : *_children)
{
Bone *childBone = (Bone *)object;
childBone->update(delta);
}
2013-06-07 10:52:32 +08:00
}
2013-09-16 12:21:18 +08:00
_boneTransformDirty = false;
2013-06-06 12:02:54 +08:00
}
2013-10-30 09:41:40 +08:00
void Bone::applyParentTransform(Bone *parent)
{
float x = _worldInfo->x;
float y = _worldInfo->y;
_worldInfo->x = x * parent->_worldTransform.a + y * parent->_worldTransform.c + parent->_worldInfo->x;
_worldInfo->y = x * parent->_worldTransform.b + y * parent->_worldTransform.d + parent->_worldInfo->y;
_worldInfo->scaleX = _worldInfo->scaleX * parent->_worldInfo->scaleX;
_worldInfo->scaleY = _worldInfo->scaleY * parent->_worldInfo->scaleY;
_worldInfo->skewX = _worldInfo->skewX + parent->_worldInfo->skewX;
_worldInfo->skewY = _worldInfo->skewY + parent->_worldInfo->skewY;
}
2013-06-06 12:02:54 +08:00
void Bone::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
}
void Bone::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-10-30 09:41:40 +08:00
void Bone::setColor(const Color3B& color)
{
NodeRGBA::setColor(color);
updateColor();
}
void Bone::setOpacity(GLubyte opacity)
{
NodeRGBA::setOpacity(opacity);
updateColor();
}
void Bone::updateColor()
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
Node *display = _displayManager->getDisplayRenderNode();
RGBAProtocol *protocol = dynamic_cast<RGBAProtocol *>(display);
if(protocol != nullptr)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
protocol->setColor(Color3B(_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
}
void Bone::updateZOrder()
2013-09-13 18:07:37 +08:00
{
2013-10-30 09:41:40 +08:00
if (_dataVersion >= VERSION_COMBINED)
2013-09-13 18:07:37 +08:00
{
2013-09-16 12:21:18 +08:00
int zorder = _tweenData->zOrder + _boneData->zOrder;
2013-09-13 18:07:37 +08:00
setZOrder(zorder);
}
else
{
2013-09-15 20:24:25 +08:00
setZOrder(_tweenData->zOrder);
2013-09-13 18:07:37 +08:00
}
}
2013-06-06 12:02:54 +08:00
void Bone::addChildBone(Bone *child)
2013-06-06 12:02:54 +08:00
{
CCASSERT( nullptr != child, "Argument must be non-nil");
CCASSERT( nullptr == child->_parentBone, "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) == CC_INVALID_INDEX)
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
}
}
void Bone::removeChildBone(Bone *bone, bool recursion)
2013-06-06 12:02:54 +08:00
{
if (_children && _children->getIndexOfObject(bone) != CC_INVALID_INDEX )
2013-06-07 10:52:32 +08:00
{
if(recursion)
{
Array *ccbones = bone->_children;
for(auto object : *ccbones)
2013-06-07 10:52:32 +08:00
{
Bone *ccBone = (Bone *)object;
bone->removeChildBone(ccBone, recursion);
2013-06-07 10:52:32 +08:00
}
}
bone->setParentBone(nullptr);
2013-06-07 10:52:32 +08:00
bone->getDisplayManager()->setCurrentDecorativeDisplay(nullptr);
2013-06-07 10:52:32 +08:00
_children->removeObject(bone);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
void Bone::removeFromParent(bool recursion)
2013-06-06 12:02:54 +08:00
{
if (nullptr != _parentBone)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_parentBone->removeChildBone(this, recursion);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
void Bone::setParentBone(Bone *parent)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_parentBone = parent;
2013-06-06 12:02:54 +08:00
}
Bone *Bone::getParentBone()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _parentBone;
2013-06-06 12:02:54 +08:00
}
void Bone::setChildArmature(Armature *armature)
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
if (_childArmature != armature)
2013-09-13 18:07:37 +08:00
{
if (armature == nullptr && _childArmature)
2013-10-30 09:41:40 +08:00
{
_childArmature->setParentBone(nullptr);
2013-10-30 09:41:40 +08:00
}
2013-09-13 18:07:37 +08:00
CC_SAFE_RETAIN(armature);
2013-09-16 12:21:18 +08:00
CC_SAFE_RELEASE(_childArmature);
_childArmature = armature;
2013-09-13 18:07:37 +08:00
}
}
2013-11-05 19:53:38 +08:00
Armature *Bone::getChildArmature() const
2013-09-13 18:07:37 +08:00
{
2013-09-16 12:21:18 +08:00
return _childArmature;
2013-06-06 12:02:54 +08:00
}
Tween *Bone::getTween()
2013-09-13 18:07:37 +08:00
{
2013-09-16 14:56:59 +08:00
return _tween;
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
void Bone::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
}
AffineTransform Bone::getNodeToArmatureTransform() const
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
return _worldTransform;
2013-06-06 12:02:54 +08:00
}
AffineTransform Bone::getNodeToWorldTransform() const
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
return AffineTransformConcat(_worldTransform, _armature->getNodeToWorldTransform());
2013-06-07 10:52:32 +08:00
}
Node *Bone::getDisplayRenderNode()
2013-06-07 10:52:32 +08:00
{
2013-09-16 12:21:18 +08:00
return _displayManager->getDisplayRenderNode();
2013-06-07 10:52:32 +08:00
}
2013-10-30 09:41:40 +08:00
DisplayType Bone::getDisplayRenderNodeType()
{
return _displayManager->getDisplayRenderNodeType();
}
void Bone::addDisplay(DisplayData *displayData, int index)
2013-06-07 10:52:32 +08:00
{
2013-09-16 12:21:18 +08:00
_displayManager->addDisplay(displayData, index);
2013-06-07 10:52:32 +08:00
}
void Bone::addDisplay(Node *display, int index)
2013-06-07 10:52:32 +08:00
{
2013-09-16 12:21:18 +08:00
_displayManager->addDisplay(display, index);
2013-06-06 12:02:54 +08:00
}
2013-10-30 09:41:40 +08:00
void Bone::removeDisplay(int index)
{
_displayManager->removeDisplay(index);
}
void Bone::changeDisplayByIndex(int index, bool force)
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
_displayManager->changeDisplayByIndex(index, force);
2013-06-06 12:02:54 +08:00
}
Array *Bone::getColliderBodyList()
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:21:18 +08:00
if (DecorativeDisplay *decoDisplay = _displayManager->getCurrentDecorativeDisplay())
2013-09-13 18:07:37 +08:00
{
if (ColliderDetector *detector = decoDisplay->getColliderDetector())
2013-09-13 18:07:37 +08:00
{
return detector->getColliderBodyList();
}
}
return nullptr;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
2013-11-01 14:36:44 +08:00
void Bone::setColliderFilter(ColliderFilter *filter)
{
Array *array = _displayManager->getDecorativeDisplayList();
for(auto object : *array)
2013-11-01 14:36:44 +08:00
{
DecorativeDisplay *decoDisplay = static_cast<DecorativeDisplay *>(object);
if (ColliderDetector *detector = decoDisplay->getColliderDetector())
{
detector->setColliderFilter(filter);
}
}
}
ColliderFilter *Bone::getColliderFilter()
{
if (DecorativeDisplay *decoDisplay = _displayManager->getCurrentDecorativeDisplay())
{
if (ColliderDetector *detector = decoDisplay->getColliderDetector())
{
return detector->getColliderFilter();
}
}
return nullptr;
2013-11-01 14:36:44 +08:00
}
}