add ui animation for binary parsing

This commit is contained in:
andyque 2014-06-18 15:55:01 +08:00
parent aa5df68b96
commit 1533374bd1
7 changed files with 339 additions and 69 deletions

View File

@ -24,6 +24,7 @@ THE SOFTWARE.
#include "cocostudio/CCActionManagerEx.h" #include "cocostudio/CCActionManagerEx.h"
#include "cocostudio/DictionaryHelper.h" #include "cocostudio/DictionaryHelper.h"
#include "cocostudio/CocoLoader.h"
using namespace cocos2d; using namespace cocos2d;
@ -70,6 +71,42 @@ void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::V
} }
_actionDic.insert(std::pair<std::string, cocos2d::Vector<ActionObject*>>(fileName, actionList)); _actionDic.insert(std::pair<std::string, cocos2d::Vector<ActionObject*>>(fileName, actionList));
} }
void ActionManagerEx::initWithBinary(const char* file,
cocos2d::Ref *root,
CocoLoader* pCocoLoader,
stExpCocoNode* pCocoNode)
{
std::string path = file;
ssize_t pos = path.find_last_of("/");
std::string fileName = path.substr(pos+1,path.length());
CCLOG("filename == %s",fileName.c_str());
cocos2d::Vector<ActionObject*> actionList;
stExpCocoNode *stChildArray = pCocoNode->GetChildArray();
stExpCocoNode *actionNode = NULL;
for (int i=0; i < pCocoNode->GetChildNum(); ++i) {
std::string key = stChildArray[i].GetName(pCocoLoader);
if (key == "actionlist") {
actionNode = &stChildArray[i];
break;
}
}
if (NULL != actionNode)
{
int actionCount = actionNode->GetChildNum();
for (int i = 0; i < actionCount; ++i) {
ActionObject* action = new ActionObject();
action->autorelease();
action->initWithBinary(pCocoLoader, actionNode->GetChildArray(), root);
actionList.pushBack(action);
}
}
_actionDic.insert(std::pair<std::string, cocos2d::Vector<ActionObject*>>(fileName, actionList));
}
ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName) ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName)

View File

