axmol/cocos/editor-support/cocosbuilder/CCBAnimationManager.cpp

1317 lines
34 KiB
C++
Raw Normal View History

#include "CCBAnimationManager.h"
#include "CCBSequence.h"
#include "CCBSequenceProperty.h"
#include "CCBReader.h"
#include "CCBKeyframe.h"
#include "CCNode+CCBRelativePositioning.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "audio/include/SimpleAudioEngine.h"
#include "CCBSelectorResolver.h"
#include <string>
#include <sstream>
#include <set>
using namespace cocos2d;
using namespace std;
using namespace cocos2d::extension;
namespace cocosbuilder {
// Implementation of CCBAinmationManager
CCBAnimationManager::CCBAnimationManager()
: _jsControlled(false)
, _owner(nullptr)
, _autoPlaySequenceId(0)
, _rootNode(nullptr)
, _rootContainerSize(Size::ZERO)
, _delegate(nullptr)
, _runningSequence(nullptr)
{
init();
}
bool CCBAnimationManager::init()
{
_target = nullptr;
_animationCompleteCallbackFunc = nullptr;
return true;
}
CCBAnimationManager::~CCBAnimationManager()
{
// DictElement *pElement = nullptr;
// CCDICT_FOREACH(_nodeSequences, pElement)
2013-01-23 15:18:13 +08:00
// {
// Node *node = (Node*)pElement->getIntKey();
2013-01-23 15:18:13 +08:00
// node->release();
// }
//
// CCDICT_FOREACH(_baseValues, pElement)
2013-01-23 15:18:13 +08:00
// {
// Node *node = (Node*)pElement->getIntKey();
2013-01-23 15:18:13 +08:00
// node->release();
// }
if (_rootNode)
{
_rootNode->stopAllActions();
}
setRootNode(nullptr);
setDelegate(nullptr);
for (auto iter = _objects.begin(); iter != _objects.end(); ++iter)
{
for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
{
iter2->second->release();
}
}
CC_SAFE_RELEASE(_target);
}
Vector<CCBSequence*>& CCBAnimationManager::getSequences()
{
return _sequences;
}
void CCBAnimationManager::setSequences(const Vector<CCBSequence*>& seq)
2012-11-22 15:15:30 +08:00
{
_sequences = seq;
2012-11-22 15:15:30 +08:00
}
int CCBAnimationManager::getAutoPlaySequenceId()
{
return _autoPlaySequenceId;
}
void CCBAnimationManager::setAutoPlaySequenceId(int autoPlaySequenceId)
{
_autoPlaySequenceId = autoPlaySequenceId;
}
Node* CCBAnimationManager::getRootNode()
{
return _rootNode;
}
void CCBAnimationManager::setRootNode(Node *pRootNode)
{
_rootNode = pRootNode;
}
void CCBAnimationManager::setDocumentControllerName(const std::string &name)
{
_documentControllerName = name;
}
std::string CCBAnimationManager::getDocumentControllerName()
{
return _documentControllerName;
}
void CCBAnimationManager::addDocumentCallbackNode(Node *node)
{
_documentCallbackNodes.pushBack(node);
}
void CCBAnimationManager::addDocumentCallbackName(std::string name)
{
_documentCallbackNames.push_back(Value(name));
}
void CCBAnimationManager::addDocumentCallbackControlEvents(Control::EventType eventType)
{
_documentCallbackControlEvents.push_back(Value(static_cast<int>(eventType)));
}
ValueVector& CCBAnimationManager::getDocumentCallbackNames()
{
return _documentCallbackNames;
}
Vector<Node*>& CCBAnimationManager::getDocumentCallbackNodes()
{
return _documentCallbackNodes;
}
ValueVector& CCBAnimationManager::getDocumentCallbackControlEvents()
{
return _documentCallbackControlEvents;
}
void CCBAnimationManager::addDocumentOutletNode(Node *node)
{
_documentOutletNodes.pushBack(node);
}
void CCBAnimationManager::addDocumentOutletName(std::string name)
{
_documentOutletNames.push_back(Value(name));
}
ValueVector& CCBAnimationManager::getDocumentOutletNames()
{
return _documentOutletNames;
}
Vector<Node*>& CCBAnimationManager::getDocumentOutletNodes()
{
return _documentOutletNodes;
}
std::string CCBAnimationManager::getLastCompletedSequenceName()
{
return _lastCompletedSequenceName;
}
ValueVector& CCBAnimationManager::getKeyframeCallbacks()
{
return _keyframeCallbacks;
}
const Size& CCBAnimationManager::getRootContainerSize()
{
return _rootContainerSize;
}
void CCBAnimationManager::setRootContainerSize(const Size &rootContainerSize)
{
_rootContainerSize.setSize(rootContainerSize.width, rootContainerSize.height);
}
CCBAnimationManagerDelegate* CCBAnimationManager::getDelegate()
{
return _delegate;
}
void CCBAnimationManager::setDelegate(CCBAnimationManagerDelegate *pDelegate)
{
CC_SAFE_RELEASE(dynamic_cast<Ref*>(_delegate));
_delegate = pDelegate;
CC_SAFE_RETAIN(dynamic_cast<Ref*>(_delegate));
}
const char* CCBAnimationManager::getRunningSequenceName()
{
if (_runningSequence)
2013-05-08 15:53:54 +08:00
{
return _runningSequence->getName();
2013-05-08 15:53:54 +08:00
}
return nullptr;
}
const Size& CCBAnimationManager::getContainerSize(Node *pNode)
{
if (pNode)
{
return pNode->getContentSize();
}
else
{
return _rootContainerSize;
}
}
// refer to CCBReader::readNodeGraph() for data structure of pSeq
void CCBAnimationManager::addNode(Node *pNode, const std::unordered_map<int, Map<std::string, CCBSequenceProperty*>>& seq)
{
// pNode->retain();
_nodeSequences[pNode] = seq;
}
void CCBAnimationManager::setBaseValue(const Value& value, Node *pNode, const std::string& propName)
{
auto& props = _baseValues[pNode];
props[propName] = value;
}
const Value& CCBAnimationManager::getBaseValue(Node *pNode, const std::string& propName)
{
auto& props = _baseValues[pNode];
return props[propName];
}
void CCBAnimationManager::setObject(Ref* obj, Node *pNode, const std::string& propName)
{
auto& props = _objects[pNode];
auto iter = props.find(propName);
if (iter != props.end())
iter->second->release();
props[propName] = obj;
obj->retain();
}
Ref* CCBAnimationManager::getObject(Node *pNode, const std::string& propName)
{
auto& props = _objects[pNode];
auto iter = props.find(propName);
if (iter != props.end())
return iter->second;
return nullptr;
}
int CCBAnimationManager::getSequenceId(const char* pSequenceName)
{
string seqName(pSequenceName);
for (auto& seq : _sequences)
{
if (seqName.compare(seq->getName()) == 0)
{
return seq->getSequenceId();
}
}
return -1;
}
CCBSequence* CCBAnimationManager::getSequence(int nSequenceId)
{
for (auto& seq : _sequences)
{
if (seq->getSequenceId() == nSequenceId)
{
return seq;
}
}
return nullptr;
}
float CCBAnimationManager::getSequenceDuration(const char *pSequenceName)
{
int id = getSequenceId(pSequenceName);
if (id != -1)
return getSequence(id)->getDuration();
return 0;
}
void CCBAnimationManager::moveAnimationsFromNode(Node* fromNode, Node* toNode)
{
// Move base values
auto baseValueIter = _baseValues.find(fromNode);
if(baseValueIter != _baseValues.end())
{
_baseValues[toNode] = baseValueIter->second;
_baseValues.erase(baseValueIter);
// fromNode->release();
// toNode->retain();
}
auto objIter = _objects.find(fromNode);
if (objIter != _objects.end())
{
_objects[toNode] = objIter->second;
_objects.erase(objIter);
}
// Move seqs
auto seqsIter = _nodeSequences.find(fromNode);
if (seqsIter != _nodeSequences.end())
{
_nodeSequences[toNode] = seqsIter->second;
_nodeSequences.erase(seqsIter);
// fromNode->release();
// toNode->retain();
}
}
// Refer to CCBReader::readKeyframe() for the real type of value
ActionInterval* CCBAnimationManager::getAction(CCBKeyframe *pKeyframe0, CCBKeyframe *pKeyframe1, const std::string& propName, Node *pNode)
{
2012-09-17 18:49:05 +08:00
float duration = pKeyframe1->getTime() - (pKeyframe0 ? pKeyframe0->getTime() : 0);
if (propName == "rotationX")
{
return CCBRotateXTo::create(duration, pKeyframe1->getValue().asFloat());
}
else if (propName == "rotationY")
{
return CCBRotateYTo::create(duration, pKeyframe1->getValue().asFloat());
}
else if (propName == "rotation")
{
return CCBRotateTo::create(duration, pKeyframe1->getValue().asFloat());
}
else if (propName == "opacity")
{
return FadeTo::create(duration, pKeyframe1->getValue().asByte());
}
else if (propName == "color")
{
auto c = pKeyframe1->getValue().asValueMap();
unsigned char r = c["r"].asByte();
unsigned char g = c["g"].asByte();
unsigned char b = c["b"].asByte();
return TintTo::create(duration, r, g, b);
}
else if (propName == "visible")
{
if (pKeyframe1->getValue().asBool())
{
return Sequence::createWithTwoActions(DelayTime::create(duration), Show::create());
}
else
{
return Sequence::createWithTwoActions(DelayTime::create(duration), Hide::create());
}
}
else if (propName == "displayFrame")
{
return Sequence::createWithTwoActions(DelayTime::create(duration),
CCBSetSpriteFrame::create(static_cast<SpriteFrame*>(pKeyframe1->getObject())));
}
else if (propName == "position")
{
// Get position type
auto& array = getBaseValue(pNode, propName).asValueVector();
CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
// Get relative position
auto value = pKeyframe1->getValue().asValueVector();
float x = value[0].asFloat();
float y = value[1].asFloat();
Size containerSize = getContainerSize(pNode->getParent());
Vector2 absPos = getAbsolutePosition(Vector2(x,y), type, containerSize, propName);
return MoveTo::create(duration, absPos);
}
else if (propName == "scale")
{
// Get position type
auto& array = getBaseValue(pNode, propName).asValueVector();
CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
// Get relative scale
auto value = pKeyframe1->getValue().asValueVector();
float x = value[0].asFloat();
float y = value[1].asFloat();
if (type == CCBReader::ScaleType::MULTIPLY_RESOLUTION)
{
float resolutionScale = CCBReader::getResolutionScale();
x *= resolutionScale;
y *= resolutionScale;
}
return ScaleTo::create(duration, x, y);
}
else if (propName == "skew")
{
// Get relative skew
auto& value = pKeyframe1->getValue().asValueVector();
float x = value[0].asFloat();
float y = value[1].asFloat();
return SkewTo::create(duration, x, y);
}
else
{
log("CCBReader: Failed to create animation for property: %s", propName.c_str());
}
return nullptr;
}
void CCBAnimationManager::setAnimatedProperty(const std::string& propName, Node *pNode, const Value& value, Ref* obj, float fTweenDuration)
{
if (fTweenDuration > 0)
{
// Create a fake keyframe to generate the action from
CCBKeyframe *kf1 = new CCBKeyframe();
kf1->autorelease();
kf1->setObject(obj);
kf1->setValue(value);
kf1->setTime(fTweenDuration);
kf1->setEasingType(CCBKeyframe::EasingType::LINEAR);
// Animate
ActionInterval *tweenAction = getAction(nullptr, kf1, propName, pNode);
pNode->runAction(tweenAction);
}
else
{
// Just set the value
if (propName == "position")
{
// Get position type
auto& array = getBaseValue(pNode, propName).asValueVector();
CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
// Get relative position
auto& valueVector = value.asValueVector();
float x = valueVector[0].asFloat();
float y = valueVector[1].asFloat();
pNode->setPosition(getAbsolutePosition(Vector2(x,y), type, getContainerSize(pNode->getParent()), propName));
}
else if (propName == "scale")
{
2012-09-18 17:04:10 +08:00
// Get scale type
auto& array = getBaseValue(pNode, propName).asValueVector();
CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
// Get relative scale
auto& valueVector = value.asValueVector();
float x = valueVector[0].asFloat();
float y = valueVector[1].asFloat();
setRelativeScale(pNode, x, y, type, propName);
}
else if(propName == "skew")
{
// Get relative scale
auto& valueVector = value.asValueVector();
float x = valueVector[0].asFloat();
float y = valueVector[1].asFloat();
pNode->setSkewX(x);
pNode->setSkewY(y);
}
else
{
// [node setValue:value forKey:name];
2012-09-18 17:04:10 +08:00
// TODO only handle rotation, opacity, displayFrame, color
if (propName == "rotation")
{
float rotate = value.asFloat();
pNode->setRotation(rotate);
} else if(propName == "rotationX")
{
float rotate = value.asFloat();
2014-03-14 15:38:43 +08:00
pNode->setRotationSkewX(rotate);
}else if(propName == "rotationY")
{
float rotate = value.asFloat();
2014-03-14 15:38:43 +08:00
pNode->setRotationSkewY(rotate);
}
else if (propName == "opacity")
{
unsigned char opacity = value.asByte();
pNode->setOpacity(opacity);
}
else if (propName == "displayFrame")
2012-09-17 18:49:05 +08:00
{
2014-01-04 09:17:37 +08:00
static_cast<Sprite*>(pNode)->setSpriteFrame(static_cast<SpriteFrame*>(obj));
2012-09-17 18:49:05 +08:00
}
else if (propName == "color")
2012-09-17 18:49:05 +08:00
{
auto c = value.asValueMap();
unsigned char r = c["r"].asByte();
unsigned char g = c["g"].asByte();
unsigned char b = c["b"].asByte();
pNode->setColor(Color3B(r, g, b));
2012-09-17 18:49:05 +08:00
}
else if (propName == "visible")
{
bool visible = value.asBool();
pNode->setVisible(visible);
}
else
{
log("unsupported property name is %s", propName.c_str());
CCASSERT(false, "unsupported property now");
}
}
}
}
void CCBAnimationManager::setFirstFrame(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
{
auto& keyframes = pSeqProp->getKeyframes();
if (keyframes.empty())
{
// Use base value (no animation)
auto& baseValue = getBaseValue(pNode, pSeqProp->getName());
auto obj = getObject(pNode, pSeqProp->getName());
CCASSERT(!baseValue.isNull(), "No baseValue found for property");
setAnimatedProperty(pSeqProp->getName(), pNode, baseValue, obj, fTweenDuration);
}
else
{
// Use first keyframe
CCBKeyframe *keyframe = keyframes.at(0);
setAnimatedProperty(pSeqProp->getName(), pNode, keyframe->getValue(), keyframe->getObject(), fTweenDuration);
}
}
ActionInterval* CCBAnimationManager::getEaseAction(ActionInterval *pAction, CCBKeyframe::EasingType easingType, float fEasingOpt)
{
if (dynamic_cast<Sequence*>(pAction))
{
return pAction;
}
if (easingType == CCBKeyframe::EasingType::LINEAR)
{
return pAction;
}
else if (easingType == CCBKeyframe::EasingType::INSTANT)
2013-01-21 11:18:06 +08:00
{
return CCBEaseInstant::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::CUBIC_IN)
{
return EaseIn::create(pAction, fEasingOpt);
}
else if (easingType == CCBKeyframe::EasingType::CUBIC_OUT)
{
return EaseOut::create(pAction, fEasingOpt);
}
else if (easingType == CCBKeyframe::EasingType::CUBIC_INOUT)
{
return EaseInOut::create(pAction, fEasingOpt);
}
else if (easingType == CCBKeyframe::EasingType::BACK_IN)
{
return EaseBackIn::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::BACK_OUT)
{
return EaseBackOut::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::BACK_INOUT)
{
return EaseBackInOut::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::BOUNCE_IN)
{
return EaseBounceIn::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::BOUNCE_OUT)
{
return EaseBounceOut::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::BOUNCE_INOUT)
{
return EaseBounceInOut::create(pAction);
}
else if (easingType == CCBKeyframe::EasingType::ELASTIC_IN)
{
return EaseElasticIn::create(pAction, fEasingOpt);
}
else if (easingType == CCBKeyframe::EasingType::ELASTIC_OUT)
{
return EaseElasticOut::create(pAction, fEasingOpt);
}
else if (easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
{
return EaseElasticInOut::create(pAction, fEasingOpt);
}
else
{
log("CCBReader: Unkown easing type %d", easingType);
return pAction;
}
}
Sequence* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* channel) {
float lastKeyframeTime = 0;
Vector<FiniteTimeAction*> actions;
auto& keyframes = channel->getKeyframes();
2013-12-12 12:07:20 +08:00
ssize_t numKeyframes = keyframes.size();
for (long i = 0; i < numKeyframes; ++i)
{
CCBKeyframe *keyframe = keyframes.at(i);
float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
lastKeyframeTime = keyframe->getTime();
if(timeSinceLastKeyframe > 0) {
actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
}
auto& keyVal = keyframe->getValue().asValueVector();
std::string selectorName = keyVal[0].asString();
CCBReader::TargetType selectorTarget = (CCBReader::TargetType)keyVal[1].asInt();
if(_jsControlled) {
std::stringstream callbackName;
callbackName << static_cast<int>(selectorTarget);
callbackName << ":" + selectorName;
auto callback = _keyframeCallFuncs.at(callbackName.str());
if (nullptr != callback)
{
CallFunc* callbackClone = callback->clone();
if (callbackClone != nullptr)
{
actions.pushBack(callbackClone);
}
}
}
else
{
Ref* target = nullptr;
if(selectorTarget == CCBReader::TargetType::DOCUMENT_ROOT)
target = _rootNode;
else if (selectorTarget == CCBReader::TargetType::OWNER)
target = _owner;
if(target != nullptr)
{
if(selectorName.length() > 0)
{
SEL_CallFuncN selCallFunc = 0;
CCBSelectorResolver* targetAsCCBSelectorResolver = dynamic_cast<CCBSelectorResolver *>(target);
if(targetAsCCBSelectorResolver != nullptr)
{
selCallFunc = targetAsCCBSelectorResolver->onResolveCCBCCCallFuncSelector(target, selectorName.c_str ());
}
if(selCallFunc == 0)
{
CCLOG("Skipping selector '%s' since no CCBSelectorResolver is present.", selectorName.c_str());
}
else
{
auto savedTarget = std::make_shared<Vector<Ref*>>();
savedTarget->pushBack(target);
auto callback = CallFuncN::create([savedTarget, selCallFunc](Node* sender){
auto t = savedTarget->at(0);
(t->*selCallFunc)(sender);
});
actions.pushBack(callback);
}
}
else
{
CCLOG("Unexpected empty selector.");
}
}
}
}
if(actions.size() < 1) return nullptr;
return Sequence::create(actions);
}
Sequence* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) {
float lastKeyframeTime = 0;
Vector<FiniteTimeAction*> actions;
auto& keyframes = channel->getKeyframes();
2013-12-12 12:07:20 +08:00
ssize_t numKeyframes = keyframes.size();
for (int i = 0; i < numKeyframes; ++i)
{
CCBKeyframe *keyframe = keyframes.at(i);
float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
lastKeyframeTime = keyframe->getTime();
if(timeSinceLastKeyframe > 0) {
actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
}
stringstream ss (stringstream::in | stringstream::out);
auto& keyVal = keyframe->getValue().asValueVector();
std::string soundFile = keyVal[0].asString();
float pitch, pan, gain;
ss << keyVal[1].asString();
ss >> pitch;
ss.flush();
ss << keyVal[2].asString();
ss >> pan;
ss.flush();
ss << keyVal[3].asString();
ss >> gain;
ss.flush();
actions.pushBack(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
}
if(actions.size() < 1) return nullptr;
return Sequence::create(actions);
}
void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
{
auto& keyframes = pSeqProp->getKeyframes();
2013-12-12 12:07:20 +08:00
ssize_t numKeyframes = keyframes.size();
if (numKeyframes > 1)
{
// Make an animation!
Vector<FiniteTimeAction*> actions;
CCBKeyframe *keyframeFirst = keyframes.at(0);
float timeFirst = keyframeFirst->getTime() + fTweenDuration;
if (timeFirst > 0)
{
actions.pushBack(DelayTime::create(timeFirst));
}
2013-12-12 12:07:20 +08:00
for (ssize_t i = 0; i < numKeyframes - 1; ++i)
{
CCBKeyframe *kf0 = keyframes.at(i);
CCBKeyframe *kf1 = keyframes.at(i+1);
ActionInterval *action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
if (action)
{
// Apply easing
action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
actions.pushBack(action);
}
}
auto seq = Sequence::create(actions);
pNode->runAction(seq);
}
}
void CCBAnimationManager::runAnimations(const char *pName, float fTweenDuration)
{
runAnimationsForSequenceNamedTweenDuration(pName, fTweenDuration);
}
void CCBAnimationManager::runAnimations(const char *pName)
{
runAnimationsForSequenceNamed(pName);
}
void CCBAnimationManager::runAnimations(int nSeqId, float fTweenDuraiton)
{
runAnimationsForSequenceIdTweenDuration(nSeqId, fTweenDuraiton);
}
void CCBAnimationManager::runAnimationsForSequenceIdTweenDuration(int nSeqId, float fTweenDuration)
{
CCASSERT(nSeqId != -1, "Sequence id couldn't be found");
_rootNode->stopAllActions();
for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
{
Node *node = nodeSeqIter->first;
node->stopAllActions();
// Refer to CCBReader::readKeyframe() for the real type of value
auto seqs = nodeSeqIter->second;
auto seqNodeProps = seqs[nSeqId];
std::set<std::string> seqNodePropNames;
if (!seqNodeProps.empty())
{
// Reset nodes that have sequence node properties, and run actions on them
for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
{
const std::string propName = iter->first;
CCBSequenceProperty *seqProp = iter->second;
2012-09-18 17:04:10 +08:00
seqNodePropNames.insert(propName);
setFirstFrame(node, seqProp, fTweenDuration);
runAction(node, seqProp, fTweenDuration);
}
}
// Reset the nodes that may have been changed by other timelines
auto& nodeBaseValues = _baseValues[node];
if (!nodeBaseValues.empty())
{
for (auto iter = nodeBaseValues.begin(); iter != nodeBaseValues.end(); ++iter)
{
if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
{
setAnimatedProperty(iter->first, node, iter->second, nullptr, fTweenDuration);
}
}
}
auto& nodeObject = _objects[node];
if (!nodeObject.empty())
{
for (auto iter = nodeObject.begin(); iter != nodeObject.end(); ++iter)
{
if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
{
setAnimatedProperty(iter->first, node, Value(), iter->second, fTweenDuration);
}
}
}
}
// Make callback at end of sequence
CCBSequence *seq = getSequence(nSeqId);
Action *completeAction = Sequence::createWithTwoActions(DelayTime::create(seq->getDuration() + fTweenDuration),
CallFunc::create( CC_CALLBACK_0(CCBAnimationManager::sequenceCompleted,this)));
_rootNode->runAction(completeAction);
// Set the running scene
if(seq->getCallbackChannel() != nullptr) {
Action* action = (Action *)actionForCallbackChannel(seq->getCallbackChannel());
if(action != nullptr) {
_rootNode->runAction(action);
}
}
if(seq->getSoundChannel() != nullptr) {
Action* action = (Action *)actionForSoundChannel(seq->getSoundChannel());
if(action != nullptr) {
_rootNode->runAction(action);
}
}
_runningSequence = getSequence(nSeqId);
}
void CCBAnimationManager::runAnimationsForSequenceNamedTweenDuration(const char *pName, float fTweenDuration)
{
int seqId = getSequenceId(pName);
runAnimationsForSequenceIdTweenDuration(seqId, fTweenDuration);
}
void CCBAnimationManager::runAnimationsForSequenceNamed(const char *pName)
{
runAnimationsForSequenceNamedTweenDuration(pName, 0);
}
void CCBAnimationManager::debug()
{
}
void CCBAnimationManager::setAnimationCompletedCallback(Ref *target, SEL_CallFunc callbackFunc) {
if (target)
{
target->retain();
}
if (_target)
{
_target->release();
}
_target = target;
_animationCompleteCallbackFunc = callbackFunc;
}
void CCBAnimationManager::setCallFunc(CallFunc* callFunc, const std::string &callbackNamed)
{
_keyframeCallFuncs.insert(callbackNamed, callFunc);
}
void CCBAnimationManager::sequenceCompleted()
{
const char *runningSequenceName = _runningSequence->getName();
int nextSeqId = _runningSequence->getChainedSequenceId();
_runningSequence = nullptr;
if(_lastCompletedSequenceName != runningSequenceName) {
_lastCompletedSequenceName = runningSequenceName;
}
if (nextSeqId != -1)
{
runAnimationsForSequenceIdTweenDuration(nextSeqId, 0);
}
if (_delegate)
{
// There may be another runAnimation() call in this delegate method
// which will assign _runningSequence
_delegate->completedAnimationSequenceNamed(runningSequenceName);
}
if (_target && _animationCompleteCallbackFunc) {
(_target->*_animationCompleteCallbackFunc)();
}
}
2013-01-21 11:18:06 +08:00
// Custom actions
/************************************************************
CCBSetSpriteFrame
************************************************************/
CCBSetSpriteFrame* CCBSetSpriteFrame::create(SpriteFrame *pSpriteFrame)
{
CCBSetSpriteFrame *ret = new CCBSetSpriteFrame();
if (ret)
{
if (ret->initWithSpriteFrame(pSpriteFrame))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
}
return ret;
}
bool CCBSetSpriteFrame::initWithSpriteFrame(SpriteFrame *pSpriteFrame)
{
_spriteFrame = pSpriteFrame;
CC_SAFE_RETAIN(_spriteFrame);
return true;
}
CCBSetSpriteFrame::~CCBSetSpriteFrame()
{
CC_SAFE_RELEASE_NULL(_spriteFrame);
}
CCBSetSpriteFrame* CCBSetSpriteFrame::clone() const
{
// no copy constructor
auto a = new CCBSetSpriteFrame();
a->initWithSpriteFrame(_spriteFrame);
a->autorelease();
return a;
}
CCBSetSpriteFrame* CCBSetSpriteFrame::reverse() const
{
// returns a copy of itself
return this->clone();
}
void CCBSetSpriteFrame::update(float time)
{
2014-01-04 09:17:37 +08:00
static_cast<Sprite*>(_target)->setSpriteFrame(_spriteFrame);
}
/************************************************************
CCBSoundEffect
************************************************************/
CCBSoundEffect* CCBSoundEffect::actionWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
CCBSoundEffect* pRet = new CCBSoundEffect();
if (pRet != nullptr && pRet->initWithSoundFile(filename, pitch, pan, gain))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
CCBSoundEffect::~CCBSoundEffect()
{
}
bool CCBSoundEffect::initWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
_soundFile = filename;
_pitch = pitch;
_pan = pan;
_gain = gain;
return true;
}
CCBSoundEffect* CCBSoundEffect::clone() const
{
// no copy constructor
auto a = new CCBSoundEffect();
a->initWithSoundFile(_soundFile, _pitch, _pan, _gain);
a->autorelease();
return a;
}
CCBSoundEffect* CCBSoundEffect::reverse() const
{
// returns a copy of itself
return this->clone();
}
void CCBSoundEffect::update(float time)
{
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_soundFile.c_str());
}
/************************************************************
CCBRotateTo
************************************************************/
CCBRotateTo* CCBRotateTo::create(float fDuration, float fAngle)
{
CCBRotateTo *ret = new CCBRotateTo();
if (ret)
{
if (ret->initWithDuration(fDuration, fAngle))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
}
return ret;
}
bool CCBRotateTo::initWithDuration(float fDuration, float fAngle)
{
if (ActionInterval::initWithDuration(fDuration))
{
_dstAngle = fAngle;
return true;
}
else
{
return false;
}
}
CCBRotateTo* CCBRotateTo::clone() const
{
// no copy constructor
auto a = new CCBRotateTo();
a->initWithDuration(_duration, _dstAngle);
a->autorelease();
return a;
}
CCBRotateTo* CCBRotateTo::reverse() const
{
CCASSERT(false, "reverse() is not supported in CCBRotateTo");
return nullptr;
}
void CCBRotateTo::startWithTarget(Node *pNode)
{
ActionInterval::startWithTarget(pNode);
_startAngle = _target->getRotation();
_diffAngle = _dstAngle - _startAngle;
}
void CCBRotateTo::update(float time)
{
_target->setRotation(_startAngle + (_diffAngle * time))
2012-09-18 17:55:03 +08:00
;
}
/************************************************************
CCBRotateXTO
************************************************************/
CCBRotateXTo* CCBRotateXTo::create(float fDuration, float fAngle)
{
CCBRotateXTo *ret = new CCBRotateXTo();
if (ret)
{
if (ret->initWithDuration(fDuration, fAngle))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
}
return ret;
}
bool CCBRotateXTo::initWithDuration(float fDuration, float fAngle)
{
if (ActionInterval::initWithDuration(fDuration))
{
_dstAngle = fAngle;
return true;
}
else
{
return false;
}
}
void CCBRotateXTo::startWithTarget(Node *pNode)
{
//CCActionInterval::startWithTarget(pNode);
_originalTarget = pNode;
_target = pNode;
_elapsed = 0.0f;
_firstTick = true;
2014-03-14 15:38:43 +08:00
_startAngle = _target->getRotationSkewX();
_diffAngle = _dstAngle - _startAngle;
}
CCBRotateXTo* CCBRotateXTo::clone() const
{
// no copy constructor
auto a = new CCBRotateXTo();
a->initWithDuration(_duration, _dstAngle);
a->autorelease();
return a;
}
CCBRotateXTo* CCBRotateXTo::reverse() const
{
CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
return nullptr;
}
void CCBRotateXTo::update(float time)
{
2014-03-14 15:38:43 +08:00
_target->setRotationSkewX(_startAngle + (_diffAngle * time));
}
/************************************************************
CCBRotateYTO
************************************************************/
CCBRotateYTo* CCBRotateYTo::create(float fDuration, float fAngle)
{
CCBRotateYTo *ret = new CCBRotateYTo();
if (ret)
{
if (ret->initWithDuration(fDuration, fAngle))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
}
return ret;
}
bool CCBRotateYTo::initWithDuration(float fDuration, float fAngle)
{
if (ActionInterval::initWithDuration(fDuration))
{
_dstAngle = fAngle;
return true;
}
else
{
return false;
}
}
CCBRotateYTo* CCBRotateYTo::clone() const
{
// no copy constructor
auto a = new CCBRotateYTo();
a->initWithDuration(_duration, _dstAngle);
a->autorelease();
return a;
}
CCBRotateYTo* CCBRotateYTo::reverse() const
{
CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
return nullptr;
}
void CCBRotateYTo::startWithTarget(Node *pNode)
{
// ActionInterval::startWithTarget(pNode);
_originalTarget = pNode;
_target = pNode;
_elapsed = 0.0f;
_firstTick = true;
2014-03-14 15:38:43 +08:00
_startAngle = _target->getRotationSkewY();
_diffAngle = _dstAngle - _startAngle;
}
void CCBRotateYTo::update(float time)
{
2014-03-14 15:38:43 +08:00
_target->setRotationSkewY(_startAngle + (_diffAngle * time));
}
2013-01-21 11:18:06 +08:00
/************************************************************
CCBEaseInstant
************************************************************/
CCBEaseInstant* CCBEaseInstant::create(ActionInterval *pAction)
2013-01-21 11:18:06 +08:00
{
CCBEaseInstant *pRet = new CCBEaseInstant();
if (pRet && pRet->initWithAction(pAction))
{
pRet->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
}
return pRet;
}
CCBEaseInstant* CCBEaseInstant::clone() const
{
// no copy constructor
auto a = new CCBEaseInstant();
a->initWithAction(_inner);
a->autorelease();
return a;
}
CCBEaseInstant* CCBEaseInstant::reverse() const
{
return CCBEaseInstant::create(_inner->reverse());
}
2013-01-21 11:18:06 +08:00
void CCBEaseInstant::update(float dt)
{
if (dt < 0)
{
_inner->update(0);
2013-01-21 11:18:06 +08:00
}
else
{
_inner->update(1);
2013-01-21 11:18:06 +08:00
}
}
}