2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/PlistSpriteSheetLoader.h"
|
|
|
|
|
|
|
|
#include "platform/FileUtils.h"
|
|
|
|
#include "2d/AutoPolygon.h"
|
|
|
|
#include "2d/SpriteFrameCache.h"
|
|
|
|
#include "base/NinePatchImageParser.h"
|
|
|
|
#include "base/NS.h"
|
|
|
|
#include "base/Macros.h"
|
|
|
|
#include "base/UTF8.h"
|
|
|
|
#include "base/Utils.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "renderer/Texture2D.h"
|
|
|
|
#include "renderer/TextureCache.h"
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void PlistSpriteSheetLoader::load(std::string_view filePath, SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!filePath.empty(), "plist filename should not be nullptr");
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
const auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
|
|
|
if (fullPath.empty())
|
|
|
|
{
|
|
|
|
// return if plist file doesn't exist
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: SpriteFrameCache: can not find %s", filePath.data());
|
2021-08-13 14:52:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
|
|
|
|
|
|
|
std::string texturePath;
|
|
|
|
|
|
|
|
if (dict.find("metadata") != dict.end())
|
|
|
|
{
|
|
|
|
auto& metadataDict = dict["metadata"].asValueMap();
|
|
|
|
// try to read texture file name from meta data
|
|
|
|
texturePath = metadataDict["textureFileName"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!texturePath.empty())
|
|
|
|
{
|
|
|
|
// build texture path relative to plist file
|
|
|
|
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath, filePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build texture path by replacing file extension
|
|
|
|
texturePath = filePath;
|
|
|
|
|
|
|
|
// remove .xxx
|
|
|
|
const auto startPos = texturePath.find_last_of('.');
|
|
|
|
if (startPos != string::npos)
|
|
|
|
{
|
|
|
|
texturePath = texturePath.erase(startPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// append .png
|
|
|
|
texturePath = texturePath.append(".png");
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
addSpriteFramesWithDictionary(dict, texturePath, filePath, cache);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void PlistSpriteSheetLoader::load(std::string_view filePath, Texture2D* texture, SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
const auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
addSpriteFramesWithDictionary(dict, texture, filePath, cache);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void PlistSpriteSheetLoader::load(std::string_view filePath, std::string_view textureFileName, SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!textureFileName.empty(), "texture name should not be null");
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2021-08-13 14:52:58 +08:00
|
|
|
addSpriteFramesWithDictionary(dict, textureFileName, filePath, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlistSpriteSheetLoader::load(const Data& content, Texture2D* texture, SpriteFrameCache& cache)
|
|
|
|
{
|
|
|
|
if (content.isNull())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* text = (char*)content.getBytes();
|
2021-08-13 14:52:58 +08:00
|
|
|
const auto length = content.getSize();
|
|
|
|
|
|
|
|
auto dict = FileUtils::getInstance()->getValueMapFromData(text, static_cast<int>(length));
|
|
|
|
addSpriteFramesWithDictionary(dict, texture, "by#addSpriteFramesWithFileContent()", cache);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void PlistSpriteSheetLoader::reload(std::string_view filePath, SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
const auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
std::string texturePath;
|
|
|
|
|
|
|
|
if (dict.find("metadata") != dict.end())
|
|
|
|
{
|
|
|
|
auto& metadataDict = dict["metadata"].asValueMap();
|
|
|
|
// try to read texture file name from meta data
|
|
|
|
texturePath = metadataDict["textureFileName"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!texturePath.empty())
|
|
|
|
{
|
|
|
|
// build texture path relative to plist file
|
|
|
|
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath, filePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build texture path by replacing file extension
|
|
|
|
texturePath = filePath;
|
|
|
|
|
|
|
|
// remove .xxx
|
|
|
|
const auto startPos = texturePath.find_last_of('.');
|
|
|
|
if (startPos != string::npos)
|
|
|
|
{
|
|
|
|
texturePath = texturePath.erase(startPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// append .png
|
|
|
|
texturePath = texturePath.append(".png");
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* texture = nullptr;
|
|
|
|
if (Director::getInstance()->getTextureCache()->reloadTexture(texturePath))
|
|
|
|
{
|
|
|
|
texture = Director::getInstance()->getTextureCache()->getTextureForKey(texturePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
reloadSpriteFramesWithDictionary(dict, texture, filePath, cache);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: SpriteFrameCache: Couldn't load texture");
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PlistSpriteSheetLoader::addSpriteFramesWithDictionary(ValueMap& dictionary,
|
|
|
|
Texture2D* texture,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view plist,
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Supported Zwoptex Formats:
|
|
|
|
|
|
|
|
ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version
|
|
|
|
ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b
|
|
|
|
ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1
|
|
|
|
ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+
|
|
|
|
|
|
|
|
Version 3 with TexturePacker 4.0 polygon mesh packing
|
|
|
|
*/
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
if (dictionary["frames"].getType() != ax::Value::Type::MAP)
|
2021-08-13 14:52:58 +08:00
|
|
|
return;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto spriteSheet = std::make_shared<SpriteSheet>();
|
2021-08-17 12:04:45 +08:00
|
|
|
spriteSheet->format = getFormat();
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteSheet->path = plist;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
auto& framesDict = dictionary["frames"].asValueMap();
|
2021-12-25 10:04:45 +08:00
|
|
|
int format = 0;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 textureSize;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// get the format
|
2021-12-31 12:12:40 +08:00
|
|
|
auto metaItr = dictionary.find("metadata"sv);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (metaItr != dictionary.end())
|
|
|
|
{
|
|
|
|
auto& metadataDict = metaItr->second.asValueMap();
|
2021-12-31 12:12:40 +08:00
|
|
|
format = optValue(metadataDict, "format"sv).asInt();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (metadataDict.find("size"sv) != metadataDict.end())
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
textureSize = SizeFromString(optValue(metadataDict, "size"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the format
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(format >= 0 && format <= 3,
|
2021-12-25 10:04:45 +08:00
|
|
|
"format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
std::vector<std::string> frameAliases;
|
|
|
|
auto textureFileName = Director::getInstance()->getTextureCache()->getTextureFilePath(texture);
|
2021-12-25 10:04:45 +08:00
|
|
|
Image* image = nullptr;
|
2021-08-13 14:52:58 +08:00
|
|
|
NinePatchImageParser parser;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : framesDict)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& frameDict = iter.second.asValueMap();
|
2021-08-13 14:52:58 +08:00
|
|
|
auto spriteFrameName = iter.first;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* spriteFrame = cache.findFrame(spriteFrameName);
|
2021-08-13 14:52:58 +08:00
|
|
|
if (spriteFrame)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format == 0)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto x = optValue(frameDict, "x"sv).asFloat();
|
|
|
|
auto y = optValue(frameDict, "y"sv).asFloat();
|
|
|
|
auto w = optValue(frameDict, "width"sv).asFloat();
|
|
|
|
auto h = optValue(frameDict, "height"sv).asFloat();
|
|
|
|
auto ox = optValue(frameDict, "offsetX"sv).asFloat();
|
|
|
|
auto oy = optValue(frameDict, "offsetY"sv).asFloat();
|
|
|
|
auto ow = optValue(frameDict, "originalWidth"sv).asInt();
|
|
|
|
auto oh = optValue(frameDict, "originalHeight"sv).asInt();
|
2021-08-13 14:52:58 +08:00
|
|
|
// check ow/oh
|
|
|
|
if (!ow || !oh)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN(
|
2022-10-12 00:15:09 +08:00
|
|
|
"axmol: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as "
|
2021-12-25 10:04:45 +08:00
|
|
|
"expected. Regenerate the .plist");
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = std::abs(ow);
|
|
|
|
oh = std::abs(oh);
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture, Rect(x, y, w, h), false, Vec2(ox, oy),
|
|
|
|
Vec2((float)ow, (float)oh));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else if (format == 1 || format == 2)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto frame = RectFromString(optValue(frameDict, "frame"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
auto rotated = false;
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
if (format == 2)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
rotated = optValue(frameDict, "rotated"sv).asBool();
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto offset = PointFromString(optValue(frameDict, "offset"sv).asString());
|
|
|
|
auto sourceSize = SizeFromString(optValue(frameDict, "sourceSize"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture, frame, rotated, offset, sourceSize);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else if (format == 3)
|
|
|
|
{
|
|
|
|
// get values
|
2021-12-31 12:12:40 +08:00
|
|
|
auto spriteSize = SizeFromString(optValue(frameDict, "spriteSize"sv).asString());
|
|
|
|
auto spriteOffset = PointFromString(optValue(frameDict, "spriteOffset"sv).asString());
|
|
|
|
auto spriteSourceSize = SizeFromString(optValue(frameDict, "spriteSourceSize"sv).asString());
|
|
|
|
auto textureRect = RectFromString(optValue(frameDict, "textureRect"sv).asString());
|
|
|
|
auto textureRotated = optValue(frameDict, "textureRotated"sv).asBool();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// get aliases
|
2021-12-31 12:12:40 +08:00
|
|
|
auto& aliases = optValue(frameDict, "aliases"sv).asValueVector();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
for (const auto& value : aliases)
|
|
|
|
{
|
|
|
|
auto oneAlias = value.asString();
|
|
|
|
if (std::find(frameAliases.begin(), frameAliases.end(), oneAlias) == frameAliases.end())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
frameAliases.emplace_back(std::move(oneAlias));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOGWARN("axmol: WARNING: an alias with name %s already exists", oneAlias.c_str());
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(
|
|
|
|
texture, Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
|
|
|
|
textureRotated, spriteOffset, spriteSourceSize);
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
if (frameDict.find("vertices") != frameDict.end())
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
using ax::utils::parseIntegerList;
|
2021-12-31 12:12:40 +08:00
|
|
|
auto vertices = parseIntegerList(optValue(frameDict, "vertices"sv).asString());
|
|
|
|
auto verticesUV = parseIntegerList(optValue(frameDict, "verticesUV"sv).asString());
|
|
|
|
auto indices = parseIntegerList(optValue(frameDict, "triangles"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
PolygonInfo info;
|
|
|
|
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
|
|
|
|
spriteFrame->setPolygonInfo(info);
|
|
|
|
}
|
|
|
|
if (frameDict.find("anchor") != frameDict.end())
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
spriteFrame->setAnchorPoint(PointFromString(optValue(frameDict, "anchor"sv).asString()));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flag = NinePatchImageParser::isNinePatchImage(spriteFrameName);
|
|
|
|
if (flag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (image == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
image = new Image();
|
2021-08-13 14:52:58 +08:00
|
|
|
image->initWithImageFile(textureFileName);
|
|
|
|
}
|
|
|
|
parser.setSpriteFrameInfo(image, spriteFrame->getRectInPixels(), spriteFrame->isRotated());
|
2021-12-25 10:04:45 +08:00
|
|
|
// texture->addSpriteFrameCapInset(spriteFrame, parser.parseCapInset());
|
2021-08-13 14:52:58 +08:00
|
|
|
cache.addSpriteFrameCapInset(spriteFrame, parser.parseCapInset(), texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add sprite frame
|
|
|
|
cache.insertFrame(spriteSheet, spriteFrameName, spriteFrame);
|
|
|
|
for (auto&& frameAlias : frameAliases)
|
|
|
|
{
|
|
|
|
cache.insertFrame(spriteSheet, frameAlias, spriteFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spriteSheet->full = true;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(image);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PlistSpriteSheetLoader::addSpriteFramesWithDictionary(ValueMap& dict,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view texturePath,
|
|
|
|
std::string_view plist,
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
std::string pixelFormatName;
|
|
|
|
if (dict.find("metadata") != dict.end())
|
|
|
|
{
|
|
|
|
auto& metadataDict = dict.at("metadata").asValueMap();
|
|
|
|
if (metadataDict.find("pixelFormat") != metadataDict.end())
|
|
|
|
{
|
|
|
|
pixelFormatName = metadataDict.at("pixelFormat").asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = nullptr;
|
2021-08-13 14:52:58 +08:00
|
|
|
static std::unordered_map<std::string, backend::PixelFormat> pixelFormats = {
|
|
|
|
{"RGBA8888", backend::PixelFormat::RGBA8},
|
|
|
|
{"RGBA4444", backend::PixelFormat::RGBA4},
|
|
|
|
{"RGB5A1", backend::PixelFormat::RGB5A1},
|
|
|
|
{"RGBA5551", backend::PixelFormat::RGB5A1},
|
|
|
|
{"RGB565", backend::PixelFormat::RGB565},
|
|
|
|
{"A8", backend::PixelFormat::A8},
|
|
|
|
{"ALPHA", backend::PixelFormat::A8},
|
|
|
|
{"I8", backend::PixelFormat::L8},
|
|
|
|
{"AI88", backend::PixelFormat::LA8},
|
|
|
|
{"ALPHA_INTENSITY", backend::PixelFormat::LA8},
|
|
|
|
//{"BGRA8888", backend::PixelFormat::BGRA8888}, no Image conversion RGBA -> BGRA
|
2021-12-25 10:04:45 +08:00
|
|
|
{"RGB888", backend::PixelFormat::RGB8}};
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
const auto pixelFormatIt = pixelFormats.find(pixelFormatName);
|
|
|
|
if (pixelFormatIt != pixelFormats.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const backend::PixelFormat pixelFormat = (*pixelFormatIt).second;
|
2022-05-13 21:06:17 +08:00
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(texturePath, pixelFormat);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(texturePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
addSpriteFramesWithDictionary(dict, texture, plist, cache);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: SpriteFrameCache: Couldn't load texture");
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PlistSpriteSheetLoader::reloadSpriteFramesWithDictionary(ValueMap& dict,
|
|
|
|
Texture2D* texture,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view plist,
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCache& cache)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
|
|
|
auto& framesDict = dict["frames"].asValueMap();
|
2021-12-25 10:04:45 +08:00
|
|
|
int format = 0;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// get the format
|
|
|
|
if (dict.find("metadata") != dict.end())
|
|
|
|
{
|
|
|
|
auto& metadataDict = dict["metadata"].asValueMap();
|
2021-12-25 10:04:45 +08:00
|
|
|
format = metadataDict["format"].asInt();
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the format
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(format >= 0 && format <= 3,
|
2021-12-25 10:04:45 +08:00
|
|
|
"format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto spriteSheet = std::make_shared<SpriteSheet>();
|
2021-08-17 12:04:45 +08:00
|
|
|
spriteSheet->format = getFormat();
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteSheet->path = plist;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : framesDict)
|
2021-08-13 14:52:58 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
const ValueMap& frameDict = iter.second.asValueMap();
|
|
|
|
std::string_view spriteFrameName = iter.first;
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
cache.eraseFrame(spriteFrameName);
|
|
|
|
|
|
|
|
SpriteFrame* spriteFrame = nullptr;
|
|
|
|
std::vector<std::string> frameAliases;
|
|
|
|
|
|
|
|
if (format == 0)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
const auto x = optValue(frameDict, "x"sv).asFloat();
|
|
|
|
const auto y = optValue(frameDict, "y"sv).asFloat();
|
|
|
|
const auto w = optValue(frameDict, "width"sv).asFloat();
|
|
|
|
const auto h = optValue(frameDict, "height"sv).asFloat();
|
|
|
|
const auto ox = optValue(frameDict, "offsetX"sv).asFloat();
|
|
|
|
const auto oy = optValue(frameDict, "offsetY"sv).asFloat();
|
|
|
|
auto ow = optValue(frameDict, "originalWidth"sv).asInt();
|
|
|
|
auto oh = optValue(frameDict, "originalHeight"sv).asInt();
|
2021-08-13 14:52:58 +08:00
|
|
|
// check ow/oh
|
|
|
|
if (!ow || !oh)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN(
|
2023-08-31 19:55:52 +08:00
|
|
|
"axmol:WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as "
|
2021-12-25 10:04:45 +08:00
|
|
|
"expected. Regenerate the .plist");
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = std::abs(ow);
|
|
|
|
oh = std::abs(oh);
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture, Rect(x, y, w, h), false, Vec2(ox, oy),
|
|
|
|
Vec2((float)ow, (float)oh));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else if (format == 1 || format == 2)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto frame = RectFromString(optValue(frameDict, "frame"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
auto rotated = false;
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
if (format == 2)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
rotated = optValue(frameDict, "rotated"sv).asBool();
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto offset = PointFromString(optValue(frameDict, "offset"sv).asString());
|
|
|
|
auto sourceSize = SizeFromString(optValue(frameDict, "sourceSize"sv).asString());
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture, frame, rotated, offset, sourceSize);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else if (format == 3)
|
|
|
|
{
|
|
|
|
// get values
|
2021-12-31 12:12:40 +08:00
|
|
|
const auto spriteSize = SizeFromString(optValue(frameDict, "spriteSize"sv).asString());
|
|
|
|
auto spriteOffset = PointFromString(optValue(frameDict, "spriteOffset"sv).asString());
|
|
|
|
auto spriteSourceSize = SizeFromString(optValue(frameDict, "spriteSourceSize"sv).asString());
|
|
|
|
const auto textureRect = RectFromString(optValue(frameDict, "textureRect"sv).asString());
|
|
|
|
const auto textureRotated = optValue(frameDict, "textureRotated"sv).asBool();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
// get aliases
|
2021-12-31 12:12:40 +08:00
|
|
|
const ValueVector& aliases = optValue(frameDict, "aliases"sv).asValueVector();
|
2021-08-13 14:52:58 +08:00
|
|
|
|
|
|
|
for (const auto& value : aliases)
|
|
|
|
{
|
|
|
|
auto oneAlias = value.asString();
|
|
|
|
if (std::find(frameAliases.begin(), frameAliases.end(), oneAlias) == frameAliases.end())
|
|
|
|
{
|
2022-08-08 13:18:33 +08:00
|
|
|
frameAliases.emplace_back(std::move(oneAlias));
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOGWARN("axmol: WARNING: an alias with name %s already exists", oneAlias.c_str());
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create frame
|
2021-12-25 10:04:45 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(
|
|
|
|
texture, Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
|
|
|
|
textureRotated, spriteOffset, spriteSourceSize);
|
2021-08-13 14:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add sprite frame
|
|
|
|
cache.insertFrame(spriteSheet, spriteFrameName, spriteFrame);
|
|
|
|
for (auto&& frameAlias : frameAliases)
|
|
|
|
{
|
|
|
|
cache.insertFrame(spriteSheet, frameAlias, spriteFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|