support relative path for reading json

This commit is contained in:
2youyou2 2014-06-11 21:11:22 +08:00
parent 09a2ceb7df
commit aa06fab776
13 changed files with 1466 additions and 47 deletions

View File

@ -181,7 +181,8 @@ Widget* GUIReader::widgetFromJsonFile(const char *fileName)
{ {
std::string jsonpath; std::string jsonpath;
rapidjson::Document jsonDict; rapidjson::Document jsonDict;
jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName); jsonpath = fileName;
// jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
size_t pos = jsonpath.find_last_of('/'); size_t pos = jsonpath.find_last_of('/');
m_strFilePath = jsonpath.substr(0,pos+1); m_strFilePath = jsonpath.substr(0,pos+1);
std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath); std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);

View File

@ -59,13 +59,14 @@ public:
*/ */
const cocos2d::Size getFileDesignSize(const char* fileName) const; const cocos2d::Size getFileDesignSize(const char* fileName) const;
const std::string& getFilePath() const { return m_strFilePath; }; void setFilePath(const std::string& strFilePath) { m_strFilePath = strFilePath; }
const std::string& getFilePath() const { return m_strFilePath; }
void registerTypeAndCallBack(const std::string& classType, void registerTypeAndCallBack(const std::string& classType,
cocos2d::ObjectFactory::Instance ins, cocos2d::ObjectFactory::Instance ins,
Ref* object, Ref* object,
SEL_ParseEvent callBack); SEL_ParseEvent callBack);
protected: protected:
GUIReader(); GUIReader();
~GUIReader(); ~GUIReader();

View File

@ -116,7 +116,12 @@ void TextureFrame::onEnter(Frame *nextFrame)
{ {
if(_sprite) if(_sprite)
{ {
_sprite->setTexture(_texture.c_str()); SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(_texture);
if(spriteFrame != nullptr)
_sprite->setSpriteFrame(spriteFrame);
else
_sprite->setTexture(_texture);
} }
} }

View File

