2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2013-10-16 16:48:39 +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
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
namespace cocostudio {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-11-05 15:32:13 +08:00
|
|
|
static ArmatureDataManager *s_sharedArmatureDataManager = nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-16 15:16:05 +08:00
|
|
|
ArmatureDataManager *ArmatureDataManager::getInstance()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
if (s_sharedArmatureDataManager == nullptr)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
s_sharedArmatureDataManager = new ArmatureDataManager();
|
2013-06-06 12:02:54 +08:00
|
|
|
if (!s_sharedArmatureDataManager || !s_sharedArmatureDataManager->init())
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(s_sharedArmatureDataManager);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s_sharedArmatureDataManager;
|
|
|
|
}
|
|
|
|
|
2013-09-17 23:03:33 +08:00
|
|
|
void ArmatureDataManager::destoryInstance()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
ArmatureDataManager::ArmatureDataManager(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
_armarureDatas = nullptr;
|
|
|
|
_animationDatas = nullptr;
|
|
|
|
_textureDatas = nullptr;
|
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
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
ArmatureDataManager::~ArmatureDataManager(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
if( _animationDatas )
|
|
|
|
{
|
|
|
|
_animationDatas->removeAllObjects();
|
|
|
|
}
|
|
|
|
if( _armarureDatas )
|
|
|
|
{
|
|
|
|
_armarureDatas->removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( _textureDatas )
|
|
|
|
{
|
|
|
|
_textureDatas->removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
_relativeDatas.clear();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
CC_SAFE_DELETE(_animationDatas);
|
|
|
|
CC_SAFE_DELETE(_armarureDatas);
|
|
|
|
CC_SAFE_DELETE(_textureDatas);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
bool ArmatureDataManager::init()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
_armarureDatas = Dictionary::create();
|
2013-09-16 12:02:57 +08:00
|
|
|
CCASSERT(_armarureDatas, "create ArmatureDataManager::_armarureDatas fail!");
|
2013-09-15 20:24:25 +08:00
|
|
|
_armarureDatas->retain();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_animationDatas = Dictionary::create();
|
2013-09-16 12:02:57 +08:00
|
|
|
CCASSERT(_animationDatas, "create ArmatureDataManager::_animationDatas fail!");
|
2013-09-15 20:24:25 +08:00
|
|
|
_animationDatas->retain();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_textureDatas = Dictionary::create();
|
2013-09-16 12:02:57 +08:00
|
|
|
CCASSERT(_textureDatas, "create ArmatureDataManager::_textureDatas fail!");
|
2013-09-15 20:24:25 +08:00
|
|
|
_textureDatas->retain();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
void ArmatureDataManager::removeArmatureFileInfo(const char *configFilePath)
|
|
|
|
{
|
|
|
|
if (RelativeData *data = getRelativeData(configFilePath))
|
|
|
|
{
|
2013-11-05 20:33:29 +08:00
|
|
|
for (std::string str : data->armatures)
|
2013-10-31 17:28:14 +08:00
|
|
|
{
|
2013-11-05 20:33:29 +08:00
|
|
|
removeArmatureData(str.c_str());
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:33:29 +08:00
|
|
|
for (std::string str : data->animations)
|
2013-10-31 17:28:14 +08:00
|
|
|
{
|
2013-11-05 20:33:29 +08:00
|
|
|
removeAnimationData(str.c_str());
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:33:29 +08:00
|
|
|
for (std::string str : data->textures)
|
2013-10-31 17:28:14 +08:00
|
|
|
{
|
2013-11-05 20:33:29 +08:00
|
|
|
removeTextureData(str.c_str());
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:33:29 +08:00
|
|
|
for (std::string str : data->plistFiles)
|
2013-10-31 17:28:14 +08:00
|
|
|
{
|
2013-11-05 20:33:29 +08:00
|
|
|
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(str.c_str());
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_relativeDatas.erase(configFilePath);
|
2013-11-06 15:25:44 +08:00
|
|
|
DataReaderHelper::getInstance()->removeConfigFile(configFilePath);
|
2013-10-31 17:28:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ArmatureDataManager::addArmatureData(const char *id, ArmatureData *armatureData, const char *configFilePath)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if(_armarureDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
if (RelativeData *data = getRelativeData(configFilePath))
|
|
|
|
{
|
|
|
|
data->armatures.push_back(id);
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_armarureDatas->setObject(armatureData, id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
ArmatureData *ArmatureDataManager::getArmatureData(const char *id)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
ArmatureData *armatureData = nullptr;
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_armarureDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
armatureData = (ArmatureData *)_armarureDatas->objectForKey(id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
return armatureData;
|
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::removeArmatureData(const char *id)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_armarureDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
_armarureDatas->removeObjectForKey(id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
void ArmatureDataManager::addAnimationData(const char *id, AnimationData *animationData, const char *configFilePath)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if(_animationDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
if (RelativeData *data = getRelativeData(configFilePath))
|
|
|
|
{
|
|
|
|
data->animations.push_back(id);
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_animationDatas->setObject(animationData, id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
AnimationData *ArmatureDataManager::getAnimationData(const char *id)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
AnimationData *animationData = nullptr;
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_animationDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
animationData = (AnimationData *)_animationDatas->objectForKey(id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
return animationData;
|
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::removeAnimationData(const char *id)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_animationDatas)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
_animationDatas->removeObjectForKey(id);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
void ArmatureDataManager::addTextureData(const char *id, TextureData *textureData, const char *configFilePath)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if(_textureDatas)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
if (RelativeData *data = getRelativeData(configFilePath))
|
|
|
|
{
|
|
|
|
data->textures.push_back(id);
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_textureDatas->setObject(textureData, id);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
TextureData *ArmatureDataManager::getTextureData(const char *id)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
TextureData *textureData = nullptr;
|
2013-09-15 20:24:25 +08:00
|
|
|
if (_textureDatas)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
textureData = (TextureData *)_textureDatas->objectForKey(id);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
return textureData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::removeTextureData(const char *id)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
if(_textureDatas)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
_textureDatas->removeObjectForKey(id);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::addArmatureFileInfo(const char *configFilePath)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
addRelativeData(configFilePath);
|
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_autoLoadSpriteFile = true;
|
2013-09-16 15:16:05 +08:00
|
|
|
DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::addArmatureFileInfoAsync(const char *configFilePath, Object *target, SEL_SCHEDULE selector)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +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
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::addArmatureFileInfo(const char *imagePath, const char *plistPath, const char *configFilePath)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
addRelativeData(configFilePath);
|
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
_autoLoadSpriteFile = false;
|
2013-09-16 15:16:05 +08:00
|
|
|
DataReaderHelper::getInstance()->addDataFromFile(configFilePath);
|
2013-09-13 18:07:37 +08:00
|
|
|
addSpriteFrameFromFile(plistPath, imagePath);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void ArmatureDataManager::addArmatureFileInfoAsync(const char *imagePath, const char *plistPath, const char *configFilePath, Object *target, SEL_SCHEDULE selector)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +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);
|
2013-06-07 10:52:32 +08:00
|
|
|
addSpriteFrameFromFile(plistPath, imagePath);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
void ArmatureDataManager::addSpriteFrameFromFile(const char *plistPath, const char *imagePath, const char *configFilePath)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
if (RelativeData *data = getRelativeData(configFilePath))
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-31 17:28:14 +08:00
|
|
|
data->plistFiles.push_back(plistPath);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-10-31 17:28:14 +08:00
|
|
|
SpriteFrameCacheHelper::getInstance()->addSpriteFrameFromFile(plistPath, imagePath);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
|
2013-09-15 19:08:45 +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-09-15 19:08:45 +08:00
|
|
|
Dictionary *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-09-15 19:08:45 +08:00
|
|
|
Dictionary *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-09-15 19:08:45 +08:00
|
|
|
Dictionary *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
|
|
|
}
|
|
|
|
|
2013-10-31 17:28:14 +08:00
|
|
|
void CCArmatureDataManager::addRelativeData(const char *configFilePath)
|
|
|
|
{
|
|
|
|
if (_relativeDatas.find(configFilePath) == _relativeDatas.end())
|
|
|
|
{
|
|
|
|
_relativeDatas[configFilePath] = RelativeData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RelativeData *CCArmatureDataManager::getRelativeData(const char* configFilePath)
|
|
|
|
{
|
|
|
|
return &_relativeDatas[configFilePath];
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|