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

481 lines
11 KiB
C++
Raw Normal View History

2013-09-20 22:23:13 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "cocostudio/CCActionNode.h"
#include "cocostudio/CCActionFrameEasing.h"
#include "cocostudio/DictionaryHelper.h"
#include "gui/UIWidget.h"
#include "gui/UIHelper.h"
2013-09-20 22:23:13 +08:00
using namespace cocos2d;
using namespace gui;
namespace cocostudio {
2013-09-20 22:23:13 +08:00
ActionNode::ActionNode()
: _currentFrameIndex(0)
, _destFrameIndex(0)
, _fUnitTime(0.1f)
, _actionTag(0)
, _actionSpawn(nullptr)
, _action(nullptr)
, _object(nullptr)
, _frameArrayNum(0)
2013-09-20 22:23:13 +08:00
{
_frameArrayNum = (int)kKeyframeMax;
for(int i = 0; i < _frameArrayNum; i++)
{
2013-12-27 12:45:05 +08:00
_frameArray.push_back( new cocos2d::Vector<ActionFrame*>());
2013-09-20 22:23:13 +08:00
}
}
ActionNode::~ActionNode()
{
if (_action == nullptr)
2013-09-20 22:23:13 +08:00
{
CC_SAFE_RELEASE_NULL(_actionSpawn);
2013-09-20 22:23:13 +08:00
}
else
{
CC_SAFE_RELEASE_NULL(_action);
2013-09-20 22:23:13 +08:00
}
2013-12-27 12:45:05 +08:00
for (auto object : _frameArray)
{
object->clear();
}
_frameArray.clear();
2013-09-20 22:23:13 +08:00
}
void ActionNode::initWithDictionary(const rapidjson::Value& dic,Object* root)
2013-09-20 22:23:13 +08:00
{
setActionTag(DICTOOL->getIntValue_json(dic, "ActionTag"));
int actionFrameCount = DICTOOL->getArrayCount_json(dic, "actionframelist");
for (int i=0; i<actionFrameCount; i++) {
const rapidjson::Value& actionFrameDic = DICTOOL->getDictionaryFromArray_json(dic, "actionframelist", i);
2013-09-20 22:23:13 +08:00
int frameInex = DICTOOL->getIntValue_json(actionFrameDic,"frameid");
bool existPosition = DICTOOL->checkObjectExist_json(actionFrameDic,"positionx");
if (existPosition)
{
float positionX = DICTOOL->getFloatValue_json(actionFrameDic, "positionx");
float positionY = DICTOOL->getFloatValue_json(actionFrameDic, "positiony");
ActionMoveFrame* actionFrame = new ActionMoveFrame();
2013-12-27 12:45:05 +08:00
actionFrame->autorelease();
2013-09-20 22:23:13 +08:00
actionFrame->setFrameIndex(frameInex);
actionFrame->setPosition(Point(positionX, positionY));
2013-12-25 19:46:09 +08:00
auto cActionArray = _frameArray.at((int)kKeyframeMove);
2013-12-27 12:45:05 +08:00
cActionArray->pushBack(actionFrame);
2013-09-20 22:23:13 +08:00
}
bool existScale = DICTOOL->checkObjectExist_json(actionFrameDic,"scalex");
if (existScale)
{
float scaleX = DICTOOL->getFloatValue_json(actionFrameDic, "scalex");
float scaleY = DICTOOL->getFloatValue_json(actionFrameDic, "scaley");
ActionScaleFrame* actionFrame = new ActionScaleFrame();
2013-12-27 12:45:05 +08:00
actionFrame->autorelease();
2013-09-20 22:23:13 +08:00
actionFrame->setFrameIndex(frameInex);
actionFrame->setScaleX(scaleX);
actionFrame->setScaleY(scaleY);
2013-12-25 19:46:09 +08:00
auto cActionArray = _frameArray.at((int)kKeyframeMove);
2013-12-27 12:45:05 +08:00
cActionArray->pushBack(actionFrame);
2013-09-20 22:23:13 +08:00
}
bool existRotation = DICTOOL->checkObjectExist_json(actionFrameDic,"rotation");
if (existRotation)
{
float rotation = DICTOOL->getFloatValue_json(actionFrameDic, "rotation");
ActionRotationFrame* actionFrame = new ActionRotationFrame();
2013-12-27 12:45:05 +08:00
actionFrame->autorelease();
2013-09-20 22:23:13 +08:00
actionFrame->setFrameIndex(frameInex);
actionFrame->setRotation(rotation);
2013-12-25 19:46:09 +08:00
auto cActionArray = _frameArray.at((int)kKeyframeMove);
2013-12-27 12:45:05 +08:00
cActionArray->pushBack(actionFrame);
2013-09-20 22:23:13 +08:00
}
bool existOpacity = DICTOOL->checkObjectExist_json(actionFrameDic,"opacity");
if (existOpacity)
{
int opacity = DICTOOL->getIntValue_json(actionFrameDic, "opacity");
ActionFadeFrame* actionFrame = new ActionFadeFrame();
2013-12-27 12:45:05 +08:00
actionFrame->autorelease();
2013-09-20 22:23:13 +08:00
actionFrame->setFrameIndex(frameInex);
actionFrame->setOpacity(opacity);
2013-12-25 19:46:09 +08:00
auto cActionArray = _frameArray.at((int)kKeyframeMove);
2013-12-27 12:45:05 +08:00
cActionArray->pushBack(actionFrame);
2013-09-20 22:23:13 +08:00
}
bool existColor = DICTOOL->checkObjectExist_json(actionFrameDic,"colorr");
if (existColor)
{
int colorR = DICTOOL->getIntValue_json(actionFrameDic, "colorr");
int colorG = DICTOOL->getIntValue_json(actionFrameDic, "colorg");
int colorB = DICTOOL->getIntValue_json(actionFrameDic, "colorb");
ActionTintFrame* actionFrame = new ActionTintFrame();
2013-12-27 12:45:05 +08:00
actionFrame->autorelease();
2013-09-20 22:23:13 +08:00
actionFrame->setFrameIndex(frameInex);
actionFrame->setColor(Color3B(colorR,colorG,colorB));
2013-12-25 19:46:09 +08:00
auto cActionArray = _frameArray.at((int)kKeyframeMove);
2013-12-27 12:45:05 +08:00
cActionArray->pushBack(actionFrame);
2013-09-20 22:23:13 +08:00
}
}
initActionNodeFromRoot(root);
}
void ActionNode::initActionNodeFromRoot(Object* root)
{
Node* rootNode = dynamic_cast<Node*>(root);
if (rootNode != nullptr)
2013-09-20 22:23:13 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* rootWidget = dynamic_cast<Widget*>(root);
if (rootWidget != nullptr)
2013-09-20 22:23:13 +08:00
{
2013-12-23 15:02:52 +08:00
Widget* widget = UIHelper::seekActionWidgetByActionTag(rootWidget, getActionTag());
if (widget != nullptr)
2013-09-20 22:23:13 +08:00
{
setObject(widget);
}
}
2013-12-26 17:01:31 +08:00
}
2013-09-20 22:23:13 +08:00
}
void ActionNode::setUnitTime(float fTime)
{
_fUnitTime = fTime;
this->refreshActionProperty();
}
float ActionNode::getUnitTime()
{
return _fUnitTime;
}
void ActionNode::setActionTag(int tag)
{
_actionTag = tag;
}
int ActionNode::getActionTag()
{
return _actionTag;
}
void ActionNode::setObject(Object* node)
{
_object = node;
}
Object* ActionNode::getObject()
{
return _object;
}
Node* ActionNode::getActionNode()
{
Node* cNode = dynamic_cast<Node*>(_object);
if (cNode != nullptr)
2013-09-20 22:23:13 +08:00
{
return cNode;
}
else
{
2013-12-23 15:02:52 +08:00
Widget* rootWidget = dynamic_cast<Widget*>(_object);
if (rootWidget != nullptr)
2013-09-20 22:23:13 +08:00
{
2013-12-24 20:22:14 +08:00
return rootWidget;
2013-09-20 22:23:13 +08:00
}
}
return nullptr;
2013-09-20 22:23:13 +08:00
}
void ActionNode::insertFrame(int index, ActionFrame* frame)
{
if (frame == nullptr)
2013-09-20 22:23:13 +08:00
{
return;
}
int frameType = frame->getFrameType();
if(frameType < _frameArray.size())
{
auto cArray = _frameArray.at(frameType);
2013-12-27 12:45:05 +08:00
cArray->insert(index, frame);
}
2013-09-20 22:23:13 +08:00
}
void ActionNode::addFrame(ActionFrame* frame)
{
if (frame == nullptr)
2013-09-20 22:23:13 +08:00
{
return;
}
int frameType = frame->getFrameType();
if(frameType < _frameArray.size())
{
auto cArray = _frameArray.at(frameType);
2013-12-27 12:45:05 +08:00
cArray->pushBack(frame);
}
2013-09-20 22:23:13 +08:00
}
void ActionNode::deleteFrame(ActionFrame* frame)
{
if (frame == nullptr)
2013-09-20 22:23:13 +08:00
{
return;
}
int frameType = frame->getFrameType();
if(frameType < _frameArray.size())
{
auto cArray = _frameArray.at(frameType);
2013-12-27 12:45:05 +08:00
cArray->eraseObject(frame);
}
2013-09-20 22:23:13 +08:00
}
void ActionNode::clearAllFrame()
{
for(auto array : _frameArray)
{
2013-12-27 12:45:05 +08:00
array->clear();
}
2013-09-20 22:23:13 +08:00
}
Spawn * ActionNode::refreshActionProperty()
{
if ( _object == nullptr )
2013-09-20 22:23:13 +08:00
{
return nullptr;
2013-09-20 22:23:13 +08:00
}
Vector<FiniteTimeAction*> cSpawnArray;
2013-09-20 22:23:13 +08:00
for (int n = 0; n < _frameArrayNum; n++)
{
2013-12-25 19:46:09 +08:00
auto cArray = _frameArray.at(n);
2013-12-27 12:45:05 +08:00
if (cArray->size() <= 0)
2013-09-20 22:23:13 +08:00
{
continue;
}
Vector<FiniteTimeAction*> cSequenceArray;
2013-12-27 12:45:05 +08:00
auto frameCount = cArray->size();
2013-09-20 22:23:13 +08:00
for (int i = 0; i < frameCount; i++)
{
2013-12-27 12:45:05 +08:00
auto frame = cArray->at(i);
2013-09-20 22:23:13 +08:00
if (i == 0)
{
}
else
{
2013-12-27 12:45:05 +08:00
auto srcFrame = cArray->at(i-1);
2013-09-20 22:23:13 +08:00
float duration = (frame->getFrameIndex() - srcFrame->getFrameIndex()) * getUnitTime();
Action* cAction = frame->getAction(duration);
if(cAction != nullptr)
cSequenceArray.pushBack(static_cast<FiniteTimeAction*>(cAction));
2013-09-20 22:23:13 +08:00
}
}
Sequence* cSequence = Sequence::create(cSequenceArray);
if (cSequence != nullptr)
2013-09-23 14:34:50 +08:00
{
cSpawnArray.pushBack(cSequence);
2013-09-23 14:34:50 +08:00
}
2013-09-20 22:23:13 +08:00
}
if (_action == nullptr)
2013-09-20 22:23:13 +08:00
{
CC_SAFE_RELEASE_NULL(_actionSpawn);
2013-09-20 22:23:13 +08:00
}
else
{
CC_SAFE_RELEASE_NULL(_action);
2013-09-20 22:23:13 +08:00
}
_actionSpawn = Spawn::create(cSpawnArray);
CC_SAFE_RETAIN(_actionSpawn);
2013-09-20 22:23:13 +08:00
return _actionSpawn;
}
void ActionNode::playAction()
2013-09-20 22:23:13 +08:00
{
if ( _object == nullptr || _actionSpawn == nullptr)
2013-09-20 22:23:13 +08:00
{
return;
}
if (_action!=nullptr)
2013-09-20 22:23:13 +08:00
{
_action->release();
}
_action = Sequence::create(_actionSpawn, nullptr);
2013-09-20 22:23:13 +08:00
_action->retain();
this->runAction();
}
void ActionNode::runAction()
{
Node* cNode = this->getActionNode();
if (cNode != nullptr && _action != nullptr)
2013-09-20 22:23:13 +08:00
{
cNode->runAction(_action);
}
}
void ActionNode::stopAction()
{
Node* cNode = this->getActionNode();
if (cNode != nullptr && _action != nullptr)
2013-09-20 22:23:13 +08:00
{
cNode->stopAction(_action);
}
}
int ActionNode::getFirstFrameIndex()
{
int frameindex = 99999;
bool bFindFrame = false;
for (int n = 0; n < _frameArrayNum; n++)
{
2013-12-25 19:46:09 +08:00
auto cArray = _frameArray.at(n);
2013-12-27 12:45:05 +08:00
if (cArray->empty())
2013-09-20 22:23:13 +08:00
{
continue;
}
bFindFrame = true;
2013-12-27 12:45:05 +08:00
auto frame = cArray->at(0);
2013-09-20 22:23:13 +08:00
int iFrameIndex = frame->getFrameIndex();
if (frameindex > iFrameIndex)
{
frameindex = iFrameIndex;
}
}
if (!bFindFrame)
{
frameindex = 0;
}
return frameindex;
}
int ActionNode::getLastFrameIndex()
{
int frameindex = -1;
bool bFindFrame = false;
for (int n = 0; n < _frameArrayNum; n++)
{
2013-12-25 19:46:09 +08:00
auto cArray = _frameArray.at(n);
2013-12-27 12:45:05 +08:00
if (cArray->size() <= 0)
2013-09-20 22:23:13 +08:00
{
continue;
}
bFindFrame = true;
2013-12-27 12:45:05 +08:00
ssize_t lastInex = cArray->size() - 1;
auto frame = cArray->at(lastInex);
2013-09-20 22:23:13 +08:00
int iFrameIndex = frame->getFrameIndex();
if (frameindex < iFrameIndex)
{
frameindex = iFrameIndex;
}
}
if (!bFindFrame)
{
frameindex = 0;
}
return frameindex;
}
bool ActionNode::updateActionToTimeLine(float fTime)
{
bool bFindFrame = false;
ActionFrame* srcFrame = nullptr;
// ActionFrame* destFrame = nullptr;
2013-09-20 22:23:13 +08:00
for (int n = 0; n < _frameArrayNum; n++)
{
2013-12-25 19:46:09 +08:00
auto cArray = _frameArray.at(n);
2013-12-27 12:45:05 +08:00
if (cArray->size() <= 0)
2013-09-20 22:23:13 +08:00
{
continue;
}
2013-12-27 12:45:05 +08:00
ssize_t frameCount = cArray->size();
2013-09-20 22:23:13 +08:00
for (int i = 0; i < frameCount; i++)
{
2013-12-27 12:45:05 +08:00
auto frame = cArray->at(i);
2013-09-20 22:23:13 +08:00
if (frame->getFrameIndex()*getUnitTime() == fTime)
{
this->easingToFrame(1.0f,1.0f,nullptr,frame);
2013-09-20 22:23:13 +08:00
bFindFrame = true;
break;
}
else if (frame->getFrameIndex()*getUnitTime() > fTime)
{
if (i == 0)
{
this->easingToFrame(1.0f,1.0f,nullptr,frame);
2013-09-20 22:23:13 +08:00
bFindFrame = false;
}
else
{
2013-12-27 12:45:05 +08:00
srcFrame = cArray->at(i-1);
2013-09-20 22:23:13 +08:00
float duration = (frame->getFrameIndex() - srcFrame->getFrameIndex())*getUnitTime();
float delaytime = fTime - srcFrame->getFrameIndex()*getUnitTime();
this->easingToFrame(duration,1.0f,nullptr,srcFrame);
2013-09-20 22:23:13 +08:00
//float easingTime = ActionFrameEasing::bounceTime(delaytime);
this->easingToFrame(duration,delaytime/duration,srcFrame,frame);
2013-09-20 22:23:13 +08:00
bFindFrame = true;
}
break;
}
}
}
return bFindFrame;
}
void ActionNode::easingToFrame(float duration,float delayTime,ActionFrame* srcFrame,ActionFrame* destFrame)
2013-09-20 22:23:13 +08:00
{
2013-12-25 19:46:09 +08:00
Action* cAction = destFrame->getAction(duration,srcFrame);
2013-09-20 22:23:13 +08:00
Node* cNode = this->getActionNode();
if (cAction == nullptr || cNode == nullptr)
2013-09-20 22:23:13 +08:00
{
return;
}
cAction->startWithTarget(cNode);
cAction->update(delayTime);
}
bool ActionNode::isActionDoneOnce()
{
if (_action == nullptr)
{
return true;
}
return _action->isDone();
}
}