mirror of https://github.com/axmolengine/axmol.git
Merge pull request #4883 from chengstory/#3582
Updates CocoStudio component parser.
This commit is contained in:
commit
b592786484
|
@ -1 +1 @@
|
|||
f0d0e2815c4a581e7a1d8895efca2c2e8bc71679
|
||||
64be2dade906f1baa9da4fdd207e4b1f9330ceb4
|
|
@ -54,8 +54,9 @@ void Component::update(float delta)
|
|||
{
|
||||
}
|
||||
|
||||
void Component::serialize(void *ar)
|
||||
bool Component::serialize(void *ar)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
Component* Component::create(void)
|
||||
|
|
|
@ -55,7 +55,7 @@ public:
|
|||
*/
|
||||
virtual void onExit();
|
||||
virtual void update(float delta);
|
||||
virtual void serialize(void* r);
|
||||
virtual bool serialize(void* r);
|
||||
virtual bool isEnabled() const;
|
||||
virtual void setEnabled(bool b);
|
||||
static Component* create(void);
|
||||
|
|
|
@ -23,10 +23,12 @@ THE SOFTWARE.
|
|||
****************************************************************************/
|
||||
|
||||
#include "cocostudio/CCComAttribute.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
IMPLEMENT_CLASS_COMPONENT_INFO(ComAttribute)
|
||||
ComAttribute::ComAttribute(void)
|
||||
{
|
||||
_name = "CCComAttribute";
|
||||
|
@ -139,6 +141,42 @@ ComAttribute* ComAttribute::create(void)
|
|||
return pRet;
|
||||
}
|
||||
|
||||
bool ComAttribute::serialize(void* r)
|
||||
{
|
||||
bool bRet = false;
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(r == nullptr);
|
||||
rapidjson::Value *v = (rapidjson::Value *)r;
|
||||
const char *className = DICTOOL->getStringValue_json(*v, "classname");
|
||||
CC_BREAK_IF(className == nullptr);
|
||||
const char *comName = DICTOOL->getStringValue_json(*v, "name");
|
||||
if (comName != nullptr)
|
||||
{
|
||||
setName(comName);
|
||||
}
|
||||
else
|
||||
{
|
||||
setName(className);
|
||||
}
|
||||
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
|
||||
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
|
||||
const char *file = DICTOOL->getStringValue_json(fileData, "path");
|
||||
CC_BREAK_IF(file == nullptr);
|
||||
std::string filePath;
|
||||
if (file != nullptr)
|
||||
{
|
||||
filePath.assign(cocos2d::CCFileUtils::getInstance()->fullPathForFilename(file));
|
||||
}
|
||||
int resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
|
||||
CC_BREAK_IF(resType != 0);
|
||||
parse(filePath.c_str());
|
||||
bRet = true;
|
||||
} while (0);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
bool ComAttribute::parse(const std::string &jsonFile)
|
||||
{
|
||||
bool ret = false;
|
||||
|
|
|
@ -25,14 +25,13 @@ THE SOFTWARE.
|
|||
#ifndef __CC_EXTENTIONS_CCCOMATTRIBUTE_H__
|
||||
#define __CC_EXTENTIONS_CCCOMATTRIBUTE_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include <string>
|
||||
#include "cocostudio/DictionaryHelper.h"
|
||||
#include "CCComBase.h"
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
class ComAttribute : public cocos2d::Component
|
||||
{
|
||||
DECLARE_CLASS_COMPONENT_INFO
|
||||
protected:
|
||||
/**
|
||||
* @js ctor
|
||||
|
@ -45,20 +44,19 @@ protected:
|
|||
virtual ~ComAttribute(void);
|
||||
|
||||
public:
|
||||
virtual bool init();
|
||||
static ComAttribute* create(void);
|
||||
|
||||
void setInt(const std::string& key, int value);
|
||||
void setFloat(const std::string& key, float value);
|
||||
void setBool(const std::string& key, bool value);
|
||||
void setString(const std::string& key, const std::string& value);
|
||||
|
||||
int getInt(const std::string& key, int def = 0) const;
|
||||
float getFloat(const std::string& key, float def = 0.0f) const;
|
||||
bool getBool(const std::string& key, bool def = false) const;
|
||||
std::string getString(const std::string& key, const std::string& def = "") const;
|
||||
|
||||
bool parse(const std::string &jsonFile);
|
||||
static ComAttribute* create(void);
|
||||
virtual bool init() override;
|
||||
virtual bool serialize(void* r) override;
|
||||
|
||||
void setInt(const std::string& key, int value);
|
||||
void setFloat(const std::string& key, float value);
|
||||
void setBool(const std::string& key, bool value);
|
||||
void setString(const std::string& key, const std::string& value);
|
||||
int getInt(const std::string& key, int def = 0) const;
|
||||
float getFloat(const std::string& key, float def = 0.0f) const;
|
||||
bool getBool(const std::string& key, bool def = false) const;
|
||||
std::string getString(const std::string& key, const std::string& def = "") const;
|
||||
bool parse(const std::string &jsonFile);
|
||||
private:
|
||||
cocos2d::ValueMap _dict;
|
||||
rapidjson::Document _doc;
|
||||
|
|
|
@ -27,6 +27,7 @@ THE SOFTWARE.
|
|||
|
||||
namespace cocostudio {
|
||||
|
||||
IMPLEMENT_CLASS_COMPONENT_INFO(ComAudio)
|
||||
ComAudio::ComAudio(void)
|
||||
: _filePath("")
|
||||
, _loop(false)
|
||||
|
@ -64,6 +65,57 @@ void ComAudio::setEnabled(bool b)
|
|||
_enabled = b;
|
||||
}
|
||||
|
||||
|
||||
bool ComAudio::serialize(void* r)
|
||||
{
|
||||
bool bRet = false;
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(r == nullptr);
|
||||
rapidjson::Value *v = (rapidjson::Value *)r;
|
||||
const char *className = DICTOOL->getStringValue_json(*v, "classname");
|
||||
CC_BREAK_IF(className == nullptr);
|
||||
const char *comName = DICTOOL->getStringValue_json(*v, "name");
|
||||
if (comName != nullptr)
|
||||
{
|
||||
setName(comName);
|
||||
}
|
||||
else
|
||||
{
|
||||
setName(className);
|
||||
}
|
||||
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
|
||||
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
|
||||
const char *file = DICTOOL->getStringValue_json(fileData, "path");
|
||||
CC_BREAK_IF(file == nullptr);
|
||||
std::string filePath;
|
||||
if (file != nullptr)
|
||||
{
|
||||
filePath.assign(cocos2d::CCFileUtils::getInstance()->fullPathForFilename(file));
|
||||
}
|
||||
int resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
|
||||
CC_BREAK_IF(resType != 0);
|
||||
if (strcmp(className, "CCBackgroundAudio") == 0)
|
||||
{
|
||||
preloadBackgroundMusic(filePath.c_str());
|
||||
bool loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false;
|
||||
setLoop(loop);
|
||||
playBackgroundMusic(filePath.c_str(), loop);
|
||||
}
|
||||
else if(strcmp(className, "CCComAudio") == 0)
|
||||
{
|
||||
preloadEffect(filePath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_BREAK_IF(true);
|
||||
}
|
||||
bRet = true;
|
||||
} while (0);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
ComAudio* ComAudio::create(void)
|
||||
{
|
||||
ComAudio * pRet = new ComAudio();
|
||||
|
@ -90,9 +142,9 @@ void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
|
|||
setLoop(false);
|
||||
}
|
||||
|
||||
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
||||
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
|
||||
{
|
||||
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, bLoop);
|
||||
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
|
||||
|
||||
}
|
||||
|
||||
|
@ -161,9 +213,9 @@ void ComAudio::setEffectsVolume(float volume)
|
|||
CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(volume);
|
||||
}
|
||||
|
||||
unsigned int ComAudio::playEffect(const char* pszFilePath, bool bLoop)
|
||||
unsigned int ComAudio::playEffect(const char* pszFilePath, bool loop)
|
||||
{
|
||||
return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, bLoop);
|
||||
return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, loop);
|
||||
}
|
||||
|
||||
unsigned int ComAudio::playEffect(const char* pszFilePath)
|
||||
|
@ -223,9 +275,9 @@ void ComAudio::setFile(const char* pszFilePath)
|
|||
_filePath.assign(pszFilePath);
|
||||
}
|
||||
|
||||
void ComAudio::setLoop(bool bLoop)
|
||||
void ComAudio::setLoop(bool loop)
|
||||
{
|
||||
_loop = bLoop;
|
||||
_loop = loop;
|
||||
}
|
||||
|
||||
const char* ComAudio::getFile()
|
||||
|
|
|
@ -25,12 +25,14 @@ THE SOFTWARE.
|
|||
#ifndef __CC_EXTENTIONS_CCCOMAUDIO_H__
|
||||
#define __CC_EXTENTIONS_CCCOMAUDIO_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "CCComBase.h"
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
class ComAudio : public cocos2d::Component
|
||||
{
|
||||
|
||||
DECLARE_CLASS_COMPONENT_INFO
|
||||
protected:
|
||||
/**
|
||||
* @js ctor
|
||||
|
@ -43,19 +45,20 @@ protected:
|
|||
virtual ~ComAudio(void);
|
||||
|
||||
public:
|
||||
virtual bool init();
|
||||
virtual bool init() override;
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
virtual void onEnter();
|
||||
virtual void onEnter() override;
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
virtual void onExit();
|
||||
virtual bool isEnabled() const;
|
||||
virtual void setEnabled(bool b);
|
||||
virtual void onExit() override;
|
||||
virtual bool isEnabled() const override;
|
||||
virtual void setEnabled(bool b) override;
|
||||
virtual bool serialize(void* r) override;
|
||||
|
||||
static ComAudio* create(void);
|
||||
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __CC_EXTENTIONS_CCCOMBASE_H__
|
||||
#define __CC_EXTENTIONS_CCCOMBASE_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "ObjectFactory.h"
|
||||
#include "DictionaryHelper.h"
|
||||
#include <string>
|
||||
|
||||
|
||||
#define DECLARE_CLASS_COMPONENT_INFO \
|
||||
public: \
|
||||
static cocostudio::ObjectFactory::TInfo Type; \
|
||||
static cocos2d::Object* createInstance(void); \
|
||||
|
||||
#define IMPLEMENT_CLASS_COMPONENT_INFO(className) \
|
||||
cocos2d::Object* className::createInstance(void) \
|
||||
{ \
|
||||
return className::create(); \
|
||||
} \
|
||||
cocostudio::ObjectFactory::TInfo className::Type(#className, &className::createInstance); \
|
||||
|
||||
#define CREATE_CLASS_COMPONENT_INFO(className) \
|
||||
cocostudio::ObjectFactory::TInfo(#className, &className::createInstance)
|
||||
|
||||
|
||||
#endif
|
|
@ -26,6 +26,7 @@ THE SOFTWARE.
|
|||
|
||||
namespace cocostudio {
|
||||
|
||||
IMPLEMENT_CLASS_COMPONENT_INFO(ComController)
|
||||
ComController::ComController(void)
|
||||
{
|
||||
_name = "CCComController";
|
||||
|
|
|
@ -25,13 +25,15 @@ THE SOFTWARE.
|
|||
#ifndef __CC_EXTENTIONS_CCCOMCONTROLLER_H__
|
||||
#define __CC_EXTENTIONS_CCCOMCONTROLLER_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "CCComBase.h"
|
||||
#include "cocostudio/CCInputDelegate.h"
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
class ComController : public cocos2d::Component, public InputDelegate
|
||||
{
|
||||
|
||||
DECLARE_CLASS_COMPONENT_INFO
|
||||
public:
|
||||
/**
|
||||
* @js ctor
|
||||
|
@ -44,12 +46,12 @@ public:
|
|||
* @lua NA
|
||||
*/
|
||||
virtual ~ComController(void);
|
||||
virtual bool init();
|
||||
virtual bool init() override;
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
virtual void onEnter();
|
||||
virtual void onEnter() override;
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
|
|
|
@ -23,9 +23,13 @@ THE SOFTWARE.
|
|||
****************************************************************************/
|
||||
|
||||
#include "cocostudio/CCComRender.h"
|
||||
#include "cocostudio/CocoStudio.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
IMPLEMENT_CLASS_COMPONENT_INFO(ComRender)
|
||||
ComRender::ComRender(void)
|
||||
: _render(nullptr)
|
||||
{
|
||||
|
@ -67,9 +71,124 @@ void ComRender::setNode(cocos2d::Node *node)
|
|||
_render = node;
|
||||
}
|
||||
|
||||
ComRender* ComRender::create(cocos2d::Node *node, const char *comName)
|
||||
|
||||
bool ComRender::serialize(void* r)
|
||||
{
|
||||
ComRender * ret = new ComRender(node, comName);
|
||||
bool bRet = false;
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(r == nullptr);
|
||||
rapidjson::Value *v = (rapidjson::Value *)r;
|
||||
const char *className = DICTOOL->getStringValue_json(*v, "classname");
|
||||
CC_BREAK_IF(className == nullptr);
|
||||
const char *comName = DICTOOL->getStringValue_json(*v, "name");
|
||||
if (comName != nullptr)
|
||||
{
|
||||
setName(comName);
|
||||
}
|
||||
else
|
||||
{
|
||||
setName(className);
|
||||
}
|
||||
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
|
||||
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
|
||||
const char *file = DICTOOL->getStringValue_json(fileData, "path");
|
||||
const char *plist = DICTOOL->getStringValue_json(fileData, "plistFile");
|
||||
CC_BREAK_IF(file == nullptr && plist == nullptr);
|
||||
std::string filePath;
|
||||
std::string plistPath;
|
||||
if (file != nullptr)
|
||||
{
|
||||
filePath.assign(cocos2d::CCFileUtils::getInstance()->fullPathForFilename(file));
|
||||
}
|
||||
if (plist != nullptr)
|
||||
{
|
||||
plistPath.assign(cocos2d::CCFileUtils::getInstance()->fullPathForFilename(plist));
|
||||
}
|
||||
int resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
|
||||
if (resType == 0)
|
||||
{
|
||||
if (strcmp(className, "CCSprite") == 0 && filePath.find(".png") != std::string::npos)
|
||||
{
|
||||
_render = Sprite::create(filePath.c_str());
|
||||
}
|
||||
else if(strcmp(className, "CCTMXTiledMap") == 0 && filePath.find(".tmx") != std::string::npos)
|
||||
{
|
||||
_render = TMXTiledMap::create(filePath.c_str());
|
||||
}
|
||||
else if(strcmp(className, "CCParticleSystemQuad") == 0 && filePath.find(".plist") != std::string::npos)
|
||||
{
|
||||
_render = ParticleSystemQuad::create(filePath.c_str());
|
||||
_render->setPosition(Point(0.0f, 0.0f));
|
||||
}
|
||||
else if(strcmp(className, "CCArmature") == 0)
|
||||
{
|
||||
std::string reDir = filePath;
|
||||
std::string file_path = "";
|
||||
size_t pos = reDir.find_last_of('/');
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
file_path = reDir.substr(0, pos+1);
|
||||
}
|
||||
rapidjson::Document doc;
|
||||
if(!readJson(filePath.c_str(), doc))
|
||||
{
|
||||
log("read json file[%s] error!\n", filePath.c_str());
|
||||
continue;
|
||||
}
|
||||
const rapidjson::Value &subData = DICTOOL->getDictionaryFromArray_json(doc, "armature_data", 0);
|
||||
const char *name = DICTOOL->getStringValue_json(subData, "name");
|
||||
ArmatureDataManager::getInstance()->addArmatureFileInfo(filePath.c_str());
|
||||
Armature *pAr = Armature::create(name);
|
||||
_render = pAr;
|
||||
const char *actionName = DICTOOL->getStringValue_json(*v, "selectedactionname");
|
||||
if (actionName != nullptr && pAr->getAnimation() != nullptr)
|
||||
{
|
||||
pAr->getAnimation()->play(actionName);
|
||||
}
|
||||
}
|
||||
else if(strcmp(className, "GUIComponent") == 0)
|
||||
{
|
||||
cocos2d::gui::Widget* widget = GUIReader::getInstance()->widgetFromJsonFile(filePath.c_str());
|
||||
_render = widget;
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_BREAK_IF(true);
|
||||
}
|
||||
}
|
||||
else if (resType == 1)
|
||||
{
|
||||
if (strcmp(className, "CCSprite") == 0)
|
||||
{
|
||||
std::string strPngFile = plistPath;
|
||||
std::string::size_type pos = strPngFile.find(".plist");
|
||||
if (pos == strPngFile.npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
strPngFile.replace(pos, strPngFile.length(), ".png");
|
||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath.c_str(), strPngFile.c_str());
|
||||
_render = Sprite::createWithSpriteFrameName(filePath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_BREAK_IF(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_BREAK_IF(true);
|
||||
}
|
||||
bRet = true;
|
||||
} while (0);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
ComRender* ComRender::create(void)
|
||||
{
|
||||
ComRender * ret = new ComRender();
|
||||
if (ret != nullptr && ret->init())
|
||||
{
|
||||
ret->autorelease();
|
||||
|
@ -81,4 +200,30 @@ ComRender* ComRender::create(cocos2d::Node *node, const char *comName)
|
|||
return ret;
|
||||
}
|
||||
|
||||
ComRender* ComRender::create(cocos2d::Node *node, const char *comName)
|
||||
{
|
||||
ComRender * ret = new ComRender(node, comName);
|
||||
if (ret != nullptr && ret->init())
|
||||
{
|
||||
ret->autorelease();
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_SAFE_DELETE(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ComRender::readJson(const std::string &fileName, rapidjson::Document &doc)
|
||||
{
|
||||
bool ret = false;
|
||||
do {
|
||||
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName);
|
||||
doc.Parse<0>(contentStr.c_str());
|
||||
CC_BREAK_IF(doc.HasParseError());
|
||||
ret = true;
|
||||
} while (0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,15 +22,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __CC_EXTENTIONS_CCCOMNODE_H__
|
||||
#define __CC_EXTENTIONS_CCCOMNODE_H__
|
||||
#ifndef __CC_EXTENTIONS_CCCOMRENDER_H__
|
||||
#define __CC_EXTENTIONS_CCCOMRENDER_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "CCComBase.h"
|
||||
|
||||
namespace cocostudio {
|
||||
|
||||
class ComRender : public cocos2d::Component
|
||||
{
|
||||
DECLARE_CLASS_COMPONENT_INFO
|
||||
protected:
|
||||
/**
|
||||
* @js ctor
|
||||
|
@ -48,16 +49,20 @@ public:
|
|||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
virtual void onEnter();
|
||||
virtual void onEnter() override;
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
virtual void onExit();
|
||||
cocos2d::Node* getNode();
|
||||
void setNode(cocos2d::Node *node);
|
||||
virtual void onExit() override;
|
||||
virtual bool serialize(void* r) override;
|
||||
virtual cocos2d::Node* getNode();
|
||||
virtual void setNode(cocos2d::Node *node);
|
||||
|
||||
static ComRender* create(void);
|
||||
static ComRender* create(cocos2d::Node *node, const char *comName);
|
||||
private:
|
||||
bool readJson(const std::string &fileName, rapidjson::Document &doc);
|
||||
|
||||
private:
|
||||
cocos2d::Node *_render;
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "cocostudio/CocoStudio.h"
|
||||
#include "gui/CocosGUI.h"
|
||||
#include "SimpleAudioEngine.h"
|
||||
#include "ObjectFactory.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
using namespace gui;
|
||||
|
@ -37,6 +38,10 @@ SceneReader::SceneReader()
|
|||
: _fnSelector(nullptr)
|
||||
, _node(nullptr)
|
||||
{
|
||||
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAttribute));
|
||||
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComRender));
|
||||
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAudio));
|
||||
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComController));
|
||||
}
|
||||
|
||||
SceneReader::~SceneReader()
|
||||
|
@ -132,259 +137,21 @@ Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* par
|
|||
break;
|
||||
}
|
||||
const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
|
||||
const char *pComName = DICTOOL->getStringValue_json(subDict, "name");
|
||||
|
||||
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(subDict, "fileData");
|
||||
std::string pPath;
|
||||
std::string pPlistFile;
|
||||
int nResType = 0;
|
||||
if (DICTOOL->checkObjectExist_json(fileData))
|
||||
Component *com = ObjectFactory::getInstance()->createComponent(comName);
|
||||
if (com != NULL)
|
||||
{
|
||||
if (com->serialize((void*)(&subDict)))
|
||||
{
|
||||
gb->addComponent(com);
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_SAFE_RELEASE(com);
|
||||
}
|
||||
}
|
||||
if(_fnSelector != nullptr)
|
||||
{
|
||||
const char *file = DICTOOL->getStringValue_json(fileData, "path");
|
||||
nResType = DICTOOL->getIntValue_json(fileData, "resourceType", - 1);
|
||||
const char *plistFile = DICTOOL->getStringValue_json(fileData, "plistFile");
|
||||
if (file != nullptr)
|
||||
{
|
||||
pPath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
|
||||
}
|
||||
|
||||
if (plistFile != nullptr)
|
||||
{
|
||||
pPlistFile.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(plistFile));
|
||||
}
|
||||
|
||||
if (file == nullptr && plistFile == nullptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (comName != nullptr && strcmp(comName, "CCSprite") == 0)
|
||||
{
|
||||
cocos2d::Sprite *pSprite = nullptr;
|
||||
|
||||
if (nResType == 0)
|
||||
{
|
||||
if (pPath.find(".png") == pPath.npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pSprite = Sprite::create(pPath.c_str());
|
||||
}
|
||||
else if (nResType == 1)
|
||||
{
|
||||
std::string pngFile = pPlistFile;
|
||||
std::string::size_type pos = pngFile.find(".plist");
|
||||
if (pos == pPath.npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pngFile.replace(pos, pngFile.length(), ".png");
|
||||
CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile(pPlistFile.c_str(), pngFile.c_str());
|
||||
pSprite = Sprite::createWithSpriteFrameName(pPath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ComRender *pRender = ComRender::create(pSprite, "CCSprite");
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pRender->setName(pComName);
|
||||
}
|
||||
|
||||
gb->addComponent(pRender);
|
||||
if (_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pSprite, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "CCTMXTiledMap") == 0)
|
||||
{
|
||||
cocos2d::TMXTiledMap *pTmx = nullptr;
|
||||
if (nResType == 0)
|
||||
{
|
||||
if (pPath.find(".tmx") == pPath.npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pTmx = TMXTiledMap::create(pPath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ComRender *pRender = ComRender::create(pTmx, "CCTMXTiledMap");
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pRender->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pRender);
|
||||
if (_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pTmx, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "CCParticleSystemQuad") == 0)
|
||||
{
|
||||
std::string::size_type pos = pPath.find(".plist");
|
||||
if (pos == pPath.npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
cocos2d::ParticleSystemQuad *pParticle = nullptr;
|
||||
if (nResType == 0)
|
||||
{
|
||||
pParticle = ParticleSystemQuad::create(pPath.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOG("unknown resourcetype on CCParticleSystemQuad!");
|
||||
}
|
||||
|
||||
pParticle->setPosition(0, 0);
|
||||
ComRender *pRender = ComRender::create(pParticle, "CCParticleSystemQuad");
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pRender->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pRender);
|
||||
if(_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pParticle, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "CCArmature") == 0)
|
||||
{
|
||||
if (nResType != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
std::string reDir = pPath;
|
||||
std::string file_path = "";
|
||||
size_t pos = reDir.find_last_of('/');
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
file_path = reDir.substr(0, pos+1);
|
||||
}
|
||||
|
||||
rapidjson::Document jsonDict;
|
||||
if(!readJson(pPath.c_str(), jsonDict))
|
||||
{
|
||||
log("read json file[%s] error!\n", pPath.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
const rapidjson::Value &subData = DICTOOL->getDictionaryFromArray_json(jsonDict, "armature_data", 0);
|
||||
const char *name = DICTOOL->getStringValue_json(subData, "name");
|
||||
|
||||
ArmatureDataManager::getInstance()->addArmatureFileInfo(pPath.c_str());
|
||||
|
||||
Armature *pAr = Armature::create(name);
|
||||
ComRender *pRender = ComRender::create(pAr, "CCArmature");
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pRender->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pRender);
|
||||
|
||||
const char *actionName = DICTOOL->getStringValue_json(subDict, "selectedactionname");
|
||||
if (actionName != nullptr && pAr->getAnimation() != nullptr)
|
||||
{
|
||||
pAr->getAnimation()->play(actionName);
|
||||
}
|
||||
if (_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pAr, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "CCComAudio") == 0)
|
||||
{
|
||||
ComAudio *pAudio = nullptr;
|
||||
if (nResType == 0)
|
||||
{
|
||||
pAudio = ComAudio::create();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pAudio->preloadEffect(pPath.c_str());
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pAudio->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pAudio);
|
||||
if(_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pAudio, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "CCComAttribute") == 0)
|
||||
{
|
||||
ComAttribute *pAttribute = nullptr;
|
||||
if (nResType == 0)
|
||||
{
|
||||
pAttribute = ComAttribute::create();
|
||||
}
|
||||
else
|
||||
{
|
||||
CCLOG("unknown resourcetype on CCComAttribute!");
|
||||
continue;
|
||||
}
|
||||
pAttribute->parse(pPath);
|
||||
gb->addComponent(pAttribute);
|
||||
if(_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(pAttribute, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
else if (comName != nullptr && strcmp(comName, "CCBackgroundAudio") == 0)
|
||||
{
|
||||
ComAudio *pAudio = nullptr;
|
||||
if (nResType == 0)
|
||||
{
|
||||
pAudio = ComAudio::create();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
pAudio->preloadBackgroundMusic(pPath.c_str());
|
||||
pAudio->setFile(pPath.c_str());
|
||||
const bool bLoop = (DICTOOL->getIntValue_json(subDict, "loop") != 0);
|
||||
pAudio->setLoop(bLoop);
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pAudio->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pAudio);
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pAudio->setName(pComName);
|
||||
}
|
||||
pAudio->playBackgroundMusic(pPath.c_str(), bLoop);
|
||||
}
|
||||
else if(comName != nullptr && strcmp(comName, "GUIComponent") == 0)
|
||||
{
|
||||
Widget* widget= GUIReader::getInstance()->widgetFromJsonFile(pPath.c_str());
|
||||
ComRender *pRender = ComRender::create(widget, "GUIComponent");
|
||||
if (pComName != nullptr)
|
||||
{
|
||||
pRender->setName(pComName);
|
||||
}
|
||||
gb->addComponent(pRender);
|
||||
if(_fnSelector != nullptr)
|
||||
{
|
||||
_fnSelector(widget, (void*)(&subDict));
|
||||
}
|
||||
_fnSelector(com, (void*)(&subDict));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -49,6 +49,7 @@
|
|||
#include "cocostudio/CCTransformHelp.h"
|
||||
#include "cocostudio/CCTweenFunction.h"
|
||||
#include "cocostudio/CCUtilMath.h"
|
||||
#include "cocostudio/CCComBase.h"
|
||||
#include "cocostudio/CCComAttribute.h"
|
||||
#include "cocostudio/CCComAudio.h"
|
||||
#include "cocostudio/CCComController.h"
|
||||
|
|
|
@ -87,7 +87,7 @@ void ObjectFactory::destroyInstance()
|
|||
CC_SAFE_DELETE(_sharedFactory);
|
||||
}
|
||||
|
||||
Object* ObjectFactory::createObject(const char *name)
|
||||
Object* ObjectFactory::createObject(const std::string &name)
|
||||
{
|
||||
Object *o = nullptr;
|
||||
do
|
||||
|
@ -100,6 +100,45 @@ Object* ObjectFactory::createObject(const char *name)
|
|||
return o;
|
||||
}
|
||||
|
||||
Component* ObjectFactory::createComponent(const std::string &name)
|
||||
{
|
||||
std::string comName;
|
||||
if (name == "CCSprite" || name == "CCTMXTiledMap" || name == "CCParticleSystemQuad" || name == "CCArmature" || name == "GUIComponent")
|
||||
{
|
||||
comName = "ComRender";
|
||||
}
|
||||
else if (name == "CCComAudio" || name == "CCBackgroundAudio")
|
||||
{
|
||||
comName = "ComAudio";
|
||||
}
|
||||
else if (name == "CCComController")
|
||||
{
|
||||
comName = "ComController";
|
||||
}
|
||||
else if (name == "CCComAttribute")
|
||||
{
|
||||
comName = "ComAttribute";
|
||||
}
|
||||
else if (name == "CCScene")
|
||||
{
|
||||
comName = "Scene";
|
||||
}
|
||||
else
|
||||
{
|
||||
CCASSERT(false, "Unregistered Component!");
|
||||
}
|
||||
Object *o = NULL;
|
||||
do
|
||||
{
|
||||
const TInfo t = _typeMap[comName];
|
||||
CC_BREAK_IF(t._fun == NULL);
|
||||
o = t._fun();
|
||||
} while (0);
|
||||
|
||||
return (Component*)o;
|
||||
|
||||
}
|
||||
|
||||
void ObjectFactory::registerType(const TInfo &t)
|
||||
{
|
||||
_typeMap.insert(std::make_pair(t._class, t));
|
||||
|
|
|
@ -26,7 +26,6 @@ THE SOFTWARE.
|
|||
#define __TRIGGERFACTORY_H__
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "CocoStudio.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
|
@ -50,7 +49,8 @@ public:
|
|||
|
||||
static ObjectFactory* getInstance();
|
||||
static void destroyInstance();
|
||||
cocos2d::Object* createObject(const char *name);
|
||||
cocos2d::Object* createObject(const std::string &name);
|
||||
cocos2d::Component* createComponent(const std::string &name);
|
||||
void registerType(const TInfo &t);
|
||||
void removeAll();
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@ THE SOFTWARE.
|
|||
|
||||
#include "cocos2d.h"
|
||||
#include "cocostudio/CocoStudio.h"
|
||||
#include "TriggerObj.h"
|
||||
#include "ObjectFactory.h"
|
||||
#include "TriggerObj.h"
|
||||
#include "TriggerMng.h"
|
||||
|
||||
|
||||
|
|
|
@ -75,6 +75,7 @@
|
|||
<ClInclude Include="..\CCColliderDetector.h" />
|
||||
<ClInclude Include="..\CCComAttribute.h" />
|
||||
<ClInclude Include="..\CCComAudio.h" />
|
||||
<ClInclude Include="..\CCComBase.h" />
|
||||
<ClInclude Include="..\CCComController.h" />
|
||||
<ClInclude Include="..\CCComRender.h" />
|
||||
<ClInclude Include="..\CCDataReaderHelper.h" />
|
||||
|
|
|
@ -296,5 +296,8 @@
|
|||
<ClInclude Include="..\TriggerObj.h">
|
||||
<Filter>trigger</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\CCComBase.h">
|
||||
<Filter>components</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -410,10 +410,10 @@ void UIComponentTest::touchEvent(Object *pSender, TouchEventType type)
|
|||
{
|
||||
case TOUCH_EVENT_BEGAN:
|
||||
{
|
||||
ComRender *pBlowFish = static_cast<ComRender*>(_node->getChildByTag(10010)->getComponent("Armature"));
|
||||
ComRender *pBlowFish = static_cast<ComRender*>(_node->getChildByTag(10010)->getComponent("CCArmature"));
|
||||
pBlowFish->getNode()->runAction(CCMoveBy::create(10.0f, Point(-1000.0f, 0)));
|
||||
|
||||
ComRender *pButterflyfish = static_cast<ComRender*>(_node->getChildByTag(10011)->getComponent("Armature"));
|
||||
ComRender *pButterflyfish = static_cast<ComRender*>(_node->getChildByTag(10011)->getComponent("CCArmature"));
|
||||
pButterflyfish->getNode()->runAction(CCMoveBy::create(10.0f, Point(-1000.0f, 0)));
|
||||
}
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue