From 1533374bd17b94447231aeab75ec7309808a2377 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 18 Jun 2014 15:55:01 +0800 Subject: [PATCH] add ui animation for binary parsing --- .../cocostudio/CCActionManagerEx.cpp | 37 ++++ .../cocostudio/CCActionManagerEx.h | 5 + .../cocostudio/CCActionNode.cpp | 144 ++++++++++++++ .../editor-support/cocostudio/CCActionNode.h | 9 +- .../cocostudio/CCActionObject.cpp | 183 ++++++++++++------ .../cocostudio/CCActionObject.h | 10 + .../cocostudio/CCSGUIReader.cpp | 20 +- 7 files changed, 339 insertions(+), 69 deletions(-) diff --git a/cocos/editor-support/cocostudio/CCActionManagerEx.cpp b/cocos/editor-support/cocostudio/CCActionManagerEx.cpp index a439f843cb..655ace9e3b 100644 --- a/cocos/editor-support/cocostudio/CCActionManagerEx.cpp +++ b/cocos/editor-support/cocostudio/CCActionManagerEx.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "cocostudio/CCActionManagerEx.h" #include "cocostudio/DictionaryHelper.h" +#include "cocostudio/CocoLoader.h" using namespace cocos2d; @@ -70,6 +71,42 @@ void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::V } _actionDic.insert(std::pair>(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 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>(fileName, actionList)); + + } ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName) diff --git a/cocos/editor-support/cocostudio/CCActionManagerEx.h b/cocos/editor-support/cocostudio/CCActionManagerEx.h index b33b099342..b198f0fa23 100644 --- a/cocos/editor-support/cocostudio/CCActionManagerEx.h +++ b/cocos/editor-support/cocostudio/CCActionManagerEx.h @@ -29,6 +29,9 @@ THE SOFTWARE. #include "cocostudio/DictionaryHelper.h" namespace cocostudio { + +class CocoLoader; +struct stExpCocoNode; class ActionManagerEx:public cocos2d::Ref { @@ -96,6 +99,8 @@ public: /*init properties with json dictionay*/ 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. * diff --git a/cocos/editor-support/cocostudio/CCActionNode.cpp b/cocos/editor-support/cocostudio/CCActionNode.cpp index f610c0e15f..33a553be2b 100644 --- a/cocos/editor-support/cocostudio/CCActionNode.cpp +++ b/cocos/editor-support/cocostudio/CCActionNode.cpp @@ -27,6 +27,7 @@ THE SOFTWARE. #include "cocostudio/DictionaryHelper.h" #include "ui/UIWidget.h" #include "ui/UIHelper.h" +#include "cocostudio/CocoLoader.h" using namespace cocos2d; using namespace ui; @@ -167,6 +168,149 @@ void ActionNode::initWithDictionary(const rapidjson::Value& dic, Ref* 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 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) { diff --git a/cocos/editor-support/cocostudio/CCActionNode.h b/cocos/editor-support/cocostudio/CCActionNode.h index f0043cb264..cbccdd0a21 100644 --- a/cocos/editor-support/cocostudio/CCActionNode.h +++ b/cocos/editor-support/cocostudio/CCActionNode.h @@ -29,7 +29,9 @@ THE SOFTWARE. #include "cocostudio/DictionaryHelper.h" namespace cocostudio { - + +class CocoLoader; +struct stExpCocoNode; /** * @js NA * @lua NA @@ -149,6 +151,7 @@ public: /*init properties with a json dictionary*/ 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. @@ -157,6 +160,10 @@ public: */ virtual bool isActionDoneOnce(); protected: + int valueToInt(std::string& value); + bool valueToBool(std::string& value); + float valueToFloat(std::string& value); + int _currentFrameIndex; int _destFrameIndex; diff --git a/cocos/editor-support/cocostudio/CCActionObject.cpp b/cocos/editor-support/cocostudio/CCActionObject.cpp index c421b40402..160881dda3 100644 --- a/cocos/editor-support/cocostudio/CCActionObject.cpp +++ b/cocos/editor-support/cocostudio/CCActionObject.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include "cocostudio/CCActionObject.h" #include "cocostudio/DictionaryHelper.h" +#include "cocostudio/CocoLoader.h" #include "base/CCDirector.h" #include "base/CCScheduler.h" @@ -44,128 +45,192 @@ ActionObject::ActionObject() , _CallBack(nullptr) , _fTotalTime(0.0f) { - _pScheduler = Director::getInstance()->getScheduler(); - CC_SAFE_RETAIN(_pScheduler); + _pScheduler = Director::getInstance()->getScheduler(); + CC_SAFE_RETAIN(_pScheduler); } ActionObject::~ActionObject() { - _actionNodeList.clear(); - CC_SAFE_RELEASE(_pScheduler); - CC_SAFE_RELEASE(_CallBack); + _actionNodeList.clear(); + CC_SAFE_RELEASE(_pScheduler); + CC_SAFE_RELEASE(_CallBack); } void ActionObject::setName(const char* name) { - _name.assign(name); + _name.assign(name); } const char* ActionObject::getName() { - return _name.c_str(); + return _name.c_str(); } void ActionObject::setLoop(bool bLoop) { - _loop = bLoop; + _loop = bLoop; } bool ActionObject::getLoop() { - return _loop; + return _loop; } void ActionObject::setUnitTime(float fTime) { - _fUnitTime = fTime; + _fUnitTime = fTime; for(const auto &e : _actionNodeList) - { + { e->setUnitTime(_fUnitTime); - } + } } float ActionObject::getUnitTime() { - return _fUnitTime; + return _fUnitTime; } float ActionObject::getCurrentTime() { - return _currentTime; + return _currentTime; } void ActionObject::setCurrentTime(float fTime) { - _currentTime = fTime; + _currentTime = fTime; } float ActionObject::getTotalTime() { - return _fTotalTime; + return _fTotalTime; } bool ActionObject::isPlaying() { - return _bPlaying; + return _bPlaying; } void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root) { - setName(DICTOOL->getStringValue_json(dic, "name")); - setLoop(DICTOOL->getBooleanValue_json(dic, "loop")); - setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime")); - int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist"); - int maxLength = 0; - for (int i=0; iautorelease(); - const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i); - actionNode->initWithDictionary(actionNodeDic,root); - actionNode->setUnitTime(getUnitTime()); - _actionNodeList.pushBack(actionNode); + setName(DICTOOL->getStringValue_json(dic, "name")); + setLoop(DICTOOL->getBooleanValue_json(dic, "loop")); + setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime")); + int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist"); + int maxLength = 0; + for (int i=0; iautorelease(); + const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i); + actionNode->initWithDictionary(actionNodeDic,root); + actionNode->setUnitTime(getUnitTime()); + _actionNodeList.pushBack(actionNode); + + int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex(); + if(length > maxLength) + maxLength = length; + } + _fTotalTime = maxLength*_fUnitTime; +} - int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex(); - if(length > maxLength) - maxLength = length; - } - _fTotalTime = maxLength*_fUnitTime; +void ActionObject::initWithBinary(CocoLoader *pCocoLoader, + stExpCocoNode *pCocoNode, + cocos2d::Ref *root) +{ + 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; iautorelease(); + + 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) { - if (node == nullptr) - { - return; - } - _actionNodeList.pushBack(node); - node->setUnitTime(_fUnitTime); + if (node == nullptr) + { + return; + } + _actionNodeList.pushBack(node); + node->setUnitTime(_fUnitTime); } void ActionObject::removeActionNode(ActionNode* node) { - if (node == nullptr) - { - return; - } - _actionNodeList.eraseObject(node); + if (node == nullptr) + { + return; + } + _actionNodeList.eraseObject(node); } void ActionObject::play() { - stop(); - this->updateToFrameByTime(0.0f); + stop(); + this->updateToFrameByTime(0.0f); for(const auto &e : _actionNodeList) - { - e->playAction(); - } - if (_loop) - { - _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kRepeatForever, 0.0f, false); - } - else - { - _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false); - } + { + e->playAction(); + } + if (_loop) + { + _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f , kRepeatForever, 0.0f, false); + } + else + { + _pScheduler->schedule(schedule_selector(ActionObject::simulationActionUpdate), this, 0.0f, false); + } } void ActionObject::play(CallFunc* func) { - this->play(); + this->play(); this->_CallBack = func; CC_SAFE_RETAIN(_CallBack); } diff --git a/cocos/editor-support/cocostudio/CCActionObject.h b/cocos/editor-support/cocostudio/CCActionObject.h index dcc8008dda..2c3f7684d6 100644 --- a/cocos/editor-support/cocostudio/CCActionObject.h +++ b/cocos/editor-support/cocostudio/CCActionObject.h @@ -31,6 +31,9 @@ THE SOFTWARE. namespace cocostudio { +class CocoLoader; +struct stExpCocoNode; + /** * @js NA * @lua NA @@ -160,10 +163,17 @@ public: /*init properties with a json dictionary*/ void initWithDictionary(const rapidjson::Value& dic, cocos2d::Ref* root); + + void initWithBinary(CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode, cocos2d::Ref* root); + /*scheduler update function*/ void simulationActionUpdate(float dt); protected: + int valueToInt(std::string& value); + bool valueToBool(std::string& value); + float valueToFloat(std::string& value); + cocos2d::Vector _actionNodeList; std::string _name; bool _loop; diff --git a/cocos/editor-support/cocostudio/CCSGUIReader.cpp b/cocos/editor-support/cocostudio/CCSGUIReader.cpp index b01774bb3d..da72f03089 100644 --- a/cocos/editor-support/cocostudio/CCSGUIReader.cpp +++ b/cocos/editor-support/cocostudio/CCSGUIReader.cpp @@ -1272,15 +1272,17 @@ Widget* WidgetPropertiesReader0300::createWidget(const rapidjson::Value& data, c } /* ********************** */ - //TODO: -// // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight)); -// const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation"); -// /* *********temp********* */ -// // ActionManager::getInstance()->releaseActions(); -// /* ********************** */ -// CCLOG("file name == [%s]",fileName); -// Ref* rootWidget = (Ref*) widget; -// ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget); + /* ********************** */ + stExpCocoNode *optionChildNode = pCocoNode->GetChildArray(); + for (int k = 0; k < pCocoNode->GetChildNum(); ++k) { + std::string key = optionChildNode[k].GetName(pCocoLoader); + if (key == "animation") { + Ref* rootWidget = (Ref*) widget; + ActionManagerEx::getInstance()->initWithBinary(fileName,rootWidget,pCocoLoader, &optionChildNode[k]); + break; + } + } + return widget; }