diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index e146fcc430..d3f6223954 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -f0d0e2815c4a581e7a1d8895efca2c2e8bc71679 \ No newline at end of file +64be2dade906f1baa9da4fdd207e4b1f9330ceb4 \ No newline at end of file diff --git a/cocos/2d/CCComponent.cpp b/cocos/2d/CCComponent.cpp index 4f8482367a..0d3deb1f47 100644 --- a/cocos/2d/CCComponent.cpp +++ b/cocos/2d/CCComponent.cpp @@ -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) diff --git a/cocos/2d/CCComponent.h b/cocos/2d/CCComponent.h index 5631c2585f..617cbe8750 100644 --- a/cocos/2d/CCComponent.h +++ b/cocos/2d/CCComponent.h @@ -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); diff --git a/cocos/editor-support/cocostudio/CCComAttribute.cpp b/cocos/editor-support/cocostudio/CCComAttribute.cpp index fc4ba9e2d4..b583952182 100644 --- a/cocos/editor-support/cocostudio/CCComAttribute.cpp +++ b/cocos/editor-support/cocostudio/CCComAttribute.cpp @@ -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; diff --git a/cocos/editor-support/cocostudio/CCComAttribute.h b/cocos/editor-support/cocostudio/CCComAttribute.h index e62d9cee94..6d432389b9 100644 --- a/cocos/editor-support/cocostudio/CCComAttribute.h +++ b/cocos/editor-support/cocostudio/CCComAttribute.h @@ -25,14 +25,13 @@ THE SOFTWARE. #ifndef __CC_EXTENTIONS_CCCOMATTRIBUTE_H__ #define __CC_EXTENTIONS_CCCOMATTRIBUTE_H__ -#include "cocos2d.h" -#include -#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; diff --git a/cocos/editor-support/cocostudio/CCComAudio.cpp b/cocos/editor-support/cocostudio/CCComAudio.cpp index 84a6bb8ea9..c513b8d861 100644 --- a/cocos/editor-support/cocostudio/CCComAudio.cpp +++ b/cocos/editor-support/cocostudio/CCComAudio.cpp @@ -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() diff --git a/cocos/editor-support/cocostudio/CCComAudio.h b/cocos/editor-support/cocostudio/CCComAudio.h index 94abd103aa..a80ca17604 100644 --- a/cocos/editor-support/cocostudio/CCComAudio.h +++ b/cocos/editor-support/cocostudio/CCComAudio.h @@ -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); diff --git a/cocos/editor-support/cocostudio/CCComBase.h b/cocos/editor-support/cocostudio/CCComBase.h new file mode 100644 index 0000000000..82c4bd11a8 --- /dev/null +++ b/cocos/editor-support/cocostudio/CCComBase.h @@ -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 + + +#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 diff --git a/cocos/editor-support/cocostudio/CCComController.cpp b/cocos/editor-support/cocostudio/CCComController.cpp index 41ab0f4401..1d22237c05 100644 --- a/cocos/editor-support/cocostudio/CCComController.cpp +++ b/cocos/editor-support/cocostudio/CCComController.cpp @@ -26,6 +26,7 @@ THE SOFTWARE. namespace cocostudio { +IMPLEMENT_CLASS_COMPONENT_INFO(ComController) ComController::ComController(void) { _name = "CCComController"; diff --git a/cocos/editor-support/cocostudio/CCComController.h b/cocos/editor-support/cocostudio/CCComController.h index d1778b84e6..0c77861e56 100644 --- a/cocos/editor-support/cocostudio/CCComController.h +++ b/cocos/editor-support/cocostudio/CCComController.h @@ -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 diff --git a/cocos/editor-support/cocostudio/CCComRender.cpp b/cocos/editor-support/cocostudio/CCComRender.cpp index dafff1ecf8..5ca698556b 100644 --- a/cocos/editor-support/cocostudio/CCComRender.cpp +++ b/cocos/editor-support/cocostudio/CCComRender.cpp @@ -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; +} + } diff --git a/cocos/editor-support/cocostudio/CCComRender.h b/cocos/editor-support/cocostudio/CCComRender.h index a7bcb9499e..255aa12e30 100644 --- a/cocos/editor-support/cocostudio/CCComRender.h +++ b/cocos/editor-support/cocostudio/CCComRender.h @@ -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; diff --git a/cocos/editor-support/cocostudio/CCSSceneReader.cpp b/cocos/editor-support/cocostudio/CCSSceneReader.cpp index 0a046c8334..36d0adc97d 100644 --- a/cocos/editor-support/cocostudio/CCSSceneReader.cpp +++ b/cocos/editor-support/cocostudio/CCSSceneReader.cpp @@ -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)); } } diff --git a/cocos/editor-support/cocostudio/CocoStudio.h b/cocos/editor-support/cocostudio/CocoStudio.h index caf7fbf00d..7a1b07bc99 100644 --- a/cocos/editor-support/cocostudio/CocoStudio.h +++ b/cocos/editor-support/cocostudio/CocoStudio.h @@ -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" diff --git a/cocos/editor-support/cocostudio/ObjectFactory.cpp b/cocos/editor-support/cocostudio/ObjectFactory.cpp index 17015867e2..268224b011 100644 --- a/cocos/editor-support/cocostudio/ObjectFactory.cpp +++ b/cocos/editor-support/cocostudio/ObjectFactory.cpp @@ -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)); diff --git a/cocos/editor-support/cocostudio/ObjectFactory.h b/cocos/editor-support/cocostudio/ObjectFactory.h index 3cdec3cd6d..26e2bd6447 100644 --- a/cocos/editor-support/cocostudio/ObjectFactory.h +++ b/cocos/editor-support/cocostudio/ObjectFactory.h @@ -26,7 +26,6 @@ THE SOFTWARE. #define __TRIGGERFACTORY_H__ #include "cocos2d.h" -#include "CocoStudio.h" #include #include @@ -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(); diff --git a/cocos/editor-support/cocostudio/TriggerBase.h b/cocos/editor-support/cocostudio/TriggerBase.h index 1558ff7b8a..921f6e96d4 100644 --- a/cocos/editor-support/cocostudio/TriggerBase.h +++ b/cocos/editor-support/cocostudio/TriggerBase.h @@ -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" diff --git a/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj b/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj index eca9c14aef..f572d6f892 100644 --- a/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj +++ b/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj @@ -75,6 +75,7 @@ + diff --git a/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj.filters b/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj.filters index 626355b8d3..bf17c59cbb 100644 --- a/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj.filters +++ b/cocos/editor-support/cocostudio/proj.win32/libCocosStudio.vcxproj.filters @@ -296,5 +296,8 @@ trigger + + components + \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp index e765f2677f..70d3ac2cbd 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp @@ -410,10 +410,10 @@ void UIComponentTest::touchEvent(Object *pSender, TouchEventType type) { case TOUCH_EVENT_BEGAN: { - ComRender *pBlowFish = static_cast(_node->getChildByTag(10010)->getComponent("Armature")); + ComRender *pBlowFish = static_cast(_node->getChildByTag(10010)->getComponent("CCArmature")); pBlowFish->getNode()->runAction(CCMoveBy::create(10.0f, Point(-1000.0f, 0))); - ComRender *pButterflyfish = static_cast(_node->getChildByTag(10011)->getComponent("Armature")); + ComRender *pButterflyfish = static_cast(_node->getChildByTag(10011)->getComponent("CCArmature")); pButterflyfish->getNode()->runAction(CCMoveBy::create(10.0f, Point(-1000.0f, 0))); } break;