@ -60,7 +60,7 @@ static const char* ClassName_Widget = "Widget";
static const char* NODE = "nodeTree"; static const char* NODE = "nodeTree";
static const char* CHILDREN = "children"; static const char* CHILDREN = "children";
static const char* CLASSNAME = "classname"; static const char* CLASSNAME = "classname";
static const char* FILE_PATH = "filePath"; static const char* FILE_PATH = "fileName";
static const char* PLIST_FILE = "plistFile"; static const char* PLIST_FILE = "plistFile";
static const char* ACTION_TAG = "actionTag"; static const char* ACTION_TAG = "actionTag";
@ -100,6 +100,8 @@ static const char* MUL_MARGIN_TOP = "top";
static const char* MUL_MARGIN_RIGHT = "right"; static const char* MUL_MARGIN_RIGHT = "right";
static const char* MUL_MARGIN_BOTTOM = "bottom"; static const char* MUL_MARGIN_BOTTOM = "bottom";
static const char* TEXTURES = "textures";
static const char* TEXTURES_PNG = "texturesPng";
static NodeCache* _sharedNodeCache = nullptr; static NodeCache* _sharedNodeCache = nullptr;
@ -119,9 +121,14 @@ void NodeCache::destroyInstance()
CC_SAFE_DELETE(_sharedNodeCache); CC_SAFE_DELETE(_sharedNodeCache);
} }
NodeCache::NodeCache()
: _recordJsonPath(true)
, _jsonPath("")
{
}
void NodeCache::purge() void NodeCache::purge()
{ {
_nodes.clear();
} }
void NodeCache::init() void NodeCache::init()
@ -133,7 +140,7 @@ void NodeCache::init()
_funcs.insert(Pair(ClassName_Sprite, std::bind(&NodeCache::loadSprite, this, _1))); _funcs.insert(Pair(ClassName_Sprite, std::bind(&NodeCache::loadSprite, this, _1)));
_funcs.insert(Pair(ClassName_Particle, std::bind(&NodeCache::loadParticle, this, _1))); _funcs.insert(Pair(ClassName_Particle, std::bind(&NodeCache::loadParticle, this, _1)));
_funcs.insert(Pair(ClassName_Panel, std::bind(&NodeCache::loadWidget, this, _1))); _funcs.insert(Pair(ClassName_Panel, std::bind(&NodeCache::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_Button, std::bind(&NodeCache::loadWidget, this, _1))); _funcs.insert(Pair(ClassName_Button, std::bind(&NodeCache::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_CheckBox, std::bind(&NodeCache::loadWidget, this, _1))); _funcs.insert(Pair(ClassName_CheckBox, std::bind(&NodeCache::loadWidget, this, _1)));
_funcs.insert(Pair(ClassName_ImageView, std::bind(&NodeCache::loadWidget, this, _1))); _funcs.insert(Pair(ClassName_ImageView, std::bind(&NodeCache::loadWidget, this, _1)));
@ -154,14 +161,20 @@ void NodeCache::init()
cocos2d::Node* NodeCache::createNode(const std::string& filename) cocos2d::Node* NodeCache::createNode(const std::string& filename)
{ {
cocos2d::Node* node = _nodes.at(filename); if(_recordJsonPath)
if (node == nullptr)
{ {
node = loadNodeWithFile(filename); std::string jsonPath = filename.substr(0, filename.find_last_of('/') + 1);
GUIReader::shareReader()->setFilePath(jsonPath);
// if(cache) _jsonPath = jsonPath;
// _nodes.insert(filename, node);
} }
else
{
GUIReader::shareReader()->setFilePath("");
_jsonPath = "";
}
cocos2d::Node* node = loadNodeWithFile(filename);
return node; return node;
} }
@ -172,20 +185,34 @@ cocos2d::Node* NodeCache::loadNodeWithFile(const std::string& fileName)
std::string fullPath = CCFileUtils::getInstance()->fullPathForFilename(fileName); std::string fullPath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fullPath); std::string contentStr = FileUtils::getInstance()->getStringFromFile(fullPath);
cocos2d::Node* node = loadNodeWithContent(contentStr);
// Load animation data from file // Load animation data from file
TimelineActionCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr); TimelineActionCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr);
return loadNodeWithContent(contentStr); return node;
} }
cocos2d::Node* NodeCache::loadNodeWithContent(const std::string& content) cocos2d::Node* NodeCache::loadNodeWithContent(const std::string& content)
{ {
rapidjson::Document doc; rapidjson::Document doc;
doc.Parse<0>(content.c_str()); doc.Parse<0>(content.c_str());
if (doc.HasParseError()) { if (doc.HasParseError())
{
CCLOG("GetParseError %s\n", doc.GetParseError()); CCLOG("GetParseError %s\n", doc.GetParseError());
} }
// decode plist
int length = DICTOOL->getArrayCount_json(doc, TEXTURES);
for(int i=0; i<length; i++)
{
std::string plist = DICTOOL->getStringValueFromArray_json(doc, TEXTURES, i);
//std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
plist = _jsonPath + plist;
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
// decode node tree
const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE); const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
return loadNode(subJson); return loadNode(subJson);
} }
@ -209,7 +236,8 @@ cocos2d::Node* NodeCache::loadNode(const rapidjson::Value& json)
{ {
const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i); const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i);
cocos2d::Node* child = loadNode(dic); cocos2d::Node* child = loadNode(dic);
if (child) { if (child)
{
node->addChild(child); node->addChild(child);
} }
} }
@ -310,13 +338,16 @@ Node* NodeCache::loadSprite(const rapidjson::Value& json)
if(filePath != nullptr) if(filePath != nullptr)
{ {
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(filePath); std::string path = filePath;
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
if(!spriteFrame) if(!spriteFrame)
{ {
sprite = Sprite::create(filePath); sprite = Sprite::create(path);
} }
else else
{ {
path = _jsonPath + path;
sprite = Sprite::createWithSpriteFrame(spriteFrame); sprite = Sprite::createWithSpriteFrame(spriteFrame);
} }

View File

@ -41,6 +41,7 @@ public:
static NodeCache* getInstance(); static NodeCache* getInstance();
static void destroyInstance(); static void destroyInstance();
NodeCache();
void purge(); void purge();
void init(); void init();
@ -49,6 +50,13 @@ public:
cocos2d::Node* loadNodeWithFile(const std::string& fileName); cocos2d::Node* loadNodeWithFile(const std::string& fileName);
cocos2d::Node* loadNodeWithContent(const std::string& content); cocos2d::Node* loadNodeWithContent(const std::string& content);
void setRecordJsonPath(bool record) { _recordJsonPath = record; }
bool isRecordJsonPath() { return _recordJsonPath; }
void setJsonPath(std::string jsonPath) { _jsonPath = jsonPath; }
std::string getJsonPath() { return _jsonPath; }
protected: protected:
cocos2d::Node* loadNode(const rapidjson::Value& json); cocos2d::Node* loadNode(const rapidjson::Value& json);
@ -70,9 +78,11 @@ protected:
typedef std::pair<std::string, NodeCreateFunc> Pair; typedef std::pair<std::string, NodeCreateFunc> Pair;
std::unordered_map<std::string, NodeCreateFunc> _funcs; std::unordered_map<std::string, NodeCreateFunc> _funcs;
cocos2d::Map<std::string, cocos2d::Node*> _nodes;
WidgetPropertiesReader0300* _guiReader; WidgetPropertiesReader0300* _guiReader;
bool _recordJsonPath;
std::string _jsonPath;
}; };
} }

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include "CCTimelineActionCache.h" #include "CCTimelineActionCache.h"
#include "CCTimelineAction.h" #include "CCTimelineAction.h"
#include "CCFrame.h" #include "CCFrame.h"
#include "CCNodeCache.h"
using namespace cocos2d; using namespace cocos2d;
@ -342,8 +343,18 @@ Frame* TimelineActionCache::loadTextureFrame(const rapidjson::Value& json)
const char* texture = DICTOOL->getStringValue_json(json, Value); const char* texture = DICTOOL->getStringValue_json(json, Value);
if(texture != NULL) if(texture != NULL)
frame->setTexture(texture); {
std::string path = texture;
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
if(spriteFrame == nullptr)
{
std::string jsonPath = NodeCache::getInstance()->getJsonPath();
path = jsonPath + texture;
}
frame->setTexture(path);
}
return frame; return frame;
} }