@ -29,6 +29,9 @@ THE SOFTWARE.
#include "cocostudio/DictionaryHelper.h" #include "cocostudio/DictionaryHelper.h"
namespace cocostudio { namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
class ActionManagerEx:public cocos2d::Ref class ActionManagerEx:public cocos2d::Ref
{ {
@ -96,6 +99,8 @@ public:
/*init properties with json dictionay*/ /*init properties with json dictionay*/
void initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root); void initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root);
void initWithBinary(const char* file, Ref* root, CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode);
/** /**
* Release all actions. * Release all actions.
* *

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#include "cocostudio/DictionaryHelper.h" #include "cocostudio/DictionaryHelper.h"
#include "ui/UIWidget.h" #include "ui/UIWidget.h"
#include "ui/UIHelper.h" #include "ui/UIHelper.h"
#include "cocostudio/CocoLoader.h"
using namespace cocos2d; using namespace cocos2d;
using namespace ui; using namespace ui;
@ -167,6 +168,149 @@ void ActionNode::initWithDictionary(const rapidjson::Value& dic, Ref* root)
} }
initActionNodeFromRoot(root); initActionNodeFromRoot(root);
} }
int ActionNode::valueToInt(std::string& value)
{
return atoi(value.c_str());
}
bool ActionNode::valueToBool(std::string& value)
{
int intValue = valueToInt(value);
if (1 == intValue) {
return true;
}else{
return false;
}
}
float ActionNode::valueToFloat(std::string& value)
{
return atof(value.c_str());
}
void ActionNode::initWithBinary(CocoLoader *pCocoLoader,
stExpCocoNode *pCocoNode,
cocos2d::Ref *root)
{
stExpCocoNode *stChildNode = pCocoNode;
int actionNodeCount = stChildNode->GetChildNum();
stChildNode = stChildNode[0].GetChildArray();
stExpCocoNode *frameListNode = NULL;
for (int i = 0; i < actionNodeCount; ++i) {
std::string key = stChildNode[i].GetName(pCocoLoader);
std::string value = stChildNode[i].GetValue();
if (key == "ActionTag") {
setActionTag(valueToInt(value));
}else if (key == "actionframelist"){
frameListNode = &stChildNode[i];
}
}
int actionFrameCount = frameListNode->GetChildNum();
stExpCocoNode *stFrameChildNode = frameListNode->GetChildArray();
for (int i=0; i<actionFrameCount; i++) {
int frameIndex;
int frameTweenType;
float positionX;
float positionY;
float scaleX;
float scaleY;
float rotation;
int opacity;
int colorR = -1;
int colorG = -1;
int colorB = -1;
std::vector<float> frameTweenParameter;
int framesCount = stFrameChildNode[i].GetChildNum();
stExpCocoNode *innerFrameNode = stFrameChildNode[i].GetChildArray();
for (int j = 0; j < framesCount; j++) {
std::string key = innerFrameNode[j].GetName(pCocoLoader);
std::string value = innerFrameNode[j].GetValue();
if (key == "frameid") {
frameIndex = valueToInt(value);
}else if(key == "tweenType"){
frameTweenType = valueToInt(value);
}else if (key == "tweenParameter"){
// There are no tweenParameter args in the json file
int tweenParameterCount = innerFrameNode[j].GetChildNum();
stExpCocoNode *tweenParameterArray = innerFrameNode[j].GetChildArray();
for (int k = 0; k < tweenParameterCount; ++k) {
std::string t_key = tweenParameterArray[j].GetName(pCocoLoader);
std::string t_value = tweenParameterArray[j].GetValue();
frameTweenParameter.push_back(valueToFloat(t_value));
}
}else if (key == "positionx"){
positionX = valueToFloat(value);
}else if (key == "positiony"){
positionY = valueToFloat(value);
ActionMoveFrame* actionFrame = new ActionMoveFrame();
actionFrame->autorelease();
actionFrame->setEasingType(frameTweenType);
actionFrame->setEasingParameter(frameTweenParameter);
actionFrame->setFrameIndex(frameIndex);
actionFrame->setPosition(Vec2(positionX, positionY));
auto cActionArray = _frameArray.at((int)kKeyframeMove);
cActionArray->pushBack(actionFrame);
}else if(key == "scalex"){
scaleX = valueToFloat(value);
}else if(key == "scaley"){
scaleY = valueToFloat(value);
ActionScaleFrame* actionFrame = new ActionScaleFrame();
actionFrame->autorelease();
actionFrame->setEasingType(frameTweenType);
actionFrame->setEasingParameter(frameTweenParameter);
actionFrame->setFrameIndex(frameIndex);
actionFrame->setScaleX(scaleX);
actionFrame->setScaleY(scaleY);
auto cActionArray = _frameArray.at((int)kKeyframeScale);
cActionArray->pushBack(actionFrame);
}else if (key == "rotation"){
rotation = valueToFloat(value);
ActionRotationFrame* actionFrame = new ActionRotationFrame();
actionFrame->autorelease();
actionFrame->setEasingType(frameTweenType);
actionFrame->setEasingParameter(frameTweenParameter);
actionFrame->setFrameIndex(frameIndex);
actionFrame->setRotation(rotation);
auto cActionArray = _frameArray.at((int)kKeyframeRotate);
cActionArray->pushBack(actionFrame);
}else if (key == "opacity"){
opacity = valueToInt(value);
ActionFadeFrame* actionFrame = new ActionFadeFrame();
actionFrame->autorelease();
actionFrame->setEasingType(frameTweenType);
actionFrame->setEasingParameter(frameTweenParameter);
actionFrame->setFrameIndex(frameIndex);
actionFrame->setOpacity(opacity);
auto cActionArray = _frameArray.at((int)kKeyframeFade);
cActionArray->pushBack(actionFrame);
}else if (key == "colorb"){
colorB = valueToInt(value);
}else if(key == "colorg"){
colorG = valueToInt(value);
}else if(key == "colorr"){
colorR = valueToInt(value);
ActionTintFrame* actionFrame = new ActionTintFrame();
actionFrame->autorelease();
actionFrame->setEasingType(frameTweenType);
actionFrame->setEasingParameter(frameTweenParameter);
actionFrame->setFrameIndex(frameIndex);
actionFrame->setColor(Color3B(colorR,colorG,colorB));
auto cActionArray = _frameArray.at((int)kKeyframeTint);
cActionArray->pushBack(actionFrame);
}
}
}
initActionNodeFromRoot(root);
}
void ActionNode::initActionNodeFromRoot(Ref* root) void ActionNode::initActionNodeFromRoot(Ref* root)
{ {

View File

@ -29,7 +29,9 @@ THE SOFTWARE.
#include "cocostudio/DictionaryHelper.h" #include "cocostudio/DictionaryHelper.h"
namespace cocostudio { namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
/** /**
* @js NA * @js NA
* @lua NA * @lua NA
@ -149,6 +151,7 @@ public:
/*init properties with a json dictionary*/ /*init properties with a json dictionary*/
virtual void initWithDictionary(const rapidjson::Value& dic, cocos2d::Ref* root); virtual void initWithDictionary(const rapidjson::Value& dic, cocos2d::Ref* root);
virtual void initWithBinary(CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode, Ref* root);
/** /**
* Gets if the action is done once time. * Gets if the action is done once time.
@ -157,6 +160,10 @@ public:
*/ */
virtual bool isActionDoneOnce(); virtual bool isActionDoneOnce();
protected: protected:
int valueToInt(std::string& value);
bool valueToBool(std::string& value);
float valueToFloat(std::string& value);
int _currentFrameIndex; int _currentFrameIndex;
int _destFrameIndex; int _destFrameIndex;

View File

@ -24,6 +24,7 @@ THE SOFTWARE.
#include "cocostudio/CCActionObject.h" #include "cocostudio/CCActionObject.h"
#include "cocostudio/DictionaryHelper.h" #include "cocostudio/DictionaryHelper.h"
#include "cocostudio/CocoLoader.h"
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/CCScheduler.h" #include "base/CCScheduler.h"
@ -44,128 +45,192 @@ ActionObject::ActionObject()
, _CallBack(nullptr) , _CallBack(nullptr)
, _fTotalTime(0.0f) , _fTotalTime(0.0f)
{ {
_pScheduler = Director::getInstance()->getScheduler(); _pScheduler = Director::getInstance()->getScheduler();
CC_SAFE_RETAIN(_pScheduler); CC_SAFE_RETAIN(_pScheduler);
} }
ActionObject::~ActionObject() ActionObject::~ActionObject()
{ {
_actionNodeList.clear(); _actionNodeList.clear();
CC_SAFE_RELEASE(_pScheduler); CC_SAFE_RELEASE(_pScheduler);
CC_SAFE_RELEASE(_CallBack); CC_SAFE_RELEASE(_CallBack);
} }
void ActionObject::setName(const char* name) void ActionObject::setName(const char* name)
{ {
_name.assign(name); _name.assign(name);
} }
const char* ActionObject::getName() const char* ActionObject::getName()
{ {
return _name.c_str(); return _name.c_str();
} }
void ActionObject::setLoop(bool bLoop) void ActionObject::setLoop(bool bLoop)
{ {
_loop = bLoop; _loop = bLoop;
} }
bool ActionObject::getLoop() bool ActionObject::getLoop()
{ {
return _loop; return _loop;
} }
void ActionObject::setUnitTime(float fTime) void ActionObject::setUnitTime(float fTime)
{ {
_fUnitTime = fTime; _fUnitTime = fTime;
for(const auto &e : _actionNodeList) for(const auto &e : _actionNodeList)
{ {
e->setUnitTime(_fUnitTime); e->setUnitTime(_fUnitTime);
} }
} }
float ActionObject::getUnitTime() float ActionObject::getUnitTime()
{ {
return _fUnitTime; return _fUnitTime;
} }
float ActionObject::getCurrentTime() float ActionObject::getCurrentTime()
{ {
return _currentTime; return _currentTime;
} }
void ActionObject::setCurrentTime(float fTime) void ActionObject::setCurrentTime(float fTime)
{ {
_currentTime = fTime; _currentTime = fTime;
} }
float ActionObject::getTotalTime() float ActionObject::getTotalTime()
{ {
return _fTotalTime; return _fTotalTime;
} }
bool ActionObject::isPlaying() bool ActionObject::isPlaying()
{ {
return _bPlaying; return _bPlaying;
} }
void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root) void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root)
{ {
setName(DICTOOL->getStringValue_json(dic, "name")); setName(DICTOOL->getStringValue_json(dic, "name"));
setLoop(DICTOOL->getBooleanValue_json(dic, "loop")); setLoop(DICTOOL->getBooleanValue_json(dic, "loop"));
setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime")); setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime"));
int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist"); int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist");
int maxLength = 0; int maxLength = 0;
for (int i=0; i<actionNodeCount; i++) { for (int i=0; i<actionNodeCount; i++) {
ActionNode* actionNode = new ActionNode(); ActionNode* actionNode = new ActionNode();
actionNode->autorelease(); actionNode->autorelease();
const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i); const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i);
actionNode->initWithDictionary(actionNodeDic,root); actionNode->initWithDictionary(actionNodeDic,root);
actionNode->setUnitTime(getUnitTime()); actionNode->setUnitTime(getUnitTime());
_actionNodeList.pushBack(actionNode); _actionNodeList.pushBack(actionNode);
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
if(length > maxLength)
maxLength = length;
}
_fTotalTime = maxLength*_fUnitTime;
}
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex(); void ActionObject::initWithBinary(CocoLoader *pCocoLoader,
if(length > maxLength) stExpCocoNode *pCocoNode,
maxLength = length; cocos2d::Ref *root)
} {
_fTotalTime = maxLength*_fUnitTime; stExpCocoNode *stChildNode = pCocoNode->GetChildArray();
stExpCocoNode *actionNodeList = NULL;
int count = pCocoNode->GetChildNum();
for (int i = 0; i < count; ++i) {
std::string key = stChildNode[i].GetName(pCocoLoader);
std::string value = stChildNode[i].GetValue();
if (key == "name") {
setName(value.c_str());
}else if (key == "loop"){
setLoop(valueToBool(value));
}else if(key == "unittime"){
setUnitTime(valueToFloat(value));
}else if (key == "actionnodelist"){
actionNodeList = &stChildNode[i];
}
}
if(NULL != actionNodeList)
{
int actionNodeCount = actionNodeList->GetChildNum();
stExpCocoNode *actionNodeArray = actionNodeList->GetChildArray();
int maxLength = 0;
for (int i=0; i<actionNodeCount; i++) {
ActionNode* actionNode = new ActionNode();
actionNode->autorelease();
actionNode->initWithBinary(pCocoLoader, &actionNodeArray[i] , root);
actionNode->setUnitTime(getUnitTime());
_actionNodeList.pushBack(actionNode);
int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
if(length > maxLength)
maxLength = length;
}
_fTotalTime = maxLength* _fUnitTime;
}
}
int ActionObject::valueToInt(std::string& value)
{
return atoi(value.c_str());
}
bool ActionObject::valueToBool(std::string& value)
{
int intValue = valueToInt(value);
if (1 == intValue) {
return true;
}else{
return false;
}
}
float ActionObject::valueToFloat(std::string& value)
{
return atof(value.c_str());
} }
void ActionObject::addActionNode(ActionNode* node) void ActionObject::addActionNode(ActionNode* node)
{ {
if (node == nullptr) if (node == nullptr)
{ {
return; return;
} }
_actionNodeList.pushBack(node); _actionNodeList.pushBack(node);
node->setUnitTime(_fUnitTime); node->setUnitTime(_fUnitTime);
} }
void ActionObject::removeActionNode(ActionNode* node) void ActionObject::removeActionNode(ActionNode* node)
{ {
if (node == nullptr) if (node == nullptr)
{ {
return; return;
} }
_actionNodeList.eraseObject(node); _actionNodeList.eraseObject(node);
} }
void ActionObject::play() void ActionObject::play()
{ {
stop(); stop();
this->updateToFrameByTime(0.0f); this->updateToFrameByTime(0.0f);
for(const auto &e : _actionNodeList) for(const auto &e : _actionNodeList)
{ {
e->playAction(); e->playAction();
} }
if (_loop) if (_loop)
{ {
_pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kRepeatForever, 0.0f, false); _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kRepeatForever, 0.0f, false);
} }
else else
{ {
_pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false); _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false);
} }
} }
void ActionObject::play(CallFunc* func) void ActionObject::play(CallFunc* func)
{ {
this->play(); this->play();
this->_CallBack = func; this->_CallBack = func;
CC_SAFE_RETAIN(_CallBack); CC_SAFE_RETAIN(_CallBack);
} }

