axmol/cocos/editor-support/cocostudio/CCArmatureDataManager.cpp

267 lines
7.6 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
2013-06-06 12:02:54 +08:00
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"
2013-06-06 12:02:54 +08:00
#include "cocostudio/CCArmatureDataManager.h"
#include "cocostudio/CCTransformHelp.h"
#include "cocostudio/CCDataReaderHelper.h"
#include "cocostudio/CCSpriteFrameCacheHelper.h"
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
2013-06-06 12:02:54 +08:00
namespace cocostudio {
2013-06-06 12:02:54 +08:00
static ArmatureDataManager *s_sharedArmatureDataManager = nullptr;
2013-06-06 12:02:54 +08:00
ArmatureDataManager *ArmatureDataManager::getInstance()
2013-06-06 12:02:54 +08:00
{
if (s_sharedArmatureDataManager == nullptr)
2013-06-06 12:02:54 +08:00
{
s_sharedArmatureDataManager = new (std::nothrow) ArmatureDataManager();
2013-06-06 12:02:54 +08:00
if (!s_sharedArmatureDataManager || !s_sharedArmatureDataManager->init())
{
CC_SAFE_DELETE(s_sharedArmatureDataManager);
}
}
return s_sharedArmatureDataManager;
}
2013-12-27 20:54:05 +08:00
void ArmatureDataManager::destroyInstance()
2013-06-06 12:02:54 +08:00
{
SpriteFrameCacheHelper::purge();
DataReaderHelper::purge();
2013-09-13 18:07:37 +08:00
CC_SAFE_RELEASE_NULL(s_sharedArmatureDataManager);
2013-06-06 12:02:54 +08:00
}
ArmatureDataManager::ArmatureDataManager(void)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
_armarureDatas.clear();
_animationDatas.clear();
_textureDatas.clear();
2013-09-15 20:24:25 +08:00
_autoLoadSpriteFile = false;
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
ArmatureDataManager::~ArmatureDataManager(void)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
_animationDatas.clear();
_armarureDatas.clear();
_textureDatas.clear();
_relativeDatas.clear();
2013-06-06 12:02:54 +08:00
}
2013-09-13 18:07:37 +08:00
bool ArmatureDataManager::init()
2013-06-06 12:02:54 +08:00
{
bool bRet = false;
do
{
2013-12-17 19:32:16 +08:00
_armarureDatas.clear();
_animationDatas.clear();
_textureDatas.clear();
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
void ArmatureDataManager::removeArmatureFileInfo(const std::string& configFilePath)
{
if (RelativeData *data = getRelativeData(configFilePath))
{
2013-11-05 20:33:29 +08:00
for (std::string str : data->armatures)
{
2013-11-05 20:33:29 +08:00
removeArmatureData(str.c_str());
}
2013-11-05 20:33:29 +08:00
for (std::string str : data->animations)
{
2013-11-05 20:33:29 +08:00
removeAnimationData(str.c_str());
}
2013-11-05 20:33:29 +08:00
for (std::string str : data->textures)
{
2013-11-05 20:33:29 +08:00
removeTextureData(str.c_str());
}
2013-11-05 20:33:29 +08:00
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)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
if (RelativeData *data = getRelativeData(configFilePath))
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
data->armatures.push_back(id);
2013-06-06 12:02:54 +08:00
}
2013-12-17 19:32:16 +08:00
_armarureDatas.insert(id, armatureData);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
ArmatureData *ArmatureDataManager::getArmatureData(const std::string& id)
2013-06-06 12:02:54 +08:00
{
ArmatureData *armatureData = nullptr;
2013-12-17 19:32:16 +08:00
armatureData = (ArmatureData *)_armarureDatas.at(id);
2013-06-06 12:02:54 +08:00
return armatureData;
}
void ArmatureDataManager::removeArmatureData(const std::string& id)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
_armarureDatas.erase(id);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ArmatureDataManager::addAnimationData(const std::string& id, AnimationData *animationData, const std::string& configFilePath)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
if (RelativeData *data = getRelativeData(configFilePath))
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
data->animations.push_back(id);
2013-06-06 12:02:54 +08:00
}
2013-12-17 19:32:16 +08:00
_animationDatas.insert(id, animationData);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
AnimationData *ArmatureDataManager::getAnimationData(const std::string& id)
2013-06-06 12:02:54 +08:00
{
AnimationData *animationData = nullptr;
2013-12-17 19:32:16 +08:00
animationData = (AnimationData *)_animationDatas.at(id);
2013-06-06 12:02:54 +08:00
return animationData;
}
void ArmatureDataManager::removeAnimationData(const std::string& id)
2013-09-13 18:07:37 +08:00
{
2013-12-17 19:32:16 +08:00
_animationDatas.erase(id);
2013-09-13 18:07:37 +08:00
}
void ArmatureDataManager::addTextureData(const std::string& id, TextureData *textureData, const std::string& configFilePath)
2013-09-13 18:07:37 +08:00
{
2013-12-17 19:32:16 +08:00
if (RelativeData *data = getRelativeData(configFilePath))
2013-09-13 18:07:37 +08:00
{
2013-12-17 19:32:16 +08:00
data->textures.push_back(id);
2013-09-13 18:07:37 +08:00
}
2013-12-17 19:32:16 +08:00
_textureDatas.insert(id, textureData);
2013-09-13 18:07:37 +08:00
}
TextureData *ArmatureDataManager::getTextureData(const std::string& id)
2013-06-06 12:02:54 +08:00
{
TextureData *textureData = nullptr;
2013-12-17 19:32:16 +08:00
textureData = (TextureData *)_textureDatas.at(id);
2013-06-06 12:02:54 +08:00
return textureData;
}
void ArmatureDataManager::removeTextureData(const std::string& id)
2013-09-13 18:07:37 +08:00
{
2013-12-17 19:32:16 +08:00
_textureDatas.erase(id);
2013-09-13 18:07:37 +08:00
}
void ArmatureDataManager::addArmatureFileInfo(const std::string& configFilePath)
2013-09-13 18:07:37 +08:00
{
addRelativeData(configFilePath);
2013-09-15 20:24:25 +08:00
_autoLoadSpriteFile = true;
DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
void ArmatureDataManager::addArmatureFileInfoAsync(const std::string& configFilePath, Ref *target, SEL_SCHEDULE selector)
2013-06-06 12:02:54 +08:00
{
addRelativeData(configFilePath);
2013-09-15 20:24:25 +08:00
_autoLoadSpriteFile = true;
2013-10-30 09:41:40 +08:00
DataReaderHelper::getInstance()->addDataFromFileAsync("", "", configFilePath, target, selector);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ArmatureDataManager::addArmatureFileInfo(const std::string& imagePath, const std::string& plistPath, const std::string& configFilePath)
2013-06-06 12:02:54 +08:00
{
addRelativeData(configFilePath);
2013-09-15 20:24:25 +08:00
_autoLoadSpriteFile = false;
DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
addSpriteFrameFromFile(plistPath, imagePath, configFilePath);
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
void ArmatureDataManager::addArmatureFileInfoAsync(const std::string& imagePath, const std::string& plistPath, const std::string& configFilePath, Ref *target, SEL_SCHEDULE selector)
2013-09-13 18:07:37 +08:00
{
addRelativeData(configFilePath);
2013-09-15 20:24:25 +08:00
_autoLoadSpriteFile = false;
2013-10-30 09:41:40 +08:00
DataReaderHelper::getInstance()->addDataFromFileAsync(imagePath, plistPath, configFilePath, target, selector);
addSpriteFrameFromFile(plistPath, imagePath, configFilePath);
2013-06-06 12:02:54 +08:00
}
void ArmatureDataManager::addSpriteFrameFromFile(const std::string& plistPath, const std::string& imagePath, const std::string& configFilePath)
2013-06-06 12:02:54 +08:00
{
if (RelativeData *data = getRelativeData(configFilePath))
2013-06-06 12:02:54 +08:00
{
data->plistFiles.push_back(plistPath);
2013-06-06 12:02:54 +08:00
}
SpriteFrameCacheHelper::getInstance()->addSpriteFrameFromFile(plistPath, imagePath);
2013-09-13 18:07:37 +08:00
}
bool ArmatureDataManager::isAutoLoadSpriteFile()
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _autoLoadSpriteFile;
2013-09-13 18:07:37 +08:00
}
2013-12-17 19:32:16 +08:00
const cocos2d::Map<std::string, ArmatureData*>& ArmatureDataManager::getArmatureDatas() const
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _armarureDatas;
2013-09-13 18:07:37 +08:00
}
2013-12-17 19:32:16 +08:00
const cocos2d::Map<std::string, AnimationData*>& ArmatureDataManager::getAnimationDatas() const
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _animationDatas;
2013-09-13 18:07:37 +08:00
}
2013-12-17 19:32:16 +08:00
const cocos2d::Map<std::string, TextureData*>& ArmatureDataManager::getTextureDatas() const
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
return _textureDatas;
2013-06-06 12:02:54 +08:00
}
2015-11-13 13:38:48 +08:00
void ArmatureDataManager::addRelativeData(const std::string& configFilePath)
{
if (_relativeDatas.find(configFilePath) == _relativeDatas.end())
{
_relativeDatas[configFilePath] = RelativeData();
}
}
2015-11-13 13:38:48 +08:00
RelativeData *ArmatureDataManager::getRelativeData(const std::string& configFilePath)
{
return &_relativeDatas[configFilePath];
}
}