axmol/cocos/editor-support/cocostudio/ActionTimeline/CCNodeReader.cpp

459 lines
15 KiB
C++
Raw Normal View History

2014-06-11 11:10:07 +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.
****************************************************************************/
2014-06-12 19:24:27 +08:00
#include "CCNodeReader.h"
#include "CCActionTimelineCache.h"
#include "CCActionTimeline.h"
2014-06-11 11:10:07 +08:00
2014-06-23 18:29:23 +08:00
#include "cocostudio/CCSGUIReader.h"
2014-06-11 11:10:07 +08:00
using namespace cocos2d;
using namespace ui;
2014-06-11 11:10:07 +08:00
namespace cocostudio {
namespace timeline{
static const char* ClassName_Node = "Node";
static const char* ClassName_SubGraph = "SubGraph";
static const char* ClassName_Sprite = "Sprite";
static const char* ClassName_Particle = "Particle";
static const char* ClassName_Panel = "Panel";
2014-06-11 11:10:07 +08:00
static const char* ClassName_Button = "Button";
static const char* ClassName_CheckBox = "CheckBox";
static const char* ClassName_ImageView = "ImageView";
static const char* ClassName_TextAtlas = "TextAtlas";
static const char* ClassName_TextBMFont = "TextBMFont";
static const char* ClassName_Text = "Text";
static const char* ClassName_LoadingBar = "LoadingBar";
static const char* ClassName_TextField = "TextField";
static const char* ClassName_Slider = "Slider";
static const char* ClassName_Layout = "Layout";
static const char* ClassName_ScrollView = "ScrollView";
static const char* ClassName_ListView = "ListView";
static const char* ClassName_PageView = "PageView";
static const char* ClassName_Widget = "Widget";
2014-06-19 14:11:37 +08:00
static const char* ClassName_Label = "Label";
2014-06-11 11:10:07 +08:00
static const char* NODE = "nodeTree";
static const char* CHILDREN = "children";
static const char* CLASSNAME = "classname";
2014-06-11 21:11:22 +08:00
static const char* FILE_PATH = "fileName";
2014-06-11 11:10:07 +08:00
static const char* PLIST_FILE = "plistFile";
2014-06-12 18:50:37 +08:00
static const char* TAG = "tag";
2014-06-11 11:10:07 +08:00
static const char* ACTION_TAG = "actionTag";
static const char* OPTIONS = "options";
static const char* WIDTH = "width";
static const char* HEIGHT = "height";
static const char* X = "x";
static const char* Y = "y";
static const char* SCALE_X = "scaleX";
static const char* SCALE_Y = "scaleY";
static const char* SKEW_X = "skewX";
static const char* SKEW_Y = "skewY";
static const char* ROTATION = "rotation";
static const char* ROTATION_SKEW_X = "rotationSkewX";
static const char* ROTATION_SKEW_Y = "rotationSkewY";
static const char* ANCHOR_X = "anchorPointX";
static const char* ANCHOR_Y = "anchorPointY";
static const char* ALPHA = "opacity";
static const char* RED = "colorR";
static const char* GREEN = "colorG";
static const char* BLUE = "colorB";
2014-06-16 15:03:53 +08:00
static const char* ZORDER = "ZOrder";
2014-06-11 11:10:07 +08:00
static const char* PARTICLE_NUM = "particleNum";
2014-06-16 15:19:58 +08:00
static const char* FLIPX = "flipX";
static const char* FLIPY = "flipY";
2014-06-16 17:20:59 +08:00
static const char* VISIBLE = "visible";
2014-06-11 11:10:07 +08:00
2014-06-11 21:11:22 +08:00
static const char* TEXTURES = "textures";
static const char* TEXTURES_PNG = "texturesPng";
2014-06-11 11:10:07 +08:00
2014-06-12 18:50:37 +08:00
2014-06-12 19:24:27 +08:00
// NodeReader
static NodeReader* _sharedNodeReader = nullptr;
2014-06-11 11:10:07 +08:00
2014-06-12 19:24:27 +08:00
NodeReader* NodeReader::getInstance()
2014-06-11 11:10:07 +08:00
{
2014-06-12 19:24:27 +08:00
if (! _sharedNodeReader)
2014-06-11 11:10:07 +08:00
{
2014-06-12 19:24:27 +08:00
_sharedNodeReader = new NodeReader();
_sharedNodeReader->init();
2014-06-11 11:10:07 +08:00
}
2014-06-12 19:24:27 +08:00
return _sharedNodeReader;
2014-06-11 11:10:07 +08:00
}
2014-06-12 19:24:27 +08:00
void NodeReader::destroyInstance()
2014-06-11 11:10:07 +08:00
{
2014-06-12 19:24:27 +08:00
CC_SAFE_DELETE(_sharedNodeReader);
2014-06-11 11:10:07 +08:00
}
2014-06-12 19:24:27 +08:00
NodeReader::NodeReader()
2014-06-11 21:11:22 +08:00
: _recordJsonPath(true)
, _jsonPath("")
{
}
2014-06-12 19:24:27 +08:00
void NodeReader::purge()
2014-06-11 11:10:07 +08:00
{
}
2014-06-12 19:24:27 +08:00
void NodeReader::init()
2014-06-11 11:10:07 +08:00
{
using namespace std::placeholders;
2014-06-12 19:24:27 +08:00
_funcs.insert(Pair(ClassName_Node, std::bind(&NodeReader::loadSimpleNode, this, _1)));
_funcs.insert(Pair(ClassName_SubGraph, std::bind(&NodeReader::loadSubGraph, this, _1)));
_funcs.insert(Pair(ClassName_Sprite, std::bind(&NodeReader::loadSprite, this, _1)));
_funcs.insert(Pair(ClassName_Particle, std::bind(&NodeReader::loadParticle, this, _1)));
_funcs.insert(Pair(ClassName_Panel, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Button, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_CheckBox, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_ImageView, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_TextAtlas, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_TextBMFont,std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Text, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_LoadingBar,std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_TextField, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Slider, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Layout, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_ScrollView,std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_ListView, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_PageView, std::bind(&NodeReader::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Widget, std::bind(&NodeReader::loadWidget, this, _1)));
2014-06-19 14:11:37 +08:00
_funcs.insert(Pair(ClassName_Label, std::bind(&NodeReader::loadWidget, this, _1)));
2014-06-11 11:10:07 +08:00
}
Node* NodeReader::createNode(const std::string& filename)
2014-06-11 11:10:07 +08:00
{
2014-06-11 21:11:22 +08:00
if(_recordJsonPath)
2014-06-11 11:10:07 +08:00
{
2014-06-11 21:11:22 +08:00
std::string jsonPath = filename.substr(0, filename.find_last_of('/') + 1);
2014-06-12 19:56:15 +08:00
GUIReader::getInstance()->setFilePath(jsonPath);
2014-06-11 11:10:07 +08:00
2014-06-11 21:11:22 +08:00
_jsonPath = jsonPath;
2014-06-11 11:10:07 +08:00
}
2014-06-11 21:11:22 +08:00
else
{
2014-06-12 19:56:15 +08:00
GUIReader::getInstance()->setFilePath("");
2014-06-11 21:11:22 +08:00
_jsonPath = "";
}
Node* node = loadNodeWithFile(filename);
2014-06-11 11:10:07 +08:00
return node;
}
Node* NodeReader::loadNodeWithFile(const std::string& fileName)
2014-06-11 11:10:07 +08:00
{
// Read content from file
2014-06-12 16:21:41 +08:00
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName);
2014-06-11 11:10:07 +08:00
Node* node = loadNodeWithContent(contentStr);
2014-06-11 21:11:22 +08:00
2014-06-11 11:10:07 +08:00
// Load animation data from file
ActionTimelineCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr);
2014-06-11 11:10:07 +08:00
2014-06-11 21:11:22 +08:00
return node;
2014-06-11 11:10:07 +08:00
}
Node* NodeReader::loadNodeWithContent(const std::string& content)
2014-06-11 11:10:07 +08:00
{
rapidjson::Document doc;
doc.Parse<0>(content.c_str());
2014-06-11 21:11:22 +08:00
if (doc.HasParseError())
{
2014-06-11 11:10:07 +08:00
CCLOG("GetParseError %s\n", doc.GetParseError());
}
2014-06-11 21:11:22 +08:00
// decode plist
int length = DICTOOL->getArrayCount_json(doc, TEXTURES);
2014-06-12 17:38:26 +08:00
2014-06-11 21:11:22 +08:00
for(int i=0; i<length; i++)
{
std::string plist = DICTOOL->getStringValueFromArray_json(doc, TEXTURES, i);
2014-06-12 17:38:26 +08:00
std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
2014-06-11 21:11:22 +08:00
plist = _jsonPath + plist;
2014-06-12 17:38:26 +08:00
png = _jsonPath + png;
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
2014-06-11 21:11:22 +08:00
}
// decode node tree
2014-06-11 11:10:07 +08:00
const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
Node* root = loadNode(subJson);
2014-06-12 16:21:41 +08:00
root->release();
return root;
2014-06-11 11:10:07 +08:00
}
Node* NodeReader::loadNode(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
Node* node = nullptr;
2014-06-11 11:10:07 +08:00
std::string nodeType = DICTOOL->getStringValue_json(json, CLASSNAME);
NodeCreateFunc func = _funcs[nodeType];
2014-06-11 11:10:07 +08:00
if (func != nullptr)
{
const rapidjson::Value& options = DICTOOL->getSubDictionary_json(json, OPTIONS);
node = func(options);
}
if(node)
{
int length = DICTOOL->getArrayCount_json(json, CHILDREN, 0);
for (int i = 0; i<length; i++)
{
const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i);
Node* child = loadNode(dic);
2014-06-11 21:11:22 +08:00
if (child)
{
2014-06-11 11:10:07 +08:00
node->addChild(child);
2014-06-12 16:21:41 +08:00
child->release();
2014-06-11 11:10:07 +08:00
}
}
}
else
{
CCLOG("Not supported NodeType: %s", nodeType.c_str());
}
return node;
}
void NodeReader::initNode(Node* node, const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
float width = DICTOOL->getFloatValue_json(json, WIDTH);
float height = DICTOOL->getFloatValue_json(json, HEIGHT);
float x = DICTOOL->getFloatValue_json(json, X);
float y = DICTOOL->getFloatValue_json(json, Y);
float scalex = DICTOOL->getFloatValue_json(json, SCALE_X, 1);
float scaley = DICTOOL->getFloatValue_json(json, SCALE_Y, 1);
float rotation = DICTOOL->getFloatValue_json(json, ROTATION);
float rotationSkewX = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_X);
float rotationSkewY = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_Y);
float skewx = DICTOOL->getFloatValue_json(json, SKEW_X);
float skewy = DICTOOL->getFloatValue_json(json, SKEW_Y);
float anchorx = DICTOOL->getFloatValue_json(json, ANCHOR_X, 0.5f);
float anchory = DICTOOL->getFloatValue_json(json, ANCHOR_Y, 0.5f);
GLubyte alpha = (GLubyte)DICTOOL->getIntValue_json(json, ALPHA, 255);
GLubyte red = (GLubyte)DICTOOL->getIntValue_json(json, RED, 255);
GLubyte green = (GLubyte)DICTOOL->getIntValue_json(json, GREEN, 255);
GLubyte blue = (GLubyte)DICTOOL->getIntValue_json(json, BLUE, 255);
2014-06-16 15:03:53 +08:00
int zorder = DICTOOL->getIntValue_json(json, ZORDER);
2014-06-12 18:50:37 +08:00
int tag = DICTOOL->getIntValue_json(json, TAG);
int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
2014-06-16 17:20:59 +08:00
bool visible = DICTOOL->getBooleanValue_json(json, VISIBLE);
2014-06-11 11:10:07 +08:00
if(x != 0 || y != 0)
2014-06-12 19:56:15 +08:00
node->setPosition(Point(x, y));
2014-06-11 11:10:07 +08:00
if(scalex != 1)
node->setScaleX(scalex);
if(scaley != 1)
node->setScaleY(scaley);
if (rotation != 0)
node->setRotation(rotation);
if(rotationSkewX != 0)
node->setRotationX(rotationSkewX);
if(rotationSkewY != 0)
node->setRotationY(rotationSkewY);
if(skewx != 0)
node->setSkewX(skewx);
if(skewy != 0)
node->setSkewY(skewy);
if(anchorx != 0.5f || anchory != 0.5f)
2014-06-12 19:56:15 +08:00
node->setAnchorPoint(Point(anchorx, anchory));
2014-06-11 11:10:07 +08:00
if(width != 0 || height != 0)
node->setContentSize(Size(width, height));
2014-06-16 15:03:53 +08:00
if(zorder != 0)
node->setZOrder(zorder);
2014-06-16 17:20:59 +08:00
if(visible != true)
node->setVisible(visible);
2014-06-12 19:56:15 +08:00
if(alpha != 255)
2014-06-11 11:10:07 +08:00
{
2014-06-12 19:56:15 +08:00
node->setOpacity(alpha);
node->setCascadeOpacityEnabled(true);
2014-06-11 11:10:07 +08:00
}
2014-06-12 19:56:15 +08:00
if(red != 255 || green != 255 || blue != 255)
{
node->setColor(Color3B(red, green, blue));
node->setCascadeColorEnabled(true);
}
2014-06-11 11:10:07 +08:00
node->setTag(tag);
node->setUserObject(ActionTimelineData::create(actionTag));
2014-06-11 11:10:07 +08:00
}
2014-06-12 19:24:27 +08:00
Node* NodeReader::loadSimpleNode(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
Node* node = Node::create();
2014-06-12 16:21:41 +08:00
node->retain();
2014-06-11 11:10:07 +08:00
initNode(node, json);
return node;
}
Node* NodeReader::loadSubGraph(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
Node* node = nullptr;
if (filePath && strcmp("", filePath) != 0)
{
node = createNode(filePath);
}
else
{
node = Node::create();
}
2014-06-12 16:21:41 +08:00
node->retain();
2014-06-11 11:10:07 +08:00
initNode(node, json);
return node;
}
2014-06-12 19:24:27 +08:00
Node* NodeReader::loadSprite(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
Sprite *sprite = nullptr;
if(filePath != nullptr)
{
2014-06-11 21:11:22 +08:00
std::string path = filePath;
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
2014-06-11 11:10:07 +08:00
if(!spriteFrame)
{
2014-06-12 20:16:39 +08:00
path = _jsonPath + path;
2014-06-11 21:11:22 +08:00
sprite = Sprite::create(path);
2014-06-11 11:10:07 +08:00
}
else
{
sprite = Sprite::createWithSpriteFrame(spriteFrame);
}
if(!sprite)
{
sprite = CCSprite::create();
CCLOG("filePath is empty. Create a sprite with no texture");
}
}
else
{
sprite = Sprite::create();
}
2014-06-12 16:21:41 +08:00
sprite->retain();
2014-06-11 11:10:07 +08:00
initNode(sprite, json);
2014-06-16 15:19:58 +08:00
bool flipX = DICTOOL->getBooleanValue_json(json, FLIPX);
bool flipY = DICTOOL->getBooleanValue_json(json, FLIPY);
if(flipX != false)
2014-06-20 10:52:14 +08:00
sprite->setFlippedX(flipX);
2014-06-16 15:19:58 +08:00
if(flipY != false)
2014-06-20 10:52:14 +08:00
sprite->setFlippedY(flipY);
2014-06-16 15:19:58 +08:00
2014-06-11 11:10:07 +08:00
return sprite;
}
2014-06-12 19:24:27 +08:00
Node* NodeReader::loadParticle(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
const char* filePath = DICTOOL->getStringValue_json(json, PLIST_FILE);
int num = DICTOOL->getIntValue_json(json, PARTICLE_NUM);
ParticleSystemQuad* particle = ParticleSystemQuad::create(filePath);
particle->setTotalParticles(num);
2014-06-12 16:21:41 +08:00
particle->retain();
2014-06-11 11:10:07 +08:00
initNode(particle, json);
return particle;
}
Node* NodeReader::loadWidget(const rapidjson::Value& json)
2014-06-11 11:10:07 +08:00
{
2014-06-12 19:12:05 +08:00
const char* str = DICTOOL->getStringValue_json(json, CLASSNAME);
if(str == nullptr)
return nullptr;
2014-06-11 11:10:07 +08:00
2014-06-12 19:12:05 +08:00
std::string classname = str;
2014-06-12 18:50:37 +08:00
2014-06-12 19:12:05 +08:00
if (classname == "Panel")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "Layout";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
else if (classname == "TextArea")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "Text";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
else if (classname == "TextButton")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "Button";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
else if (classname == "Label")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "Text";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
else if (classname == "LabelAtlas")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "TextAtlas";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
else if (classname == "LabelBMFont")
2014-06-12 18:50:37 +08:00
{
2014-06-12 19:12:05 +08:00
classname = "TextBMFont";
2014-06-12 18:50:37 +08:00
}
2014-06-12 19:12:05 +08:00
std::string readerName = classname;
2014-06-11 11:10:07 +08:00
readerName.append("Reader");
Widget* widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(classname));
2014-06-12 16:21:41 +08:00
widget->retain();
2014-06-11 11:10:07 +08:00
WidgetReaderProtocol* reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
2014-06-23 18:29:23 +08:00
WidgetPropertiesReader0300* guiReader = new WidgetPropertiesReader0300();
guiReader->setPropsForAllWidgetFromJsonDictionary(reader, widget, json);
CC_SAFE_DELETE(guiReader);
int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
widget->setUserObject(ActionTimelineData::create(actionTag));
2014-06-11 11:10:07 +08:00
return widget;
}
}
}