2014-06-04 18:17:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-17 15:28:14 +08:00
|
|
|
#include "3d/CCAnimate3D.h"
|
|
|
|
#include "3d/CCSprite3D.h"
|
2014-07-30 18:32:34 +08:00
|
|
|
#include "3d/CCSkeleton3D.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2015-03-26 11:30:22 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2015-07-17 11:08:27 +08:00
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-06-04 18:17:09 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-03-26 09:12:05 +08:00
|
|
|
std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeInAnimates;
|
|
|
|
std::unordered_map<Node*, Animate3D*> Animate3D::s_fadeOutAnimates;
|
|
|
|
std::unordered_map<Node*, Animate3D*> Animate3D::s_runningAnimates;
|
2014-08-29 18:25:42 +08:00
|
|
|
float Animate3D::_transTime = 0.1f;
|
2014-08-28 18:53:15 +08:00
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
//create Animate3D using Animation.
|
|
|
|
Animate3D* Animate3D::create(Animation3D* animation)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto animate = new (std::nothrow) Animate3D();
|
2015-03-09 16:29:57 +08:00
|
|
|
animate->init(animation);
|
2014-06-04 18:17:09 +08:00
|
|
|
animate->autorelease();
|
|
|
|
|
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
2014-06-20 18:54:09 +08:00
|
|
|
Animate3D* Animate3D::create(Animation3D* animation, float fromTime, float duration)
|
2014-06-20 12:01:54 +08:00
|
|
|
{
|
2015-03-09 16:29:57 +08:00
|
|
|
auto animate = new (std::nothrow) Animate3D();
|
|
|
|
animate->init(animation, fromTime, duration);
|
|
|
|
animate->autorelease();
|
2014-06-20 18:54:09 +08:00
|
|
|
|
2015-03-09 16:29:57 +08:00
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
|
|
|
Animate3D* Animate3D::createWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
|
|
|
|
{
|
|
|
|
auto animate = new (std::nothrow) Animate3D();
|
|
|
|
animate->initWithFrames(animation, startFrame, endFrame, frameRate);
|
|
|
|
animate->autorelease();
|
|
|
|
|
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animate3D::init(Animation3D* animation)
|
|
|
|
{
|
|
|
|
_animation = animation;
|
|
|
|
animation->retain();
|
|
|
|
setDuration(animation->getDuration());
|
|
|
|
setOriginInterval(animation->getDuration());
|
2015-04-24 11:49:28 +08:00
|
|
|
setQuality(Configuration::getInstance()->getAnimate3DQuality());
|
2015-03-09 16:29:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animate3D::init(Animation3D* animation, float fromTime, float duration)
|
|
|
|
{
|
2014-06-20 18:54:09 +08:00
|
|
|
float fullDuration = animation->getDuration();
|
2014-06-20 12:01:54 +08:00
|
|
|
if (duration > fullDuration - fromTime)
|
2014-10-10 17:44:45 +08:00
|
|
|
duration = fullDuration - fromTime;
|
|
|
|
|
2015-03-09 16:29:57 +08:00
|
|
|
_start = fromTime / fullDuration;
|
|
|
|
_last = duration / fullDuration;
|
|
|
|
setDuration(duration);
|
|
|
|
setOriginInterval(duration);
|
|
|
|
_animation = animation;
|
|
|
|
animation->retain();
|
2015-04-24 11:49:28 +08:00
|
|
|
setQuality(Configuration::getInstance()->getAnimate3DQuality());
|
2015-03-09 16:29:57 +08:00
|
|
|
return true;
|
2014-10-10 17:44:45 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 16:29:57 +08:00
|
|
|
bool Animate3D::initWithFrames(Animation3D* animation, int startFrame, int endFrame, float frameRate)
|
2014-10-10 17:44:45 +08:00
|
|
|
{
|
|
|
|
float perFrameTime = 1.f / frameRate;
|
|
|
|
float fromTime = startFrame * perFrameTime;
|
|
|
|
float duration = (endFrame - startFrame) * perFrameTime;
|
2015-07-10 15:50:09 +08:00
|
|
|
_frameRate = frameRate;
|
2015-03-09 16:29:57 +08:00
|
|
|
init(animation, fromTime, duration);
|
|
|
|
return true;
|
2014-06-20 12:01:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
/** returns a clone of action */
|
|
|
|
Animate3D* Animate3D::clone() const
|
|
|
|
{
|
|
|
|
auto animate = const_cast<Animate3D*>(this);
|
2014-06-17 19:18:56 +08:00
|
|
|
auto copy = Animate3D::create(animate->_animation);
|
2014-06-04 18:17:09 +08:00
|
|
|
|
2014-07-09 09:53:15 +08:00
|
|
|
copy->_absSpeed = _absSpeed;
|
2014-06-20 17:55:25 +08:00
|
|
|
copy->_weight = _weight;
|
2014-06-04 18:17:09 +08:00
|
|
|
copy->_elapsed = _elapsed;
|
2014-06-20 12:01:54 +08:00
|
|
|
copy->_start = _start;
|
|
|
|
copy->_last = _last;
|
2014-07-09 09:53:15 +08:00
|
|
|
copy->_playReverse = _playReverse;
|
2014-06-26 18:35:12 +08:00
|
|
|
copy->setDuration(animate->getDuration());
|
2015-01-15 11:32:28 +08:00
|
|
|
copy->setOriginInterval(animate->getOriginInterval());
|
2014-06-04 18:17:09 +08:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns a new action that performs the exactly the reverse action */
|
|
|
|
Animate3D* Animate3D::reverse() const
|
|
|
|
{
|
|
|
|
auto animate = clone();
|
2014-07-09 09:53:15 +08:00
|
|
|
animate->_playReverse = !animate->_playReverse;
|
2014-06-04 18:17:09 +08:00
|
|
|
return animate;
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:12:05 +08:00
|
|
|
Node* findChildByNameRecursively(Node* node, const std::string &childName)
|
|
|
|
{
|
|
|
|
const std::string& name = node->getName();
|
|
|
|
if (name == childName)
|
|
|
|
return node;
|
|
|
|
|
|
|
|
const Vector<Node*>& children = node->getChildren();
|
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
|
|
|
Node* findNode = findChildByNameRecursively(child, childName);
|
|
|
|
if (findNode)
|
|
|
|
return findNode;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
//! called before the action start. It will also set the target.
|
|
|
|
void Animate3D::startWithTarget(Node *target)
|
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
bool needReMap = (_target != target);
|
2014-06-04 18:17:09 +08:00
|
|
|
ActionInterval::startWithTarget(target);
|
|
|
|
|
2015-04-20 19:33:16 +08:00
|
|
|
if (needReMap)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
_boneCurves.clear();
|
|
|
|
_nodeCurves.clear();
|
|
|
|
|
|
|
|
bool hasCurve = false;
|
|
|
|
Sprite3D* sprite = dynamic_cast<Sprite3D*>(target);
|
|
|
|
|
|
|
|
if(sprite)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
if (_animation)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves();
|
|
|
|
for (const auto& iter: boneCurves)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
const std::string& boneName = iter.first;
|
|
|
|
auto skin = sprite->getSkeleton();
|
|
|
|
if(skin)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
auto bone = skin->getBoneByName(boneName);
|
|
|
|
if (bone)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
|
|
|
auto curve = _animation->getBoneCurveByName(boneName);
|
2015-04-20 19:33:16 +08:00
|
|
|
_boneCurves[bone] = curve;
|
|
|
|
hasCurve = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Node* node = nullptr;
|
|
|
|
if (target->getName() == boneName)
|
|
|
|
node = target;
|
|
|
|
else
|
|
|
|
node = findChildByNameRecursively(target, boneName);
|
|
|
|
|
|
|
|
if (node)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
auto curve = _animation->getBoneCurveByName(boneName);
|
|
|
|
if (curve)
|
|
|
|
{
|
|
|
|
_nodeCurves[node] = curve;
|
|
|
|
hasCurve = true;
|
|
|
|
}
|
2015-03-26 09:12:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 19:33:16 +08:00
|
|
|
else
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
const std::unordered_map<std::string, Animation3D::Curve*>& boneCurves = _animation->getBoneCurves();
|
|
|
|
for (const auto& iter: boneCurves)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
const std::string& boneName = iter.first;
|
|
|
|
Node* node = nullptr;
|
|
|
|
if (target->getName() == boneName)
|
|
|
|
node = target;
|
|
|
|
else
|
|
|
|
node = findChildByNameRecursively(target, boneName);
|
|
|
|
|
|
|
|
if (node)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-20 19:33:16 +08:00
|
|
|
auto curve = _animation->getBoneCurveByName(boneName);
|
|
|
|
if (curve)
|
|
|
|
{
|
|
|
|
_nodeCurves[node] = curve;
|
|
|
|
hasCurve = true;
|
|
|
|
}
|
2015-03-26 09:12:05 +08:00
|
|
|
}
|
2015-04-20 19:33:16 +08:00
|
|
|
|
2015-03-26 09:12:05 +08:00
|
|
|
}
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2015-04-20 19:33:16 +08:00
|
|
|
|
|
|
|
if (!hasCurve)
|
|
|
|
{
|
2015-09-09 11:37:41 +08:00
|
|
|
CCLOG("warning: no animation found for the skeleton");
|
2015-04-20 19:33:16 +08:00
|
|
|
}
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
|
2015-04-20 17:49:01 +08:00
|
|
|
auto runningAction = s_runningAnimates.find(target);
|
2014-08-28 18:53:15 +08:00
|
|
|
if (runningAction != s_runningAnimates.end())
|
|
|
|
{
|
|
|
|
//make the running action fade out
|
|
|
|
auto action = (*runningAction).second;
|
2014-08-29 18:25:42 +08:00
|
|
|
if (action != this)
|
|
|
|
{
|
2015-03-03 11:04:34 +08:00
|
|
|
if (_transTime < 0.001f)
|
|
|
|
{
|
2015-04-20 17:49:01 +08:00
|
|
|
s_runningAnimates[target] = this;
|
2015-03-03 11:04:34 +08:00
|
|
|
_state = Animate3D::Animate3DState::Running;
|
|
|
|
_weight = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-20 17:49:01 +08:00
|
|
|
s_fadeOutAnimates[target] = action;
|
2015-03-03 11:04:34 +08:00
|
|
|
action->_state = Animate3D::Animate3DState::FadeOut;
|
|
|
|
action->_accTransTime = 0.0f;
|
|
|
|
action->_weight = 1.0f;
|
|
|
|
action->_lastTime = 0.f;
|
2016-05-09 15:21:19 +08:00
|
|
|
s_runningAnimates.erase(target);
|
2015-04-20 17:49:01 +08:00
|
|
|
s_fadeInAnimates[target] = this;
|
2015-03-03 11:04:34 +08:00
|
|
|
_accTransTime = 0.0f;
|
|
|
|
_state = Animate3D::Animate3DState::FadeIn;
|
|
|
|
_weight = 0.f;
|
|
|
|
_lastTime = 0.f;
|
|
|
|
}
|
2014-08-29 18:25:42 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-20 19:45:31 +08:00
|
|
|
auto it = s_fadeInAnimates.find(target);
|
|
|
|
if (it != s_fadeInAnimates.end())
|
|
|
|
{
|
|
|
|
s_fadeInAnimates.erase(it);
|
|
|
|
}
|
2015-04-20 17:49:01 +08:00
|
|
|
s_runningAnimates[target] = this;
|
2014-08-28 18:53:15 +08:00
|
|
|
_state = Animate3D::Animate3DState::Running;
|
|
|
|
_weight = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::stop()
|
|
|
|
{
|
2014-09-02 10:40:29 +08:00
|
|
|
removeFromMap();
|
2014-08-28 18:53:15 +08:00
|
|
|
|
|
|
|
ActionInterval::stop();
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//! called every frame with it's delta time. DON'T override unless you know what you are doing.
|
|
|
|
void Animate3D::step(float dt)
|
|
|
|
{
|
2015-01-14 10:40:47 +08:00
|
|
|
ActionInterval::step(dt);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
|
2015-07-17 11:08:27 +08:00
|
|
|
bool cmpEventInfoAsc(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
|
|
|
|
{
|
|
|
|
return info1->frame < info2->frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmpEventInfoDes(Animate3D::Animate3DDisplayedEventInfo* info1, Animate3D::Animate3DDisplayedEventInfo* info2)
|
|
|
|
{
|
|
|
|
return info1->frame > info2->frame;
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
void Animate3D::update(float t)
|
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
if (_target)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-29 18:25:42 +08:00
|
|
|
if (_state == Animate3D::Animate3DState::FadeIn && _lastTime > 0.f)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
|
|
|
_accTransTime += (t - _lastTime) * getDuration();
|
|
|
|
|
|
|
|
_weight = _accTransTime / _transTime;
|
|
|
|
if (_weight >= 1.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
_accTransTime = _transTime;
|
|
|
|
_weight = 1.0f;
|
2014-08-29 18:25:42 +08:00
|
|
|
_state = Animate3D::Animate3DState::Running;
|
2015-04-20 17:49:01 +08:00
|
|
|
s_fadeInAnimates.erase(_target);
|
|
|
|
s_runningAnimates[_target] = this;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
2014-08-29 18:25:42 +08:00
|
|
|
else if (_state == Animate3D::Animate3DState::FadeOut && _lastTime > 0.f)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
|
|
|
_accTransTime += (t - _lastTime) * getDuration();
|
|
|
|
|
|
|
|
_weight = 1 - _accTransTime / _transTime;
|
|
|
|
if (_weight <= 0.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2014-08-28 18:53:15 +08:00
|
|
|
_accTransTime = _transTime;
|
|
|
|
_weight = 0.0f;
|
|
|
|
|
2015-04-20 17:49:01 +08:00
|
|
|
s_fadeOutAnimates.erase(_target);
|
2015-07-13 16:10:48 +08:00
|
|
|
_target->stopAction(this);
|
|
|
|
return;
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
2015-07-10 15:50:09 +08:00
|
|
|
float lastTime = _lastTime;
|
2014-08-28 18:53:15 +08:00
|
|
|
_lastTime = t;
|
|
|
|
|
2015-04-24 11:49:28 +08:00
|
|
|
if (_quality != Animate3DQuality::QUALITY_NONE)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
if (_weight > 0.0f)
|
2015-03-26 09:12:05 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
float transDst[3], rotDst[4], scaleDst[3];
|
|
|
|
float* trans = nullptr, *rot = nullptr, *scale = nullptr;
|
2015-07-10 15:50:09 +08:00
|
|
|
if (_playReverse){
|
2015-04-24 11:49:28 +08:00
|
|
|
t = 1 - t;
|
2015-07-10 15:50:09 +08:00
|
|
|
lastTime = 1.0 - lastTime;
|
|
|
|
}
|
2015-04-24 11:49:28 +08:00
|
|
|
|
|
|
|
t = _start + t * _last;
|
2015-07-10 15:50:09 +08:00
|
|
|
lastTime = _start + lastTime * _last;
|
2015-04-24 11:49:28 +08:00
|
|
|
|
|
|
|
for (const auto& it : _boneCurves) {
|
|
|
|
auto bone = it.first;
|
|
|
|
auto curve = it.second;
|
|
|
|
if (curve->translateCurve)
|
|
|
|
{
|
|
|
|
curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
|
|
|
|
trans = &transDst[0];
|
|
|
|
}
|
|
|
|
if (curve->rotCurve)
|
|
|
|
{
|
|
|
|
curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
|
|
|
|
rot = &rotDst[0];
|
|
|
|
}
|
|
|
|
if (curve->scaleCurve)
|
|
|
|
{
|
|
|
|
curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
|
|
|
|
scale = &scaleDst[0];
|
|
|
|
}
|
|
|
|
bone->setAnimationValue(trans, rot, scale, this, _weight);
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
2015-04-24 11:49:28 +08:00
|
|
|
|
|
|
|
for (const auto& it : _nodeCurves)
|
2014-08-28 18:53:15 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
auto node = it.first;
|
|
|
|
auto curve = it.second;
|
|
|
|
Mat4 transform;
|
|
|
|
if (curve->translateCurve)
|
|
|
|
{
|
|
|
|
curve->translateCurve->evaluate(t, transDst, _translateEvaluate);
|
|
|
|
transform.translate(transDst[0], transDst[1], transDst[2]);
|
|
|
|
}
|
|
|
|
if (curve->rotCurve)
|
|
|
|
{
|
|
|
|
curve->rotCurve->evaluate(t, rotDst, _roteEvaluate);
|
|
|
|
Quaternion qua(rotDst[0], rotDst[1], rotDst[2], rotDst[3]);
|
|
|
|
transform.rotate(qua);
|
|
|
|
}
|
|
|
|
if (curve->scaleCurve)
|
|
|
|
{
|
|
|
|
curve->scaleCurve->evaluate(t, scaleDst, _scaleEvaluate);
|
|
|
|
transform.scale(scaleDst[0], scaleDst[1], scaleDst[2]);
|
|
|
|
}
|
|
|
|
node->setAdditionalTransform(&transform);
|
2014-08-28 18:53:15 +08:00
|
|
|
}
|
2015-07-17 11:08:27 +08:00
|
|
|
if (!_keyFrameUserInfos.empty()){
|
2015-07-10 15:50:09 +08:00
|
|
|
float prekeyTime = lastTime * getDuration() * _frameRate;
|
|
|
|
float keyTime = t * getDuration() * _frameRate;
|
2015-07-17 11:08:27 +08:00
|
|
|
std::vector<Animate3DDisplayedEventInfo*> eventInfos;
|
|
|
|
for (auto keyFrame : _keyFrameUserInfos)
|
|
|
|
{
|
|
|
|
if ((!_playReverse && keyFrame.first >= prekeyTime && keyFrame.first < keyTime)
|
|
|
|
|| (_playReverse && keyFrame.first >= keyTime && keyFrame.first < prekeyTime))
|
2015-07-13 11:43:13 +08:00
|
|
|
{
|
2015-07-17 11:08:27 +08:00
|
|
|
auto& frameEvent = _keyFrameEvent[keyFrame.first];
|
|
|
|
if (frameEvent == nullptr)
|
|
|
|
frameEvent = new (std::nothrow) EventCustom(Animate3DDisplayedNotification);
|
|
|
|
auto eventInfo = &_displayedEventInfo[keyFrame.first];
|
|
|
|
eventInfo->target = _target;
|
|
|
|
eventInfo->frame = keyFrame.first;
|
|
|
|
eventInfo->userInfo = &_keyFrameUserInfos[keyFrame.first];
|
|
|
|
eventInfos.push_back(eventInfo);
|
|
|
|
frameEvent->setUserData((void*)eventInfo);
|
|
|
|
}
|
2015-07-10 15:50:09 +08:00
|
|
|
}
|
2015-07-17 11:08:27 +08:00
|
|
|
std::sort(eventInfos.begin(), eventInfos.end(), _playReverse ? cmpEventInfoDes : cmpEventInfoAsc);
|
|
|
|
for (auto eventInfo : eventInfos) {
|
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(_keyFrameEvent[eventInfo->frame]);
|
2015-07-13 11:43:13 +08:00
|
|
|
}
|
2015-07-10 15:50:09 +08:00
|
|
|
}
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:53:15 +08:00
|
|
|
float Animate3D::getSpeed() const
|
|
|
|
{
|
|
|
|
return _playReverse ? -_absSpeed : _absSpeed;
|
|
|
|
}
|
|
|
|
void Animate3D::setSpeed(float speed)
|
|
|
|
{
|
|
|
|
_absSpeed = fabsf(speed);
|
|
|
|
_playReverse = speed < 0;
|
2015-01-15 11:32:28 +08:00
|
|
|
_duration = _originInterval / _absSpeed;
|
2014-07-09 09:53:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Animate3D::setWeight(float weight)
|
|
|
|
{
|
|
|
|
CCASSERT(weight >= 0.0f, "invalid weight");
|
|
|
|
_weight = fabsf(weight);
|
|
|
|
}
|
|
|
|
|
2015-01-15 11:32:28 +08:00
|
|
|
void Animate3D::setOriginInterval(float interval)
|
|
|
|
{
|
|
|
|
_originInterval = interval;
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:49:28 +08:00
|
|
|
void Animate3D::setQuality(Animate3DQuality quality)
|
2015-03-26 11:30:22 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
if (quality == Animate3DQuality::QUALITY_HIGH)
|
2015-03-26 11:30:22 +08:00
|
|
|
{
|
|
|
|
_translateEvaluate = EvaluateType::INT_LINEAR;
|
|
|
|
_roteEvaluate = EvaluateType::INT_QUAT_SLERP;
|
|
|
|
_scaleEvaluate = EvaluateType::INT_LINEAR;
|
|
|
|
}
|
2015-04-24 11:49:28 +08:00
|
|
|
else if(quality == Animate3DQuality::QUALITY_LOW)
|
2015-03-30 15:57:58 +08:00
|
|
|
{
|
|
|
|
_translateEvaluate = EvaluateType::INT_NEAR;
|
|
|
|
_roteEvaluate = EvaluateType::INT_NEAR;
|
|
|
|
_scaleEvaluate = EvaluateType::INT_NEAR;
|
|
|
|
}
|
2015-04-24 11:49:28 +08:00
|
|
|
_quality = quality;
|
2015-03-26 11:30:22 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 11:49:28 +08:00
|
|
|
Animate3DQuality Animate3D::getQuality() const
|
2015-03-26 11:30:22 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
return _quality;
|
2015-03-26 11:30:22 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 17:34:14 +08:00
|
|
|
const ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame) const
|
|
|
|
{
|
|
|
|
auto iter = _keyFrameUserInfos.find(keyFrame);
|
|
|
|
if (iter != _keyFrameUserInfos.end())
|
|
|
|
return &iter->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap* Animate3D::getKeyFrameUserInfo(int keyFrame)
|
2015-07-10 15:50:09 +08:00
|
|
|
{
|
|
|
|
auto iter = _keyFrameUserInfos.find(keyFrame);
|
|
|
|
if (iter != _keyFrameUserInfos.end())
|
2015-07-15 17:34:14 +08:00
|
|
|
return &iter->second;
|
2015-07-10 15:50:09 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-15 17:34:14 +08:00
|
|
|
void Animate3D::setKeyFrameUserInfo(int keyFrame, const ValueMap &userInfo)
|
2015-07-10 15:50:09 +08:00
|
|
|
{
|
|
|
|
_keyFrameUserInfos[keyFrame] = userInfo;
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
Animate3D::Animate3D()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _state(Animate3D::Animate3DState::Running)
|
|
|
|
, _animation(nullptr)
|
|
|
|
, _absSpeed(1.f)
|
2014-06-13 19:20:19 +08:00
|
|
|
, _weight(1.f)
|
2014-06-20 12:01:54 +08:00
|
|
|
, _start(0.f)
|
|
|
|
, _last(1.f)
|
2014-07-09 09:59:18 +08:00
|
|
|
, _playReverse(false)
|
2014-08-28 18:53:15 +08:00
|
|
|
, _accTransTime(0.0f)
|
|
|
|
, _lastTime(0.0f)
|
2015-01-15 11:32:28 +08:00
|
|
|
, _originInterval(0.0f)
|
2015-07-10 15:50:09 +08:00
|
|
|
, _frameRate(30.0f)
|
2014-06-04 18:17:09 +08:00
|
|
|
{
|
2015-04-24 11:49:28 +08:00
|
|
|
setQuality(Animate3DQuality::QUALITY_HIGH);
|
2014-06-04 18:17:09 +08:00
|
|
|
}
|
|
|
|
Animate3D::~Animate3D()
|
|
|
|
{
|
2014-09-02 10:40:29 +08:00
|
|
|
removeFromMap();
|
|
|
|
|
2015-07-17 11:08:27 +08:00
|
|
|
for (auto& it : _keyFrameEvent) {
|
|
|
|
delete it.second;
|
|
|
|
}
|
|
|
|
_keyFrameEvent.clear();
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
CC_SAFE_RELEASE(_animation);
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:40:29 +08:00
|
|
|
void Animate3D::removeFromMap()
|
|
|
|
{
|
|
|
|
//remove this action from map
|
|
|
|
if (_target)
|
|
|
|
{
|
2015-07-13 16:10:48 +08:00
|
|
|
auto it = s_fadeInAnimates.find(_target);
|
|
|
|
if (it != s_fadeInAnimates.end() && it->second == this)
|
|
|
|
s_fadeInAnimates.erase(it);
|
|
|
|
|
|
|
|
it = s_fadeOutAnimates.find(_target);
|
|
|
|
if (it != s_fadeOutAnimates.end() && it->second == this)
|
|
|
|
s_fadeOutAnimates.erase(it);
|
|
|
|
|
|
|
|
it = s_runningAnimates.find(_target);
|
|
|
|
if (it != s_runningAnimates.end() && it->second == this)
|
|
|
|
s_runningAnimates.erase(it);
|
2014-09-02 10:40:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:17:09 +08:00
|
|
|
NS_CC_END
|