mirror of https://github.com/axmolengine/axmol.git
support relative path for reading json
This commit is contained in:
parent
09a2ceb7df
commit
aa06fab776
|
@ -181,7 +181,8 @@ Widget* GUIReader::widgetFromJsonFile(const char *fileName)
|
|||
{
|
||||
std::string jsonpath;
|
||||
rapidjson::Document jsonDict;
|
||||
jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
|
||||
jsonpath = fileName;
|
||||
// jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
|
||||
size_t pos = jsonpath.find_last_of('/');
|
||||
m_strFilePath = jsonpath.substr(0,pos+1);
|
||||
std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
|
||||
|
|
|
@ -59,13 +59,14 @@ public:
|
|||
*/
|
||||
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,
|
||||
cocos2d::ObjectFactory::Instance ins,
|
||||
Ref* object,
|
||||
SEL_ParseEvent callBack);
|
||||
|
||||
|
||||
protected:
|
||||
GUIReader();
|
||||
~GUIReader();
|
||||
|
|
|
@ -116,7 +116,12 @@ void TextureFrame::onEnter(Frame *nextFrame)
|
|||
{
|
||||
if(_sprite)
|
||||
{
|
||||
_sprite->setTexture(_texture.c_str());
|
||||
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(_texture);
|
||||
|
||||
if(spriteFrame != nullptr)
|
||||
_sprite->setSpriteFrame(spriteFrame);
|
||||
else
|
||||
_sprite->setTexture(_texture);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ static const char* ClassName_Widget = "Widget";
|
|||
static const char* NODE = "nodeTree";
|
||||
static const char* CHILDREN = "children";
|
||||
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* 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_BOTTOM = "bottom";
|
||||
|
||||
static const char* TEXTURES = "textures";
|
||||
static const char* TEXTURES_PNG = "texturesPng";
|
||||
|
||||
static NodeCache* _sharedNodeCache = nullptr;
|
||||
|
||||
|
@ -119,9 +121,14 @@ void NodeCache::destroyInstance()
|
|||
CC_SAFE_DELETE(_sharedNodeCache);
|
||||
}
|
||||
|
||||
NodeCache::NodeCache()
|
||||
: _recordJsonPath(true)
|
||||
, _jsonPath("")
|
||||
{
|
||||
}
|
||||
|
||||
void NodeCache::purge()
|
||||
{
|
||||
_nodes.clear();
|
||||
}
|
||||
|
||||
void NodeCache::init()
|
||||
|
@ -154,14 +161,20 @@ void NodeCache::init()
|
|||
|
||||
cocos2d::Node* NodeCache::createNode(const std::string& filename)
|
||||
{
|
||||
cocos2d::Node* node = _nodes.at(filename);
|
||||
if (node == nullptr)
|
||||
if(_recordJsonPath)
|
||||
{
|
||||
node = loadNodeWithFile(filename);
|
||||
std::string jsonPath = filename.substr(0, filename.find_last_of('/') + 1);
|
||||
GUIReader::shareReader()->setFilePath(jsonPath);
|
||||
|
||||
// if(cache)
|
||||
// _nodes.insert(filename, node);
|
||||
_jsonPath = jsonPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
GUIReader::shareReader()->setFilePath("");
|
||||
_jsonPath = "";
|
||||
}
|
||||
|
||||
cocos2d::Node* node = loadNodeWithFile(filename);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
@ -172,20 +185,34 @@ cocos2d::Node* NodeCache::loadNodeWithFile(const std::string& fileName)
|
|||
std::string fullPath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
|
||||
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fullPath);
|
||||
|
||||
cocos2d::Node* node = loadNodeWithContent(contentStr);
|
||||
|
||||
// Load animation data from file
|
||||
TimelineActionCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr);
|
||||
|
||||
return loadNodeWithContent(contentStr);
|
||||
return node;
|
||||
}
|
||||
|
||||
cocos2d::Node* NodeCache::loadNodeWithContent(const std::string& content)
|
||||
{
|
||||
rapidjson::Document doc;
|
||||
doc.Parse<0>(content.c_str());
|
||||
if (doc.HasParseError()) {
|
||||
if (doc.HasParseError())
|
||||
{
|
||||
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);
|
||||
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);
|
||||
cocos2d::Node* child = loadNode(dic);
|
||||
if (child) {
|
||||
if (child)
|
||||
{
|
||||
node->addChild(child);
|
||||
}
|
||||
}
|
||||
|
@ -310,13 +338,16 @@ Node* NodeCache::loadSprite(const rapidjson::Value& json)
|
|||
|
||||
if(filePath != nullptr)
|
||||
{
|
||||
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(filePath);
|
||||
std::string path = filePath;
|
||||
|
||||
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
|
||||
if(!spriteFrame)
|
||||
{
|
||||
sprite = Sprite::create(filePath);
|
||||
sprite = Sprite::create(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
path = _jsonPath + path;
|
||||
sprite = Sprite::createWithSpriteFrame(spriteFrame);
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ public:
|
|||
static NodeCache* getInstance();
|
||||
static void destroyInstance();
|
||||
|
||||
NodeCache();
|
||||
void purge();
|
||||
|
||||
void init();
|
||||
|
@ -49,6 +50,13 @@ public:
|
|||
|
||||
cocos2d::Node* loadNodeWithFile(const std::string& fileName);
|
||||
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:
|
||||
|
||||
cocos2d::Node* loadNode(const rapidjson::Value& json);
|
||||
|
@ -70,9 +78,11 @@ protected:
|
|||
typedef std::pair<std::string, NodeCreateFunc> Pair;
|
||||
|
||||
std::unordered_map<std::string, NodeCreateFunc> _funcs;
|
||||
cocos2d::Map<std::string, cocos2d::Node*> _nodes;
|
||||
|
||||
WidgetPropertiesReader0300* _guiReader;
|
||||
bool _recordJsonPath;
|
||||
|
||||
std::string _jsonPath;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ THE SOFTWARE.
|
|||
#include "CCTimelineActionCache.h"
|
||||
#include "CCTimelineAction.h"
|
||||
#include "CCFrame.h"
|
||||
#include "CCNodeCache.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
|
@ -342,8 +343,18 @@ Frame* TimelineActionCache::loadTextureFrame(const rapidjson::Value& json)
|
|||
const char* texture = DICTOOL->getStringValue_json(json, Value);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,26 +68,26 @@ bool AppDelegate::applicationDidFinishLaunching()
|
|||
auto fileUtils = FileUtils::getInstance();
|
||||
std::vector<std::string> searchPaths;
|
||||
|
||||
if (screenSize.height > 320)
|
||||
{
|
||||
auto resourceSize = Size(960, 640);
|
||||
searchPaths.push_back("hd");
|
||||
searchPaths.push_back("ccs-res/hd");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");
|
||||
searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");
|
||||
searchPaths.push_back("ccs-res");
|
||||
director->setContentScaleFactor(resourceSize.height/designSize.height);
|
||||
}
|
||||
else
|
||||
// if (screenSize.height > 320)
|
||||
// {
|
||||
// auto resourceSize = Size(960, 640);
|
||||
// searchPaths.push_back("hd");
|
||||
// searchPaths.push_back("ccs-res/hd");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");
|
||||
// searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");
|
||||
// searchPaths.push_back("ccs-res");
|
||||
// director->setContentScaleFactor(resourceSize.height/designSize.height);
|
||||
// }
|
||||
// else
|
||||
{
|
||||
searchPaths.push_back("ccs-res");
|
||||
searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest");
|
||||
|
|
|
@ -201,18 +201,16 @@ void TestTimelineAction::onEnter()
|
|||
{
|
||||
TimelineActionTestLayer::onEnter();
|
||||
|
||||
FileUtils::getInstance()->addSearchPath("E:/cocos2d-x/cocos2d-x/tests/cpp-tests/Resources/TimelineAction");
|
||||
|
||||
for(int i=0; i<1; i++)
|
||||
for(int i=0; i<100; i++)
|
||||
{
|
||||
node = NodeCache::getInstance()->createNode("NewUi11123_1.json");
|
||||
action = TimelineActionCache::getInstance()->createAction("NewUi11123_1.json");
|
||||
node = NodeCache::getInstance()->createNode("TimelineAction/boy_1.ExportJson");
|
||||
action = TimelineActionCache::getInstance()->createAction("TimelineAction/boy_1.ExportJson");
|
||||
|
||||
node->runAction(action);
|
||||
action->gotoFrameAndPlay(0);
|
||||
action->gotoFrameAndPlay(0, 60, true);
|
||||
|
||||
//node->setScale(0.4f);
|
||||
node->setPosition(100+i*5,160);
|
||||
node->setScale(0.4f);
|
||||
node->setPosition(-200+i*5,0);
|
||||
|
||||
addChild(node);
|
||||
}
|
||||
|
|
|
@ -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 |
|
@ -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
Loading…
Reference in New Issue