View File

@ -68,26 +68,26 @@ bool AppDelegate::applicationDidFinishLaunching()
auto fileUtils = FileUtils::getInstance(); auto fileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths; std::vector<std::string> searchPaths;
if (screenSize.height > 320) // if (screenSize.height > 320)
{ // {
auto resourceSize = Size(960, 640); // auto resourceSize = Size(960, 640);
searchPaths.push_back("hd"); // searchPaths.push_back("hd");
searchPaths.push_back("ccs-res/hd"); // searchPaths.push_back("ccs-res/hd");
searchPaths.push_back("ccs-res/hd/scenetest"); // searchPaths.push_back("ccs-res/hd/scenetest");
searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest"); // searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");
searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest"); // searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");
searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest"); // searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");
searchPaths.push_back("ccs-res"); // searchPaths.push_back("ccs-res");
director->setContentScaleFactor(resourceSize.height/designSize.height); // director->setContentScaleFactor(resourceSize.height/designSize.height);
} // }
else // else
{ {
searchPaths.push_back("ccs-res"); searchPaths.push_back("ccs-res");
searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest"); searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest");

View File

@ -201,18 +201,16 @@ void TestTimelineAction::onEnter()
{ {
TimelineActionTestLayer::onEnter(); TimelineActionTestLayer::onEnter();
FileUtils::getInstance()->addSearchPath("E:/cocos2d-x/cocos2d-x/tests/cpp-tests/Resources/TimelineAction"); for(int i=0; i<100; i++)
for(int i=0; i<1; i++)
{ {
node = NodeCache::getInstance()->createNode("NewUi11123_1.json"); node = NodeCache::getInstance()->createNode("TimelineAction/boy_1.ExportJson");
action = TimelineActionCache::getInstance()->createAction("NewUi11123_1.json"); action = TimelineActionCache::getInstance()->createAction("TimelineAction/boy_1.ExportJson");
node->runAction(action); node->runAction(action);
action->gotoFrameAndPlay(0); action->gotoFrameAndPlay(0, 60, true);
//node->setScale(0.4f); node->setScale(0.4f);
node->setPosition(100+i*5,160); node->setPosition(-200+i*5,0);
addChild(node); addChild(node);
} }

View File

@ -0,0 +1,160 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>testAnimationResource/6.png</key>
<dict>
<key>width</key>
<integer>474</integer>
<key>height</key>
<integer>402</integer>
<key>originalWidth</key>
<integer>474</integer>
<key>originalHeight</key>
<integer>402</integer>
<key>x</key>
<integer>0</integer>
<key>y</key>
<integer>0</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/8.png</key>
<dict>
<key>width</key>
<integer>135</integer>
<key>height</key>
<integer>180</integer>
<key>originalWidth</key>
<integer>135</integer>
<key>originalHeight</key>
<integer>180</integer>
<key>x</key>
<integer>476</integer>
<key>y</key>
<integer>0</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/3.png</key>
<dict>
<key>width</key>
<integer>247</integer>
<key>height</key>
<integer>479</integer>
<key>originalWidth</key>
<integer>247</integer>
<key>originalHeight</key>
<integer>479</integer>
<key>x</key>
<integer>613</integer>
<key>y</key>
<integer>0</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/5.png</key>
<dict>
<key>width</key>
<integer>190</integer>
<key>height</key>
<integer>236</integer>
<key>originalWidth</key>
<integer>190</integer>
<key>originalHeight</key>
<integer>236</integer>
<key>x</key>
<integer>0</integer>
<key>y</key>
<integer>481</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/4.png</key>
<dict>
<key>width</key>
<integer>141</integer>
<key>height</key>
<integer>110</integer>
<key>originalWidth</key>
<integer>141</integer>
<key>originalHeight</key>
<integer>110</integer>
<key>x</key>
<integer>192</integer>
<key>y</key>
<integer>481</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/2.png</key>
<dict>
<key>width</key>
<integer>186</integer>
<key>height</key>
<integer>252</integer>
<key>originalWidth</key>
<integer>186</integer>
<key>originalHeight</key>
<integer>252</integer>
<key>x</key>
<integer>335</integer>
<key>y</key>
<integer>481</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/7.png</key>
<dict>
<key>width</key>
<integer>146</integer>
<key>height</key>
<integer>112</integer>
<key>originalWidth</key>
<integer>146</integer>
<key>originalHeight</key>
<integer>112</integer>
<key>x</key>
<integer>523</integer>
<key>y</key>
<integer>481</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>0</integer>
<key>textureFileName</key>
<string>boy0.png</string>
<key>realTextureFileName</key>
<string>boy0.png</string>
<key>size</key>
<string>{1024,1024}</string>
</dict>
<key>texture</key>
<dict>
<key>width</key>
<integer>1024</integer>
<key>height</key>
<integer>1024</integer>
</dict>
</dict>
</plist>

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 KiB

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>testAnimationResource/hat.png</key>
<dict>
<key>width</key>
<integer>452</integer>
<key>height</key>
<integer>458</integer>
<key>originalWidth</key>
<integer>452</integer>
<key>originalHeight</key>
<integer>458</integer>
<key>x</key>
<integer>0</integer>
<key>y</key>
<integer>0</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
<key>testAnimationResource/1.png</key>
<dict>
<key>width</key>
<integer>201</integer>
<key>height</key>
<integer>320</integer>
<key>originalWidth</key>
<integer>201</integer>
<key>originalHeight</key>
<integer>320</integer>
<key>x</key>
<integer>454</integer>
<key>y</key>
<integer>0</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>0</integer>
<key>textureFileName</key>
<string>boy1.png</string>
<key>realTextureFileName</key>
<string>boy1.png</string>
<key>size</key>
<string>{1024,1024}</string>
</dict>
<key>texture</key>
<dict>
<key>width</key>
<integer>1024</integer>
<key>height</key>
<integer>1024</integer>
</dict>
</dict>
</plist>

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

File diff suppressed because it is too large Load Diff