axmol/extensions/spine/SkeletonAnimation.cpp

328 lines
11 KiB
C++
Raw Normal View History

2014-10-15 11:23:02 +08:00
/******************************************************************************
2019-12-12 23:26:12 +08:00
* Spine Runtimes License Agreement
2020-01-05 03:09:32 +08:00
* Last updated January 1, 2020. Replaces all prior versions.
*
2020-01-05 03:09:32 +08:00
* Copyright (c) 2013-2020, Esoteric Software LLC
*
2019-12-12 23:26:12 +08:00
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
2019-12-12 23:26:12 +08:00
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
2020-01-05 03:09:32 +08:00
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2014-10-15 11:23:02 +08:00
*****************************************************************************/
#include <spine/SkeletonAnimation.h>
#include <spine/spine-cocos2dx.h>
2019-12-12 23:26:12 +08:00
#include <spine/Extension.h>
2014-10-15 11:23:02 +08:00
#include <algorithm>
USING_NS_AX;
2014-10-15 11:23:02 +08:00
using std::min;
using std::max;
using std::vector;
namespace spine {
typedef struct _TrackEntryListeners {
2020-01-05 03:09:32 +08:00
StartListener startListener;
InterruptListener interruptListener;
EndListener endListener;
DisposeListener disposeListener;
CompleteListener completeListener;
EventListener eventListener;
} _TrackEntryListeners;
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
void animationCallback (AnimationState* state, EventType type, TrackEntry* entry, Event* event) {
((SkeletonAnimation*)state->getRendererObject())->onAnimationStateEvent(entry, type, event);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
void trackEntryCallback (AnimationState* state, EventType type, TrackEntry* entry, Event* event) {
((SkeletonAnimation*)state->getRendererObject())->onTrackEntryEvent(entry, type, event);
2020-01-05 03:09:32 +08:00
if (type == EventType_Dispose) {
2019-12-12 23:26:12 +08:00
if (entry->getRendererObject()) {
delete (spine::_TrackEntryListeners*)entry->getRendererObject();
entry->setRendererObject(NULL);
}
2020-01-05 03:09:32 +08:00
}
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
static _TrackEntryListeners* getListeners (TrackEntry* entry) {
if (!entry->getRendererObject()) {
entry->setRendererObject(new spine::_TrackEntryListeners());
entry->setListener(trackEntryCallback);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
return (_TrackEntryListeners*)entry->getRendererObject();
2014-10-15 11:23:02 +08:00
}
2020-01-05 03:09:32 +08:00
2014-10-15 11:23:02 +08:00
//
2019-12-12 23:26:12 +08:00
SkeletonAnimation* SkeletonAnimation::createWithData (SkeletonData* skeletonData, bool ownsSkeletonData) {
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithData(skeletonData, ownsSkeletonData);
2014-10-15 11:23:02 +08:00
node->autorelease();
return node;
}
2019-12-12 23:26:12 +08:00
SkeletonAnimation* SkeletonAnimation::createWithJsonFile (const std::string& skeletonJsonFile, Atlas* atlas, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithJsonFile(skeletonJsonFile, atlas, scale);
2014-10-15 11:23:02 +08:00
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithJsonFile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
2019-12-12 23:26:12 +08:00
node->initWithJsonFile(skeletonJsonFile, atlasFile, scale);
2014-10-15 11:23:02 +08:00
node->autorelease();
return node;
}
2019-12-12 23:26:12 +08:00
SkeletonAnimation* SkeletonAnimation::createWithBinaryFile (const std::string& skeletonBinaryFile, Atlas* atlas, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithBinaryFile(skeletonBinaryFile, atlas, scale);
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithBinaryFile (const std::string& skeletonBinaryFile, const std::string& atlasFile, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
2019-12-12 23:26:12 +08:00
node->initWithBinaryFile(skeletonBinaryFile, atlasFile, scale);
node->autorelease();
return node;
}
2014-10-15 11:23:02 +08:00
void SkeletonAnimation::initialize () {
super::initialize();
2014-10-15 11:23:02 +08:00
_ownsAnimationStateData = true;
2020-01-05 03:09:32 +08:00
_updateOnlyIfVisible = false;
2019-12-12 23:26:12 +08:00
_state = new (__FILE__, __LINE__) AnimationState(new (__FILE__, __LINE__) AnimationStateData(_skeleton->getData()));
_state->setRendererObject(this);
_state->setListener(animationCallback);
_firstDraw = true;
2014-10-15 11:23:02 +08:00
}
2015-04-14 15:23:16 +08:00
SkeletonAnimation::SkeletonAnimation ()
: SkeletonRenderer() {
}
2014-10-15 11:23:02 +08:00
SkeletonAnimation::~SkeletonAnimation () {
2019-12-12 23:26:12 +08:00
if (_ownsAnimationStateData) delete _state->getData();
delete _state;
2014-10-15 11:23:02 +08:00
}
void SkeletonAnimation::update (float deltaTime) {
2020-01-05 03:09:32 +08:00
if (_updateOnlyIfVisible && !isVisible()) return;
2014-10-15 11:23:02 +08:00
super::update(deltaTime);
deltaTime *= _timeScale;
2020-01-05 03:09:32 +08:00
if (_preUpdateListener) _preUpdateListener(this);
2019-12-12 23:26:12 +08:00
_state->update(deltaTime);
_state->apply(*_skeleton);
_skeleton->updateWorldTransform();
2020-01-05 03:09:32 +08:00
if (_postUpdateListener) _postUpdateListener(this);
2019-12-12 23:26:12 +08:00
}
void SkeletonAnimation::draw(axis::Renderer *renderer, const axis::Mat4 &transform, uint32_t transformFlags) {
2019-12-12 23:26:12 +08:00
if (_firstDraw) {
_firstDraw = false;
update(0);
}
super::draw(renderer, transform, transformFlags);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setAnimationStateData (AnimationStateData* stateData) {
2014-10-15 11:23:02 +08:00
CCASSERT(stateData, "stateData cannot be null.");
2020-01-05 03:09:32 +08:00
if (_ownsAnimationStateData) delete _state->getData();
2019-12-12 23:26:12 +08:00
delete _state;
2014-10-15 11:23:02 +08:00
_ownsAnimationStateData = false;
2019-12-12 23:26:12 +08:00
_state = new (__FILE__, __LINE__) AnimationState(stateData);
_state->setRendererObject(this);
_state->setListener(animationCallback);
2014-10-15 11:23:02 +08:00
}
void SkeletonAnimation::setMix (const std::string& fromAnimation, const std::string& toAnimation, float duration) {
2019-12-12 23:26:12 +08:00
_state->getData()->setMix(fromAnimation.c_str(), toAnimation.c_str(), duration);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
TrackEntry* SkeletonAnimation::setAnimation (int trackIndex, const std::string& name, bool loop) {
Animation* animation = _skeleton->getData()->findAnimation(name.c_str());
2014-10-15 11:23:02 +08:00
if (!animation) {
log("Spine: Animation not found: %s", name.c_str());
return 0;
}
2019-12-12 23:26:12 +08:00
return _state->setAnimation(trackIndex, animation, loop);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
TrackEntry* SkeletonAnimation::addAnimation (int trackIndex, const std::string& name, bool loop, float delay) {
Animation* animation = _skeleton->getData()->findAnimation(name.c_str());
2014-10-15 11:23:02 +08:00
if (!animation) {
log("Spine: Animation not found: %s", name.c_str());
return 0;
}
2019-12-12 23:26:12 +08:00
return _state->addAnimation(trackIndex, animation, loop, delay);
2014-10-15 11:23:02 +08:00
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
TrackEntry* SkeletonAnimation::setEmptyAnimation (int trackIndex, float mixDuration) {
return _state->setEmptyAnimation(trackIndex, mixDuration);
2017-11-02 09:06:13 +08:00
}
void SkeletonAnimation::setEmptyAnimations (float mixDuration) {
2019-12-12 23:26:12 +08:00
_state->setEmptyAnimations(mixDuration);
2017-11-02 09:06:13 +08:00
}
2019-12-12 23:26:12 +08:00
TrackEntry* SkeletonAnimation::addEmptyAnimation (int trackIndex, float mixDuration, float delay) {
return _state->addEmptyAnimation(trackIndex, mixDuration, delay);
2017-11-02 09:06:13 +08:00
}
2019-12-12 23:26:12 +08:00
Animation* SkeletonAnimation::findAnimation(const std::string& name) const {
return _skeleton->getData()->findAnimation(name.c_str());
}
2014-10-15 11:23:02 +08:00
2019-12-12 23:26:12 +08:00
TrackEntry* SkeletonAnimation::getCurrent (int trackIndex) {
return _state->getCurrent(trackIndex);
2014-10-15 11:23:02 +08:00
}
void SkeletonAnimation::clearTracks () {
2019-12-12 23:26:12 +08:00
_state->clearTracks();
2014-10-15 11:23:02 +08:00
}
void SkeletonAnimation::clearTrack (int trackIndex) {
2019-12-12 23:26:12 +08:00
_state->clearTrack(trackIndex);
2014-10-15 11:23:02 +08:00
}
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::onAnimationStateEvent (TrackEntry* entry, EventType type, Event* event) {
2014-10-15 11:23:02 +08:00
switch (type) {
2019-12-12 23:26:12 +08:00
case EventType_Start:
if (_startListener) _startListener(entry);
2014-10-15 11:23:02 +08:00
break;
2020-01-05 03:09:32 +08:00
case EventType_Interrupt:
if (_interruptListener) _interruptListener(entry);
break;
2019-12-12 23:26:12 +08:00
case EventType_End:
if (_endListener) _endListener(entry);
2014-10-15 11:23:02 +08:00
break;
2020-01-05 03:09:32 +08:00
case EventType_Dispose:
if (_disposeListener) _disposeListener(entry);
break;
2019-12-12 23:26:12 +08:00
case EventType_Complete:
if (_completeListener) _completeListener(entry);
2014-10-15 11:23:02 +08:00
break;
2019-12-12 23:26:12 +08:00
case EventType_Event:
if (_eventListener) _eventListener(entry, event);
2014-10-15 11:23:02 +08:00
break;
}
}
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::onTrackEntryEvent (TrackEntry* entry, EventType type, Event* event) {
if (!entry->getRendererObject()) return;
_TrackEntryListeners* listeners = (_TrackEntryListeners*)entry->getRendererObject();
2014-10-15 11:23:02 +08:00
switch (type) {
2019-12-12 23:26:12 +08:00
case EventType_Start:
if (listeners->startListener) listeners->startListener(entry);
2014-10-15 11:23:02 +08:00
break;
2020-01-05 03:09:32 +08:00
case EventType_Interrupt:
if (listeners->interruptListener) listeners->interruptListener(entry);
break;
2019-12-12 23:26:12 +08:00
case EventType_End:
if (listeners->endListener) listeners->endListener(entry);
2014-10-15 11:23:02 +08:00
break;
2020-01-05 03:09:32 +08:00
case EventType_Dispose:
if (listeners->disposeListener) listeners->disposeListener(entry);
break;
2019-12-12 23:26:12 +08:00
case EventType_Complete:
if (listeners->completeListener) listeners->completeListener(entry);
2014-10-15 11:23:02 +08:00
break;
2019-12-12 23:26:12 +08:00
case EventType_Event:
if (listeners->eventListener) listeners->eventListener(entry, event);
2014-10-15 11:23:02 +08:00
break;
}
}
void SkeletonAnimation::setStartListener (const StartListener& listener) {
_startListener = listener;
}
2020-01-05 03:09:32 +08:00
void SkeletonAnimation::setInterruptListener (const InterruptListener& listener) {
2020-01-05 03:09:32 +08:00
_interruptListener = listener;
}
2020-01-05 03:09:32 +08:00
2014-10-15 11:23:02 +08:00
void SkeletonAnimation::setEndListener (const EndListener& listener) {
_endListener = listener;
}
2020-01-05 03:09:32 +08:00
void SkeletonAnimation::setDisposeListener (const DisposeListener& listener) {
2020-01-05 03:09:32 +08:00
_disposeListener = listener;
}
2014-10-15 11:23:02 +08:00
void SkeletonAnimation::setCompleteListener (const CompleteListener& listener) {
_completeListener = listener;
}
void SkeletonAnimation::setEventListener (const EventListener& listener) {
_eventListener = listener;
}
2020-01-05 03:09:32 +08:00
void SkeletonAnimation::setPreUpdateWorldTransformsListener(const UpdateWorldTransformsListener &listener) {
_preUpdateListener = listener;
}
void SkeletonAnimation::setPostUpdateWorldTransformsListener(const UpdateWorldTransformsListener &listener) {
_postUpdateListener = listener;
}
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackStartListener (TrackEntry* entry, const StartListener& listener) {
2014-10-15 11:23:02 +08:00
getListeners(entry)->startListener = listener;
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackInterruptListener (TrackEntry* entry, const InterruptListener& listener) {
2020-01-05 03:09:32 +08:00
getListeners(entry)->interruptListener = listener;
}
2014-10-15 11:23:02 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackEndListener (TrackEntry* entry, const EndListener& listener) {
2014-10-15 11:23:02 +08:00
getListeners(entry)->endListener = listener;
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackDisposeListener (TrackEntry* entry, const DisposeListener& listener) {
2020-01-05 03:09:32 +08:00
getListeners(entry)->disposeListener = listener;
}
2014-10-15 11:23:02 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackCompleteListener (TrackEntry* entry, const CompleteListener& listener) {
2014-10-15 11:23:02 +08:00
getListeners(entry)->completeListener = listener;
}
2019-12-12 23:26:12 +08:00
void SkeletonAnimation::setTrackEventListener (TrackEntry* entry, const EventListener& listener) {
2014-10-15 11:23:02 +08:00
getListeners(entry)->eventListener = listener;
}
2019-12-12 23:26:12 +08:00
AnimationState* SkeletonAnimation::getState() const {
2014-10-15 11:23:02 +08:00
return _state;
}
2020-01-05 03:09:32 +08:00
void SkeletonAnimation::setUpdateOnlyIfVisible(bool status) {
_updateOnlyIfVisible = status;
}
2014-10-15 11:23:02 +08:00
}