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