diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index aec1518abf..7ce9d7aceb 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -85,8 +85,6 @@ Armature::Armature() , _batchNode(nullptr) , _parentBone(nullptr) , _armatureTransformDirty(true) - , _boneDic(nullptr) - , _topBoneList(nullptr) , _animation(nullptr) { } @@ -94,16 +92,9 @@ Armature::Armature() Armature::~Armature(void) { - if(nullptr != _boneDic) - { - _boneDic->removeAllObjects(); - CC_SAFE_DELETE(_boneDic); - } - if (nullptr != _topBoneList) - { - _topBoneList->removeAllObjects(); - CC_SAFE_DELETE(_topBoneList); - } + _boneDic.clear(); + _topBoneList.clear(); + CC_SAFE_DELETE(_animation); } @@ -125,16 +116,10 @@ bool Armature::init(const char *name) _animation = new ArmatureAnimation(); _animation->init(this); - CC_SAFE_DELETE(_boneDic); - _boneDic = new Dictionary(); - - CC_SAFE_DELETE(_topBoneList); - _topBoneList = new Array(); - _topBoneList->init(); - - _blendFunc.src = CC_BLEND_SRC; - _blendFunc.dst = CC_BLEND_DST; + _boneDic.clear(); + _topBoneList.clear(); + _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; _name = name == nullptr ? "" : name; @@ -155,22 +140,18 @@ bool Armature::init(const char *name) _armatureData = armatureData; - - DictElement *_element = nullptr; - Dictionary *boneDataDic = &armatureData->boneDataDic; - CCDICT_FOREACH(boneDataDic, _element) + for (auto& element : armatureData->boneDataDic) { - Bone *bone = createBone(_element->getStrKey()); + Bone *bone = createBone(element.first.c_str()); //! init bone's Tween to 1st movement's 1st frame do { - MovementData *movData = animationData->getMovement(animationData->movementNames.at(0).c_str()); CC_BREAK_IF(!movData); MovementBoneData *movBoneData = movData->getMovementBoneData(bone->getName().c_str()); - CC_BREAK_IF(!movBoneData || movBoneData->frameList.count() <= 0); + CC_BREAK_IF(!movBoneData || movBoneData->frameList.size() <= 0); FrameData *frameData = movBoneData->getFrameData(0); CC_BREAK_IF(!frameData); @@ -255,28 +236,28 @@ Bone *Armature::createBone(const char *boneName) void Armature::addBone(Bone *bone, const char *parentName) { CCASSERT( bone != nullptr, "Argument must be non-nil"); - CCASSERT(_boneDic->objectForKey(bone->getName()) == nullptr, "bone already added. It can't be added again"); + CCASSERT(_boneDic.at(bone->getName()) == nullptr, "bone already added. It can't be added again"); if (nullptr != parentName) { - Bone *boneParent = (Bone *)_boneDic->objectForKey(parentName); + Bone *boneParent = _boneDic.at(parentName); if (boneParent) { boneParent->addChildBone(bone); } else { - _topBoneList->addObject(bone); + _topBoneList.pushBack(bone); } } else { - _topBoneList->addObject(bone); + _topBoneList.pushBack(bone); } bone->setArmature(this); - _boneDic->setObject(bone, bone->getName()); + _boneDic.insert(bone->getName(), bone); addChild(bone); } @@ -288,18 +269,18 @@ void Armature::removeBone(Bone *bone, bool recursion) bone->setArmature(nullptr); bone->removeFromParent(recursion); - if (_topBoneList->containsObject(bone)) + if (_topBoneList.contains(bone)) { - _topBoneList->removeObject(bone); + _topBoneList.eraseObject(bone); } - _boneDic->removeObjectForKey(bone->getName()); + _boneDic.erase(bone->getName()); removeChild(bone, true); } Bone *Armature::getBone(const char *name) const { - return (Bone *)_boneDic->objectForKey(name); + return _boneDic.at(name); } @@ -315,24 +296,24 @@ void Armature::changeBoneParent(Bone *bone, const char *parentName) if (parentName != nullptr) { - Bone *boneParent = (Bone *)_boneDic->objectForKey(parentName); + Bone *boneParent = _boneDic.at(parentName); if (boneParent) { boneParent->addChildBone(bone); - if (_topBoneList->containsObject(bone)) + if (_topBoneList.contains(bone)) { - _topBoneList->removeObject(bone); + _topBoneList.eraseObject(bone); } } else { - _topBoneList->addObject(bone); + _topBoneList.pushBack(bone); } } } -Dictionary *Armature::getBoneDic() const +const cocos2d::Map& Armature::getBoneDic() const { return _boneDic; } @@ -376,10 +357,9 @@ void Armature::update(float dt) { _animation->update(dt); - for (auto object : *_topBoneList) - { - static_cast(object)->update(dt); - } + std::for_each(_topBoneList.begin(), _topBoneList.end(), [&dt](Bone* bone){ + bone->update(dt); + }); _armatureTransformDirty = false; } @@ -392,7 +372,7 @@ void Armature::draw() } - for (auto object : _children) + for (auto& object : _children) { if (Bone *bone = dynamic_cast(object)) { @@ -408,12 +388,11 @@ void Armature::draw() Skin *skin = static_cast(node); skin->updateTransform(); - BlendType blendType = bone->getBlendType(); + bool blendDirty = bone->isBlendDirty(); - if (blendType != BLEND_NORMAL) + if (blendDirty) { - updateBlendType(blendType); - skin->setBlendFunc(_blendFunc); + skin->setBlendFunc(bone->getBlendFunc()); } skin->draw(); } @@ -440,47 +419,6 @@ void Armature::draw() } -void Armature::updateBlendType(BlendType blendType) -{ - BlendFunc blendFunc; - switch (blendType) - { - case BLEND_NORMAL: - { - blendFunc.src = CC_BLEND_SRC; - blendFunc.dst = CC_BLEND_DST; - } - break; - case BLEND_ADD: - { - blendFunc.src = GL_SRC_ALPHA; - blendFunc.dst = GL_ONE; - } - break; - case BLEND_MULTIPLY: - { - blendFunc.src = GL_DST_COLOR; - blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA; - } - break; - case BLEND_SCREEN: - { - blendFunc.src = GL_ONE; - blendFunc.dst = GL_ONE_MINUS_SRC_COLOR; - } - break; - default: - { - blendFunc.src = CC_BLEND_SRC; - blendFunc.dst = CC_BLEND_DST; - } - break; - } - GL::blendFunc(blendFunc.src, blendFunc.dst); -} - - - void Armature::visit() { // quick return if not visible. children won't be drawn. @@ -518,7 +456,7 @@ Rect Armature::getBoundingBox() const Rect boundingBox = Rect(0, 0, 0, 0); - for(auto object : _children) + for_each(_children.begin(), _children.end(), [&minx, &miny, &maxx, &maxy, &first, &boundingBox](Node *object) { if (Bone *bone = dynamic_cast(object)) { @@ -543,7 +481,8 @@ Rect Armature::getBoundingBox() const boundingBox.setRect(minx, miny, maxx - minx, maxy - miny); } - } + + }); return RectApplyTransform(boundingBox, getNodeToParentTransform()); } @@ -568,12 +507,10 @@ Bone *Armature::getBoneAtPoint(float x, float y) const void Armature::setParentBone(Bone *parentBone) { _parentBone = parentBone; - - DictElement *element = nullptr; - CCDICT_FOREACH(_boneDic, element) + + for (auto& element : _boneDic) { - Bone *bone = static_cast(element->getObject()); - bone->setArmature(this); + element.second->setArmature(this); } } @@ -582,15 +519,50 @@ Bone *Armature::getParentBone() const return _parentBone; } +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT + void CCArmature::setColliderFilter(ColliderFilter *filter) { - DictElement *element = nullptr; - CCDICT_FOREACH(_boneDic, element) + for (auto& element : _boneDic) { - Bone *bone = static_cast(element->getObject()); - bone->setColliderFilter(filter); + element.second->setColliderFilter(filter); } } +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + +void CCArmature::drawContour() +{ + for(auto& element : _boneDic) + { + Bone *bone = element.second; + ColliderDetector *detector = bone->getColliderDetector(); + + if (!detector) + continue; + + const cocos2d::Vector& bodyList = detector->getColliderBodyList(); + + for (auto& object : bodyList) + { + ColliderBody *body = static_cast(object); + const std::vector &vertexList = body->getCalculatedVertexList(); + + unsigned long length = vertexList.size(); + Point *points = new Point[length]; + for (unsigned long i = 0; iSetUserData(this); - for(auto object : *_children) + for(auto& object : _children) { if (Bone *bone = dynamic_cast(object)) { - Array *displayList = bone->getDisplayManager()->getDecorativeDisplayList(); + auto displayList = bone->getDisplayManager()->getDecorativeDisplayList(); for(auto displayObject : displayList) { @@ -654,20 +626,20 @@ void Armature::setBody(cpBody *body) _body = body; _body->data = this; - for(auto object: _children) + for (auto& object : _children) { if (Bone *bone = dynamic_cast(object)) { - Array *displayList = bone->getDisplayManager()->getDecorativeDisplayList(); + auto displayList = bone->getDisplayManager()->getDecorativeDisplayList(); - for(auto displayObject: *displayList) + for_each(displayList.begin(), displayList.end(), [&body](DecorativeDisplay* displayObject) { - ColliderDetector *detector = static_cast(displayObject)->getColliderDetector(); + ColliderDetector *detector = displayObject->getColliderDetector(); if (detector != nullptr) { - detector->setBody(_body); + detector->setBody(body); } - } + }); } } } diff --git a/cocos/editor-support/cocostudio/CCArmature.h b/cocos/editor-support/cocostudio/CCArmature.h index 0a6b76ca98..b16b1d0056 100644 --- a/cocos/editor-support/cocostudio/CCArmature.h +++ b/cocos/editor-support/cocostudio/CCArmature.h @@ -143,7 +143,7 @@ public: * Get Armature's bone dictionary * @return Armature's bone dictionary */ - cocos2d::Dictionary *getBoneDic() const; + const cocos2d::Map& getBoneDic() const; /** * This boundingBox will calculate all bones' boundingBox every time @@ -184,7 +184,12 @@ public: virtual bool getArmatureTransformDirty() const; + +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT virtual void setColliderFilter(ColliderFilter *filter); +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + virtual void drawContour(); +#endif virtual void setArmatureData(ArmatureData *armatureData) { _armatureData = armatureData; } @@ -242,12 +247,6 @@ protected: */ Bone *createBone(const char *boneName ); - /**! Update blend function - * @js NA - * @lua NA - */ - void updateBlendType(BlendType blendType); - protected: ArmatureData *_armatureData; @@ -259,9 +258,9 @@ protected: mutable bool _armatureTransformDirty; - cocos2d::Dictionary *_boneDic; //! The dictionary of the bones, include all bones in the armature, no matter it is the direct bone or the indirect bone. It is different from m_pChindren. + cocos2d::Map _boneDic; //! The dictionary of the bones, include all bones in the armature, no matter it is the direct bone or the indirect bone. It is different from m_pChindren. - cocos2d::Array *_topBoneList; + cocos2d::Vector _topBoneList; cocos2d::BlendFunc _blendFunc; //! It's required for CCTextureProtocol inheritance diff --git a/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp b/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp index 4f8b22f17c..624c2cf6a6 100644 --- a/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp +++ b/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp @@ -54,21 +54,25 @@ ArmatureAnimation::ArmatureAnimation() , _armature(nullptr) , _movementID("") , _toIndex(0) - , _tweenList(nullptr) , _ignoreFrameEvent(false) + , _onMovementList(false) + , _movementListLoop(false) + , _movementListDurationTo(-1) , _userObject(nullptr) , _movementEventCallFunc(nullptr) , _frameEventCallFunc(nullptr) , _movementEventTarget(nullptr) , _frameEventTarget(nullptr) + + , _movementEventListener(nullptr) + , _frameEventListener(nullptr) { } ArmatureAnimation::~ArmatureAnimation(void) { - CC_SAFE_RELEASE_NULL(_tweenList); CC_SAFE_RELEASE_NULL(_animationData); CC_SAFE_RELEASE_NULL(_userObject); @@ -81,8 +85,7 @@ bool ArmatureAnimation::init(Armature *armature) { _armature = armature; - _tweenList = new Array(); - _tweenList->init(); + _tweenList.clear(); bRet = true; } @@ -92,31 +95,31 @@ bool ArmatureAnimation::init(Armature *armature) } -void ArmatureAnimation:: pause() +void ArmatureAnimation::pause() { - for(auto object : *_tweenList) + for_each(_tweenList.begin(), _tweenList.end(), [](Tween *tween) { - static_cast(object)->pause(); - } + tween->pause(); + }); ProcessBase::pause(); } void ArmatureAnimation::resume() { - for(auto object : *_tweenList) + for_each(_tweenList.begin(), _tweenList.end(), [](Tween *tween) { - static_cast(object)->resume(); - } + tween->resume(); + }); ProcessBase::resume(); } void ArmatureAnimation::stop() { - for(auto object : *_tweenList) + for_each(_tweenList.begin(), _tweenList.end(), [](Tween *tween) { - static_cast(object)->stop(); - } - _tweenList->removeAllObjects(); + tween->stop(); + }); + _tweenList.clear(); ProcessBase::stop(); } @@ -142,11 +145,10 @@ void ArmatureAnimation::setSpeedScale(float speedScale) _processScale = !_movementData ? _speedScale : _speedScale * _movementData->scale; - DictElement *element = nullptr; - const Dictionary *dict = _armature->getBoneDic(); - CCDICT_FOREACH(dict, element) + const Map& map = _armature->getBoneDic(); + for(auto& element : map) { - Bone *bone = static_cast(element->getObject()); + Bone *bone = element.second; bone->getTween()->setProcessScale(_processScale); if (bone->getChildArmature()) @@ -161,30 +163,8 @@ float ArmatureAnimation::getSpeedScale() const return _speedScale; } -void ArmatureAnimation::setAnimationInternal(float animationInternal) -{ - if(animationInternal == _animationInternal) - { - return; - } - _animationInternal = animationInternal; - - DictElement *element = nullptr; - const Dictionary *dict = _armature->getBoneDic(); - CCDICT_FOREACH(dict, element) - { - Bone *bone = static_cast(element->getObject()); - bone->getTween()->setAnimationInternal(_animationInternal); - if (bone->getChildArmature()) - { - bone->getChildArmature()->getAnimation()->setAnimationInternal(_animationInternal); - } - } -} - - -void ArmatureAnimation::play(const char *animationName, int durationTo, int durationTween, int loop, int tweenEasing) +void ArmatureAnimation::play(const char *animationName, int durationTo, int loop) { CCASSERT(_animationData, "_animationData can not be null"); @@ -201,12 +181,12 @@ void ArmatureAnimation::play(const char *animationName, int durationTo, int dura //! Further processing parameters durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo; - durationTween = (durationTween == -1) ? _movementData->durationTween : durationTween; - durationTween = (durationTween == 0) ? _movementData->duration : durationTween; + int durationTween = _movementData->durationTween == 0 ? _rawDuration : _movementData->durationTween; - tweenEasing = (tweenEasing == TWEEN_EASING_MAX) ? _movementData->tweenEasing : tweenEasing; + TweenType tweenEasing = _movementData->tweenEasing; loop = (loop < 0) ? _movementData->loop : loop; + _onMovementList = false; ProcessBase::play(durationTo, durationTween, loop, tweenEasing); @@ -224,36 +204,31 @@ void ArmatureAnimation::play(const char *animationName, int durationTo, int dura else { _loopType = ANIMATION_NO_LOOP; - _rawDuration --; } _durationTween = durationTween; } MovementBoneData *movementBoneData = nullptr; - _tweenList->removeAllObjects(); + _tweenList.clear(); - DictElement *element = nullptr; - const Dictionary *dict = _armature->getBoneDic(); - - CCDICT_FOREACH(dict, element) + const Map& map = _armature->getBoneDic(); + for(auto& element : map) { - Bone *bone = static_cast(element->getObject()); - movementBoneData = static_cast(_movementData->movBoneDataDic.objectForKey(bone->getName())); + Bone *bone = element.second; + movementBoneData = static_cast(_movementData->movBoneDataDic.at(bone->getName())); Tween *tween = bone->getTween(); - if(movementBoneData && movementBoneData->frameList.count() > 0) + if(movementBoneData && movementBoneData->frameList.size() > 0) { - _tweenList->addObject(tween); + _tweenList.pushBack(tween); movementBoneData->duration = _movementData->duration; tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing); tween->setProcessScale(_processScale); - tween->setAnimationInternal(_animationInternal); if (bone->getChildArmature()) { bone->getChildArmature()->getAnimation()->setProcessScale(_processScale); - bone->getChildArmature()->getAnimation()->setAnimationInternal(_animationInternal); } } else @@ -272,13 +247,46 @@ void ArmatureAnimation::play(const char *animationName, int durationTo, int dura } -void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, int loop, int tweenEasing) +void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int loop) { std::vector &movName = _animationData->movementNames; CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size())); std::string animationName = movName.at(animationIndex); - play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing); + play(animationName.c_str(), durationTo, loop); +} + + +void ArmatureAnimation::play(const std::vector& movementNames, int durationTo, bool loop) +{ + _movementList.clear(); + _movementListLoop = loop; + _movementListDurationTo = durationTo; + _onMovementList = true; + _movementIndex = 0; + + _movementList = movementNames; + + updateMovementList(); +} + +void ArmatureAnimation::playByIndex(const std::vector& movementIndexes, int durationTo, bool loop) +{ + _movementList.clear(); + _movementListLoop = loop; + _movementListDurationTo = durationTo; + _onMovementList = true; + _movementIndex = 0; + + std::vector &movName = _animationData->movementNames; + + for(auto& index : movementIndexes) + { + std::string name = movName.at(index); + _movementList.push_back(name); + } + + updateMovementList(); } void ArmatureAnimation::gotoAndPlay(int frameIndex) @@ -296,13 +304,14 @@ void ArmatureAnimation::gotoAndPlay(int frameIndex) _isComplete = _isPause = false; ProcessBase::gotoFrame(frameIndex); - _currentPercent = (float)_curFrameIndex / (float)_movementData->duration; + _currentPercent = (float)_curFrameIndex / ((float)_movementData->duration-1); _currentFrame = _nextFrameIndex * _currentPercent; - for(auto object : *_tweenList) + + for_each(_tweenList.begin(), _tweenList.end(), [&frameIndex](Tween* tween) { - static_cast(object)->gotoAndPlay(frameIndex); - } + tween->gotoAndPlay(frameIndex); + }); _armature->update(0); @@ -315,7 +324,7 @@ void ArmatureAnimation::gotoAndPause(int frameIndex) pause(); } -int ArmatureAnimation::getMovementCount() const +long ArmatureAnimation::getMovementCount() const { return _animationData->getMovementCount(); } @@ -324,21 +333,50 @@ void ArmatureAnimation::update(float dt) { ProcessBase::update(dt); - for(auto object : *_tweenList) + for_each(_tweenList.begin(), _tweenList.end(), [&dt](Tween* tween) { - static_cast(object)->update(dt); - } + tween->update(dt); + }); while (_frameEventQueue.size() > 0) { - FrameEvent *frameEvent = _frameEventQueue.front(); + FrameEvent *event = _frameEventQueue.front(); _frameEventQueue.pop(); _ignoreFrameEvent = true; - (_frameEventTarget->*_frameEventCallFunc)(frameEvent->bone, frameEvent->frameEventName, frameEvent->originFrameIndex, frameEvent->currentFrameIndex); + + if(_frameEventTarget) + { + (_frameEventTarget->*_frameEventCallFunc)(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex); + } + + if (_frameEventListener) + { + _frameEventListener(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex); + } + + _ignoreFrameEvent = false; - CC_SAFE_DELETE(frameEvent); + CC_SAFE_DELETE(event); + } + + while (_movementEventQueue.size() > 0) + { + MovementEvent *event = _movementEventQueue.front(); + _movementEventQueue.pop(); + + if(_movementEventTarget) + { + (_movementEventTarget->*_movementEventCallFunc)(event->armature, event->movementType, event->movementID); + } + + if (_movementEventListener) + { + _movementEventListener(event->armature, event->movementType, event->movementID); + } + + CC_SAFE_DELETE(event); } } @@ -361,10 +399,7 @@ void ArmatureAnimation::updateHandler() { _nextFrameIndex = _durationTween; - if (_movementEventTarget && _movementEventCallFunc) - { - (_movementEventTarget->*_movementEventCallFunc)(_armature, START, _movementID.c_str()); - } + movementEvent(_armature, START, _movementID.c_str()); break; } @@ -377,10 +412,9 @@ void ArmatureAnimation::updateHandler() _isComplete = true; _isPlaying = false; - if (_movementEventTarget && _movementEventCallFunc) - { - (_movementEventTarget->*_movementEventCallFunc)(_armature, COMPLETE, _movementID.c_str()); - } + movementEvent(_armature, COMPLETE, _movementID.c_str()); + + updateMovementList(); } break; case ANIMATION_TO_LOOP_FRONT: @@ -390,10 +424,7 @@ void ArmatureAnimation::updateHandler() _currentFrame = _nextFrameIndex == 0 ? 0 : fmodf(_currentFrame, _nextFrameIndex); _nextFrameIndex = _durationTween > 0 ? _durationTween : 1; - if (_movementEventTarget && _movementEventCallFunc) - { - (_movementEventTarget->*_movementEventCallFunc)(_armature, START, _movementID.c_str()); - } + movementEvent(_armature, START, _movementID.c_str()); } break; default: @@ -402,10 +433,7 @@ void ArmatureAnimation::updateHandler() _currentFrame = fmodf(_currentFrame, _nextFrameIndex); _toIndex = 0; - if (_movementEventTarget && _movementEventCallFunc) - { - (_movementEventTarget->*_movementEventCallFunc)(_armature, LOOP_COMPLETE, _movementID.c_str()); - } + movementEvent(_armature, LOOP_COMPLETE, _movementID.c_str()); } break; } @@ -433,6 +461,15 @@ void ArmatureAnimation::setFrameEventCallFunc(Object *target, SEL_FrameEventCall _frameEventCallFunc = callFunc; } +void ArmatureAnimation::setMovementEventCallFunc(std::function listener) +{ + _movementEventListener = listener; +} +void ArmatureAnimation::setFrameEventCallFunc(std::function listener) +{ + _frameEventListener = listener; +} + void ArmatureAnimation::setUserObject(Object *pUserObject) { CC_SAFE_RETAIN(pUserObject); @@ -442,7 +479,7 @@ void ArmatureAnimation::setUserObject(Object *pUserObject) void ArmatureAnimation::frameEvent(Bone *bone, const char *frameEventName, int originFrameIndex, int currentFrameIndex) { - if (_frameEventTarget && _frameEventCallFunc) + if ((_frameEventTarget && _frameEventCallFunc) || _frameEventListener) { FrameEvent *frameEvent = new FrameEvent(); frameEvent->bone = bone; @@ -453,4 +490,50 @@ void ArmatureAnimation::frameEvent(Bone *bone, const char *frameEventName, int o _frameEventQueue.push(frameEvent); } } + + +void ArmatureAnimation::movementEvent(Armature *armature, MovementEventType movementType, const char *movementID) +{ + if ((_movementEventTarget && _movementEventCallFunc) || _movementEventListener) + { + MovementEvent *movementEvent = new MovementEvent(); + movementEvent->armature = armature; + movementEvent->movementType = movementType; + movementEvent->movementID = movementID; + _movementEventQueue.push(movementEvent); + } +} + + +void ArmatureAnimation::updateMovementList() +{ + if (_onMovementList) + { + if (_movementListLoop) + { + play(_movementList.at(_movementIndex).c_str(), _movementListDurationTo, 0); + _movementIndex++; + + if (_movementIndex >= _movementList.size()) + { + _movementIndex = 0; + } + } + else + { + if (_movementIndex < _movementList.size()) + { + play(_movementList.at(_movementIndex).c_str(), _movementListDurationTo, 0); + _movementIndex++; + } + else + { + _onMovementList = false; + } + } + + _onMovementList = true; + } +} + } diff --git a/cocos/editor-support/cocostudio/CCArmatureAnimation.h b/cocos/editor-support/cocostudio/CCArmatureAnimation.h index 9d87a11ab7..25955ecc49 100644 --- a/cocos/editor-support/cocostudio/CCArmatureAnimation.h +++ b/cocos/editor-support/cocostudio/CCArmatureAnimation.h @@ -27,6 +27,7 @@ THE SOFTWARE. #define __CCANIMATION_H__ #include "cocostudio/CCProcessBase.h" +#include "cocostudio/CCTween.h" #include namespace cocostudio { @@ -57,6 +58,13 @@ struct FrameEvent int currentFrameIndex; }; +struct MovementEvent +{ + Armature *armature; + MovementEventType movementType; + const char *movementID; +}; + class ArmatureAnimation : public ProcessBase { public: @@ -98,7 +106,7 @@ public: virtual float getSpeedScale() const; //! The animation update speed - virtual void setAnimationInternal(float animationInternal); + CC_DEPRECATED_ATTRIBUTE virtual void setAnimationInternal(float animationInternal) {} using ProcessBase::play; /** @@ -109,33 +117,23 @@ public: * It's meaning is changing to this animation need how many frames * * -1 : use the value from MovementData get from flash design panel - * @param durationTween The frame count you want to play in the game. - * if _durationTween is 80, then the animation will played 80 frames in a loop - * - * -1 : use the value from MovementData get from flash design panel - * * @param loop Whether the animation is loop * * loop < 0 : use the value from MovementData get from flash design panel * loop = 0 : this animation is not loop * loop > 0 : this animation is loop - * - * @param tweenEasing Tween easing is used for calculate easing effect - * - * TWEEN_EASING_MAX : use the value from MovementData get from flash design panel - * -1 : fade out - * 0 : line - * 1 : fade in - * 2 : fade in and out - * */ - void play(const char *animationName, int durationTo = -1, int durationTween = -1, int loop = -1, int tweenEasing = TWEEN_EASING_MAX); + virtual void play(const char *animationName, int durationTo = -1, int loop = -1); /** * Play animation by index, the other param is the same to play. * @param animationIndex the animation index you want to play */ - void playByIndex(int animationIndex, int durationTo = -1, int durationTween = -1, int loop = -1, int tweenEasing = TWEEN_EASING_MAX); + virtual void playByIndex(int animationIndex, int durationTo = -1, int loop = -1); + + + virtual void play(const std::vector& movementNames, int durationTo = -1, bool loop = true); + virtual void playByIndex(const std::vector& movementIndexes, int durationTo = -1, bool loop = true); /** * Go to specified frame and play current movement. @@ -171,7 +169,7 @@ public: /** * Get movement count */ - int getMovementCount() const; + long getMovementCount() const; void update(float dt); @@ -185,13 +183,16 @@ public: * Set armature's movement event callback function * To disconnect this event, just setMovementEventCallFunc(nullptr, nullptr); */ - void setMovementEventCallFunc(cocos2d::Object *target, SEL_MovementEventCallFunc callFunc); + CC_DEPRECATED_ATTRIBUTE void setMovementEventCallFunc(cocos2d::Object *target, SEL_MovementEventCallFunc callFunc); /** * Set armature's frame event callback function * To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr); */ - void setFrameEventCallFunc(cocos2d::Object *target, SEL_FrameEventCallFunc callFunc); + CC_DEPRECATED_ATTRIBUTE void setFrameEventCallFunc(cocos2d::Object *target, SEL_FrameEventCallFunc callFunc); + + void setMovementEventCallFunc(std::function listener); + void setFrameEventCallFunc(std::function listener); virtual void setAnimationData(AnimationData *data) { @@ -255,6 +256,13 @@ protected: */ void frameEvent(Bone *bone, const char *frameEventName, int originFrameIndex, int currentFrameIndex); + /** + * Emit a movement event + */ + void movementEvent(Armature *armature, MovementEventType movementType, const char *movementID); + + void updateMovementList(); + bool isIgnoreFrameEvent() const { return _ignoreFrameEvent; } friend class Tween; @@ -273,11 +281,19 @@ protected: int _toIndex; //! The frame index in MovementData->m_pMovFrameDataArr, it's different from m_iFrameIndex. - cocos2d::Array *_tweenList; + cocos2d::Vector _tweenList; bool _ignoreFrameEvent; std::queue _frameEventQueue; + std::queue _movementEventQueue; + + std::vector _movementList; + + bool _onMovementList; + bool _movementListLoop; + unsigned int _movementIndex; + int _movementListDurationTo; cocos2d::Object *_userObject; protected: @@ -301,6 +317,10 @@ protected: cocos2d::Object *_movementEventTarget; cocos2d::Object *_frameEventTarget; + + + std::function _movementEventListener; + std::function _frameEventListener; }; } diff --git a/cocos/editor-support/cocostudio/CCArmatureDataManager.cpp b/cocos/editor-support/cocostudio/CCArmatureDataManager.cpp index 57f9bf5cdc..6e6f6bd642 100644 --- a/cocos/editor-support/cocostudio/CCArmatureDataManager.cpp +++ b/cocos/editor-support/cocostudio/CCArmatureDataManager.cpp @@ -56,34 +56,20 @@ void ArmatureDataManager::destoryInstance() ArmatureDataManager::ArmatureDataManager(void) { - _armarureDatas = nullptr; - _animationDatas = nullptr; - _textureDatas = nullptr; + _armarureDatas.clear(); + _animationDatas.clear(); + _textureDatas.clear(); _autoLoadSpriteFile = false; } ArmatureDataManager::~ArmatureDataManager(void) { - if( _animationDatas ) - { - _animationDatas->removeAllObjects(); - } - if( _armarureDatas ) - { - _armarureDatas->removeAllObjects(); - } - - if( _textureDatas ) - { - _textureDatas->removeAllObjects(); - } + _animationDatas.clear(); + _armarureDatas.clear(); + _textureDatas.clear(); _relativeDatas.clear(); - - CC_SAFE_DELETE(_animationDatas); - CC_SAFE_DELETE(_armarureDatas); - CC_SAFE_DELETE(_textureDatas); } @@ -92,17 +78,9 @@ bool ArmatureDataManager::init() bool bRet = false; do { - _armarureDatas = Dictionary::create(); - CCASSERT(_armarureDatas, "create ArmatureDataManager::_armarureDatas fail!"); - _armarureDatas->retain(); - - _animationDatas = Dictionary::create(); - CCASSERT(_animationDatas, "create ArmatureDataManager::_animationDatas fail!"); - _animationDatas->retain(); - - _textureDatas = Dictionary::create(); - CCASSERT(_textureDatas, "create ArmatureDataManager::_textureDatas fail!"); - _textureDatas->retain(); + _armarureDatas.clear(); + _animationDatas.clear(); + _textureDatas.clear(); bRet = true; } @@ -143,97 +121,70 @@ void ArmatureDataManager::removeArmatureFileInfo(const char *configFilePath) void ArmatureDataManager::addArmatureData(const char *id, ArmatureData *armatureData, const char *configFilePath) { - if(_armarureDatas) + if (RelativeData *data = getRelativeData(configFilePath)) { - if (RelativeData *data = getRelativeData(configFilePath)) - { - data->armatures.push_back(id); - } - - _armarureDatas->setObject(armatureData, id); + data->armatures.push_back(id); } + + _armarureDatas.insert(id, armatureData); } ArmatureData *ArmatureDataManager::getArmatureData(const char *id) { ArmatureData *armatureData = nullptr; - if (_armarureDatas) - { - armatureData = (ArmatureData *)_armarureDatas->objectForKey(id); - } + armatureData = (ArmatureData *)_armarureDatas.at(id); return armatureData; } void ArmatureDataManager::removeArmatureData(const char *id) { - if (_armarureDatas) - { - _armarureDatas->removeObjectForKey(id); - } + _armarureDatas.erase(id); } void ArmatureDataManager::addAnimationData(const char *id, AnimationData *animationData, const char *configFilePath) { - if(_animationDatas) + if (RelativeData *data = getRelativeData(configFilePath)) { - if (RelativeData *data = getRelativeData(configFilePath)) - { - data->animations.push_back(id); - } - - _animationDatas->setObject(animationData, id); + data->animations.push_back(id); } + + _animationDatas.insert(id, animationData); } AnimationData *ArmatureDataManager::getAnimationData(const char *id) { AnimationData *animationData = nullptr; - if (_animationDatas) - { - animationData = (AnimationData *)_animationDatas->objectForKey(id); - } + animationData = (AnimationData *)_animationDatas.at(id); return animationData; } void ArmatureDataManager::removeAnimationData(const char *id) { - if (_animationDatas) - { - _animationDatas->removeObjectForKey(id); - } + _animationDatas.erase(id); } void ArmatureDataManager::addTextureData(const char *id, TextureData *textureData, const char *configFilePath) { - if(_textureDatas) + if (RelativeData *data = getRelativeData(configFilePath)) { - if (RelativeData *data = getRelativeData(configFilePath)) - { - data->textures.push_back(id); - } - - _textureDatas->setObject(textureData, id); + data->textures.push_back(id); } + + _textureDatas.insert(id, textureData); } TextureData *ArmatureDataManager::getTextureData(const char *id) { TextureData *textureData = nullptr; - if (_textureDatas) - { - textureData = (TextureData *)_textureDatas->objectForKey(id); - } + textureData = (TextureData *)_textureDatas.at(id); return textureData; } void ArmatureDataManager::removeTextureData(const char *id) { - if(_textureDatas) - { - _textureDatas->removeObjectForKey(id); - } + _textureDatas.erase(id); } void ArmatureDataManager::addArmatureFileInfo(const char *configFilePath) @@ -285,15 +236,15 @@ bool ArmatureDataManager::isAutoLoadSpriteFile() return _autoLoadSpriteFile; } -Dictionary *ArmatureDataManager::getArmatureDatas() const +const cocos2d::Map& ArmatureDataManager::getArmatureDatas() const { return _armarureDatas; } -Dictionary *ArmatureDataManager::getAnimationDatas() const +const cocos2d::Map& ArmatureDataManager::getAnimationDatas() const { return _animationDatas; } -Dictionary *ArmatureDataManager::getTextureDatas() const +const cocos2d::Map& ArmatureDataManager::getTextureDatas() const { return _textureDatas; } diff --git a/cocos/editor-support/cocostudio/CCArmatureDataManager.h b/cocos/editor-support/cocostudio/CCArmatureDataManager.h index b73dc99ef9..e684e615ff 100644 --- a/cocos/editor-support/cocostudio/CCArmatureDataManager.h +++ b/cocos/editor-support/cocostudio/CCArmatureDataManager.h @@ -168,9 +168,9 @@ public: bool isAutoLoadSpriteFile(); - cocos2d::Dictionary *getArmatureDatas() const; - cocos2d::Dictionary *getAnimationDatas() const; - cocos2d::Dictionary *getTextureDatas() const; + const cocos2d::Map& getArmatureDatas() const; + const cocos2d::Map& getAnimationDatas() const; + const cocos2d::Map& getTextureDatas() const; protected: void addRelativeData(const char* configFilePath); @@ -181,21 +181,21 @@ private: * @key std::string * @value ArmatureData * */ - cocos2d::Dictionary *_armarureDatas; + cocos2d::Map _armarureDatas; /** * @brief save animation datas * @key std::string * @value AnimationData * */ - cocos2d::Dictionary *_animationDatas; + cocos2d::Map _animationDatas; /** * @brief save texture datas * @key std::string * @value TextureData * */ - cocos2d::Dictionary *_textureDatas; + cocos2d::Map _textureDatas; bool _autoLoadSpriteFile; diff --git a/cocos/editor-support/cocostudio/CCArmatureDefine.cpp b/cocos/editor-support/cocostudio/CCArmatureDefine.cpp index 4f48e0024e..3873a918ba 100644 --- a/cocos/editor-support/cocostudio/CCArmatureDefine.cpp +++ b/cocos/editor-support/cocostudio/CCArmatureDefine.cpp @@ -28,7 +28,7 @@ namespace cocostudio { const char *armatureVersion() { - return "1.0.0.0"; + return "1.1.0.0"; } } diff --git a/cocos/editor-support/cocostudio/CCArmatureDefine.h b/cocos/editor-support/cocostudio/CCArmatureDefine.h index c79b58ff81..e2c8894cf8 100644 --- a/cocos/editor-support/cocostudio/CCArmatureDefine.h +++ b/cocos/editor-support/cocostudio/CCArmatureDefine.h @@ -42,11 +42,11 @@ THE SOFTWARE. #endif #ifndef ENABLE_PHYSICS_CHIPMUNK_DETECT -#define ENABLE_PHYSICS_CHIPMUNK_DETECT 1 +#define ENABLE_PHYSICS_CHIPMUNK_DETECT 0 #endif #ifndef ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX -#define ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX 0 +#define ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX 1 #endif #define CS_RETURN_IF(cond) if (cond) return diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index 12d627ce45..1aadd0893c 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -74,7 +74,8 @@ Bone::Bone() // _worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0); kmMat4Identity(&_worldTransform); _boneTransformDirty = true; - _blendType = BLEND_NORMAL; + _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; + _blendDirty = false; _worldInfo = nullptr; _armatureParentBone = nullptr; @@ -228,7 +229,8 @@ void Bone::update(float delta) DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty()); - std::for_each(_children.begin(), _children.end(), [&delta](Node* obj){ + std::for_each(_children.begin(), _children.end(), [&delta](Node* obj) + { Bone *childBone = static_cast(obj); childBone->update(delta); }); @@ -249,6 +251,15 @@ void Bone::applyParentTransform(Bone *parent) } +void CCBone::setBlendFunc(const BlendFunc& blendFunc) +{ + if (_blendFunc.src != blendFunc.src && _blendFunc.dst != blendFunc.dst) + { + _blendFunc = blendFunc; + _blendDirty = true; + } +} + void Bone::updateDisplayedColor(const Color3B &parentColor) { _realColor = Color3B(255, 255, 255); @@ -429,25 +440,31 @@ void Bone::changeDisplayByIndex(int index, bool force) _displayManager->changeDisplayByIndex(index, force); } -Array *Bone::getColliderBodyList() + +void Bone::changeDisplayByName(const char *name, bool force) +{ + _displayManager->changeDisplayByName(name, force); +} + +ColliderDetector* Bone::getColliderDetector() const { if (DecorativeDisplay *decoDisplay = _displayManager->getCurrentDecorativeDisplay()) { if (ColliderDetector *detector = decoDisplay->getColliderDetector()) { - return detector->getColliderBodyList(); + return detector; } } return nullptr; } - +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT void Bone::setColliderFilter(ColliderFilter *filter) { - Array *array = _displayManager->getDecorativeDisplayList(); + auto array = _displayManager->getDecorativeDisplayList(); - for(auto object : *array) + for(auto& object : array) { DecorativeDisplay *decoDisplay = static_cast(object); if (ColliderDetector *detector = decoDisplay->getColliderDetector()) @@ -467,6 +484,6 @@ ColliderFilter *Bone::getColliderFilter() } return nullptr; } - +#endif } diff --git a/cocos/editor-support/cocostudio/CCBone.h b/cocos/editor-support/cocostudio/CCBone.h index 0c7017deb4..aee8cb235d 100644 --- a/cocos/editor-support/cocostudio/CCBone.h +++ b/cocos/editor-support/cocostudio/CCBone.h @@ -92,6 +92,7 @@ public: void removeDisplay(int index); void changeDisplayByIndex(int index, bool force); + void changeDisplayByName(const char *name, bool force); /** * Add a child to this bone, and it will let this child call setParent(Bone *parent) function to set self to it's parent @@ -161,10 +162,12 @@ public: /* * Get the ColliderBody list in this bone. The object in the Array is ColliderBody. */ - virtual cocos2d::Array *getColliderBodyList(); + virtual ColliderDetector* getColliderDetector() const; +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT virtual void setColliderFilter(ColliderFilter *filter); virtual ColliderFilter *getColliderFilter(); +#endif virtual void setBoneData(BoneData *boneData); virtual BoneData *getBoneData() const; @@ -188,8 +191,18 @@ public: */ CC_DEPRECATED_ATTRIBUTE virtual bool getIgnoreMovementBoneData() const { return isIgnoreMovementBoneData(); } - virtual void setBlendType(BlendType type) { _blendType = type; } - virtual BlendType getBlendType() const { return _blendType; } + + /* + * Set blend function + */ + virtual void setBlendFunc(const cocos2d::BlendFunc& blendFunc); + virtual cocos2d::BlendFunc getBlendFunc(void) { return _blendFunc; } + + /* + * Set if blend function is dirty + */ + virtual void setBlendDirty(bool dirty) { _blendDirty = dirty; } + virtual bool isBlendDirty(void) { return _blendDirty; } virtual FrameData *getTweenData() const { return _tweenData; } @@ -220,7 +233,8 @@ protected: */ bool _ignoreMovementBoneData; - BlendType _blendType; + cocos2d::BlendFunc _blendFunc; + bool _blendDirty; Tween *_tween; //! Calculate tween effect diff --git a/cocos/editor-support/cocostudio/CCColliderDetector.cpp b/cocos/editor-support/cocostudio/CCColliderDetector.cpp index 74cf64ecbf..d07397e65b 100644 --- a/cocos/editor-support/cocostudio/CCColliderDetector.cpp +++ b/cocos/editor-support/cocostudio/CCColliderDetector.cpp @@ -26,11 +26,7 @@ THE SOFTWARE. #include "cocostudio/CCBone.h" #include "cocostudio/CCTransformHelp.h" -#if ENABLE_PHYSICS_BOX2D_DETECT -#include "Box2D/Box2D.h" -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT -#include "chipmunk.h" -#endif + using namespace cocos2d; @@ -38,7 +34,7 @@ namespace cocostudio { #if ENABLE_PHYSICS_BOX2D_DETECT -ColliderFilter::ColliderFilter(unsigned short categoryBits, unsigned short maskBits, signed short groupIndex) +ColliderFilter::ColliderFilter(uint16 categoryBits, uint16 maskBits, int16 groupIndex) : _categoryBits(categoryBits) , _maskBits(maskBits) , _groupIndex(groupIndex) @@ -56,7 +52,7 @@ void ColliderFilter::updateShape(b2Fixture *fixture) } #elif ENABLE_PHYSICS_CHIPMUNK_DETECT -ColliderFilter::ColliderFilter(uintptr_t collisionType, uintptr_t group) +ColliderFilter::ColliderFilter(cpCollisionType collisionType, cpGroup group) : _collisionType(collisionType) , _group(group) { @@ -95,18 +91,24 @@ ColliderBody::ColliderBody(ContourData *contourData) CC_SAFE_RETAIN(_calculatedVertexList); #endif } +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX +ColliderBody::ColliderBody(ContourData *contourData) + : _contourData(contourData) +{ + CC_SAFE_RETAIN(_contourData); +} #endif ColliderBody::~ColliderBody() { CC_SAFE_RELEASE(_contourData); - CC_SAFE_DELETE(_filter); -#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - CC_SAFE_RELEASE(_calculatedVertexList); +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT + CC_SAFE_DELETE(_filter); #endif } +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT void ColliderBody::setColliderFilter(ColliderFilter *filter) { *_filter = *filter; @@ -115,6 +117,7 @@ ColliderFilter *ColliderBody::getColliderFilter() { return _filter; } +#endif @@ -143,27 +146,30 @@ ColliderDetector *ColliderDetector::create(Bone *bone) } ColliderDetector::ColliderDetector() - : _colliderBodyList(nullptr) - , _filter(nullptr) - , _active(false) + : _active(false) { +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT _body = nullptr; + _filter = nullptr; +#endif } ColliderDetector::~ColliderDetector() { - _colliderBodyList->removeAllObjects(); - CC_SAFE_DELETE(_colliderBodyList); + _colliderBodyList.clear(); + +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT CC_SAFE_DELETE(_filter); +#endif } bool ColliderDetector::init() { - _colliderBodyList = Array::create(); - CCASSERT(_colliderBodyList, "create _colliderBodyList failed!"); - _colliderBodyList->retain(); + _colliderBodyList.clear(); +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT _filter = new ColliderFilter(); +#endif return true; } @@ -179,46 +185,50 @@ bool ColliderDetector::init(Bone *bone) void ColliderDetector::addContourData(ContourData *contourData) { ColliderBody *colliderBody = new ColliderBody(contourData); - _colliderBodyList->addObject(colliderBody); + _colliderBodyList.pushBack(colliderBody); colliderBody->release(); #if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - CCArray *calculatedVertexList = colliderBody->getCalculatedVertexList(); + std::vector &calculatedVertexList = colliderBody->_calculatedVertexList; - int num = contourData->vertexList.size(); - for (int i = 0; i < num; i++) + unsigned long num = contourData->vertexList.size(); + for (unsigned long i = 0; i < num; i++) { - ContourVertex2 *newVertex = new ContourVertex2(0, 0); - calculatedVertexList->addObject(newVertex); - newVertex->release(); + calculatedVertexList.push_back(Point()); } #endif } -void ColliderDetector::addContourDataList(Array *contourDataList) +void ColliderDetector::addContourDataList(cocos2d::Vector &contourDataList) { - for(auto object : *contourDataList) + for_each(contourDataList.begin(), contourDataList.end(), [this](ContourData *contourData) { - addContourData((ContourData *)object); - } + this->addContourData(contourData); + }); } void ColliderDetector::removeContourData(ContourData *contourData) { - for(auto object : *_colliderBodyList) - { - ColliderBody *body = (ColliderBody*)object; + std::vector eraseList; + + for_each(_colliderBodyList.begin(), _colliderBodyList.end(), [&contourData, this, &eraseList](ColliderBody *body) + { if (body && body->getContourData() == contourData) { - _colliderBodyList->removeObject(body); + eraseList.push_back(body); } - } + }); + + for_each(eraseList.begin(), eraseList.end(), [this](ColliderBody *body) + { + this->_colliderBodyList.eraseObject(body); + }); } void ColliderDetector::removeAll() { - _colliderBodyList->removeAllObjects(); + _colliderBodyList.clear(); } @@ -240,7 +250,7 @@ void ColliderDetector::setActive(bool active) } else { - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; b2Fixture *fixture = colliderBody->getB2Fixture(); @@ -255,7 +265,7 @@ void ColliderDetector::setActive(bool active) { if (_active) { - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; cpShape *shape = colliderBody->getShape(); @@ -267,7 +277,7 @@ void ColliderDetector::setActive(bool active) } else { - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; cpShape *shape = colliderBody->getShape(); @@ -286,16 +296,18 @@ bool ColliderDetector::getActive() return _active; } -Array *ColliderDetector::getColliderBodyList() +const cocos2d::Vector& ColliderDetector::getColliderBodyList() { return _colliderBodyList; } + +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT void ColliderDetector::setColliderFilter(ColliderFilter *filter) { *_filter = *filter; - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; colliderBody->setColliderFilter(filter); @@ -317,6 +329,7 @@ ColliderFilter *ColliderDetector::getColliderFilter() { return _filter; } +#endif Point helpPoint; @@ -328,7 +341,7 @@ void ColliderDetector::updateTransform(kmMat4 &t) return; } - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; ContourData *contourData = colliderBody->getContourData(); @@ -347,22 +360,22 @@ void ColliderDetector::updateTransform(kmMat4 &t) } #endif - int num = contourData->vertexList.count(); - ContourVertex2 **vs = (ContourVertex2 **)contourData->vertexList.data->arr; + unsigned long num = contourData->vertexList.size(); + std::vector &vs = contourData->vertexList; #if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - ContourVertex2 **cvs = (ContourVertex2 **)colliderBody->getCalculatedVertexList()->data->arr; + std::vector &cvs = colliderBody->_calculatedVertexList; #endif - for (int i = 0; i < num; i++) + for (unsigned long i = 0; i < num; i++) { - helpPoint.setPoint( vs[i]->x, vs[i]->y); + helpPoint.setPoint( vs.at(i).x, vs.at(i).y); helpPoint = PointApplyTransform(helpPoint, t); #if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - cvs[i]->x = helpPoint.x; - cvs[i]->y = helpPoint.y; + cvs.at(i).x = helpPoint.x; + cvs.at(i).y = helpPoint.y; #endif #if ENABLE_PHYSICS_BOX2D_DETECT @@ -383,8 +396,8 @@ void ColliderDetector::updateTransform(kmMat4 &t) } #if ENABLE_PHYSICS_CHIPMUNK_DETECT - cpConvexHull(num, shape->verts, nullptr, nullptr, 0); - for (int i = 0; i < num; i++) + cpConvexHull((int)num, shape->verts, nullptr, nullptr, 0); + for (unsigned long i = 0; i < num; i++) { cpVect b = shape->verts[(i + 1) % shape->numVerts]; cpVect n = cpvnormalize(cpvperp(cpvsub(b, shape->verts[i]))); @@ -402,26 +415,23 @@ void ColliderDetector::setBody(b2Body *pBody) { _body = pBody; - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; ContourData *contourData = colliderBody->getContourData(); - const Array *array = &contourData->vertexList; - Object *object = nullptr; - + b2Vec2 *b2bv = new b2Vec2[contourData->vertexList.size()]; int i = 0; - for(auto object : *array) + for(auto& v : contourData->vertexList) { - ContourVertex2 *v = (ContourVertex2 *)object; - b2bv[i].Set(v->x / PT_RATIO, v->y / PT_RATIO); + b2bv[i].Set(v.x / PT_RATIO, v.y / PT_RATIO); i++; } b2PolygonShape polygon; - polygon.Set(b2bv, contourData->vertexList.size()); + polygon.Set(b2bv, (int)contourData->vertexList.size()); CC_SAFE_DELETE(b2bv); @@ -452,22 +462,22 @@ void ColliderDetector::setBody(cpBody *pBody) { _body = pBody; - for(auto object : *_colliderBodyList) + for(auto& object : _colliderBodyList) { ColliderBody *colliderBody = (ColliderBody *)object; ContourData *contourData = colliderBody->getContourData(); - int num = contourData->vertexList.count(); - ContourVertex2 **vs = (ContourVertex2 **)contourData->vertexList.data->arr; + ssize_t num = contourData->vertexList.size(); + auto vs = contourData->vertexList; cpVect *verts = new cpVect[num]; for (int i = 0; i < num; i++) { - verts[num - 1 - i].x = vs[i]->x; - verts[num - 1 - i].y = vs[i]->y; + verts[num - 1 - i].x = vs.at(i).x; + verts[num - 1 - i].y = vs.at(i).y; } - cpShape *shape = cpPolyShapeNew(_body, num, verts, cpvzero); + cpShape *shape = cpPolyShapeNew(_body, (int)num, verts, cpvzero); shape->sensor = true; shape->data = _bone; diff --git a/cocos/editor-support/cocostudio/CCColliderDetector.h b/cocos/editor-support/cocostudio/CCColliderDetector.h index 10a5e9ae04..f7c4e73d2a 100644 --- a/cocos/editor-support/cocostudio/CCColliderDetector.h +++ b/cocos/editor-support/cocostudio/CCColliderDetector.h @@ -34,14 +34,12 @@ THE SOFTWARE. #if ENABLE_PHYSICS_CHIPMUNK_DETECT -struct cpBody; -struct cpShape; +#include "chipmunk.h" #elif ENABLE_PHYSICS_BOX2D_DETECT -class b2Body; -class b2Fixture; -struct b2Filter; +#include "Box2D/Box2D.h" #endif + namespace cocostudio { class Bone; @@ -56,34 +54,34 @@ public: virtual ~ColliderFilter() { } #if ENABLE_PHYSICS_BOX2D_DETECT public: - ColliderFilter(unsigned short categoryBits = 0x0001, unsigned short maskBits = 0xFFFF, signed short groupIndex = 0); + ColliderFilter(uint16 categoryBits = 0x0001, uint16 maskBits = 0xFFFF, int16 groupIndex = 0); void updateShape(b2Fixture *fixture); - virtual void setCategoryBits(unsigned short categoryBits) { _categoryBits = categoryBits; } - virtual unsigned short getCategoryBits() const { return _categoryBits; } + virtual void setCategoryBits(uint16 categoryBits) { _categoryBits = categoryBits; } + virtual uint16 getCategoryBits() const { return _categoryBits; } - virtual void setMaskBits(unsigned short maskBits) { _maskBits = maskBits; } - virtual unsigned short getMaskBits() const { return _maskBits; } + virtual void setMaskBits(uint16 maskBits) { _maskBits = maskBits; } + virtual uint16 getMaskBits() const { return _maskBits; } - virtual void setGroupIndex(signed short groupIndex) { _groupIndex = groupIndex; } - virtual signed short getGroupIndex() const { return _groupIndex; } + virtual void setGroupIndex(int16 groupIndex) { _groupIndex = groupIndex; } + virtual int16 getGroupIndex() const { return _groupIndex; } protected: - unsigned short _categoryBits; - unsigned short _maskBits; - signed short _groupIndex; + uint16 _categoryBits; + uint16 _maskBits; + int16 _groupIndex; #elif ENABLE_PHYSICS_CHIPMUNK_DETECT public: - ColliderFilter(uintptr_t collisionType = 0, uintptr_t group = 0); + ColliderFilter(cpCollisionType collisionType = 0, cpGroup group = 0); void updateShape(cpShape *shape); - virtual void setCollisionType(uintptr_t collisionType) { _collisionType = collisionType; } - virtual uintptr_t getCollisionType() const { return _collisionType; } + virtual void setCollisionType(cpCollisionType collisionType) { _collisionType = collisionType; } + virtual cpCollisionType getCollisionType() const { return _collisionType; } - virtual void setGroup(uintptr_t group) { _group = group; } - virtual uintptr_t getGroup() const { return _group; } + virtual void setGroup(cpGroup group) { _group = group; } + virtual cpGroup getGroup() const { return _group; } protected: - uintptr_t _collisionType; - uintptr_t _group; + cpCollisionType _collisionType; + cpGroup _group; #endif }; @@ -95,8 +93,10 @@ public: inline ContourData *getContourData() { return _contourData; } +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT void setColliderFilter(ColliderFilter *filter); ColliderFilter *getColliderFilter(); +#endif #if ENABLE_PHYSICS_BOX2D_DETECT virtual void setB2Fixture(b2Fixture *fixture) { _fixture = fixture; } @@ -104,25 +104,25 @@ public: #elif ENABLE_PHYSICS_CHIPMUNK_DETECT virtual void setShape(cpShape *shape) { _shape = shape; } virtual cpShape *getShape() const { return _shape; } +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + virtual const std::vector &getCalculatedVertexList() const { return _calculatedVertexList; } #endif -#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - virtual const cocos2d::Array *getCalculatedVertexList() const { return _calculatedVertexList; } -#endif private: #if ENABLE_PHYSICS_BOX2D_DETECT b2Fixture *_fixture; + ColliderFilter *_filter; #elif ENABLE_PHYSICS_CHIPMUNK_DETECT cpShape *_shape; + ColliderFilter *_filter; +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + std::vector _calculatedVertexList; #endif ContourData *_contourData; - ColliderFilter *_filter; -#if ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX - cocos2d::Array *_calculatedVertexList; -#endif + friend class ColliderDetector; }; /* @@ -150,7 +150,7 @@ public: virtual bool init(Bone *bone); void addContourData(ContourData *contourData); - void addContourDataList(cocos2d::Array *contourDataList); + void addContourDataList(cocos2d::Vector &contourDataList); void removeContourData(ContourData *contourData); void removeAll(); @@ -160,10 +160,12 @@ public: void setActive(bool active); bool getActive(); - cocos2d::Array *getColliderBodyList(); + const cocos2d::Vector& getColliderBodyList(); +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT virtual void setColliderFilter(ColliderFilter *filter); virtual ColliderFilter *getColliderFilter(); +#endif virtual void setBone(Bone *bone) { _bone = bone; } virtual Bone *getBone() const { return _bone; } @@ -176,15 +178,16 @@ public: virtual cpBody *getBody() const; #endif protected: - cocos2d::Array *_colliderBodyList; - ColliderFilter *_filter; + cocos2d::Vector _colliderBodyList; Bone *_bone; #if ENABLE_PHYSICS_BOX2D_DETECT b2Body *_body; + ColliderFilter *_filter; #elif ENABLE_PHYSICS_CHIPMUNK_DETECT cpBody *_body; + ColliderFilter *_filter; #endif protected: diff --git a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp index b462ef72de..5a80d77b24 100644 --- a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp +++ b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp @@ -73,6 +73,8 @@ static const char *A_EVENT = "evt"; static const char *A_SOUND = "sd"; static const char *A_SOUND_EFFECT = "sdE"; static const char *A_TWEEN_EASING = "twE"; +//static const char *A_EASING_PARAM_NUMBER = "twEPN"; +static const char *A_EASING_PARAM = "twEP"; //static const char *A_TWEEN_ROTATE = "twR"; static const char *A_IS_ARMATURE = "isArmature"; static const char *A_DISPLAY_TYPE = "displayType"; @@ -93,6 +95,8 @@ static const char *A_COCOS2D_PIVOT_X = "cocos2d_pX"; static const char *A_COCOS2D_PIVOT_Y = "cocos2d_pY"; static const char *A_BLEND_TYPE = "bd"; +static const char *A_BLEND_SRC = "bd_src"; +static const char *A_BLEND_DST = "bd_dst"; static const char *A_ALPHA = "a"; static const char *A_RED = "r"; @@ -820,8 +824,8 @@ MovementBoneData *DataReaderHelper::decodeMovementBone(tinyxml2::XMLElement *mov } } - int length = 0; - int index = 0; + unsigned long length = 0; + unsigned long index = 0; int parentTotalDuration = 0; int currentDuration = 0; @@ -882,24 +886,24 @@ MovementBoneData *DataReaderHelper::decodeMovementBone(tinyxml2::XMLElement *mov frameXML = frameXML->NextSiblingElement(FRAME); } - + //! Change rotation range from (-180 -- 180) to (-infinity -- infinity) - FrameData **frames = (FrameData **)movBoneData->frameList.data->arr; - for (int j = movBoneData->frameList.count() - 1; j >= 0; j--) + auto frames = movBoneData->frameList; + for (long j = movBoneData->frameList.size() - 1; j >= 0; j--) { if (j > 0) { - float difSkewX = frames[j]->skewX - frames[j - 1]->skewX; - float difSkewY = frames[j]->skewY - frames[j - 1]->skewY; + float difSkewX = frames.at(j)->skewX - frames.at(j-1)->skewX; + float difSkewY = frames.at(j)->skewY - frames.at(j-1)->skewY; if (difSkewX < -M_PI || difSkewX > M_PI) { - frames[j - 1]->skewX = difSkewX < 0 ? frames[j - 1]->skewX - 2 * M_PI : frames[j - 1]->skewX + 2 * M_PI; + frames.at(j-1)->skewX = difSkewX < 0 ? frames.at(j-1)->skewX - 2 * M_PI : frames.at(j-1)->skewX + 2 * M_PI; } if (difSkewY < -M_PI || difSkewY > M_PI) { - frames[j - 1]->skewY = difSkewY < 0 ? frames[j - 1]->skewY - 2 * M_PI : frames[j - 1]->skewY + 2 * M_PI; + frames.at(j-1)->skewY = difSkewY < 0 ? frames.at(j-1)->skewY - 2 * M_PI : frames.at(j-1)->skewY + 2 * M_PI; } } } @@ -907,7 +911,7 @@ MovementBoneData *DataReaderHelper::decodeMovementBone(tinyxml2::XMLElement *mov // FrameData *frameData = new FrameData(); - frameData->copy((FrameData *)movBoneData->frameList.getLastObject()); + frameData->copy((FrameData *)movBoneData->frameList.back()); frameData->frameID = movBoneData->duration; movBoneData->addFrameData(frameData); frameData->release(); @@ -939,6 +943,11 @@ FrameData *DataReaderHelper::decodeFrame(tinyxml2::XMLElement *frameXML, tinyxm frameData->strSoundEffect = frameXML->Attribute(A_SOUND_EFFECT); } + bool tweenFrame = false; + if (frameXML->QueryBoolAttribute(A_TWEEN_FRAME, &tweenFrame) == tinyxml2::XML_SUCCESS) + { + frameData->isTween = tweenFrame; + } if (dataInfo->flashToolVersion >= VERSION_2_0) @@ -998,7 +1007,38 @@ FrameData *DataReaderHelper::decodeFrame(tinyxml2::XMLElement *frameXML, tinyxm } if ( frameXML->QueryIntAttribute(A_BLEND_TYPE, &blendType) == tinyxml2::XML_SUCCESS ) { - frameData->blendType = (BlendType)blendType; + switch (blendType) + { + case BLEND_NORMAL: + { + frameData->blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; + } + break; + case BLEND_ADD: + { + frameData->blendFunc.src = GL_SRC_ALPHA; + frameData->blendFunc.dst = GL_ONE; + } + break; + case BLEND_MULTIPLY: + { + frameData->blendFunc.src = GL_DST_COLOR; + frameData->blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA; + } + break; + case BLEND_SCREEN: + { + frameData->blendFunc.src = GL_ONE; + frameData->blendFunc.dst = GL_ONE_MINUS_SRC_COLOR; + } + break; + default: + { + frameData->blendFunc.src = CC_BLEND_SRC; + frameData->blendFunc.dst = CC_BLEND_DST; + } + break; + } } tinyxml2::XMLElement *colorTransformXML = frameXML->FirstChildElement(A_COLOR_TRANSFORM); @@ -1128,14 +1168,13 @@ ContourData *DataReaderHelper::decodeContour(tinyxml2::XMLElement *contourXML, D while (vertexDataXML) { - ContourVertex2 *vertex = new ContourVertex2(0, 0); - vertex->release(); + Point vertex; - vertexDataXML->QueryFloatAttribute(A_X, &vertex->x); - vertexDataXML->QueryFloatAttribute(A_Y, &vertex->y); + vertexDataXML->QueryFloatAttribute(A_X, &vertex.x); + vertexDataXML->QueryFloatAttribute(A_Y, &vertex.y); - vertex->y = -vertex->y; - contourData->vertexList.addObject(vertex); + vertex.y = -vertex.y; + contourData->vertexList.push_back(vertex); vertexDataXML = vertexDataXML->NextSiblingElement(CONTOUR_VERTEX); } @@ -1362,11 +1401,11 @@ DisplayData *DataReaderHelper::decodeBoneDisplay(JsonDictionary &json, DataInfo { if (dataInfo->asyncStruct) { - static_cast(displayData)->plist = dataInfo->asyncStruct->baseFilePath + plist; + static_cast(displayData)->displayName = dataInfo->asyncStruct->baseFilePath + plist; } else { - static_cast(displayData)->plist = dataInfo->baseFilePath + plist; + static_cast(displayData)->displayName = dataInfo->baseFilePath + plist; } } } @@ -1474,22 +1513,22 @@ MovementBoneData *DataReaderHelper::decodeMovementBone(JsonDictionary &json, Dat if (dataInfo->cocoStudioVersion < VERSION_CHANGE_ROTATION_RANGE) { //! Change rotation range from (-180 -- 180) to (-infinity -- infinity) - FrameData **frames = (FrameData **)movementBoneData->frameList.data->arr; - for (int i = movementBoneData->frameList.count() - 1; i >= 0; i--) + auto frames = movementBoneData->frameList; + for (long i = frames.size() - 1; i >= 0; i--) { if (i > 0) { - float difSkewX = frames[i]->skewX - frames[i - 1]->skewX; - float difSkewY = frames[i]->skewY - frames[i - 1]->skewY; + float difSkewX = frames.at(i)->skewX - frames.at(i-1)->skewX; + float difSkewY = frames.at(i)->skewY - frames.at(i-1)->skewY; if (difSkewX < -M_PI || difSkewX > M_PI) { - frames[i - 1]->skewX = difSkewX < 0 ? frames[i - 1]->skewX - 2 * M_PI : frames[i - 1]->skewX + 2 * M_PI; + frames.at(i-1)->skewX = difSkewX < 0 ? frames.at(i-1)->skewX - 2 * M_PI : frames.at(i-1)->skewX + 2 * M_PI; } if (difSkewY < -M_PI || difSkewY > M_PI) { - frames[i - 1]->skewY = difSkewY < 0 ? frames[i - 1]->skewY - 2 * M_PI : frames[i - 1]->skewY + 2 * M_PI; + frames.at(i-1)->skewY = difSkewY < 0 ? frames.at(i-1)->skewY - 2 * M_PI : frames.at(i-1)->skewY + 2 * M_PI; } } } @@ -1497,10 +1536,10 @@ MovementBoneData *DataReaderHelper::decodeMovementBone(JsonDictionary &json, Dat if (dataInfo->cocoStudioVersion < VERSION_COMBINED) { - if (movementBoneData->frameList.count() > 0) + if (movementBoneData->frameList.size() > 0) { FrameData *frameData = new FrameData(); - frameData->copy((FrameData *)movementBoneData->frameList.getLastObject()); + frameData->copy((FrameData *)movementBoneData->frameList.back()); movementBoneData->addFrameData(frameData); frameData->release(); @@ -1519,7 +1558,8 @@ FrameData *DataReaderHelper::decodeFrame(JsonDictionary &json, DataInfo *dataInf frameData->tweenEasing = (TweenType)json.getItemIntValue(A_TWEEN_EASING, Linear); frameData->displayIndex = json.getItemIntValue(A_DISPLAY_INDEX, 0); - frameData->blendType = (BlendType)json.getItemIntValue(A_BLEND_TYPE, 0); + frameData->blendFunc.src = (GLenum)(json.getItemIntValue(A_BLEND_SRC, BlendFunc::ALPHA_NON_PREMULTIPLIED.src)); + frameData->blendFunc.dst = (GLenum)(json.getItemIntValue(A_BLEND_DST, BlendFunc::ALPHA_NON_PREMULTIPLIED.dst)); frameData->isTween = (bool)json.getItemBoolvalue(A_TWEEN_FRAME, true); const char *event = json.getItemStringValue(A_EVENT); @@ -1537,6 +1577,18 @@ FrameData *DataReaderHelper::decodeFrame(JsonDictionary &json, DataInfo *dataInf frameData->frameID = json.getItemIntValue(A_FRAME_INDEX, 0); } + + int length = json.getArrayItemCount(A_EASING_PARAM); + if (length != 0) + { + frameData->easingParams = new float[length]; + + for (int i = 0; i < length; i++) + { + frameData->easingParams[i] = json.getFloatValueFromArray(A_EASING_PARAM, i, 0); + } + } + return frameData; } @@ -1561,7 +1613,7 @@ TextureData *DataReaderHelper::decodeTexture(JsonDictionary &json) { JsonDictionary *dic = json.getSubItemFromArray(CONTOUR_DATA, i); ContourData *contourData = decodeContour(*dic); - textureData->contourDataList.addObject(contourData); + textureData->contourDataList.pushBack(contourData); contourData->release(); delete dic; @@ -1580,13 +1632,12 @@ ContourData *DataReaderHelper::decodeContour(JsonDictionary &json) { JsonDictionary *dic = json.getSubItemFromArray(VERTEX_POINT, i); - ContourVertex2 *vertex = new ContourVertex2(0, 0); + Point vertex; - vertex->x = dic->getItemFloatValue(A_X, 0); - vertex->y = dic->getItemFloatValue(A_Y, 0); + vertex.x = dic->getItemFloatValue(A_X, 0); + vertex.y = dic->getItemFloatValue(A_Y, 0); - contourData->vertexList.addObject(vertex); - vertex->release(); + contourData->vertexList.push_back(vertex); delete dic; } diff --git a/cocos/editor-support/cocostudio/CCDatas.cpp b/cocos/editor-support/cocostudio/CCDatas.cpp index 35d6461efc..d9706a8d26 100644 --- a/cocos/editor-support/cocostudio/CCDatas.cpp +++ b/cocos/editor-support/cocostudio/CCDatas.cpp @@ -158,58 +158,42 @@ const char *DisplayData::changeDisplayToTexture(const char *displayName) DisplayData::DisplayData(void) : displayType(CS_DISPLAY_MAX) + , displayName("") { } -DisplayData::~DisplayData(void) +void DisplayData::copy(DisplayData *displayData) { + displayName = displayData->displayName; + displayType = displayData->displayType; } SpriteDisplayData::SpriteDisplayData(void) - : displayName("") { displayType = CS_DISPLAY_SPRITE; } -SpriteDisplayData::~SpriteDisplayData() -{ -} -void SpriteDisplayData::copy(SpriteDisplayData *displayData) +void SpriteDisplayData::copy(DisplayData *displayData) { - displayName = displayData->displayName; - displayType = displayData->displayType; + DisplayData::copy(displayData); - skinData = displayData->skinData; + if (SpriteDisplayData *sdd = dynamic_cast(displayData)) + { + skinData = sdd->skinData; + } } ArmatureDisplayData::ArmatureDisplayData(void) - : displayName("") { displayType = CS_DISPLAY_ARMATURE; } -ArmatureDisplayData::~ArmatureDisplayData() -{ -} - -void ArmatureDisplayData::copy(ArmatureDisplayData *displayData) -{ - displayName = displayData->displayName; - displayType = displayData->displayType; -} - ParticleDisplayData::ParticleDisplayData(void) - : plist("") { displayType = CS_DISPLAY_PARTICLE; } -void ParticleDisplayData::copy(ParticleDisplayData *displayData) -{ - plist = displayData->plist; - displayType = displayData->displayType; -} BoneData::BoneData(void) @@ -224,18 +208,17 @@ BoneData::~BoneData(void) bool BoneData::init() { - displayDataList.init(); return true; } void BoneData::addDisplayData(DisplayData *displayData) { - displayDataList.addObject(displayData); + displayDataList.pushBack(displayData); } DisplayData *BoneData::getDisplayData(int index) { - return static_cast(displayDataList.getObjectAtIndex(index)); + return displayDataList.at(index); } @@ -255,21 +238,23 @@ bool ArmatureData::init() void ArmatureData::addBoneData(BoneData *boneData) { - boneDataDic.setObject(boneData, boneData->name); + boneDataDic.insert(boneData->name, boneData); } BoneData *ArmatureData::getBoneData(const char *boneName) { - return static_cast(boneDataDic.objectForKey(boneName)); + return static_cast(boneDataDic.at(boneName)); } FrameData::FrameData(void) : frameID(0) , duration(1) , tweenEasing(Linear) + , easingParamNumber(0) + , easingParams(NULL) , isTween(true) , displayIndex(0) - , blendType(BLEND_NORMAL) + , blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED) , strEvent("") , strMovement("") @@ -280,6 +265,7 @@ FrameData::FrameData(void) FrameData::~FrameData(void) { + CC_SAFE_DELETE(easingParams); } void FrameData::copy(const BaseData *baseData) @@ -290,8 +276,21 @@ void FrameData::copy(const BaseData *baseData) { duration = frameData->duration; displayIndex = frameData->displayIndex; + tweenEasing = frameData->tweenEasing; - blendType = frameData->blendType; + easingParamNumber = frameData->easingParamNumber; + + CC_SAFE_DELETE(easingParams); + if (easingParamNumber != 0) + { + easingParams = new float[easingParamNumber]; + for (int i = 0; ieasingParams[i]; + } + } + + blendFunc = frameData->blendFunc; } } @@ -309,17 +308,17 @@ MovementBoneData::~MovementBoneData(void) bool MovementBoneData::init() { - return frameList.init(); + return true; } void MovementBoneData::addFrameData(FrameData *frameData) { - frameList.addObject(frameData); + frameList.pushBack(frameData); } FrameData *MovementBoneData::getFrameData(int index) { - return static_cast(frameList.getObjectAtIndex(index)); + return frameList.at(index); } @@ -341,12 +340,12 @@ MovementData::~MovementData(void) void MovementData::addMovementBoneData(MovementBoneData *movBoneData) { - movBoneDataDic.setObject(movBoneData, movBoneData->name); + movBoneDataDic.insert(movBoneData->name, movBoneData); } MovementBoneData *MovementData::getMovementBoneData(const char *boneName) { - return static_cast(movBoneDataDic.objectForKey(boneName)); + return movBoneDataDic.at(boneName); } @@ -361,18 +360,18 @@ AnimationData::~AnimationData(void) void AnimationData::addMovement(MovementData *movData) { - movementDataDic.setObject(movData, movData->name); + movementDataDic.insert(movData->name, movData); movementNames.push_back(movData->name); } MovementData *AnimationData::getMovement(const char *movementName) { - return static_cast(movementDataDic.objectForKey(movementName)); + return movementDataDic.at(movementName); } -int AnimationData::getMovementCount() +ssize_t AnimationData::getMovementCount() { - return movementDataDic.count(); + return movementDataDic.size(); } @@ -387,15 +386,12 @@ ContourData::~ContourData() bool ContourData::init() { - return vertexList.init(); + return true; } -void ContourData::addVertex(Point *vertex) +void ContourData::addVertex(Point &vertex) { - ContourVertex2 *vertex2 = new ContourVertex2(vertex->x, vertex->y); - vertex2->autorelease(); - - vertexList.addObject(vertex2); + vertexList.push_back(vertex); } TextureData::TextureData() @@ -413,17 +409,17 @@ TextureData::~TextureData() bool TextureData::init() { - return contourDataList.init(); + return true; } void TextureData::addContourData(ContourData *contourData) { - contourDataList.addObject(contourData); + contourDataList.pushBack(contourData); } ContourData *TextureData::getContourData(int index) { - return static_cast(contourDataList.getObjectAtIndex(index)); + return contourDataList.at(index); } diff --git a/cocos/editor-support/cocostudio/CCDatas.h b/cocos/editor-support/cocostudio/CCDatas.h index febc44cf1d..2343d63b11 100644 --- a/cocos/editor-support/cocostudio/CCDatas.h +++ b/cocos/editor-support/cocostudio/CCDatas.h @@ -146,9 +146,12 @@ public: * @js NA * @lua NA */ - virtual ~DisplayData(void); + virtual ~DisplayData(void) {} + + virtual void copy(DisplayData *displayData); DisplayType displayType; //! mark which type your display is + std::string displayName; }; @@ -169,21 +172,10 @@ public: * @js NA * @lua NA */ - virtual ~SpriteDisplayData(); + virtual ~SpriteDisplayData() {}; - void setParam(const char *pszDisplayName) { this->displayName = pszDisplayName; } - - void copy(SpriteDisplayData *displayData); + void copy(DisplayData *displayData); public: - /** - * If DisplayType is CS_DISPLAY_SPRITE, then Bone will use this image name to create a Sprite from CCSpriteFrameCache. - * It should note that when use this name to create Sprite from CCSpriteFrameCache, you should use _displayName + ".png", because when use Texture Packer to pack single image file, the name have ".png". - * - * If DisplayType is CS_DISPLAY_ARMATURE, the name is the Armature's name. When Bone init display and type is CS_DISPLAY_ARMATURE, - * then Bone will create a Armature. - */ - std::string displayName; - BaseData skinData; }; @@ -204,20 +196,7 @@ public: * @js NA * @lua NA */ - virtual ~ArmatureDisplayData(); - - void setParam(const char *pszDisplayName) { this->displayName = pszDisplayName; } - void copy(ArmatureDisplayData *displayData); -public: - /** - * If DisplayType is CS_DISPLAY_SPRITE, then Bone will use this image name to create a Sprite from CCSpriteFrameCache. - * It should note that when use this name to create Sprite from CCSpriteFrameCache, you should use _displayName + ".png", because when use Texture Packer to pack single image file, the name have ".png". - * - * If DisplayType is CS_DISPLAY_ARMATURE, the name is the Armature's name. When Bone init display and type is CS_DISPLAY_ARMATURE, - * then Bone will create a Armature. - */ - std::string displayName; - + virtual ~ArmatureDisplayData() {} }; /** @@ -238,12 +217,6 @@ public: * @lua NA */ virtual ~ParticleDisplayData() {}; - - void setParam(const char *pszPlistName) { this->plist = pszPlistName; } - - void copy(ParticleDisplayData *displayData); -public: - std::string plist; }; @@ -277,7 +250,7 @@ public: public: std::string name; //! the bone's name std::string parentName; //! the bone parent's name - cocos2d::Array displayDataList; //! save DisplayData informations for the Bone + cocos2d::Vector displayDataList; //! save DisplayData informations for the Bone cocos2d::AffineTransform boneDataTransform; }; @@ -309,7 +282,7 @@ public: BoneData *getBoneData(const char *boneName); public: std::string name; - cocos2d::Dictionary boneDataDic; + cocos2d::Map boneDataDic; float dataVersion; }; @@ -354,7 +327,11 @@ public: public: int frameID; int duration; //! The frame will last duration frames + TweenType tweenEasing; //! Every frame's tween easing effect + int easingParamNumber; + float *easingParams; + bool isTween; //! Whether it's a tween key frame /** @@ -363,7 +340,7 @@ public: */ int displayIndex; - BlendType blendType; + cocos2d::BlendFunc blendFunc; std::string strEvent; /** @@ -403,7 +380,7 @@ public: float duration; //! this Bone in this movement will last m_iDuration frames std::string name; //! bone name - cocos2d::Array frameList; + cocos2d::Vector frameList; }; /** @@ -461,7 +438,7 @@ public: * @key const char * * @value MovementBoneData * */ - cocos2d::Dictionary movBoneDataDic; + cocos2d::Map movBoneDataDic; }; @@ -489,25 +466,14 @@ public: void addMovement(MovementData *movData); MovementData *getMovement(const char *movementName); - int getMovementCount(); + ssize_t getMovementCount(); public: std::string name; - cocos2d::Dictionary movementDataDic; + cocos2d::Map movementDataDic; std::vector movementNames; }; -struct ContourVertex2 : public cocos2d::Object -{ - ContourVertex2(float xx, float yy) - { - this->x = xx; - this->y = yy; - } - - float x; - float y; -}; /* * ContourData include a contour vertex information @@ -530,9 +496,9 @@ public: ~ContourData(void); virtual bool init(); - virtual void addVertex(cocos2d::Point *vertex); + virtual void addVertex(cocos2d::Point &vertex); public: - cocos2d::Array vertexList; //! Save contour vertex info, vertex saved in a Point + std::vector vertexList; //! Save contour vertex info, vertex saved in a Point }; @@ -572,7 +538,7 @@ public: std::string name; //! The texture's name - cocos2d::Array contourDataList; + cocos2d::Vector contourDataList; }; diff --git a/cocos/editor-support/cocostudio/CCDecorativeDisplay.cpp b/cocos/editor-support/cocostudio/CCDecorativeDisplay.cpp index 81cc4bc751..73efa5443c 100644 --- a/cocos/editor-support/cocostudio/CCDecorativeDisplay.cpp +++ b/cocos/editor-support/cocostudio/CCDecorativeDisplay.cpp @@ -45,7 +45,7 @@ DecorativeDisplay::DecorativeDisplay() , _displayData(nullptr) { -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX _colliderDetector = nullptr; #endif } @@ -56,7 +56,7 @@ DecorativeDisplay::~DecorativeDisplay(void) CC_SAFE_RELEASE_NULL(_displayData); CC_SAFE_RELEASE_NULL(_display); -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX CC_SAFE_RELEASE_NULL(_colliderDetector); #endif } diff --git a/cocos/editor-support/cocostudio/CCDecorativeDisplay.h b/cocos/editor-support/cocostudio/CCDecorativeDisplay.h index 8eb09f78c3..eee97e9019 100644 --- a/cocos/editor-support/cocostudio/CCDecorativeDisplay.h +++ b/cocos/editor-support/cocostudio/CCDecorativeDisplay.h @@ -30,7 +30,7 @@ THE SOFTWARE. #include "cocostudio/CCDatas.h" -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX #include "cocostudio/CCColliderDetector.h" #endif @@ -71,7 +71,7 @@ public: } virtual DisplayData *getDisplayData() const { return _displayData; } -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX virtual void setColliderDetector(ColliderDetector *detector) { if (_colliderDetector != detector) @@ -87,7 +87,7 @@ protected: cocos2d::Node *_display; DisplayData *_displayData; -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX ColliderDetector *_colliderDetector; #endif }; diff --git a/cocos/editor-support/cocostudio/CCDisplayFactory.cpp b/cocos/editor-support/cocostudio/CCDisplayFactory.cpp index 8ebd027a13..a1e2df89d1 100644 --- a/cocos/editor-support/cocostudio/CCDisplayFactory.cpp +++ b/cocos/editor-support/cocostudio/CCDisplayFactory.cpp @@ -75,30 +75,6 @@ void DisplayFactory::updateDisplay(Bone *bone, float dt, bool dirty) Node *display = bone->getDisplayRenderNode(); CS_RETURN_IF(!display); -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT - if (dirty) - { - DecorativeDisplay *decoDisplay = bone->getDisplayManager()->getCurrentDecorativeDisplay(); - ColliderDetector *detector = decoDisplay->getColliderDetector(); - if (detector) - { - do - { - CC_BREAK_IF(!detector->getBody()); - - kmMat4 displayTransform = display->getNodeToParentTransform(); - Point anchorPoint = display->getAnchorPointInPoints(); - anchorPoint = PointApplyTransform(anchorPoint, displayTransform); - displayTransform.mat[12] = anchorPoint.x; - displayTransform.mat[13] = anchorPoint.y; - kmMat4 t = TransformConcat(displayTransform, bone->getArmature()->getNodeToParentTransform()); - detector->updateTransform(t); - } - while (0); - } - } -#endif - switch(bone->getDisplayRenderNodeType()) { case CS_DISPLAY_SPRITE: @@ -119,6 +95,33 @@ void DisplayFactory::updateDisplay(Bone *bone, float dt, bool dirty) } break; } + + +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + if (dirty) + { + DecorativeDisplay *decoDisplay = bone->getDisplayManager()->getCurrentDecorativeDisplay(); + ColliderDetector *detector = decoDisplay->getColliderDetector(); + if (detector) + { + do + { +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT + CC_BREAK_IF(!detector->getBody()); +#endif + + kmMat4 displayTransform = display->getNodeToParentTransform(); + Point anchorPoint = display->getAnchorPointInPoints(); + anchorPoint = PointApplyTransform(anchorPoint, displayTransform); + displayTransform.mat[12] = anchorPoint.x; + displayTransform.mat[13] = anchorPoint.y; + kmMat4 t = TransformConcat(displayTransform, bone->getArmature()->getNodeToParentTransform()); + detector->updateTransform(t); + } + while (0); + } + } +#endif } @@ -200,13 +203,13 @@ void DisplayFactory::initSpriteDisplay(Bone *bone, DecorativeDisplay *decoDispla } -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT - if (textureData && textureData->contourDataList.count() > 0) +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + if (textureData && textureData->contourDataList.size() > 0) { //! create ContourSprite ColliderDetector *colliderDetector = ColliderDetector::create(bone); - colliderDetector->addContourDataList(&textureData->contourDataList); + colliderDetector->addContourDataList(textureData->contourDataList); decoDisplay->setColliderDetector(colliderDetector); } @@ -254,7 +257,7 @@ void DisplayFactory::addParticleDisplay(Bone *bone, DecorativeDisplay *decoDispl void DisplayFactory::createParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay) { ParticleDisplayData *displayData = (ParticleDisplayData *)decoDisplay->getDisplayData(); - ParticleSystem *system = ParticleSystemQuad::create(displayData->plist.c_str()); + ParticleSystem *system = ParticleSystemQuad::create(displayData->displayName.c_str()); system->removeFromParent(); diff --git a/cocos/editor-support/cocostudio/CCDisplayManager.cpp b/cocos/editor-support/cocostudio/CCDisplayManager.cpp index 3c502f2fe7..7207f26194 100644 --- a/cocos/editor-support/cocostudio/CCDisplayManager.cpp +++ b/cocos/editor-support/cocostudio/CCDisplayManager.cpp @@ -46,8 +46,7 @@ DisplayManager *DisplayManager::create(Bone *bone) DisplayManager::DisplayManager() - : _decoDisplayList(nullptr) - , _displayRenderNode(nullptr) + : _displayRenderNode(nullptr) , _displayType(CS_DISPLAY_MAX) , _currentDecoDisplay(nullptr) , _displayIndex(-1) @@ -59,7 +58,7 @@ DisplayManager::DisplayManager() DisplayManager::~DisplayManager() { - CC_SAFE_DELETE(_decoDisplayList); + _decoDisplayList.clear(); if( _displayRenderNode ) { @@ -93,14 +92,14 @@ void DisplayManager::addDisplay(DisplayData *displayData, int index) { DecorativeDisplay *decoDisplay = nullptr; - if( (index >= 0) && (index < _decoDisplayList->count()) ) + if( (index >= 0) && (index < _decoDisplayList.size()) ) { - decoDisplay = (DecorativeDisplay *)_decoDisplayList->getObjectAtIndex(index); + decoDisplay = (DecorativeDisplay *)_decoDisplayList.at(index); } else { decoDisplay = DecorativeDisplay::create(); - _decoDisplayList->addObject(decoDisplay); + _decoDisplayList.pushBack(decoDisplay); } DisplayFactory::addDisplay(_bone, decoDisplay, displayData); @@ -117,14 +116,14 @@ void DisplayManager::addDisplay(Node *display, int index) { DecorativeDisplay *decoDisplay = nullptr; - if( (index >= 0) && (index < _decoDisplayList->count()) ) + if( (index >= 0) && (index < _decoDisplayList.size()) ) { - decoDisplay = (DecorativeDisplay *)_decoDisplayList->getObjectAtIndex(index); + decoDisplay = _decoDisplayList.at(index); } else { decoDisplay = DecorativeDisplay::create(); - _decoDisplayList->addObject(decoDisplay); + _decoDisplayList.pushBack(decoDisplay); } DisplayData *displayData = nullptr; @@ -144,9 +143,9 @@ void DisplayManager::addDisplay(Node *display, int index) { bool find = false; - for (int i = _decoDisplayList->count()-2; i>=0; i--) + for (long i = _decoDisplayList.size()-2; i>=0; i--) { - DecorativeDisplay *dd = static_cast(_decoDisplayList->getObjectAtIndex(i)); + DecorativeDisplay *dd = _decoDisplayList.at(i); SpriteDisplayData *sdd = static_cast(dd->getDisplayData()); if (sdd) { @@ -179,6 +178,7 @@ void DisplayManager::addDisplay(Node *display, int index) else if(Armature *armature = dynamic_cast(display)) { displayData = ArmatureDisplayData::create(); + displayData->displayName = armature->getName(); armature->setParentBone(_bone); } else @@ -205,17 +205,17 @@ void DisplayManager::removeDisplay(int index) _displayIndex = -1; } - _decoDisplayList->removeObjectAtIndex(index); + _decoDisplayList.erase(index); } -Array *DisplayManager::getDecorativeDisplayList() const +const cocos2d::Vector& DisplayManager::getDecorativeDisplayList() const { return _decoDisplayList; } void DisplayManager::changeDisplayByIndex(int index, bool force) { - CCASSERT( (_decoDisplayList ? index < (int)_decoDisplayList->count() : true), "the _index value is out of range"); + CCASSERT( index < (int)_decoDisplayList.size(), "the _index value is out of range"); _forceChangeDisplay = force; @@ -238,14 +238,26 @@ void DisplayManager::changeDisplayByIndex(int index, bool force) } - DecorativeDisplay *decoDisplay = (DecorativeDisplay *)_decoDisplayList->getObjectAtIndex(_displayIndex); + DecorativeDisplay *decoDisplay = (DecorativeDisplay *)_decoDisplayList.at(_displayIndex); setCurrentDecorativeDisplay(decoDisplay); } +void CCDisplayManager::changeDisplayByName(const char *name, bool force) +{ + for (int i = 0; i<_decoDisplayList.size(); i++) + { + if (_decoDisplayList.at(i)->getDisplayData()->displayName == name) + { + changeDisplayByIndex(i, force); + break; + } + } +} + void DisplayManager::setCurrentDecorativeDisplay(DecorativeDisplay *decoDisplay) { -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector()) { _currentDecoDisplay->getColliderDetector()->setActive(false); @@ -254,7 +266,7 @@ void DisplayManager::setCurrentDecorativeDisplay(DecorativeDisplay *decoDisplay) _currentDecoDisplay = decoDisplay; -#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX if (_currentDecoDisplay && _currentDecoDisplay->getColliderDetector()) { _currentDecoDisplay->getColliderDetector()->setActive(true); @@ -325,18 +337,16 @@ DecorativeDisplay *DisplayManager::getCurrentDecorativeDisplay() const DecorativeDisplay *DisplayManager::getDecorativeDisplayByIndex( int index) const { - return (DecorativeDisplay *)_decoDisplayList->getObjectAtIndex(index); + return _decoDisplayList.at(index); } void DisplayManager::initDisplayList(BoneData *boneData) { - CC_SAFE_DELETE(_decoDisplayList); - _decoDisplayList = Array::create(); - _decoDisplayList->retain(); + _decoDisplayList.clear(); CS_RETURN_IF(!boneData); - for(auto object : boneData->displayDataList) + for(auto& object : boneData->displayDataList) { DisplayData *displayData = static_cast(object); @@ -345,7 +355,7 @@ void DisplayManager::initDisplayList(BoneData *boneData) DisplayFactory::createDisplay(_bone, decoDisplay); - _decoDisplayList->addObject(decoDisplay); + _decoDisplayList.pushBack(decoDisplay); } } diff --git a/cocos/editor-support/cocostudio/CCDisplayManager.h b/cocos/editor-support/cocostudio/CCDisplayManager.h index bf08ceaffe..991a03255c 100644 --- a/cocos/editor-support/cocostudio/CCDisplayManager.h +++ b/cocos/editor-support/cocostudio/CCDisplayManager.h @@ -72,7 +72,7 @@ public: void removeDisplay(int index); - cocos2d::Array *getDecorativeDisplayList() const; + const cocos2d::Vector& getDecorativeDisplayList() const; /** * Change display by index. You can just use this method to change display in the display list. @@ -85,6 +85,7 @@ public: */ void changeDisplayByIndex(int index, bool force); + void changeDisplayByName(const char *name, bool force); cocos2d::Node *getDisplayRenderNode() const; DisplayType getDisplayRenderNodeType() const; @@ -129,7 +130,7 @@ public: virtual void setForceChangeDisplay(bool force) { _forceChangeDisplay = force; } virtual bool isForceChangeDisplay() const { return _forceChangeDisplay; } protected: - cocos2d::Array *_decoDisplayList; + cocos2d::Vector _decoDisplayList; //! Display render node. cocos2d::Node *_displayRenderNode; //! Display render node type diff --git a/cocos/editor-support/cocostudio/CCProcessBase.cpp b/cocos/editor-support/cocostudio/CCProcessBase.cpp index f13769e3f7..b55585c047 100644 --- a/cocos/editor-support/cocostudio/CCProcessBase.cpp +++ b/cocos/editor-support/cocostudio/CCProcessBase.cpp @@ -38,16 +38,12 @@ ProcessBase::ProcessBase(void) , _rawDuration(0) , _loopType(ANIMATION_LOOP_BACK) , _tweenEasing(Linear) + , _animationInternal(1/60.0f) , _durationTween(0) , _currentFrame(0) , _curFrameIndex(0) , _isLoopBack(false) { - /* - * set _animationInternal defualt value to Director::getInstance() - * ->getAnimationInterval(), in line with game update speed - */ - _animationInternal = CCDirector::getInstance()->getAnimationInterval(); } @@ -73,8 +69,6 @@ void ProcessBase::stop() { _isComplete = true; _isPlaying = false; - _currentFrame = 0; - _currentPercent = 0; } void ProcessBase::play(int durationTo, int durationTween, int loop, int tweenEasing) @@ -157,7 +151,7 @@ void ProcessBase::gotoFrame(int frameIndex) int ProcessBase::getCurrentFrameIndex() { - _curFrameIndex = _rawDuration * _currentPercent; + _curFrameIndex = (_rawDuration-1) * _currentPercent; return _curFrameIndex; } diff --git a/cocos/editor-support/cocostudio/CCProcessBase.h b/cocos/editor-support/cocostudio/CCProcessBase.h index 6f86f79a0e..cb7db59a71 100644 --- a/cocos/editor-support/cocostudio/CCProcessBase.h +++ b/cocos/editor-support/cocostudio/CCProcessBase.h @@ -124,8 +124,6 @@ public: virtual float getCurrentPercent() const { return _currentPercent; } virtual int getRawDuration() const { return _rawDuration; } - virtual void setAnimationInternal(float animationInternal) { _animationInternal = animationInternal; } - virtual float getAnimationInternal() const { return _animationInternal; } protected: virtual void gotoFrame(int frameIndex); diff --git a/cocos/editor-support/cocostudio/CCSkin.cpp b/cocos/editor-support/cocostudio/CCSkin.cpp index 7b4a100069..00d8137a73 100644 --- a/cocos/editor-support/cocostudio/CCSkin.cpp +++ b/cocos/editor-support/cocostudio/CCSkin.cpp @@ -213,6 +213,17 @@ kmMat4 Skin::getNodeToWorldTransformAR() const return TransformConcat(displayTransform, _bone->getArmature()->getNodeToWorldTransform()); } +void Skin::draw() +{ + kmMat4 mv; + kmGLGetMatrix(KM_GL_MODELVIEW, &mv); + + //TODO implement z order + QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand(); + renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv); + Director::getInstance()->getRenderer()->addCommand(renderCommand); +} + void Skin::setBone(Bone *bone) { _bone = bone; diff --git a/cocos/editor-support/cocostudio/CCSkin.h b/cocos/editor-support/cocostudio/CCSkin.h index 2c580c3038..10dd72c5eb 100644 --- a/cocos/editor-support/cocostudio/CCSkin.h +++ b/cocos/editor-support/cocostudio/CCSkin.h @@ -50,6 +50,9 @@ public: kmMat4 getNodeToWorldTransform() const override; kmMat4 getNodeToWorldTransformAR() const; + + virtual void draw() override; + /** * @js NA * @lua NA diff --git a/cocos/editor-support/cocostudio/CCTween.cpp b/cocos/editor-support/cocostudio/CCTween.cpp index c4055ba1c9..ccd5a74eeb 100644 --- a/cocos/editor-support/cocostudio/CCTween.cpp +++ b/cocos/editor-support/cocostudio/CCTween.cpp @@ -141,7 +141,7 @@ void Tween::play(MovementBoneData *movementBoneData, int durationTo, int duratio } _frameTweenEasing = Linear; } - else if (_movementBoneData->frameList.count() > 1) + else if (_movementBoneData->frameList.size() > 1) { _durationTween = durationTween * _movementBoneData->scale; @@ -176,7 +176,7 @@ void Tween::gotoAndPlay(int frameIndex) _isPlaying = true; _isComplete = _isPause = false; - _currentPercent = (float)_curFrameIndex / (float)_rawDuration; + _currentPercent = (float)_curFrameIndex / ((float)_rawDuration-1); _currentFrame = _nextFrameIndex * _currentPercent; } @@ -338,7 +338,7 @@ void Tween::arriveKeyFrame(FrameData *keyFrameData) _bone->updateZOrder(); //! Update blend type - _bone->setBlendType(keyFrameData->blendType); + _bone->setBlendFunc(keyFrameData->blendFunc); //! Update child armature's movement Armature *childAramture = _bone->getChildArmature(); @@ -394,7 +394,7 @@ float Tween::updateFrameData(float currentPercent) currentPercent = fmodf(currentPercent, 1); } - float playedTime = (float)_rawDuration * currentPercent; + float playedTime = ((float)_rawDuration-1) * currentPercent; //! If play to current frame's front or back, then find current frame again @@ -404,25 +404,25 @@ float Tween::updateFrameData(float currentPercent) * Get frame length, if _toIndex >= _length, then set _toIndex to 0, start anew. * _toIndex is next index will play */ - int length = _movementBoneData->frameList.count(); - FrameData **frames = (FrameData **)_movementBoneData->frameList.data->arr; + long length = _movementBoneData->frameList.size(); + cocos2d::Vector &frames = _movementBoneData->frameList; FrameData *from = nullptr; FrameData *to = nullptr; - if (playedTime < frames[0]->frameID) + if (playedTime < frames.at(0)->frameID) { - from = to = frames[0]; + from = to = frames.at(0); setBetween(from, to); return _currentPercent; } - if(playedTime >= frames[length - 1]->frameID) + if(playedTime >= frames.at(length - 1)->frameID) { // If _passLastFrame is true and playedTime >= frames[length - 1]->frameID, then do not need to go on. if (_passLastFrame) { - from = to = frames[length - 1]; + from = to = frames.at(length - 1); setBetween(from, to); return _currentPercent; } @@ -437,7 +437,7 @@ float Tween::updateFrameData(float currentPercent) do { _fromIndex = _toIndex; - from = frames[_fromIndex]; + from = frames.at(_fromIndex); _totalDuration = from->frameID; _toIndex = _fromIndex + 1; @@ -446,7 +446,7 @@ float Tween::updateFrameData(float currentPercent) _toIndex = 0; } - to = frames[_toIndex]; + to = frames.at(_toIndex); //! Guaranteed to trigger frame event if(from->strEvent.length() != 0 && !_animation->isIgnoreFrameEvent()) @@ -477,7 +477,7 @@ float Tween::updateFrameData(float currentPercent) TweenType tweenType = (_frameTweenEasing != Linear) ? _frameTweenEasing : _tweenEasing; if (tweenType != TWEEN_EASING_MAX && tweenType != Linear && !_passLastFrame) { - currentPercent = TweenFunction::tweenTo(0, 1, currentPercent, 1, tweenType); + currentPercent = TweenFunction::tweenTo(currentPercent, tweenType, _from->easingParams); } return currentPercent; diff --git a/cocos/editor-support/cocostudio/CCTweenFunction.cpp b/cocos/editor-support/cocostudio/CCTweenFunction.cpp index 74e4471768..808e314c7e 100644 --- a/cocos/editor-support/cocostudio/CCTweenFunction.cpp +++ b/cocos/editor-support/cocostudio/CCTweenFunction.cpp @@ -25,389 +25,454 @@ THE SOFTWARE. #include "cocostudio/CCTweenFunction.h" #include "cocostudio/CCUtilMath.h" +#ifndef M_PI_X_2 +#define M_PI_X_2 (float)M_PI * 2.0f +#endif + namespace cocostudio { -float TweenFunction::tweenTo(float from, float change, float time, float duration, TweenType tweenType) +float TweenFunction::tweenTo(float time, TweenType type, float *easingParam) { float delta = 0; - switch (tweenType) + switch (type) { + case CUSTOM_EASING: + delta = customEase(time, easingParam); + break; + case Linear: - delta = linear(time, 0, 1, duration); + delta = linear(time); break; case Sine_EaseIn: - delta = sineEaseIn(time, 0, 1, duration); + delta = sineEaseIn(time); break; case Sine_EaseOut: - delta = sineEaseOut(time, 0, 1, duration); + delta = sineEaseOut(time); break; case Sine_EaseInOut: - delta = sineEaseInOut(time, 0, 1, duration); + delta = sineEaseInOut(time); break; case Quad_EaseIn: - delta = quadEaseIn(time, 0, 1, duration); + delta = quadEaseIn(time); break; case Quad_EaseOut: - delta = quadEaseOut(time, 0, 1, duration); + delta = quadEaseOut(time); break; case Quad_EaseInOut: - delta = quadEaseInOut(time, 0, 1, duration); + delta = quadEaseInOut(time); break; case Cubic_EaseIn: - delta = cubicEaseIn(time, 0, 1, duration); + delta = cubicEaseIn(time); break; case Cubic_EaseOut: - delta = cubicEaseOut(time, 0, 1, duration); + delta = cubicEaseOut(time); break; case Cubic_EaseInOut: - delta = cubicEaseInOut(time, 0, 1, duration); + delta = cubicEaseInOut(time); break; case Quart_EaseIn: - delta = quartEaseIn(time, 0, 1, duration); + delta = quartEaseIn(time); break; case Quart_EaseOut: - delta = quartEaseOut(time, 0, 1, duration); + delta = quartEaseOut(time); break; case Quart_EaseInOut: - delta = quartEaseInOut(time, 0, 1, duration); + delta = quartEaseInOut(time); break; case Quint_EaseIn: - delta = quintEaseIn(time, 0, 1, duration); + delta = quintEaseIn(time); break; case Quint_EaseOut: - delta = quintEaseOut(time, 0, 1, duration); + delta = quintEaseOut(time); break; case Quint_EaseInOut: - delta = quintEaseInOut(time, 0, 1, duration); + delta = quintEaseInOut(time); break; case Expo_EaseIn: - delta = expoEaseIn(time, 0, 1, duration); + delta = expoEaseIn(time); break; case Expo_EaseOut: - delta = expoEaseOut(time, 0, 1, duration); + delta = expoEaseOut(time); break; case Expo_EaseInOut: - delta = expoEaseInOut(time, 0, 1, duration); + delta = expoEaseInOut(time); break; case Circ_EaseIn: - delta = circEaseIn(time, 0, 1, duration); + delta = circEaseIn(time); break; case Circ_EaseOut: - delta = circEaseOut(time, 0, 1, duration); + delta = circEaseOut(time); break; case Circ_EaseInOut: - delta = circEaseInOut(time, 0, 1, duration); + delta = circEaseInOut(time); break; case Elastic_EaseIn: - delta = elasticEaseIn(time, 0, 1, duration); + delta = elasticEaseIn(time, easingParam); break; case Elastic_EaseOut: - delta = elasticEaseOut(time, 0, 1, duration); + delta = elasticEaseOut(time, easingParam); break; case Elastic_EaseInOut: - delta = elasticEaseInOut(time, 0, 1, duration); + delta = elasticEaseInOut(time, easingParam); break; case Back_EaseIn: - delta = backEaseIn(time, 0, 1, duration); + delta = backEaseIn(time); break; case Back_EaseOut: - delta = backEaseOut(time, 0, 1, duration); + delta = backEaseOut(time); break; case Back_EaseInOut: - delta = backEaseInOut(time, 0, 1, duration); + delta = backEaseInOut(time); break; case Bounce_EaseIn: - delta = bounceEaseIn(time, 0, 1, duration); + delta = bounceEaseIn(time); break; case Bounce_EaseOut: - delta = bounceEaseOut(time, 0, 1, duration); + delta = bounceEaseOut(time); break; case Bounce_EaseInOut: - delta = bounceEaseInOut(time, 0, 1, duration); + delta = bounceEaseInOut(time); break; default: - delta = sineEaseInOut(time, 0, 1, duration); + delta = sineEaseInOut(time); break; } return delta; } -float TweenFunction::linear(float t, float b, float c, float d) +// Linear +float TweenFunction::linear(float time) { - return c * t / d + b; + return time; } -float TweenFunction::quadEaseIn(float t, float b, float c, float d) + +// Sine Ease +float TweenFunction::sineEaseIn(float time) { - t /= d; - return c * t * t + b; + return -1 * cosf(time * (float)M_PI_2) + 1; } -float TweenFunction::quadEaseOut(float t, float b, float c, float d) +float TweenFunction::sineEaseOut(float time) { - t /= d; - return -c * t * (t - 2) + b; + return sinf(time * (float)M_PI_2); } -float TweenFunction::quadEaseInOut(float t, float b, float c, float d) +float TweenFunction::sineEaseInOut(float time) { - t = t/d*2; - if ((t) < 1) - return c / 2 * t * t + b; - --t; - return -c / 2 * (t * (t - 2) - 1) + b; + return -0.5f * (cosf((float)M_PI * time) - 1); } -float TweenFunction::cubicEaseIn(float t, float b, float c, float d) + +// Quad Ease +float TweenFunction::quadEaseIn(float time) { - t /= d; - return c * t * t * t + b; + return time * time; } -float TweenFunction::cubicEaseOut(float t, float b, float c, float d) +float TweenFunction::quadEaseOut(float time) { - t = t / d - 1; - return c * (t * t * t + 1) + b; + return -1 * time * (time - 2); } -float TweenFunction::cubicEaseInOut(float t, float b, float c, float d) +float TweenFunction::quadEaseInOut(float time) { - t = t/d*2; - if ((t) < 1) - return c / 2 * t * t * t + b; - t -= 2; - return c / 2 * (t * t * t + 2) + b; + time = time*2; + if (time < 1) + return 0.5f * time * time; + --time; + return -0.5f * (time * (time - 2) - 1); } -float TweenFunction::quartEaseIn(float t, float b, float c, float d) + + +// Cubic Ease +float TweenFunction::cubicEaseIn(float time) { - t /= d; - return c * t * t * t * t + b; + return time * time * time; } -float TweenFunction::quartEaseOut(float t, float b, float c, float d) +float TweenFunction::cubicEaseOut(float time) { - t = t / d - 1; - return -c * (t * t * t * t - 1) + b; + time -= 1; + return (time * time * time + 1); } -float TweenFunction::quartEaseInOut(float t, float b, float c, float d) +float TweenFunction::cubicEaseInOut(float time) { - t = t/d*2; - if ((t) < 1) - return c / 2 * t * t * t * t + b; - t -= 2; - return -c / 2 * (t * t * t * t - 2) + b; + time = time*2; + if (time < 1) + return 0.5f * time * time * time; + time -= 2; + return 0.5f * (time * time * time + 2); } -float TweenFunction::quintEaseIn(float t, float b, float c, float d) + +// Quart Ease +float TweenFunction::quartEaseIn(float time) { - t /= d; - return c * t * t * t * t * t + b; + return time * time * time * time; } -float TweenFunction::quintEaseOut(float t, float b, float c, float d) +float TweenFunction::quartEaseOut(float time) { - t = t / d - 1; - return c * (t * t * t * t * t + 1) + b; + time -= 1; + return -(time * time * time * time - 1); } -float TweenFunction::quintEaseInOut(float t, float b, float c, float d) +float TweenFunction::quartEaseInOut(float time) { - t = t/d*2; - if ((t) < 1) - return c / 2 * t * t * t * t * t + b; - t -= 2; - return c / 2 * (t * t * t * t * t + 2) + b; + time = time*2; + if (time < 1) + return 0.5f * time * time * time * time; + time -= 2; + return -0.5f * (time * time * time * time - 2); } -float TweenFunction::sineEaseIn(float t, float b, float c, float d) + +// Quint Ease +float TweenFunction::quintEaseIn(float time) { - return -c * cos(t / d * (M_PI / 2)) + c + b; + return time * time * time * time * time; } -float TweenFunction::sineEaseOut(float t, float b, float c, float d) +float TweenFunction::quintEaseOut(float time) { - return c * sin(t / d * (M_PI / 2)) + b; + time -=1; + return (time * time * time * time * time + 1); } -float TweenFunction::sineEaseInOut(float t, float b, float c, float d) +float TweenFunction::quintEaseInOut(float time) { - return -c / 2 * (cos(M_PI * t / d) - 1) + b; + time = time*2; + if (time < 1) + return 0.5f * time * time * time * time * time; + time -= 2; + return 0.5f * (time * time * time * time * time + 2); } -float TweenFunction::expoEaseIn(float t, float b, float c, float d) -{ - return (t == 0) ? b : c * pow(2, 10 * (t / d - 1)) + b; -} -float TweenFunction::expoEaseOut(float t, float b, float c, float d) -{ - return (t == d) ? b + c : c * (-pow(2, -10 * t / d) + 1) + b; -} -float TweenFunction::expoEaseInOut(float t, float b, float c, float d) -{ - if (t == 0) - return b; - if (t == d) - return b + c; - if ((t /= d / 2) < 1) - return c / 2 * pow(2, 10 * (t - 1)) + b; - --t; - return c / 2 * (-pow(2, -10 * t) + 2) + b; -} -float TweenFunction::circEaseIn(float t, float b, float c, float d) +// Expo Ease +float TweenFunction::expoEaseIn(float time) { - t /= d; - return -c * (sqrt(1 - t * t) - 1) + b; + return time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f; } -float TweenFunction::circEaseOut(float t, float b, float c, float d) +float TweenFunction::expoEaseOut(float time) { - t = t / d - 1; - return c * sqrt(1 - t * t) + b; + return time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1); } -float TweenFunction::circEaseInOut(float t, float b, float c, float d) +float TweenFunction::expoEaseInOut(float time) { - t = t/d*2; - if ((t) < 1) - return -c / 2 * (sqrt(1 - t * t) - 1) + b; - t -= 2; - return c / 2 * (sqrt(1 - t * t) + 1) + b; -} - -float TweenFunction::elasticEaseIn(float t, float b, float c, float d, float a, float p) -{ - float s = 0; - if (t == 0) - return b; - t /= d; - if (t == 1) - return b + c; - if (!p) - p = d * .3; - if (!a || a < abs(c)) + time /= 0.5f; + if (time < 1) { - a = c; - s = p / 4; - } - else - s = p / (2 * M_PI) * asin(c / a); - t -= 1; - return -(a * pow(2, 10 * t) * sin((t * d - s) * (2 * M_PI) / p)) + b; -} -float TweenFunction::elasticEaseOut(float t, float b, float c, float d, float a, float p) -{ - float s = 0; - if (t == 0) - return b; - t /= d; - if (t == 1) - return b + c; - if (!p) - p = d * .3; - if (!a || a < abs(c)) - { - a = c; - s = p / 4; - } - else - s = p / (2 * M_PI) * asin(c / a); - return (a * pow(2, -10 * t) * sin((t * d - s) * (2 * M_PI) / p) + c + b); -} -float TweenFunction::elasticEaseInOut(float t, float b, float c, float d, float a, float p) -{ - float s = 0; - if (t == 0) - return b; - t = t/d*2; - if ((t) == 2) - return b + c; - if (!p) - p = d * (.3 * 1.5); - if (!a || a < abs(c)) - { - a = c; - s = p / 4; - } - else - s = p / (2 * M_PI) * asin(c / a); - if (t < 1) - { - t -= 1; - return -.5 * (a * pow(2, 10 * t) * sin((t * d - s) * (2 * M_PI) / p)) + b; - } - t -= 1; - return a * pow(2, -10 * t) * sin((t * d - s) * (2 * M_PI) / p) * .5 + c + b; -} - -float TweenFunction::backEaseIn(float t, float b, float c, float d, float s) -{ - if (s == 0) - s = 1.70158f; - t /= d; - return c * t * t * ((s + 1) * t - s) + b; -} -float TweenFunction::backEaseOut(float t, float b, float c, float d, float s) -{ - if (s == 0) - s = 1.70158f; - t = t / d - 1; - return c * (t * t * ((s + 1) * t + s) + 1) + b; -} -float TweenFunction::backEaseInOut(float t, float b, float c, float d, float s) -{ - if (s == 0) - s = 1.70158f; - if ((t /= d / 2) < 1) - { - s *= (1.525f); - return c / 2 * (t * t * ((s + 1) * t - s)) + b; - } - - t -= 2; - s *= (1.525f); - return c / 2 * (t * t * ((s + 1) * t + s) + 2) + b; -} - -float TweenFunction::bounceEaseIn(float t, float b, float c, float d) -{ - return c - bounceEaseOut(d - t, 0, c, d) + b; -} - -float TweenFunction::bounceEaseOut(float t, float b, float c, float d) -{ - t /= d; - if (t < (1 / 2.75f)) - { - return c * (7.5625f * t * t) + b; - } - else if (t < (2 / 2.75f)) - { - t -= (1.5f / 2.75f); - return c * (7.5625f * t * t + .75f) + b; - } - else if (t < (2.5f / 2.75f)) - { - t -= (2.25f / 2.75f); - return c * (7.5625f * t * t + .9375f) + b; + time = 0.5f * powf(2, 10 * (time - 1)); } else { - t -= (2.625f / 2.75f); - return c * (7.5625f * t * t + .984375f) + b; + time = 0.5f * (-powf(2, -10 * (time - 1)) + 2); + } + + return time; +} + + +// Circ Ease +float TweenFunction::circEaseIn(float time) +{ + return -1 * (sqrt(1 - time * time) - 1); +} +float TweenFunction::circEaseOut(float time) +{ + time = time - 1; + return sqrt(1 - time * time); +} +float TweenFunction::circEaseInOut(float time) +{ + time = time * 2; + if (time < 1) + return -0.5f * (sqrt(1 - time * time) - 1); + time -= 2; + return 0.5f * (sqrt(1 - time * time) + 1); +} + + +// Elastic Ease +float TweenFunction::elasticEaseIn(float time, float *easingParam) +{ + float period = 0.3f; + + if (easingParam != NULL) + { + period = easingParam[0]; + } + + float newT = 0; + if (time == 0 || time == 1) + { + newT = time; + } + else + { + float s = period / 4; + time = time - 1; + newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / period); + } + + return newT; +} +float TweenFunction::elasticEaseOut(float time, float *easingParam) +{ + float period = 0.3f; + + if (easingParam != NULL) + { + period = easingParam[0]; + } + + float newT = 0; + if (time == 0 || time == 1) + { + newT = time; + } + else + { + float s = period / 4; + newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) + 1; + } + + return newT; +} +float TweenFunction::elasticEaseInOut(float time, float *easingParam) +{ + float period = 0.3f; + + if (easingParam != NULL) + { + period = easingParam[0]; + } + + float newT = 0; + if (time == 0 || time == 1) + { + newT = time; + } + else + { + time = time * 2; + if (! period) + { + period = 0.3f * 1.5f; + } + + float s = period / 4; + + time = time - 1; + if (time < 0) + { + newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / period); + } + else + { + newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) * 0.5f + 1; + } + } + return newT; +} + + +// Back Ease +float TweenFunction::backEaseIn(float time) +{ + float overshoot = 1.70158f; + return time * time * ((overshoot + 1) * time - overshoot); +} +float TweenFunction::backEaseOut(float time) +{ + float overshoot = 1.70158f; + + time = time - 1; + return time * time * ((overshoot + 1) * time + overshoot) + 1; +} +float TweenFunction::backEaseInOut(float time) +{ + float overshoot = 1.70158f * 1.525f; + + time = time * 2; + if (time < 1) + { + return (time * time * ((overshoot + 1) * time - overshoot)) / 2; + } + else + { + time = time - 2; + return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1; } } -float TweenFunction::bounceEaseInOut(float t, float b, float c, float d) + + +// Bounce Ease +float bounceTime(float time) { - if (t < d / 2) - return bounceEaseIn(t * 2, 0, c, d) * .5 + b; + if (time < 1 / 2.75) + { + return 7.5625f * time * time; + } else + if (time < 2 / 2.75) + { + time -= 1.5f / 2.75f; + return 7.5625f * time * time + 0.75f; + } else + if(time < 2.5 / 2.75) + { + time -= 2.25f / 2.75f; + return 7.5625f * time * time + 0.9375f; + } + + time -= 2.625f / 2.75f; + return 7.5625f * time * time + 0.984375f; +} +float TweenFunction::bounceEaseIn(float time) +{ + return 1 - bounceTime(1 - time); +} + +float TweenFunction::bounceEaseOut(float time) +{ + return bounceTime(time); +} + +float TweenFunction::bounceEaseInOut(float time) +{ + float newT = 0; + if (time < 0.5f) + { + time = time * 2; + newT = (1 - bounceTime(1 - time)) * 0.5f; + } else - return bounceEaseOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b; + { + newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f; + } + + return newT; +} + + +// Custom Ease +float TweenFunction::customEase(float time, float *easingParam) +{ + if (easingParam) + { + float tt = 1-time; + return easingParam[1]*tt*tt*tt + 3*easingParam[3]*time*tt*tt + 3*easingParam[5]*time*time*tt + easingParam[7]*time*time*time; + } + return time; } diff --git a/cocos/editor-support/cocostudio/CCTweenFunction.h b/cocos/editor-support/cocostudio/CCTweenFunction.h index dfef81a18b..d845636244 100644 --- a/cocos/editor-support/cocostudio/CCTweenFunction.h +++ b/cocos/editor-support/cocostudio/CCTweenFunction.h @@ -33,7 +33,7 @@ namespace cocostudio { enum TweenType { - TWEEN_EASING_MIN = -1, + CUSTOM_EASING = -1, Linear, @@ -41,6 +41,7 @@ enum TweenType Sine_EaseOut, Sine_EaseInOut, + Quad_EaseIn, Quad_EaseOut, Quad_EaseInOut, @@ -80,56 +81,58 @@ enum TweenType TWEEN_EASING_MAX = 10000 }; /** - * @js NA - * @lua NA - */ +* @js NA +* @lua NA +*/ class TweenFunction { public: - static float tweenTo(float from, float change, float time, float duration, TweenType tweenType); + static float tweenTo(float time, TweenType type, float *easingParam); - static float linear(float t, float b, float c, float d); + static float linear(float time); - static float sineEaseIn(float t, float b, float c, float d); - static float sineEaseOut(float t, float b, float c, float d); - static float sineEaseInOut(float t, float b, float c, float d); + static float sineEaseIn(float time); + static float sineEaseOut(float time); + static float sineEaseInOut(float time); - static float quadEaseIn(float t, float b, float c, float d); - static float quadEaseOut(float t, float b, float c, float d); - static float quadEaseInOut(float t, float b, float c, float d); + static float quadEaseIn(float time); + static float quadEaseOut(float time); + static float quadEaseInOut(float time); - static float cubicEaseIn(float t, float b, float c, float d); - static float cubicEaseOut(float t, float b, float c, float d); - static float cubicEaseInOut(float t, float b, float c, float d); + static float cubicEaseIn(float time); + static float cubicEaseOut(float time); + static float cubicEaseInOut(float time); - static float quartEaseIn(float t, float b, float c, float d); - static float quartEaseOut(float t, float b, float c, float d); - static float quartEaseInOut(float t, float b, float c, float d); + static float quartEaseIn(float time); + static float quartEaseOut(float time); + static float quartEaseInOut(float time); - static float quintEaseIn(float t, float b, float c, float d); - static float quintEaseOut(float t, float b, float c, float d); - static float quintEaseInOut(float t, float b, float c, float d); + static float quintEaseIn(float time); + static float quintEaseOut(float time); + static float quintEaseInOut(float time); - static float expoEaseIn(float t, float b, float c, float d); - static float expoEaseOut(float t, float b, float c, float d); - static float expoEaseInOut(float t, float b, float c, float d); + static float expoEaseIn(float time); + static float expoEaseOut(float time); + static float expoEaseInOut(float time); - static float circEaseIn(float t, float b, float c, float d); - static float circEaseOut(float t, float b, float c, float d); - static float circEaseInOut(float t, float b, float c, float d); + static float circEaseIn(float time); + static float circEaseOut(float time); + static float circEaseInOut(float time); - static float elasticEaseIn(float t, float b, float c, float d, float a = 0, float p = 0); - static float elasticEaseOut(float t, float b, float c, float d, float a = 0, float p = 0); - static float elasticEaseInOut(float t, float b, float c, float d, float a = 0, float p = 0); + static float elasticEaseIn(float time, float *easingParam); + static float elasticEaseOut(float time, float *easingParam); + static float elasticEaseInOut(float time, float *easingParam); - static float backEaseIn(float t, float b, float c, float d, float s = 0); - static float backEaseOut(float t, float b, float c, float d, float s = 0); - static float backEaseInOut(float t, float b, float c, float d, float s = 0); + static float backEaseIn(float time); + static float backEaseOut(float time); + static float backEaseInOut(float time); - static float bounceEaseIn(float t, float b, float c, float d); - static float bounceEaseOut(float t, float b, float c, float d); - static float bounceEaseInOut(float t, float b, float c, float d); + static float bounceEaseIn(float time); + static float bounceEaseOut(float time); + static float bounceEaseInOut(float time); + + static float customEase(float time, float *easingParam); }; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp index d1b11e0623..059313e410 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.cpp @@ -51,7 +51,7 @@ Layer *CreateLayer(int index) case TEST_USE_DIFFERENT_PICTURE: pLayer = new TestUseMutiplePicture(); break; - case TEST_BCOLLIDER_DETECTOR: + case TEST_COLLIDER_DETECTOR: pLayer = new TestColliderDetector(); break; case TEST_BOUDINGBOX: @@ -66,6 +66,15 @@ Layer *CreateLayer(int index) case TEST_ARMATURE_NESTING_2: pLayer = new TestArmatureNesting2(); break; + case TEST_PLAY_SEVERAL_MOVEMENT: + pLayer = new TestPlaySeveralMovement(); + break; + case TEST_EASING: + pLayer = new TestEasing(); + break; + case TEST_CHANGE_ANIMATION_INTERNAL: + pLayer = new TestChangeAnimationInternal(); + break; default: break; } @@ -251,15 +260,7 @@ void TestAsynchronousLoading::onEnter() ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/horse.ExportJson", this, schedule_selector(TestAsynchronousLoading::dataLoaded)); ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/bear.ExportJson", this, schedule_selector(TestAsynchronousLoading::dataLoaded)); ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/HeroAnimation.ExportJson", this, schedule_selector(TestAsynchronousLoading::dataLoaded)); - - //! load data directly - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/knight.png", "armature/knight.plist", "armature/knight.xml"); - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml"); - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/robot.png", "armature/robot.plist", "armature/robot.xml"); - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml"); - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml"); - // ArmatureDataManager::getInstance()->addArmatureFileInfo("armature/Cowboy.ExportJson"); - + ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/testEasing.ExportJson", this, schedule_selector(TestAsynchronousLoading::dataLoaded)); } std::string TestAsynchronousLoading::title() const @@ -526,7 +527,7 @@ void TestAnimationEvent::onEnter() * Set armature's movement event callback function * To disconnect this event, just setMovementEventCallFunc(nullptr, nullptr); */ - armature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(TestAnimationEvent::animationEvent)); + armature->getAnimation()->setMovementEventCallFunc(CC_CALLBACK_0(TestAnimationEvent::animationEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); addChild(armature); } std::string TestAnimationEvent::title() const @@ -580,10 +581,9 @@ void TestFrameEvent::onEnter() /* * Set armature's frame event callback function - * To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr); + * To disconnect this event, just setFrameEventCallFunc(nullptr); */ - armature->getAnimation()->setFrameEventCallFunc(this, frameEvent_selector(TestFrameEvent::onFrameEvent)); - + armature->getAnimation()->setFrameEventCallFunc(CC_CALLBACK_0(TestFrameEvent::onFrameEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); addChild(armature); schedule( schedule_selector(TestFrameEvent::checkAction) ); @@ -750,10 +750,9 @@ void TestColliderDetector::onEnter() /* * Set armature's frame event callback function - * To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr); + * To disconnect this event, just setFrameEventCallFunc(nullptr); */ - armature->getAnimation()->setFrameEventCallFunc(this, frameEvent_selector(TestColliderDetector::onFrameEvent)); - + armature->getAnimation()->setFrameEventCallFunc(CC_CALLBACK_0(TestColliderDetector::onFrameEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); addChild(armature); armature2 = Armature::create("Cowboy"); @@ -763,7 +762,11 @@ void TestColliderDetector::onEnter() armature2->setPosition(Point(VisibleRect::right().x - 60, VisibleRect::left().y)); addChild(armature2); +#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT bullet = cocos2d::extension::PhysicsSprite::createWithSpriteFrameName("25.png"); +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX + bullet = Sprite::createWithSpriteFrameName("25.png"); +#endif addChild(bullet); initWorld(); @@ -846,7 +849,7 @@ void TestColliderDetector::onExit() } void TestColliderDetector::draw() { - ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); + GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION ); kmGLPushMatrix(); world->DrawDebugData(); kmGLPopMatrix(); @@ -861,8 +864,7 @@ void TestColliderDetector::update(float delta) { Contact &contact = *it; - Bone *ba = (Bone *)contact.fixtureA->GetUserData(); - Bone *bb = (Bone *)contact.fixtureB->GetUserData(); + Bone *bb = static_cast(contact.fixtureB->GetUserData); bb->getArmature()->setVisible(false); } @@ -1013,6 +1015,62 @@ void TestColliderDetector::initWorld() cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, nullptr, nullptr, endHit, nullptr); } +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX +void TestColliderDetector::update(float delta) +{ + armature2->setVisible(true); + + Rect rect = bullet->getBoundingBox(); + + // This code is just telling how to get the vertex. + // For a more accurate collider detection, you need to implemente yourself. + const Map& map = armature2->getBoneDic(); + for(auto element : map) + { + Bone *bone = element.second; + ColliderDetector *detector = bone->getColliderDetector(); + + if (!detector) + continue; + + const cocos2d::Vector& bodyList = detector->getColliderBodyList(); + + for (auto object : bodyList) + { + ColliderBody *body = static_cast(object); + const std::vector &vertexList = body->getCalculatedVertexList(); + + float minx, miny, maxx, maxy = 0; + int length = vertexList.size(); + for (int i = 0; i maxx ? vertex.x : maxx; + maxy = vertex.y > maxy ? vertex.y : maxy; + } + } + Rect temp = Rect(minx, miny, maxx - minx, maxy - miny); + + if (temp.intersectsRect(rect)) + { + armature2->setVisible(false); + } + } + } +} +void TestColliderDetector::draw() +{ + armature2->drawContour(); +} #endif @@ -1293,3 +1351,122 @@ Armature * TestArmatureNesting2::createMount(const char *name, Point position) return armature; } + +void TestPlaySeveralMovement::onEnter() +{ + ArmatureTestLayer::onEnter(); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestPlaySeveralMovement::onTouchesEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); + + std::string name[] = {"Walk", "FireMax", "Fire"}; + std::vector names(name, name+3); +// int index[] = {0, 1, 2}; +// std::vector indexes(index, index+3); + + Armature *armature = NULL; + armature = Armature::create("Cowboy"); + armature->getAnimation()->play(names); +// armature->getAnimation()->playByIndex(indexes); + armature->setScale(0.2f); + + armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y/*-100*/)); + addChild(armature); +} +std::string TestPlaySeveralMovement::title() const +{ + return "Test play several movement"; +} + +std::string TestPlaySeveralMovement::subtitle()const +{ + return "Movement is played one by one"; +} + + +void TestEasing::onEnter() +{ + ArmatureTestLayer::onEnter(); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestPlaySeveralMovement::onTouchesEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); + + animationID = 0; + + armature = Armature::create("testEasing"); + armature->getAnimation()->playByIndex(0); + armature->setScale(0.8f); + + armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y)); + addChild(armature); + + updateSubTitle(); +} + +std::string TestEasing::title() const +{ + return "Test easing effect"; +} +std::string TestEasing::subtitle() const +{ + return "Current easing : "; +} +void TestEasing::onTouchesEnded(const std::vector& touches, Event* event) +{ + animationID++; + animationID = animationID % armature->getAnimation()->getMovementCount(); + armature->getAnimation()->playByIndex(animationID); + + updateSubTitle(); +} +void TestEasing::updateSubTitle() +{ + std::string str = subtitle() + armature->getAnimation()->getCurrentMovementID(); + LabelTTF *label = (LabelTTF *)getChildByTag(10001); + label->setString(str.c_str()); +} + + + + +void TestChangeAnimationInternal::onEnter() +{ + ArmatureTestLayer::onEnter(); + + auto listener = EventListenerTouchAllAtOnce::create(); + listener->onTouchesEnded = CC_CALLBACK_2(TestPlaySeveralMovement::onTouchesEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); + + Armature *armature = NULL; + armature = Armature::create("Cowboy"); + armature->getAnimation()->playByIndex(0); + armature->setScale(0.2f); + + armature->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y)); + addChild(armature); +} +void TestChangeAnimationInternal::onExit() +{ + Director::getInstance()->setAnimationInterval(1/60.0f); +} +std::string TestChangeAnimationInternal::title() +{ + return "Test change animation internal"; +} +std::string TestChangeAnimationInternal::subtitle() +{ + return "Touch to change animation internal"; +} +void TestChangeAnimationInternal::onTouchesEnded(const std::vector& touches, Event* event) +{ + if (Director::getInstance()->getAnimationInterval() == 1/30.0f) + { + Director::getInstance()->setAnimationInterval(1/60.0f); + } + else + { + Director::getInstance()->setAnimationInterval(1/30.0f); + } +} diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h index 73765ab543..b118589f64 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioArmatureTest/ArmatureScene.h @@ -16,7 +16,7 @@ class ArmatureTestScene : public TestScene { -public: +public: ArmatureTestScene(bool bPortrait = false); virtual void runThisTest(); @@ -37,11 +37,14 @@ enum { TEST_FRAME_EVENT, TEST_PARTICLE_DISPLAY, TEST_USE_DIFFERENT_PICTURE, - TEST_BCOLLIDER_DETECTOR, + TEST_COLLIDER_DETECTOR, TEST_BOUDINGBOX, TEST_ANCHORPOINT, TEST_ARMATURE_NESTING, TEST_ARMATURE_NESTING_2, + TEST_PLAY_SEVERAL_MOVEMENT, + TEST_EASING, + TEST_CHANGE_ANIMATION_INTERNAL, TEST_LAYER_COUNT }; @@ -257,6 +260,25 @@ public: void destroyCPBody(cpBody *body); }; +#elif ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX +class TestColliderDetector : public ArmatureTestLayer +{ +public: + ~TestColliderDetector(); + + virtual void onEnter(); + virtual std::string title() const override; + virtual void update(float delta); + virtual void draw(); + + void onFrameEvent(cocostudio::Bone *bone, const char *evt, int originFrameIndex, int currentFrameIndex); + + void initWorld() {}; + cocostudio::Armature *armature; + cocostudio::Armature *armature2; + + cocos2d::Sprite *bullet; +}; #endif @@ -327,4 +349,39 @@ public: bool touchedMenu; }; + +class TestPlaySeveralMovement : public ArmatureTestLayer +{ +public: + virtual void onEnter(); + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + + +class TestEasing : public ArmatureTestLayer +{ +public: + virtual void onEnter(); + virtual std::string title() const override; + virtual std::string subtitle() const override; + + void onTouchesEnded(const std::vector& touches, Event* event); + void updateSubTitle(); + + int animationID; + cocostudio::Armature *armature; +}; + +class TestChangeAnimationInternal : public ArmatureTestLayer +{ +public: + virtual void onEnter(); + virtual void onExit(); + virtual std::string title(); + virtual std::string subtitle(); + + void onTouchesEnded(const std::vector& touches, Event* event); +}; + #endif // __HELLOWORLD_SCENE_H__