123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /****************************************************************************
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- 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.
- ****************************************************************************/
- #include "2d/CCSpriteFrameCache.h"
- #include "editor-support/cocostudio/CCArmatureDataManager.h"
- #include "editor-support/cocostudio/CCTransformHelp.h"
- #include "editor-support/cocostudio/CCDataReaderHelper.h"
- #include "editor-support/cocostudio/CCSpriteFrameCacheHelper.h"
- using namespace cocos2d;
- namespace cocostudio {
- static ArmatureDataManager *s_sharedArmatureDataManager = nullptr;
- ArmatureDataManager *ArmatureDataManager::getInstance()
- {
- if (s_sharedArmatureDataManager == nullptr)
- {
- s_sharedArmatureDataManager = new (std::nothrow) ArmatureDataManager();
- if (!s_sharedArmatureDataManager || !s_sharedArmatureDataManager->init())
- {
- CC_SAFE_DELETE(s_sharedArmatureDataManager);
- }
- }
- return s_sharedArmatureDataManager;
- }
- void ArmatureDataManager::destroyInstance()
- {
- SpriteFrameCacheHelper::purge();
- DataReaderHelper::purge();
- CC_SAFE_RELEASE_NULL(s_sharedArmatureDataManager);
- }
- ArmatureDataManager::ArmatureDataManager(void)
- {
- _armarureDatas.clear();
- _animationDatas.clear();
- _textureDatas.clear();
- _autoLoadSpriteFile = false;
- }
- ArmatureDataManager::~ArmatureDataManager(void)
- {
- _animationDatas.clear();
- _armarureDatas.clear();
- _textureDatas.clear();
- _relativeDatas.clear();
- }
- bool ArmatureDataManager::init()
- {
- bool bRet = false;
- do
- {
- _armarureDatas.clear();
- _animationDatas.clear();
- _textureDatas.clear();
- bRet = true;
- }
- while (0);
- return bRet;
- }
- void ArmatureDataManager::removeArmatureFileInfo(const std::string& configFilePath)
- {
- if (RelativeData *data = getRelativeData(configFilePath))
- {
- for (std::string str : data->armatures)
- {
- removeArmatureData(str);
- }
- for (std::string str : data->animations)
- {
- removeAnimationData(str);
- }
- for (std::string str : data->textures)
- {
- removeTextureData(str);
- }
- for (std::string str : data->plistFiles)
- {
- SpriteFrameCacheHelper::getInstance()->removeSpriteFrameFromFile(str);
- }
- _relativeDatas.erase(configFilePath);
- DataReaderHelper::getInstance()->removeConfigFile(configFilePath);
- }
- }
- void ArmatureDataManager::addArmatureData(const std::string& id, ArmatureData *armatureData, const std::string& configFilePath)
- {
- if (RelativeData *data = getRelativeData(configFilePath))
- {
- data->armatures.push_back(id);
- }
- _armarureDatas.insert(id, armatureData);
- }
- ArmatureData *ArmatureDataManager::getArmatureData(const std::string& id)
- {
- return dynamic_cast<ArmatureData*>(_armarureDatas.at(id));
- }
- void ArmatureDataManager::removeArmatureData(const std::string& id)
- {
- _armarureDatas.erase(id);
- }
- void ArmatureDataManager::addAnimationData(const std::string& id, AnimationData *animationData, const std::string& configFilePath)
- {
- if (RelativeData *data = getRelativeData(configFilePath))
- {
- data->animations.push_back(id);
- }
- _animationDatas.insert(id, animationData);
- }
- AnimationData *ArmatureDataManager::getAnimationData(const std::string& id)
- {
- return dynamic_cast<AnimationData*>(_animationDatas.at(id));
- }
- void ArmatureDataManager::removeAnimationData(const std::string& id)
- {
- _animationDatas.erase(id);
- }
- void ArmatureDataManager::addTextureData(const std::string& id, TextureData *textureData, const std::string& configFilePath)
- {
- if (RelativeData *data = getRelativeData(configFilePath))
- {
- data->textures.push_back(id);
- }
- _textureDatas.insert(id, textureData);
- }
- TextureData *ArmatureDataManager::getTextureData(const std::string& id)
- {
- return dynamic_cast<TextureData*>(_textureDatas.at(id));
- }
- void ArmatureDataManager::removeTextureData(const std::string& id)
- {
- _textureDatas.erase(id);
- }
- void ArmatureDataManager::addArmatureFileInfo(const std::string& configFilePath)
- {
- addRelativeData(configFilePath);
- _autoLoadSpriteFile = true;
- DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
- }
- void ArmatureDataManager::addArmatureFileInfoAsync(const std::string& configFilePath, Ref *target, SEL_SCHEDULE selector)
- {
- addRelativeData(configFilePath);
- _autoLoadSpriteFile = true;
- DataReaderHelper::getInstance()->addDataFromFileAsync("", "", configFilePath, target, selector);
- }
- void ArmatureDataManager::addArmatureFileInfo(const std::string& imagePath, const std::string& plistPath, const std::string& configFilePath)
- {
- addRelativeData(configFilePath);
- _autoLoadSpriteFile = false;
- DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
- addSpriteFrameFromFile(plistPath, imagePath, configFilePath);
- }
- void ArmatureDataManager::addArmatureFileInfoAsync(const std::string& imagePath, const std::string& plistPath, const std::string& configFilePath, Ref *target, SEL_SCHEDULE selector)
- {
- addRelativeData(configFilePath);
- _autoLoadSpriteFile = false;
- DataReaderHelper::getInstance()->addDataFromFileAsync(imagePath, plistPath, configFilePath, target, selector);
- addSpriteFrameFromFile(plistPath, imagePath, configFilePath);
- }
- void ArmatureDataManager::addSpriteFrameFromFile(const std::string& plistPath, const std::string& imagePath, const std::string& configFilePath)
- {
- if (RelativeData *data = getRelativeData(configFilePath))
- {
- data->plistFiles.push_back(plistPath);
- }
- SpriteFrameCacheHelper::getInstance()->addSpriteFrameFromFile(plistPath, imagePath);
- }
- bool ArmatureDataManager::isAutoLoadSpriteFile()
- {
- return _autoLoadSpriteFile;
- }
- const cocos2d::Map<std::string, ArmatureData*>& ArmatureDataManager::getArmatureDatas() const
- {
- return _armarureDatas;
- }
- const cocos2d::Map<std::string, AnimationData*>& ArmatureDataManager::getAnimationDatas() const
- {
- return _animationDatas;
- }
- const cocos2d::Map<std::string, TextureData*>& ArmatureDataManager::getTextureDatas() const
- {
- return _textureDatas;
- }
- void ArmatureDataManager::addRelativeData(const std::string& configFilePath)
- {
- if (_relativeDatas.find(configFilePath) == _relativeDatas.end())
- {
- _relativeDatas[configFilePath] = RelativeData();
- }
- }
- RelativeData *ArmatureDataManager::getRelativeData(const std::string& configFilePath)
- {
- return &_relativeDatas[configFilePath];
- }
- }
|