axmol/cocos/2d/CCSpriteFrameCache.cpp

825 lines
28 KiB
C++
Raw Normal View History

/****************************************************************************
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.
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"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include <vector>
2014-08-28 17:03:29 +08:00
#include "2d/CCSprite.h"
#include "2d/CCAutoPolygon.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
#include "base/CCNS.h"
#include "base/ccMacros.h"
#include "base/ccUTF8.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCDirector.h"
2014-08-28 17:03:29 +08:00
#include "renderer/CCTexture2D.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "renderer/CCTextureCache.h"
#include "base/CCNinePatchImageParser.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
using namespace std;
NS_CC_BEGIN
static SpriteFrameCache *_sharedSpriteFrameCache = nullptr;
SpriteFrameCache* SpriteFrameCache::getInstance()
{
if (! _sharedSpriteFrameCache)
{
_sharedSpriteFrameCache = new (std::nothrow) SpriteFrameCache();
_sharedSpriteFrameCache->init();
}
return _sharedSpriteFrameCache;
}
void SpriteFrameCache::destroyInstance()
{
CC_SAFE_RELEASE_NULL(_sharedSpriteFrameCache);
}
2014-05-30 15:13:59 +08:00
bool SpriteFrameCache::init()
{
_spriteFramesAliases.reserve(20);
_spriteFramesCache.init();
return true;
}
2014-05-30 15:13:59 +08:00
SpriteFrameCache::~SpriteFrameCache()
{
}
void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
{
std::string delim(" ");
size_t n = std::count(string.begin(), string.end(), ' ');
res.resize(n+1);
size_t start = 0U;
size_t end = string.find(delim);
int i=0;
while (end != std::string::npos)
{
res[i++] = atoi(string.substr(start, end - start).c_str());
start = end + delim.length();
end = string.find(delim, start);
}
res[i] = atoi(string.substr(start, end).c_str());
}
void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
const Size &spriteSize,
const std::vector<int> &vertices,
const std::vector<int> &verticesUV,
const std::vector<int> &triangleIndices,
PolygonInfo &info)
{
size_t vertexCount = vertices.size();
size_t indexCount = triangleIndices.size();
float scaleFactor = CC_CONTENT_SCALE_FACTOR();
V3F_C4B_T2F *vertexData = new (std::nothrow) V3F_C4B_T2F[vertexCount];
for (size_t i = 0; i < vertexCount/2; i++)
{
vertexData[i].colors = Color4B::WHITE;
vertexData[i].vertices = Vec3(vertices[i*2] / scaleFactor,
(spriteSize.height - vertices[i*2+1]) / scaleFactor,
0);
vertexData[i].texCoords = Tex2F(verticesUV[i*2] / textureSize.width,
verticesUV[i*2+1] / textureSize.height);
}
unsigned short *indexData = new unsigned short[indexCount];
for (size_t i = 0; i < indexCount; i++)
{
indexData[i] = static_cast<unsigned short>(triangleIndices[i]);
}
2016-04-18 16:01:19 +08:00
info.triangles.vertCount = static_cast<int>(vertexCount);
info.triangles.verts = vertexData;
2016-04-18 16:01:19 +08:00
info.triangles.indexCount = static_cast<int>(indexCount);
info.triangles.indices = indexData;
info.setRect(Rect(0, 0, spriteSize.width, spriteSize.height));
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture, const std::string &plist)
{
/*
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
*/
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
return;
ValueMap& framesDict = dictionary["frames"].asValueMap();
int format = 0;
Size textureSize;
// get the format
auto metaItr = dictionary.find("metadata");
if (metaItr != dictionary.end())
{
ValueMap& metadataDict = metaItr->second.asValueMap();
format = metadataDict["format"].asInt();
if(metadataDict.find("size") != metadataDict.end())
{
textureSize = SizeFromString(metadataDict["size"].asString());
}
}
// check the format
CCASSERT(format >=0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
auto textureFileName = Director::getInstance()->getTextureCache()->getTextureFilePath(texture);
Image* image = nullptr;
NinePatchImageParser parser;
for (auto& iter : framesDict)
{
ValueMap& frameDict = iter.second.asValueMap();
std::string spriteFrameName = iter.first;
SpriteFrame* spriteFrame = _spriteFramesCache.at(spriteFrameName);
if (spriteFrame)
{
continue;
}
if(format == 0)
{
float x = frameDict["x"].asFloat();
float y = frameDict["y"].asFloat();
float w = frameDict["width"].asFloat();
float h = frameDict["height"].asFloat();
float ox = frameDict["offsetX"].asFloat();
float oy = frameDict["offsetY"].asFloat();
int ow = frameDict["originalWidth"].asInt();
int oh = frameDict["originalHeight"].asInt();
// check ow/oh
if(!ow || !oh)
{
2015-09-09 11:37:41 +08:00
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist");
}
// abs ow/oh
2016-07-11 12:01:09 +08:00
ow = std::abs(ow);
oh = std::abs(oh);
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
Rect(x, y, w, h),
false,
Vec2(ox, oy),
Size((float)ow, (float)oh)
);
}
else if(format == 1 || format == 2)
{
Rect frame = RectFromString(frameDict["frame"].asString());
bool rotated = false;
// rotation
if (format == 2)
{
rotated = frameDict["rotated"].asBool();
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 offset = PointFromString(frameDict["offset"].asString());
Size sourceSize = SizeFromString(frameDict["sourceSize"].asString());
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
frame,
rotated,
offset,
sourceSize
);
}
else if (format == 3)
{
// get values
Size spriteSize = SizeFromString(frameDict["spriteSize"].asString());
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 spriteOffset = PointFromString(frameDict["spriteOffset"].asString());
Size spriteSourceSize = SizeFromString(frameDict["spriteSourceSize"].asString());
Rect textureRect = RectFromString(frameDict["textureRect"].asString());
bool textureRotated = frameDict["textureRotated"].asBool();
// get aliases
ValueVector& aliases = frameDict["aliases"].asValueVector();
for(const auto &value : aliases) {
std::string oneAlias = value.asString();
if (_spriteFramesAliases.find(oneAlias) != _spriteFramesAliases.end())
{
CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str());
}
_spriteFramesAliases[oneAlias] = Value(spriteFrameName);
}
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
textureRotated,
spriteOffset,
spriteSourceSize);
if(frameDict.find("vertices") != frameDict.end())
{
std::vector<int> vertices;
parseIntegerList(frameDict["vertices"].asString(), vertices);
std::vector<int> verticesUV;
parseIntegerList(frameDict["verticesUV"].asString(), verticesUV);
std::vector<int> indices;
parseIntegerList(frameDict["triangles"].asString(), indices);
PolygonInfo info;
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
spriteFrame->setPolygonInfo(info);
}
if (frameDict.find("anchor") != frameDict.end())
{
spriteFrame->setAnchorPoint(PointFromString(frameDict["anchor"].asString()));
}
}
bool flag = NinePatchImageParser::isNinePatchImage(spriteFrameName);
if(flag)
{
if (image == nullptr) {
image = new (std::nothrow) Image();
image->initWithImageFile(textureFileName);
}
parser.setSpriteFrameInfo(image, spriteFrame->getRectInPixels(), spriteFrame->isRotated());
texture->addSpriteFrameCapInset(spriteFrame, parser.parseCapInset());
}
// add sprite frame
_spriteFramesCache.insertFrame(plist, spriteFrameName, spriteFrame);
}
CC_SAFE_DELETE(image);
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dict, const std::string &texturePath, const std::string &plist)
{
std::string pixelFormatName;
if (dict.find("metadata") != dict.end())
{
ValueMap& metadataDict = dict.at("metadata").asValueMap();
if (metadataDict.find("pixelFormat") != metadataDict.end())
{
pixelFormatName = metadataDict.at("pixelFormat").asString();
}
}
Texture2D *texture = nullptr;
static std::unordered_map<std::string, Texture2D::PixelFormat> pixelFormats = {
{"RGBA8888", Texture2D::PixelFormat::RGBA8888},
{"RGBA4444", Texture2D::PixelFormat::RGBA4444},
{"RGB5A1", Texture2D::PixelFormat::RGB5A1},
2016-02-12 22:25:11 +08:00
{"RGBA5551", Texture2D::PixelFormat::RGB5A1},
{"RGB565", Texture2D::PixelFormat::RGB565},
{"A8", Texture2D::PixelFormat::A8},
2016-02-12 22:25:11 +08:00
{"ALPHA", Texture2D::PixelFormat::A8},
{"I8", Texture2D::PixelFormat::I8},
{"AI88", Texture2D::PixelFormat::AI88},
2016-02-12 22:25:11 +08:00
{"ALPHA_INTENSITY", Texture2D::PixelFormat::AI88},
//{"BGRA8888", Texture2D::PixelFormat::BGRA8888}, no Image conversion RGBA -> BGRA
{"RGB888", Texture2D::PixelFormat::RGB888}
};
auto pixelFormatIt = pixelFormats.find(pixelFormatName);
if (pixelFormatIt != pixelFormats.end())
{
const Texture2D::PixelFormat pixelFormat = (*pixelFormatIt).second;
const Texture2D::PixelFormat currentPixelFormat = Texture2D::getDefaultAlphaPixelFormat();
Texture2D::setDefaultAlphaPixelFormat(pixelFormat);
texture = Director::getInstance()->getTextureCache()->addImage(texturePath);
Texture2D::setDefaultAlphaPixelFormat(currentPixelFormat);
}
else
{
texture = Director::getInstance()->getTextureCache()->addImage(texturePath);
}
if (texture)
{
addSpriteFramesWithDictionary(dict, texture, plist);
}
else
{
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
}
}
2014-05-30 15:13:59 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, Texture2D *texture)
{
if (_spriteFramesCache.hasPlist(plist))
2014-05-30 15:10:57 +08:00
{
return; // We already added it
}
2014-05-30 15:13:59 +08:00
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
addSpriteFramesWithDictionary(dict, texture, plist);
}
void SpriteFrameCache::addSpriteFramesWithFileContent(const std::string& plist_content, Texture2D *texture)
{
2014-08-21 16:21:23 +08:00
ValueMap dict = FileUtils::getInstance()->getValueMapFromData(plist_content.c_str(), static_cast<int>(plist_content.size()));
addSpriteFramesWithDictionary(dict, texture, "by#addSpriteFramesWithFileContent()");
}
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName)
{
CCASSERT(textureFileName.size()>0, "texture name should not be null");
if (_spriteFramesCache.hasPlist(plist))
{
return; // We already added it
}
const std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
addSpriteFramesWithDictionary(dict, textureFileName, plist);
}
2014-05-30 15:13:59 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist)
{
CCASSERT(!plist.empty(), "plist filename should not be nullptr");
2015-01-21 14:15:30 +08:00
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
if (fullPath.empty())
2015-01-21 14:15:30 +08:00
{
// return if plist file doesn't exist
CCLOG("cocos2d: SpriteFrameCache: can not find %s", plist.c_str());
return;
}
if (!_spriteFramesCache.hasPlist(plist))
{
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
string texturePath("");
if (dict.find("metadata") != dict.end())
{
ValueMap& 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, plist);
}
else
{
// build texture path by replacing file extension
2014-05-30 15:13:59 +08:00
texturePath = plist;
// remove .xxx
size_t startPos = texturePath.find_last_of(".");
texturePath = texturePath.erase(startPos);
// append .png
texturePath = texturePath.append(".png");
CCLOG("cocos2d: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
}
addSpriteFramesWithDictionary(dict, texturePath, plist);
}
}
2015-04-24 11:40:15 +08:00
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(const std::string& plist) const
{
return _spriteFramesCache.hasPlist(plist);
}
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, const std::string& frameName)
{
Scale9Sprite refactor (#16891) * Squashed commit of the following: commit 026eee59c9ece7fc59d71fead41104f375f72eb5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Nov 22 21:45:12 2016 -0800 yay! everything works! commit a645c99d44f0d7674e6238afae7628d8d2a030b1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Nov 22 14:55:31 2016 -0800 more fixes in Scale9Sprite commit 3a688f4ea70c0abe311232c65ed14a7e9acc7611 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 21 23:06:45 2016 -0800 rotated sprites commit 45e2085df3aa4b52236c817f7db7c1f6602c1e95 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 21 17:10:44 2016 -0800 android patch 9: fixes is not 100% backward compatible since the previous version had some artifacts. fixed the artifacts. now it renders ok commit 4655d894387fa40a7e63b074a36bfed20e41d5f9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 21 16:31:29 2016 -0800 sprite: correct size for "fake scale9" commit cb98aba27cc1c82a986268d5b74afb55b6cca2f3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Nov 18 21:55:51 2016 -0800 sprite: adds setStrechEnabled for backward compatibility and other fixes commit c0fe67aac54cd62e3846810d56f87133f3a7343b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Nov 18 18:49:42 2016 -0800 implemts missing functions in scale9sprite commit e471ef318725ccabc9eca2c062e285e9ca4f9a81 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Nov 18 18:42:30 2016 -0800 "batched" works commit 33124bfb14d1f275d4b0ffca8989975625a8a76d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 16 18:59:38 2016 -0800 more fixes commit 149f8206269843af63a1739885c00ed8d94c239c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 16 18:37:12 2016 -0800 scale 9 rotated not fixed yet. but making progress commit 27f98275d94ce00c8d5ff0b6f74b5c22723cc3c3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 16 15:32:57 2016 -0800 sprite: streching works ok with normal sprites commit 2519498cac8b77a32dee5bdf1805be56fbda5ed6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 16 12:12:28 2016 -0800 somes fixes... still not working commit 43580d0b4aa976b0200717611a286bf1035c472d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Nov 15 23:15:22 2016 -0800 more bug fixes related to untrimmed setCenterRect... ...in SCale9Sprite commit 407247616e506afaa0415c58ad7fce38efed0cd9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Nov 15 22:13:38 2016 -0800 spritecale9: untrimmed vs. trimmed capInsets WIP. there still a few bugs commit 3d845b2af3500be287fabb04bb6ba0ec04dbb401 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 14 23:11:17 2016 -0800 started progressbar commit 0a44b00daf723869b7105689890f29cd53a62608 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 14 17:43:23 2016 -0800 adds EditBox test commit 330304bd9b2e762de95c2f27f1ae3ceee83b2fbf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 9 16:57:08 2016 -0800 adds elastic to scroll view commit a06a35f8691ff56a143953c44acd947b519150b6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 9 15:58:29 2016 -0800 yay! scrollview works commit 37748a885c62eca8c00d2c9c11ee103f1d95e71c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 9 15:33:58 2016 -0800 yet more scrollview fixes commit a6e563b497adef7b141ac7c92c844d479b244c59 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 9 14:48:28 2016 -0800 initial scroll view commit 4243edd7548e5533ca03c26d5042f8dc5517445a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Nov 8 17:50:15 2016 -0800 sanity check in frames. missing files commit 159d88603ad22f3c01761b60bacec98d52b9041d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 7 19:21:00 2016 -0800 three buttons working commit 6764cfc7937a1a81d6993d5e32a21f17457b8e0d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 7 18:50:22 2016 -0800 missing files commit 11c1fa3812e706a7d34137fe2626f05ab73ee3e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 7 18:49:59 2016 -0800 button works! commit 26b6de4c28fc973e332dd51fcd763a6249c0b4c2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 7 18:19:47 2016 -0800 Scale9: more fixes. complies with odd Rect::ZERO behavior commit ce561268096afe322f25034eeab3508a60027afd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Nov 7 11:50:42 2016 -0800 label: setTitleLabel commit 51b5536cd2af36896002621f75c7310960c3f5e7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 2 17:41:08 2016 -0700 UIButton kind of working commit 7562a49a761ef70c383a99d2addefc63d77e279f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 2 11:11:42 2016 -0700 missing resoruces commit a9890dee5da0104bff6872cc04e8786a6c0a9018 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 2 10:54:55 2016 -0700 setCenterRect to .cpp file commit c9c4bb55ed3fc55709eca46c43d1bfe5301c317f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Nov 2 00:19:51 2016 -0700 removed unused paramter commit e47732934cca79c08e6fe96e46ebdf075508e568 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Oct 28 18:19:31 2016 -0700 flipping works! commit c9769bacd321aeddf84e1772dd270f12194d2f04 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Oct 28 17:57:53 2016 -0700 spirte flip fixes... not finished yet commit 1cbdc8e12cc6d0d2728c06dfabaada001784a178 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Oct 28 09:53:16 2016 -0700 centerRect changes commit aeb1b14c00331dabba640f52d79ab0e255fa3407 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Oct 28 09:10:41 2016 -0700 changes from v3 commit e7a61c4a20d786c2bc1d6f57e33bf3ac95cb6f21 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 26 17:01:15 2016 -0700 sprite: rotated slice 9 works as expected commit 91049389426051deb49a9d951364bf60abaf4532 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 26 15:39:54 2016 -0700 more bug fixes in scale9sprite support commit 9feb12449078441c439df16dc7bcfb4506c7a226 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 26 13:22:52 2016 -0700 Android's Patch 9 works as expected commit 1cf46afe3e7f7f5f714d30779caefc25a9bb1b3b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 26 10:18:47 2016 -0700 Sprite: uses capInsets instead of centerRect just to share the same API as Scale9Sprite commit e944fa0248a7eb58012aacb229de1a9b814e0aef Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 25 18:59:42 2016 -0700 scale9sprite support fixes commit b08765740d480b6ae1491800161b40d88f458767 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 25 17:38:47 2016 -0700 more scale9sprite fixes commit 388a34d93de5e6007a2cecea959c929f26427cab Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 25 17:20:01 2016 -0700 UI::Scale9Sprite refactoring subclass of Sprite... much cleaner! commit c84ec8ee654f74d4b38be2d0a3d7ffe6adbc6faf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Oct 24 20:58:36 2016 -0700 supports tiled sprites commit d2b18eb47a647b29319488bd53edac9f304acb84 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Oct 24 19:21:57 2016 -0700 testing tiled sprite commit b4832ab2facc01cbb624db587ede01fe7acf23e8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Oct 24 16:53:19 2016 -0700 using correct top-left coordinates for setCenterRect commit 29d8de86a71cc597b2338f59f7951a28ee0c93b2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Oct 24 16:46:13 2016 -0700 fixes from scale_sprite9 branch commit ccbe1063bea7db75397ecaf9046aac3dd977a621 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 20 19:01:50 2016 -0700 updated example commit 24f7f85d4eba80f8e543810dcbc1a02447900bdc Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 20 18:47:28 2016 -0700 more fixes in nine slice commit df3358fae68f2d81baf3a8cbe033a4c400ea50da Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 20 18:22:14 2016 -0700 anchor point fixes commit 4ac7409bc72425abb0dc747b956ec346fbd1b2fa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 20 17:01:48 2016 -0700 support for slice-9 from creator commit a3221375d3a9defc2e03df0b1c2da1b36dfdd491 Merge: 73e5389 d7490d9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 20 14:13:38 2016 -0700 Merge branch 'sprite_scale9' into creator_cpp_support commit d7490d94896ad0ce8705dc0695252cee34329b5e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 13 14:34:52 2016 -0700 Adds slice9 support for Sprite. how to use it: // points coords sprite->setCenterRect(Rect(x,y,w,h)); // normalized coords sprite->setCenterRectNormalized(Rect(x,y,w,h)); starts scale9sprite in sprite more slice 9 changes sprite 9 slice works? kind of works correct anchor point slice 9 works, at least with non-rotated atlases streched works ok better Y invert code. cleaner, compatible with the previous code yay, scaling workings... need a better api now sets scale correctly yay! works as expected! more fixes and tests better test for box setContentSize() changes size in non-9-slice mode sprites as well setCenterRect() -> setCenterRectNormalized() yet another test adds setPositionNormalized() adds setCenterRect() tests remove devel team from xcode tests: add one more tests fun test! improved test yet another test for slice 9 commit 73e5389ef719880096067d0f7d0bffd6e1f3cfd1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Sun Oct 9 18:54:22 2016 -0700 kind of ProgressBar support commit 4db5e9e7369e93ceba1d9512ab88c26b843c43d5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Oct 7 16:23:54 2016 -0700 new generatred ui files commit 75aa06f3d39df8c6aa898d5e6810b3cd8015c07b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Oct 6 10:44:59 2016 -0700 bette canvas support commit 89beacac478fc7f58ef930963b6393f3da4a743c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 5 21:47:11 2016 -0700 sprites tests commit 5fe930ad50c2c1b6d868982709c7b659176f9889 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 5 18:35:44 2016 -0700 more label tests commit d3f615a097d080d78f2ebd95f3e2388da45f92db Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Oct 5 18:08:24 2016 -0700 label tests commit 2a4018922ce8d36117f7092fc324e37cf75fdeab Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 4 21:52:56 2016 -0700 label fixes commit 41fafbd023de63e376d014311605f2b156add307 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 4 19:14:58 2016 -0700 updated test commit 06919fc28fffbc5a20cd14bdcc2ad2ff997e0da1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Oct 4 18:50:45 2016 -0700 creator tests: works commit 6e362e7cc7f68dc216b569a7f7f90aaa110c0e4a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 27 23:15:56 2016 -0700 autogen: new data commit e78078b2ad84b4e364827a66f1c459de7384a91d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 26 18:18:57 2016 -0700 fix: initial test for CPP support for Creator * adds more documentation * adds more documentation
2016-11-24 09:59:00 +08:00
CCASSERT(frame, "frame should not be nil");
_spriteFramesCache.insertFrame("by#addSpriteFrame()", frameName, frame);
}
void SpriteFrameCache::removeSpriteFrames()
{
_spriteFramesAliases.clear();
_spriteFramesCache.clear();
}
void SpriteFrameCache::removeUnusedSpriteFrames()
{
bool removed = false;
std::vector<std::string> toRemoveFrames;
for (auto& iter : _spriteFramesCache.getSpriteFrames())
{
SpriteFrame* spriteFrame = iter.second;
2014-01-22 13:47:29 +08:00
if( spriteFrame->getReferenceCount() == 1 )
{
toRemoveFrames.push_back(iter.first);
spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter.first.c_str());
removed = true;
}
}
if( removed )
2012-06-12 01:43:07 +08:00
{
_spriteFramesCache.eraseFrames(toRemoveFrames);
}
}
void SpriteFrameCache::removeSpriteFrameByName(const std::string& name)
{
// explicit nil handling
if (name.empty())
return;
// Is this an alias ?
bool foundAlias = _spriteFramesAliases.find(name) != _spriteFramesAliases.end();
std::string key = foundAlias ? _spriteFramesAliases[name].asString() : "";
if (!key.empty())
{
_spriteFramesAliases.erase(key);
}
_spriteFramesCache.eraseFrame(name);
}
void SpriteFrameCache::removeSpriteFramesFromFile(const std::string& plist)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
if (dict.empty())
{
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFile: create dict by %s fail.",plist.c_str());
return;
}
removeSpriteFramesFromDictionary(dict);
2012-06-12 01:43:07 +08:00
// remove it from the cache
_spriteFramesCache.erasePlistIndex(plist);
}
void SpriteFrameCache::removeSpriteFramesFromFileContent(const std::string& plist_content)
{
2014-08-21 16:21:23 +08:00
ValueMap dict = FileUtils::getInstance()->getValueMapFromData(plist_content.data(), static_cast<int>(plist_content.size()));
if (dict.empty())
{
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFileContent: create dict by fail.");
return;
}
removeSpriteFramesFromDictionary(dict);
}
void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
{
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
return;
const ValueMap& framesDict = dictionary["frames"].asValueMap();
std::vector<std::string> keysToRemove;
for (const auto& iter : framesDict)
{
if (_spriteFramesCache.at(iter.first))
{
keysToRemove.push_back(iter.first);
}
}
_spriteFramesCache.eraseFrames(keysToRemove);
}
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
{
std::vector<std::string> keysToRemove;
for (auto& iter : _spriteFramesCache.getSpriteFrames())
{
std::string key = iter.first;
SpriteFrame* frame = _spriteFramesCache.at(key);
if (frame && (frame->getTexture() == texture))
{
keysToRemove.push_back(key);
}
}
_spriteFramesCache.eraseFrames(keysToRemove);
}
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(const std::string& name)
{
SpriteFrame* frame = _spriteFramesCache.at(name);
if (!frame)
{
// try alias dictionary
if (_spriteFramesAliases.find(name) != _spriteFramesAliases.end())
{
std::string key = _spriteFramesAliases[name].asString();
if (!key.empty())
{
frame = _spriteFramesCache.at(key);
if (!frame)
{
CCLOG("cocos2d: SpriteFrameCache: Frame aliases '%s' isn't found", key.c_str());
}
}
}
else
{
CCLOG("cocos2d: SpriteFrameCache: Frame '%s' isn't found", name.c_str());
}
}
return frame;
}
void SpriteFrameCache::reloadSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture, const std::string &plist)
{
ValueMap& framesDict = dictionary["frames"].asValueMap();
int format = 0;
// get the format
if (dictionary.find("metadata") != dictionary.end())
{
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
format = metadataDict["format"].asInt();
}
// check the format
CCASSERT(format >= 0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
for (auto& iter : framesDict)
{
ValueMap& frameDict = iter.second.asValueMap();
std::string spriteFrameName = iter.first;
_spriteFramesCache.eraseFrame(spriteFrameName);
SpriteFrame* spriteFrame = nullptr;
if (format == 0)
{
float x = frameDict["x"].asFloat();
float y = frameDict["y"].asFloat();
float w = frameDict["width"].asFloat();
float h = frameDict["height"].asFloat();
float ox = frameDict["offsetX"].asFloat();
float oy = frameDict["offsetY"].asFloat();
int ow = frameDict["originalWidth"].asInt();
int oh = frameDict["originalHeight"].asInt();
// check ow/oh
if (!ow || !oh)
{
2016-07-25 01:53:22 +08:00
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist");
}
// abs ow/oh
2016-07-11 12:01:09 +08:00
ow = std::abs(ow);
oh = std::abs(oh);
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
Rect(x, y, w, h),
false,
Vec2(ox, oy),
Size((float)ow, (float)oh)
);
}
else if (format == 1 || format == 2)
{
Rect frame = RectFromString(frameDict["frame"].asString());
bool rotated = false;
// rotation
if (format == 2)
{
rotated = frameDict["rotated"].asBool();
}
Vec2 offset = PointFromString(frameDict["offset"].asString());
Size sourceSize = SizeFromString(frameDict["sourceSize"].asString());
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
frame,
rotated,
offset,
sourceSize
);
}
else if (format == 3)
{
// get values
Size spriteSize = SizeFromString(frameDict["spriteSize"].asString());
Vec2 spriteOffset = PointFromString(frameDict["spriteOffset"].asString());
Size spriteSourceSize = SizeFromString(frameDict["spriteSourceSize"].asString());
Rect textureRect = RectFromString(frameDict["textureRect"].asString());
bool textureRotated = frameDict["textureRotated"].asBool();
// get aliases
ValueVector& aliases = frameDict["aliases"].asValueVector();
for (const auto &value : aliases) {
std::string oneAlias = value.asString();
if (_spriteFramesAliases.find(oneAlias) != _spriteFramesAliases.end())
{
CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str());
}
_spriteFramesAliases[oneAlias] = Value(spriteFrameName);
}
// create frame
spriteFrame = SpriteFrame::createWithTexture(texture,
Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
textureRotated,
spriteOffset,
spriteSourceSize);
}
// add sprite frame
_spriteFramesCache.insertFrame(plist, spriteFrameName, spriteFrame);
}
}
bool SpriteFrameCache::reloadTexture(const std::string& plist)
{
CCASSERT(plist.size()>0, "plist filename should not be nullptr");
if (_spriteFramesCache.hasPlist(plist)) {
_spriteFramesCache.erasePlistIndex(plist);
}
else
{
//If one plist has't be loaded, we don't load it here.
return false;
}
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
string texturePath("");
if (dict.find("metadata") != dict.end())
{
ValueMap& 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, plist);
}
else
{
// build texture path by replacing file extension
texturePath = plist;
// remove .xxx
size_t startPos = texturePath.find_last_of(".");
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, plist);
}
else
{
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
}
return true;
}
void SpriteFrameCache::PlistFramesCache::insertFrame(const std::string &plist, const std::string &frame, SpriteFrame *spriteFrame)
{
_spriteFrames.insert(frame, spriteFrame); //add SpriteFrame
_indexPlist2Frames[plist].insert(frame); //insert index plist->[frameName]
_indexFrame2plist[frame] = plist; //insert index frameName->plist
}
bool SpriteFrameCache::PlistFramesCache::isPlistUsed(const std::string &plist) const
{
//plist loaded && not empty
auto it = _indexPlist2Frames.find(plist);
return it != _indexPlist2Frames.end() && !it->second.empty();
}
bool SpriteFrameCache::PlistFramesCache::eraseFrame(const std::string &frame)
{
_spriteFrames.erase(frame); //drop SpriteFrame
auto itFrame = _indexFrame2plist.find(frame);
if (itFrame != _indexFrame2plist.end())
{
_indexPlist2Frames[itFrame->second].erase(frame); //update index plist->[frameNames]
_indexFrame2plist.erase(itFrame); //update index frame->plist
return true;
}
return false;
}
bool SpriteFrameCache::PlistFramesCache::eraseFrames(const std::vector<std::string> &frames)
{
auto ret = false;
for (const auto & frame : frames)
{
ret |= eraseFrame(frame);
}
return ret;
}
bool SpriteFrameCache::PlistFramesCache::erasePlistIndex(const std::string &plist)
{
auto it = _indexPlist2Frames.find(plist);
if (it == _indexPlist2Frames.end()) return false;
auto &frames = it->second;
for (auto f : frames)
{
// !!do not!! call `_spriteFrames.erase(f);` to erase SpriteFrame
// only erase index here
_indexFrame2plist.erase(f); //erase plist frame frameName->plist
}
_indexPlist2Frames.erase(plist); //update index plist->[frameNames]
return true;
}
void SpriteFrameCache::PlistFramesCache::clear()
{
_indexPlist2Frames.clear();
_indexFrame2plist.clear();
_spriteFrames.clear();
}
bool SpriteFrameCache::PlistFramesCache::hasFrame(const std::string &frame) const
{
return _indexFrame2plist.find(frame) != _indexFrame2plist.end();
}
bool SpriteFrameCache::PlistFramesCache::hasPlist(const std::string &plist) const
{
return _indexPlist2Frames.find(plist) != _indexPlist2Frames.end();
}
SpriteFrame * SpriteFrameCache::PlistFramesCache::at(const std::string &frame)
{
return _spriteFrames.at(frame);
}
Map<std::string, SpriteFrame*>& SpriteFrameCache::PlistFramesCache::getSpriteFrames()
{
return _spriteFrames;
}
NS_CC_END