2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Jason Booth
|
|
|
|
Copyright (c) 2009 Robert J Payne
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-01-29 22:30:14 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-06-10 02:25:43 +08:00
|
|
|
https://axmol.dev/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/SpriteFrameCache.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/Sprite.h"
|
|
|
|
#include "2d/AutoPolygon.h"
|
|
|
|
#include "2d/PlistSpriteSheetLoader.h"
|
|
|
|
#include "platform/FileUtils.h"
|
|
|
|
#include "base/Macros.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "renderer/Texture2D.h"
|
|
|
|
#include "base/NinePatchImageParser.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
namespace ax
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
static SpriteFrameCache* _sharedSpriteFrameCache = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
SpriteFrameCache* SpriteFrameCache::getInstance()
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
if (!_sharedSpriteFrameCache)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedSpriteFrameCache = new SpriteFrameCache();
|
2019-11-23 20:27:39 +08:00
|
|
|
_sharedSpriteFrameCache->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedSpriteFrameCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::destroyInstance()
|
|
|
|
{
|
2024-01-29 22:30:14 +08:00
|
|
|
AX_SAFE_DELETE(_sharedSpriteFrameCache);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrameCache::init()
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
_spriteFrames.reserve(20);
|
|
|
|
clear();
|
|
|
|
|
2021-08-20 12:43:19 +08:00
|
|
|
registerSpriteSheetLoader(std::make_shared<PlistSpriteSheetLoader>());
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCache::~SpriteFrameCache() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName,
|
|
|
|
std::string_view textureFileName,
|
2021-12-25 10:04:45 +08:00
|
|
|
uint32_t spriteSheetFormat)
|
2021-08-17 07:04:51 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
|
2021-08-17 07:04:51 +08:00
|
|
|
if (loader)
|
|
|
|
{
|
|
|
|
loader->load(spriteSheetFileName, textureFileName, *this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName,
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture,
|
|
|
|
uint32_t spriteSheetFormat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (loader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
loader->load(spriteSheetFileName, texture, *this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName, uint32_t spriteSheetFormat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (loader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
loader->load(spriteSheetFileName, *this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFileContent(const Data& content,
|
|
|
|
Texture2D* texture,
|
|
|
|
uint32_t spriteSheetFormat)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (loader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
loader->load(content, texture, *this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(std::string_view plist) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
return isSpriteSheetInUse(plist) && isPlistFull(plist);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, std::string_view frameName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(frame, "frame should not be nil");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
const std::string name = "by#addSpriteFrame()";
|
2021-12-25 10:04:45 +08:00
|
|
|
auto&& itr = _spriteSheets.find(name);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (itr != _spriteSheets.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
insertFrame(itr->second, frameName, frame);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto spriteSheet = std::make_shared<SpriteSheet>();
|
2021-08-13 14:52:58 +08:00
|
|
|
spriteSheet->path = name;
|
|
|
|
insertFrame(spriteSheet, frameName, frame);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::removeSpriteFrames()
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::removeUnusedSpriteFrames()
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
auto removed = false;
|
2022-10-13 21:44:42 +08:00
|
|
|
std::vector<std::string_view> toRemoveFrames;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2022-07-11 19:34:31 +08:00
|
|
|
const auto& frames = getSpriteFrames();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : frames)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
auto* spriteFrame = iter.second;
|
2021-08-17 16:49:14 +08:00
|
|
|
if (spriteFrame->getReferenceCount() == 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
toRemoveFrames.emplace_back(iter.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
|
2024-07-05 07:51:36 +08:00
|
|
|
AXLOGD("SpriteFrameCache: removing unused frame: {}", iter.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
if (removed)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
eraseFrames(toRemoveFrames);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 21:21:53 +08:00
|
|
|
void SpriteFrameCache::removeUnusedSpriteSheets()
|
|
|
|
{
|
|
|
|
std::vector<std::string_view> willRemoveSpriteSheetFileNames;
|
|
|
|
for (auto&& it : _spriteSheets)
|
|
|
|
{
|
|
|
|
bool isUsed = false;
|
|
|
|
for (auto&& frame : it.second->frames)
|
|
|
|
{
|
|
|
|
auto spriteFrame = getSpriteFrameByName(frame);
|
|
|
|
if (spriteFrame && spriteFrame->getReferenceCount() > 1)
|
|
|
|
{
|
|
|
|
isUsed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isUsed)
|
|
|
|
willRemoveSpriteSheetFileNames.push_back(it.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& spriteSheetFileName : willRemoveSpriteSheetFileNames)
|
|
|
|
{
|
2024-07-05 07:51:36 +08:00
|
|
|
AXLOGD("SpriteFrameCache: removing unused sprite sheet file : {}", spriteSheetFileName);
|
2024-04-23 21:21:53 +08:00
|
|
|
removeSpriteSheet(spriteSheetFileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFrameByName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// explicit nil handling
|
|
|
|
if (name.empty())
|
|
|
|
return;
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
eraseFrame(name);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromFile(std::string_view atlasPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// const auto fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
|
|
|
|
// auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
|
|
|
// if (dict.empty())
|
2021-08-13 14:52:58 +08:00
|
|
|
//{
|
2024-07-05 07:51:36 +08:00
|
|
|
// AXLOGD("axmol:SpriteFrameCache:removeSpriteFramesFromFile: create dict by {} fail.",plist);
|
2021-12-25 10:04:45 +08:00
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// removeSpriteFramesFromDictionary(dict);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// remove it from the cache
|
2021-08-13 14:52:58 +08:00
|
|
|
removeSpriteSheet(atlasPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromFileContent(std::string_view plist_content)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto dict =
|
|
|
|
FileUtils::getInstance()->getValueMapFromData(plist_content.data(), static_cast<int>(plist_content.size()));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (dict.empty())
|
|
|
|
{
|
2024-07-05 07:51:36 +08:00
|
|
|
AXLOGD("SpriteFrameCache:removeSpriteFramesFromFileContent: create dict by fail.");
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeSpriteFramesFromDictionary(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
if (dictionary["frames"].getType() != ax::Value::Type::MAP)
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto& framesDict = dictionary["frames"].asValueMap();
|
2022-10-13 21:44:42 +08:00
|
|
|
std::vector<std::string_view> keysToRemove;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
for (const auto& iter : framesDict)
|
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
if (findFrame(iter.first))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
keysToRemove.emplace_back(iter.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
eraseFrames(keysToRemove);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
|
|
|
|
{
|
2022-10-13 21:44:42 +08:00
|
|
|
std::vector<std::string_view> keysToRemove;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : getSpriteFrames())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-08-27 23:28:51 +08:00
|
|
|
auto* frame = findFrame(iter.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (frame && (frame->getTexture() == texture))
|
|
|
|
{
|
2024-08-27 23:28:51 +08:00
|
|
|
keysToRemove.emplace_back(iter.first);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
eraseFrames(keysToRemove);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
auto* frame = findFrame(name);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!frame)
|
|
|
|
{
|
2024-07-05 07:51:36 +08:00
|
|
|
AXLOGD("axmol: SpriteFrameCache: Frame '{}' isn't found", name);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::reloadTexture(std::string_view spriteSheetFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!spriteSheetFileName.empty(), "plist filename should not be nullptr");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto spriteSheetItr = _spriteSheets.find(spriteSheetFileName);
|
|
|
|
if (spriteSheetItr == _spriteSheets.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return false; // Sprite sheet wasn't loaded, so don't reload it
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto format = spriteSheetItr->second->format;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
if (isSpriteSheetInUse(spriteSheetFileName))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
removeSpriteSheet(
|
|
|
|
spriteSheetFileName); // we've removed the associated entry, so spriteSheetItr is no longer valid
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// If one plist hasn't be loaded, we don't load it here.
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
auto* loader = getSpriteSheetLoader(format);
|
|
|
|
if (loader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
loader->reload(spriteSheetFileName, *this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrameCache::insertFrame(const std::shared_ptr<SpriteSheet>& spriteSheet,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view frameName,
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* spriteFrame)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
spriteSheet->frames.emplace(frameName);
|
2021-12-25 10:04:45 +08:00
|
|
|
_spriteFrames.insert(frameName, spriteFrame); // add SpriteFrame
|
2021-12-31 12:12:40 +08:00
|
|
|
_spriteSheets[spriteSheet->path] = spriteSheet;
|
|
|
|
hlookup::set_item(_spriteFrameToSpriteSheetMap, frameName,
|
|
|
|
spriteSheet); // _spriteFrameToSpriteSheetMap[frameName] = spriteSheet; // insert
|
|
|
|
// index frameName->plist
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::eraseFrame(std::string_view frameName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-01-03 23:02:17 +08:00
|
|
|
// drop SpriteFrame
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto itFrame = _spriteFrameToSpriteSheetMap.find(frameName);
|
2024-08-27 23:28:51 +08:00
|
|
|
bool found = itFrame != _spriteFrameToSpriteSheetMap.end();
|
|
|
|
if (found)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
auto& spriteSheet = itFrame->second;
|
|
|
|
spriteSheet->full = false;
|
|
|
|
spriteSheet->frames.erase(frameName);
|
2020-09-17 13:45:38 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
if (spriteSheet->frames.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
_spriteSheets.erase(spriteSheet->path);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_spriteFrameToSpriteSheetMap.erase(itFrame); // update index frame->plist
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// erase all sprite sheets if all frames are erased
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (_spriteFrameToSpriteSheetMap.empty())
|
2021-08-13 14:52:58 +08:00
|
|
|
//{
|
|
|
|
// _spriteSheets.clear();
|
|
|
|
//}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2023-01-03 23:02:17 +08:00
|
|
|
_spriteFrames.erase(frameName);
|
2024-08-27 23:28:51 +08:00
|
|
|
return found;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-10-13 21:44:42 +08:00
|
|
|
bool SpriteFrameCache::eraseFrames(const std::vector<std::string_view>& frames)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto ret = false;
|
2021-08-13 14:52:58 +08:00
|
|
|
for (const auto& frame : frames)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret |= eraseFrame(frame);
|
|
|
|
}
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::removeSpriteSheet(std::string_view spriteSheetFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
auto it = _spriteSheets.find(spriteSheetFileName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _spriteSheets.end())
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
auto& frames = it->second->frames;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& f : frames)
|
|
|
|
{
|
|
|
|
// !!do not!! call `_spriteFrames.erase(f);` to erase SpriteFrame
|
|
|
|
// only erase index here
|
2021-08-13 14:52:58 +08:00
|
|
|
_spriteFrames.erase(f);
|
2021-12-25 10:04:45 +08:00
|
|
|
_spriteFrameToSpriteSheetMap.erase(f); // erase plist frame frameName->plist
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_spriteSheets.erase(spriteSheetFileName); // update index plist->[frameNames]
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
void SpriteFrameCache::clear()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
_spriteSheets.clear();
|
|
|
|
_spriteFrameToSpriteSheetMap.clear();
|
2019-11-23 20:27:39 +08:00
|
|
|
_spriteFrames.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::hasFrame(std::string_view frame) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
return _spriteFrameToSpriteSheetMap.find(frame) != _spriteFrameToSpriteSheetMap.end();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteFrameCache::isSpriteSheetInUse(std::string_view spriteSheetFileName) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto spriteSheetItr = _spriteSheets.find(spriteSheetFileName);
|
|
|
|
return spriteSheetItr != _spriteSheets.end() && !spriteSheetItr->second->frames.empty();
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
SpriteFrame* SpriteFrameCache::findFrame(std::string_view frame)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _spriteFrames.at(frame);
|
|
|
|
}
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2024-04-23 21:21:53 +08:00
|
|
|
std::string_view SpriteFrameCache::getSpriteFrameName(SpriteFrame* frame)
|
|
|
|
{
|
|
|
|
for (auto& it : _spriteFrames)
|
|
|
|
{
|
|
|
|
if (it.second == frame)
|
|
|
|
{
|
|
|
|
return it.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:52:58 +08:00
|
|
|
void SpriteFrameCache::addSpriteFrameCapInset(SpriteFrame* spriteFrame, const Rect& capInsets, Texture2D* texture)
|
|
|
|
{
|
|
|
|
texture->addSpriteFrameCapInset(spriteFrame, capInsets);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
StringMap<SpriteFrame*>& SpriteFrameCache::getSpriteFrames()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _spriteFrames;
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:43:19 +08:00
|
|
|
void SpriteFrameCache::registerSpriteSheetLoader(std::shared_ptr<ISpriteSheetLoader> loader)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto format = loader->getFormat();
|
|
|
|
|
|
|
|
if (_spriteSheetLoaders.find(format) != _spriteSheetLoaders.end())
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:04:45 +08:00
|
|
|
_spriteSheetLoaders.emplace(format, std::move(loader));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-17 12:04:45 +08:00
|
|
|
void SpriteFrameCache::deregisterSpriteSheetLoader(uint32_t spriteSheetFormat)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto&& itr = _spriteSheetLoaders.find(spriteSheetFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (itr != _spriteSheetLoaders.end())
|
|
|
|
{
|
|
|
|
_spriteSheetLoaders.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:04:45 +08:00
|
|
|
ISpriteSheetLoader* SpriteFrameCache::getSpriteSheetLoader(uint32_t spriteSheetFormat)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-08-17 12:04:45 +08:00
|
|
|
auto&& itr = _spriteSheetLoaders.find(spriteSheetFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (itr != _spriteSheetLoaders.end())
|
|
|
|
{
|
|
|
|
return itr->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
}
|