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/DictionaryHelper.h"
#include "cocostudio/CocoLoader.h"
using namespace cocos2d;
@ -71,6 +72,42 @@ void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::V
_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)
{

View File

@ -30,6 +30,9 @@ THE SOFTWARE.
namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
class ActionManagerEx:public cocos2d::Ref
{
public:
@ -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.
*

View File

@ -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;
@ -168,6 +169,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<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)
{
Node* rootNode = dynamic_cast<Node*>(root);

View File

@ -30,6 +30,8 @@ THE SOFTWARE.
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;

View File

@ -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"
@ -127,6 +128,70 @@ void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root)
_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; 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)
{
if (node == nullptr)

View File

@ -31,6 +31,9 @@ THE SOFTWARE.
namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
/**
* @js NA
* @lua NA
@ -161,9 +164,16 @@ 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<ActionNode*> _actionNodeList;
std::string _name;
bool _loop;

View File

@ -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;
}