View File

@ -31,6 +31,9 @@ THE SOFTWARE.
namespace cocostudio { namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
/** /**
* @js NA * @js NA
* @lua NA * @lua NA
@ -160,10 +163,17 @@ public:
/*init properties with a json dictionary*/ /*init properties with a json dictionary*/
void initWithDictionary(const rapidjson::Value& dic, cocos2d::Ref* root); void initWithDictionary(const rapidjson::Value& dic, cocos2d::Ref* root);
void initWithBinary(CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode, cocos2d::Ref* root);
/*scheduler update function*/ /*scheduler update function*/
void simulationActionUpdate(float dt); void simulationActionUpdate(float dt);
protected: protected:
int valueToInt(std::string& value);
bool valueToBool(std::string& value);
float valueToFloat(std::string& value);
cocos2d::Vector<ActionNode*> _actionNodeList; cocos2d::Vector<ActionNode*> _actionNodeList;
std::string _name; std::string _name;
bool _loop; bool _loop;

View File

@ -1272,15 +1272,17 @@ Widget* WidgetPropertiesReader0300::createWidget(const rapidjson::Value& data, c
} }
/* ********************** */ /* ********************** */
//TODO: /* ********************** */
// // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight)); stExpCocoNode *optionChildNode = pCocoNode->GetChildArray();
// const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation"); for (int k = 0; k < pCocoNode->GetChildNum(); ++k) {
// /* *********temp********* */ std::string key = optionChildNode[k].GetName(pCocoLoader);
// // ActionManager::getInstance()->releaseActions(); if (key == "animation") {
// /* ********************** */ Ref* rootWidget = (Ref*) widget;
// CCLOG("file name == [%s]",fileName); ActionManagerEx::getInstance()->initWithBinary(fileName,rootWidget,pCocoLoader, &optionChildNode[k]);
// Ref* rootWidget = (Ref*) widget; break;
// ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget); }
}
return widget; return widget;
} }