mirror of https://github.com/axmolengine/axmol.git
Sprite::capInsets -> Sprite::centerRect (#16770)
* Sprite::capInsets -> Sprite::centerRect as requested by @minggo * fixes flip issues in Sprite with slice 9
This commit is contained in:
parent
3ab1be61a7
commit
e0809d869c
|
@ -311,7 +311,7 @@ Sprite::Sprite(void)
|
|||
, _texture(nullptr)
|
||||
, _spriteFrame(nullptr)
|
||||
, _insideBounds(true)
|
||||
, _capInsetsNormalized(0,0,1,1)
|
||||
, _centerRectNormalized(0,0,1,1)
|
||||
, _numberOfSlices(1)
|
||||
, _quads(nullptr)
|
||||
, _strechFactor(Vec2::ONE)
|
||||
|
@ -436,10 +436,10 @@ void Sprite::updatePoly()
|
|||
CCASSERT(_numberOfSlices == 9, "Invalid number of slices");
|
||||
|
||||
// center rect
|
||||
const float x1 = _capInsetsNormalized.origin.x;
|
||||
const float y1 = _capInsetsNormalized.origin.y;
|
||||
const float x2 = _capInsetsNormalized.origin.x + _capInsetsNormalized.size.width;
|
||||
const float y2 = _capInsetsNormalized.origin.y + _capInsetsNormalized.size.height;
|
||||
const float cx1 = _centerRectNormalized.origin.x;
|
||||
const float cy1 = _centerRectNormalized.origin.y;
|
||||
const float cx2 = _centerRectNormalized.origin.x + _centerRectNormalized.size.width;
|
||||
const float cy2 = _centerRectNormalized.origin.y + _centerRectNormalized.size.height;
|
||||
|
||||
// "O"riginal rect
|
||||
const float oox = _rect.origin.x;
|
||||
|
@ -449,46 +449,91 @@ void Sprite::updatePoly()
|
|||
|
||||
// textCoords Data: Y must be inverted.
|
||||
const float u0 = oox + osw * 0;
|
||||
const float u1 = oox + osw * x1;
|
||||
const float u2 = oox + osw * x2;
|
||||
const float v0 = ooy + osh - (osh * y1);
|
||||
const float v1 = ooy + osh * (1-y2);
|
||||
const float u1 = oox + osw * cx1;
|
||||
const float u2 = oox + osw * cx2;
|
||||
const float v0 = ooy + osh - (osh * cy1);
|
||||
const float v1 = ooy + osh * (1 - cy2);
|
||||
const float v2 = ooy + osh * 0;
|
||||
|
||||
const Rect texRects[9] = {
|
||||
Rect(u0, v0, osw * x1, osh * y1), // bottom-left
|
||||
Rect(u1, v0, osw * (x2-x1), osh * y1), // bottom
|
||||
Rect(u2, v0, osw * (1-x2), osh * y1), // bottom-right
|
||||
Rect(u0, v0, osw * cx1, osh * cy1), // bottom-left
|
||||
Rect(u1, v0, osw * (cx2-cx1), osh * cy1), // bottom
|
||||
Rect(u2, v0, osw * (1-cx2), osh * cy1), // bottom-right
|
||||
|
||||
Rect(u0, v1, osw * x1, osh * (y2-y1)), // left
|
||||
Rect(u1, v1, osw * (x2-x1), osh * (y2-y1)), // center
|
||||
Rect(u2, v1, osw * (1-x2), osh * (y2-y1)), // right
|
||||
Rect(u0, v1, osw * cx1, osh * (cy2-cy1)), // left
|
||||
Rect(u1, v1, osw * (cx2-cx1), osh * (cy2-cy1)), // center
|
||||
Rect(u2, v1, osw * (1-cx2), osh * (cy2-cy1)), // right
|
||||
|
||||
Rect(u0, v2, osw * x1, osh * (1-y2)), // top-left
|
||||
Rect(u1, v2, osw * (x2-x1), osh * (1-y2)), // top
|
||||
Rect(u2, v2, osw * (1-x2), osh * (1-y2)), // top-right
|
||||
Rect(u0, v2, osw * cx1, osh * (1-cy2)), // top-left
|
||||
Rect(u1, v2, osw * (cx2-cx1), osh * (1-cy2)), // top
|
||||
Rect(u2, v2, osw * (1-cx2), osh * (1-cy2)), // top-right
|
||||
};
|
||||
|
||||
// vertex Data.
|
||||
const float x2_x1_strech = osw * (x2-x1) * _strechFactor.x;
|
||||
const float y2_y1_strech = osh * (y2-y1) * _strechFactor.y;
|
||||
|
||||
// sizes
|
||||
float x0_s = osw * cx1;
|
||||
float x1_s = osw * (cx2-cx1) * _strechFactor.x;
|
||||
float x2_s = osw * (1-cx2);
|
||||
float y0_s = osh * cy1;
|
||||
float y1_s = osh * (cy2-cy1) * _strechFactor.y;
|
||||
float y2_s = osh * (1-cy2);
|
||||
|
||||
// It is easier to call "updateXY", but it will be slower.
|
||||
// so the flipping is calculated here at the cost of adding
|
||||
// just a little bit more of complexity.
|
||||
|
||||
// swap sizes to calculate offset correctly
|
||||
if (_flippedX)
|
||||
std::swap(x0_s, x2_s);
|
||||
if (_flippedY)
|
||||
std::swap(y0_s, y2_s);
|
||||
|
||||
// origins
|
||||
float x0 = 0;
|
||||
float x1 = x0 + x0_s;
|
||||
float x2 = x1 + x1_s;
|
||||
float y0 = 0;
|
||||
float y1 = y0 + y0_s;
|
||||
float y2 = y1 + y1_s;
|
||||
|
||||
// swap origin, but restore size to its original value
|
||||
if (_flippedX) {
|
||||
std::swap(x0, x2);
|
||||
std::swap(x0_s, x2_s);
|
||||
}
|
||||
if (_flippedY) {
|
||||
std::swap(y0, y2);
|
||||
std::swap(y0_s, y2_s);
|
||||
}
|
||||
|
||||
const Rect verticesRects[9] = {
|
||||
Rect(osw * 0, osh * 0, osw * x1, osh * y1), // bottom-left
|
||||
Rect(osw * x1, osh * 0, x2_x1_strech, osh * y1), // bottom
|
||||
Rect(osw * x1 + x2_x1_strech, osh * 0, osw * (1-x2), osh * y1), // bottom-right
|
||||
Rect(x0, y0, x0_s, y0_s), // bottom-left
|
||||
Rect(x1, y0, x1_s, y0_s), // bottom
|
||||
Rect(x2, y0, x2_s, y0_s), // bottom-right
|
||||
|
||||
Rect(osw * 0, osh * y1, osw * x1, y2_y1_strech), // left
|
||||
Rect(osw * x1, osh * y1, x2_x1_strech, y2_y1_strech), // center
|
||||
Rect(osw * x1 + x2_x1_strech, osh * y1, osw * (1-x2), y2_y1_strech), // right
|
||||
Rect(x0, y1, x0_s, y1_s), // left
|
||||
Rect(x1, y1, x1_s, y1_s), // center
|
||||
Rect(x2, y1, x2_s, y1_s), // right
|
||||
|
||||
Rect(osw * 0, osh * y1 + y2_y1_strech, osw * x1, osh * (1-y2)), // top-left
|
||||
Rect(osw * x1, osh * y1 + y2_y1_strech, x2_x1_strech, osh * (1-y2)), // top
|
||||
Rect(osw * x1 + x2_x1_strech, osh * y1 + y2_y1_strech, osw * (1-x2), osh * (1-y2)), // top-right
|
||||
Rect(x0, y2, x0_s, y2_s), // top-left
|
||||
Rect(x1, y2, x1_s, y2_s), // top
|
||||
Rect(x2, y2, x2_s, y2_s), // top-right
|
||||
};
|
||||
|
||||
const int rotatedIdx[] = {6, 3, 0, 7, 4, 1, 8, 5, 2};
|
||||
static const int normalIdx[] = {
|
||||
0, 1, 2,
|
||||
3, 4, 5,
|
||||
6, 7 ,8
|
||||
};
|
||||
static const int rotatedIdx[] = {
|
||||
6, 3, 0,
|
||||
7, 4, 1,
|
||||
8, 5, 2};
|
||||
const int* idx = _rectRotated ? rotatedIdx : normalIdx;
|
||||
|
||||
for (int i=0; i<_numberOfSlices; ++i) {
|
||||
const int texIdx = _rectRotated ? rotatedIdx[i] : i;
|
||||
int texIdx = idx[i];
|
||||
setTextureCoords(texRects[texIdx], &_quads[i]);
|
||||
setVertexCoords(verticesRects[i], _rect.size, &_quads[i]);
|
||||
}
|
||||
|
@ -496,14 +541,14 @@ void Sprite::updatePoly()
|
|||
}
|
||||
}
|
||||
|
||||
void Sprite::setCapInsetsNormalized(const cocos2d::Rect &rectTopLeft)
|
||||
void Sprite::setCenterRectNormalized(const cocos2d::Rect &rectTopLeft)
|
||||
{
|
||||
// FIMXE: Rect is has origin on top-left (like text coordinate).
|
||||
// but all the logic has been done using bottom-left as origin. So it is easier to invert Y
|
||||
// here, than in the rest of the places... but it is not as clean.
|
||||
Rect rect(rectTopLeft.origin.x, 1 - rectTopLeft.origin.y - rectTopLeft.size.height, rectTopLeft.size.width, rectTopLeft.size.height);
|
||||
if (!_capInsetsNormalized.equals(rect)) {
|
||||
_capInsetsNormalized = rect;
|
||||
if (!_centerRectNormalized.equals(rect)) {
|
||||
_centerRectNormalized = rect;
|
||||
|
||||
// convert it to 1-slice
|
||||
if (rect.equals(Rect(0,0,1,1))) {
|
||||
|
@ -532,7 +577,7 @@ void Sprite::setCapInsetsNormalized(const cocos2d::Rect &rectTopLeft)
|
|||
}
|
||||
}
|
||||
|
||||
void Sprite::setCapInsets(const cocos2d::Rect &rectInPoints)
|
||||
void Sprite::setCenterRect(const cocos2d::Rect &rectInPoints)
|
||||
{
|
||||
if (!_originalContentSize.equals(Size::ZERO))
|
||||
{
|
||||
|
@ -541,20 +586,23 @@ void Sprite::setCapInsets(const cocos2d::Rect &rectInPoints)
|
|||
const float y = rect.origin.y / _rect.size.height;
|
||||
const float w = rect.size.width / _rect.size.width;
|
||||
const float h = rect.size.height / _rect.size.height;
|
||||
setCapInsetsNormalized(Rect(x,y,w,h));
|
||||
setCenterRectNormalized(Rect(x,y,w,h));
|
||||
}
|
||||
}
|
||||
|
||||
Rect Sprite::getCapInsetsNormalized() const
|
||||
Rect Sprite::getCenterRectNormalized() const
|
||||
{
|
||||
// FIXME: _capInsetsNormalized is in bottom-left coords, but should converted to top-left
|
||||
Rect ret(_capInsetsNormalized.origin.x, 1 - _capInsetsNormalized.origin.y - _capInsetsNormalized.size.height, _capInsetsNormalized.size.width, _capInsetsNormalized.size.height);
|
||||
// FIXME: _centerRectNormalized is in bottom-left coords, but should converted to top-left
|
||||
Rect ret(_centerRectNormalized.origin.x,
|
||||
1 - _centerRectNormalized.origin.y - _centerRectNormalized.size.height,
|
||||
_centerRectNormalized.size.width,
|
||||
_centerRectNormalized.size.height);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Rect Sprite::getCapInsets() const
|
||||
Rect Sprite::getCenterRect() const
|
||||
{
|
||||
Rect rect = getCapInsetsNormalized();
|
||||
Rect rect = getCenterRectNormalized();
|
||||
rect.origin.x *= _rect.size.width;
|
||||
rect.origin.y *= _rect.size.height;
|
||||
rect.size.width *= _rect.size.width;
|
||||
|
@ -1120,13 +1168,13 @@ void Sprite::updateStretchFactor()
|
|||
}
|
||||
else
|
||||
{
|
||||
const float x1 = _rect.size.width * _capInsetsNormalized.origin.x;
|
||||
const float x2 = _rect.size.width * _capInsetsNormalized.size.width;
|
||||
const float x3 = _rect.size.width * (1 - _capInsetsNormalized.origin.x - _capInsetsNormalized.size.width);
|
||||
const float x1 = _rect.size.width * _centerRectNormalized.origin.x;
|
||||
const float x2 = _rect.size.width * _centerRectNormalized.size.width;
|
||||
const float x3 = _rect.size.width * (1 - _centerRectNormalized.origin.x - _centerRectNormalized.size.width);
|
||||
|
||||
const float y1 = _rect.size.height * _capInsetsNormalized.origin.y;
|
||||
const float y2 = _rect.size.height * _capInsetsNormalized.size.height;
|
||||
const float y3 = _rect.size.height * (1 - _capInsetsNormalized.origin.y - _capInsetsNormalized.size.height);
|
||||
const float y1 = _rect.size.height * _centerRectNormalized.origin.y;
|
||||
const float y2 = _rect.size.height * _centerRectNormalized.size.height;
|
||||
const float y3 = _rect.size.height * (1 - _centerRectNormalized.origin.y - _centerRectNormalized.size.height);
|
||||
|
||||
const float x_factor = (adjustedWidth - x1 - x3) / x2;
|
||||
const float y_factor = (adjustedHeight - y1 - y3) / y2;
|
||||
|
@ -1140,10 +1188,12 @@ void Sprite::setFlippedX(bool flippedX)
|
|||
if (_flippedX != flippedX)
|
||||
{
|
||||
_flippedX = flippedX;
|
||||
|
||||
for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
|
||||
auto& v = _polyInfo.triangles.verts[i].vertices;
|
||||
v.x = _contentSize.width -v.x;
|
||||
v.x = _contentSize.width - v.x;
|
||||
}
|
||||
|
||||
if (_textureAtlas) {
|
||||
setDirty(true);
|
||||
}
|
||||
|
@ -1160,10 +1210,12 @@ void Sprite::setFlippedY(bool flippedY)
|
|||
if (_flippedY != flippedY)
|
||||
{
|
||||
_flippedY = flippedY;
|
||||
|
||||
for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
|
||||
auto& v = _polyInfo.triangles.verts[i].vertices;
|
||||
v.y = _contentSize.height -v.y;
|
||||
v.y = _contentSize.height - v.y;
|
||||
}
|
||||
|
||||
if (_textureAtlas) {
|
||||
setDirty(true);
|
||||
}
|
||||
|
@ -1279,7 +1331,7 @@ void Sprite::setSpriteFrame(SpriteFrame *spriteFrame)
|
|||
}
|
||||
if (spriteFrame->hasCenterRect())
|
||||
{
|
||||
setCapInsets(spriteFrame->getCapInsets());
|
||||
setCenterRect(spriteFrame->getCenterRect());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -248,7 +248,7 @@ public:
|
|||
virtual void setVertexRect(const Rect& rect);
|
||||
|
||||
/**
|
||||
* setCapInsetsNormalized
|
||||
* setCenterRectNormalized
|
||||
*
|
||||
* Useful to implement "9 sliced" sprites.
|
||||
* The default value is (0,0) - (1,1), which means that only one "slice" will be used: From top-left (0,0) to bottom-right (1,1).
|
||||
|
@ -259,27 +259,27 @@ public:
|
|||
*
|
||||
* Limitations: Does not work when the sprite is part of `SpriteBatchNode`.
|
||||
*/
|
||||
virtual void setCapInsetsNormalized(const Rect& rect);
|
||||
virtual void setCenterRectNormalized(const Rect& rect);
|
||||
|
||||
/**
|
||||
* getCapInsetsNormalized
|
||||
* getCenterRectNormalized
|
||||
*
|
||||
* Returns the CapInsets in normalized coordinates
|
||||
* Returns the CenterRect in normalized coordinates
|
||||
*/
|
||||
virtual Rect getCapInsetsNormalized() const;
|
||||
virtual Rect getCenterRectNormalized() const;
|
||||
|
||||
/* setCapInsets
|
||||
/* setCenterRect
|
||||
*
|
||||
* Like `setCapInsetsNormalized`, but instead of being in normalized coordinates, it is in points coordinates
|
||||
* Like `setCenterRectNormalized`, but instead of being in normalized coordinates, it is in points coordinates
|
||||
*/
|
||||
virtual void setCapInsets(const Rect& rect);
|
||||
virtual void setCenterRect(const Rect& rect);
|
||||
|
||||
/**
|
||||
* @brief Returns the Cap Insets rect
|
||||
*
|
||||
* @return Scale9Sprite's cap inset.
|
||||
*/
|
||||
virtual Rect getCapInsets() const;
|
||||
virtual Rect getCenterRect() const;
|
||||
|
||||
|
||||
/** @{
|
||||
|
@ -630,7 +630,7 @@ protected:
|
|||
|
||||
void updatePoly();
|
||||
void updateStretchFactor();
|
||||
|
||||
|
||||
//
|
||||
// Data used when the sprite is rendered using a SpriteSheet
|
||||
//
|
||||
|
@ -661,7 +661,7 @@ protected:
|
|||
Rect _rect; /// Rectangle of Texture2D
|
||||
bool _rectRotated; /// Whether the texture is rotated
|
||||
|
||||
Rect _capInsetsNormalized; /// Rectangle to implement "slice 9"
|
||||
Rect _centerRectNormalized; /// Rectangle to implement "slice 9"
|
||||
int _numberOfSlices; /// how many sprite slices: 1 or 9
|
||||
Vec2 _strechFactor; /// strech factor to match the contentSize. for 1- and 9- slice sprites
|
||||
Size _originalContentSize; /// original content size
|
||||
|
|
|
@ -104,7 +104,7 @@ bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect, bool rot
|
|||
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
||||
_rotated = rotated;
|
||||
_anchorPoint = Vec2(NAN, NAN);
|
||||
_capInsetsNormalized = Rect(NAN, NAN, NAN, NAN);
|
||||
_centerRect = Rect(NAN, NAN, NAN, NAN);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rec
|
|||
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
||||
_rotated = rotated;
|
||||
_anchorPoint = Vec2(NAN, NAN);
|
||||
_capInsetsNormalized = Rect(NAN, NAN, NAN, NAN);
|
||||
_centerRect = Rect(NAN, NAN, NAN, NAN);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -155,14 +155,14 @@ void SpriteFrame::setRectInPixels(const Rect& rectInPixels)
|
|||
_rect = CC_RECT_PIXELS_TO_POINTS(rectInPixels);
|
||||
}
|
||||
|
||||
void SpriteFrame::setCapInsets(const Rect& centerRect)
|
||||
void SpriteFrame::setCenterRectInPixels(const Rect& centerRect)
|
||||
{
|
||||
_capInsetsNormalized = CC_RECT_PIXELS_TO_POINTS(centerRect);
|
||||
_centerRect = CC_RECT_PIXELS_TO_POINTS(centerRect);
|
||||
}
|
||||
|
||||
bool SpriteFrame::hasCenterRect() const
|
||||
{
|
||||
return !std::isnan(_capInsetsNormalized.origin.x);
|
||||
return !std::isnan(_centerRect.origin.x);
|
||||
}
|
||||
|
||||
const Vec2& SpriteFrame::getOffset() const
|
||||
|
|
|
@ -139,7 +139,7 @@ public:
|
|||
*
|
||||
* @return The center rect of the sprite frame in points
|
||||
*/
|
||||
const Rect& getCapInsets() const { return _capInsetsNormalized; }
|
||||
const Rect& getCenterRect() const { return _centerRect; }
|
||||
|
||||
/**
|
||||
* setCenterRect
|
||||
|
@ -153,7 +153,7 @@ public:
|
|||
* Limitations: Does not work when the sprite is part of `SpriteBatchNode`.
|
||||
* @param centerRect the Rect in points
|
||||
*/
|
||||
void setCapInsets(const Rect& centerRect);
|
||||
void setCenterRectInPixels(const Rect& centerRect);
|
||||
|
||||
/** hasCenterRect
|
||||
@return Whether or not it has a centerRect
|
||||
|
@ -292,7 +292,7 @@ protected:
|
|||
Rect _rectInPixels;
|
||||
bool _rotated;
|
||||
Rect _rect;
|
||||
Rect _capInsetsNormalized;
|
||||
Rect _centerRect;
|
||||
Vec2 _offsetInPixels;
|
||||
Size _originalSizeInPixels;
|
||||
Texture2D *_texture;
|
||||
|
|
|
@ -135,6 +135,7 @@ SpriteTests::SpriteTests()
|
|||
ADD_TEST_CASE(SpriteSlice9Test7);
|
||||
ADD_TEST_CASE(SpriteSlice9Test8);
|
||||
ADD_TEST_CASE(SpriteSlice9Test9);
|
||||
ADD_TEST_CASE(SpriteSlice9Test10);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
@ -5269,9 +5270,9 @@ SpriteSlice9Test1::SpriteSlice9Test1()
|
|||
s3->runAction(action3);
|
||||
|
||||
if (i==2) {
|
||||
s3->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s1->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s3->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s1->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
}
|
||||
|
||||
// "anchor points"
|
||||
|
@ -5334,9 +5335,9 @@ SpriteSlice9Test2::SpriteSlice9Test2()
|
|||
s3->runAction(action3);
|
||||
|
||||
if (i==2) {
|
||||
s3->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s1->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s3->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s1->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
}
|
||||
|
||||
// "anchor points"
|
||||
|
@ -5398,9 +5399,9 @@ SpriteSlice9Test3::SpriteSlice9Test3()
|
|||
|
||||
// enable slice 9, only in the first row
|
||||
if (i==2) {
|
||||
s1->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s3->setCapInsetsNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s1->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s2->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
s3->setCenterRectNormalized(Rect(0.4, 0.4, 0.2, 0.2));
|
||||
}
|
||||
|
||||
|
||||
|
@ -5463,9 +5464,9 @@ SpriteSlice9Test4::SpriteSlice9Test4()
|
|||
|
||||
// enable slice 9, only in the first row
|
||||
if (i==2) {
|
||||
s1->setCapInsets(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
s2->setCapInsets(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
s3->setCapInsets(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
s1->setCenterRect(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
s2->setCenterRect(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
s3->setCenterRect(CC_RECT_PIXELS_TO_POINTS(Rect(6, 14, 2, 4)));
|
||||
}
|
||||
|
||||
// "anchor points"
|
||||
|
@ -5507,7 +5508,7 @@ SpriteSlice9Test5::SpriteSlice9Test5()
|
|||
s1->setPosition(s.width/2-s.width/3, s.height/2);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setContentSize(Size(s.width/3, s.height));
|
||||
s1->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s1->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[0] = s1;
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
|
@ -5517,7 +5518,7 @@ SpriteSlice9Test5::SpriteSlice9Test5()
|
|||
s2->setPosition(s.width*2/4, s.height/2);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setContentSize(Size(s.width/3, s.height));
|
||||
s2->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s2->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[1] = s2;
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
|
@ -5527,7 +5528,7 @@ SpriteSlice9Test5::SpriteSlice9Test5()
|
|||
s3->setPosition(s.width/2+s.width/3, s.height/2);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setContentSize(Size(s.width/3, s.height));
|
||||
s3->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s3->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[2] = s3;
|
||||
|
||||
scheduleUpdate();
|
||||
|
@ -5555,7 +5556,7 @@ void SpriteSlice9Test5::update(float dt)
|
|||
}
|
||||
|
||||
Rect rect(x,y,0.2, 0.2);
|
||||
_sprites[i]->setCapInsetsNormalized(rect);
|
||||
_sprites[i]->setCenterRectNormalized(rect);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5580,7 +5581,7 @@ SpriteSlice9Test6::SpriteSlice9Test6()
|
|||
s1->setPosition(s.width/2-s.width/3, s.height/2);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setContentSize(Size(s.width/3, s.height));
|
||||
s1->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s1->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[0] = s1;
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
|
@ -5590,7 +5591,7 @@ SpriteSlice9Test6::SpriteSlice9Test6()
|
|||
s2->setPosition(s.width*2/4, s.height/2);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setContentSize(Size(s.width/3, s.height));
|
||||
s2->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s2->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[1] = s2;
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
|
@ -5600,7 +5601,7 @@ SpriteSlice9Test6::SpriteSlice9Test6()
|
|||
s3->setPosition(s.width/2+s.width/3, s.height/2);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setContentSize(Size(s.width/3, s.height));
|
||||
s3->setCapInsetsNormalized(Rect(0,0,1,1));
|
||||
s3->setCenterRectNormalized(Rect(0,0,1,1));
|
||||
_sprites[2] = s3;
|
||||
|
||||
scheduleUpdate();
|
||||
|
@ -5628,7 +5629,7 @@ void SpriteSlice9Test6::update(float dt)
|
|||
}
|
||||
|
||||
Rect rect((1-x)/2, (1-y)/2, x, y);
|
||||
_sprites[i]->setCapInsetsNormalized(rect);
|
||||
_sprites[i]->setCenterRectNormalized(rect);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5653,7 +5654,7 @@ SpriteSlice9Test7::SpriteSlice9Test7()
|
|||
s1->setPosition(s.width/2-s.width/3, s.height/2);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setContentSize(Size(s.width/3, s.height));
|
||||
s1->setCapInsetsNormalized(Rect(0,0,0.5,0.5));
|
||||
s1->setCenterRectNormalized(Rect(0,0,0.5,0.5));
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
auto s2 = Sprite::create("Images/grossinis_heads.png");
|
||||
|
@ -5662,7 +5663,7 @@ SpriteSlice9Test7::SpriteSlice9Test7()
|
|||
s2->setPosition(s.width*2/4, s.height/2);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setContentSize(Size(s.width/3, s.height));
|
||||
s2->setCapInsetsNormalized(Rect(0.25,0.25,0.5,0.5));
|
||||
s2->setCenterRectNormalized(Rect(0.25,0.25,0.5,0.5));
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
auto s3 = Sprite::create("Images/grossinis_heads.png");
|
||||
|
@ -5671,7 +5672,7 @@ SpriteSlice9Test7::SpriteSlice9Test7()
|
|||
s3->setPosition(s.width/2+s.width/3, s.height/2);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setContentSize(Size(s.width/3, s.height));
|
||||
s3->setCapInsetsNormalized(Rect(0.5,0.5,0.5,0.5));
|
||||
s3->setCenterRectNormalized(Rect(0.5,0.5,0.5,0.5));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
@ -5687,29 +5688,63 @@ SpriteSlice9Test8::SpriteSlice9Test8()
|
|||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("animations/grossini.plist");
|
||||
|
||||
|
||||
//
|
||||
// flip BEFORE contentSize
|
||||
//
|
||||
auto s1 = Sprite::createWithSpriteFrameName("grossinis_sister1.png");
|
||||
addChild(s1);
|
||||
s1->setPosition(s.width/2-s.width/3, s.height/2);
|
||||
s1->setPosition(s.width/2-s.width/3, s.height*2/3);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s1->setContentSize(s1->getContentSize()*2);
|
||||
s1->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s1->setFlippedX(true);
|
||||
s1->setContentSize(s1->getContentSize()*2);
|
||||
|
||||
auto s2 = Sprite::createWithSpriteFrameName("grossini.png");
|
||||
addChild(s2);
|
||||
s2->setPosition(s.width*2/4, s.height/2);
|
||||
s2->setPosition(s.width*2/4, s.height*2/3);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s2->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s2->setFlippedX(true);
|
||||
s2->setFlippedY(true);
|
||||
s2->setContentSize(s2->getContentSize()*2);
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
auto s3 = Sprite::createWithSpriteFrameName("grossinis_sister2.png");
|
||||
addChild(s3);
|
||||
s3->setPosition(s.width/2+s.width/3, s.height/2);
|
||||
s3->setPosition(s.width/2+s.width/3, s.height*2/3);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s3->setContentSize(s3->getContentSize()*2);
|
||||
s3->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s3->setFlippedY(true);
|
||||
s3->setContentSize(s3->getContentSize()*2);
|
||||
|
||||
//
|
||||
// flip AFTER contentSize
|
||||
//
|
||||
auto s4 = Sprite::createWithSpriteFrameName("grossinis_sister1.png");
|
||||
addChild(s4);
|
||||
s4->setPosition(s.width/2-s.width/3, s.height*1/3);
|
||||
s4->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s4->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s4->setFlippedX(true);
|
||||
s4->setContentSize(s4->getContentSize()*2);
|
||||
|
||||
auto s5 = Sprite::createWithSpriteFrameName("grossini.png");
|
||||
addChild(s5);
|
||||
s5->setPosition(s.width*2/4, s.height*1/3);
|
||||
s5->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s5->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s5->setFlippedX(true);
|
||||
s5->setFlippedY(true);
|
||||
s5->setContentSize(s5->getContentSize()*2);
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
auto s6 = Sprite::createWithSpriteFrameName("grossinis_sister2.png");
|
||||
addChild(s6);
|
||||
s6->setPosition(s.width/2+s.width/3, s.height*1/3);
|
||||
s6->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s6->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s6->setFlippedY(true);
|
||||
s6->setContentSize(s6->getContentSize()*2);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
|
@ -5721,6 +5756,73 @@ SpriteSlice9Test9::SpriteSlice9Test9()
|
|||
{
|
||||
Size s = Director::getInstance()->getVisibleSize();
|
||||
|
||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("animations/grossini_family.plist");
|
||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("animations/grossini.plist");
|
||||
|
||||
|
||||
// flipped BEFORE content size
|
||||
auto s1 = Sprite::createWithSpriteFrameName("grossinis_sister1.png");
|
||||
addChild(s1);
|
||||
s1->setPosition(s.width/2-s.width/3, s.height*2/3);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setCenterRectNormalized(Rect(2/3.f, 2/3.f, 1/3.f, 1/3.f));
|
||||
s1->setFlippedX(true);
|
||||
s1->setContentSize(s1->getContentSize()*2);
|
||||
|
||||
auto s2 = Sprite::createWithSpriteFrameName("grossini.png");
|
||||
addChild(s2);
|
||||
s2->setPosition(s.width*2/4, s.height*2/3);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 2/3.f, 2/3.f));
|
||||
s2->setFlippedX(true);
|
||||
s2->setFlippedY(true);
|
||||
s2->setContentSize(s2->getContentSize()*2);
|
||||
|
||||
auto s3 = Sprite::createWithSpriteFrameName("grossinis_sister2.png");
|
||||
addChild(s3);
|
||||
s3->setPosition(s.width/2+s.width/3, s.height*2/3);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setCenterRectNormalized(Rect(0.1f, 0.1f, 0.8f, 0.8f));
|
||||
s3->setFlippedY(true);
|
||||
s3->setContentSize(s3->getContentSize()*2);
|
||||
|
||||
|
||||
// flipped AFTER content size
|
||||
auto s4 = Sprite::createWithSpriteFrameName("grossinis_sister1.png");
|
||||
addChild(s4);
|
||||
s4->setPosition(s.width/2-s.width/3, s.height*1/3);
|
||||
s4->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s4->setCenterRectNormalized(Rect(2/3.f, 2/3.f, 1/3.f, 1/3.f));
|
||||
s4->setContentSize(s4->getContentSize()*2);
|
||||
s4->setFlippedX(true);
|
||||
|
||||
auto s5 = Sprite::createWithSpriteFrameName("grossini.png");
|
||||
addChild(s5);
|
||||
s5->setPosition(s.width*2/4, s.height*1/3);
|
||||
s5->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s5->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 2/3.f, 2/3.f));
|
||||
s5->setContentSize(s5->getContentSize()*2);
|
||||
s5->setFlippedX(true);
|
||||
s5->setFlippedY(true);
|
||||
|
||||
auto s6 = Sprite::createWithSpriteFrameName("grossinis_sister2.png");
|
||||
addChild(s6);
|
||||
s6->setPosition(s.width/2+s.width/3, s.height*1/3);
|
||||
s6->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s6->setCenterRectNormalized(Rect(0.1f, 0.1f, 0.8f, 0.8f));
|
||||
s6->setContentSize(s6->getContentSize()*2);
|
||||
s6->setFlippedY(true);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
//
|
||||
// Slice9 Test #10
|
||||
//
|
||||
//------------------------------------------------------------------
|
||||
SpriteSlice9Test10::SpriteSlice9Test10()
|
||||
{
|
||||
Size s = Director::getInstance()->getVisibleSize();
|
||||
|
||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
|
||||
|
||||
|
||||
|
@ -5728,7 +5830,7 @@ SpriteSlice9Test9::SpriteSlice9Test9()
|
|||
addChild(s1);
|
||||
s1->setPosition(s.width/2-s.width/3, s.height/2);
|
||||
s1->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s1->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s1->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s1->setContentSize(s1->getContentSize()*1.5);
|
||||
s1->setFlippedX(true);
|
||||
|
||||
|
@ -5736,7 +5838,7 @@ SpriteSlice9Test9::SpriteSlice9Test9()
|
|||
addChild(s2);
|
||||
s2->setPosition(s.width*2/4, s.height/2);
|
||||
s2->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s2->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s2->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s2->setContentSize(s2->getContentSize()*1.5);
|
||||
|
||||
//Create reference sprite that's rotating based on there anchor point
|
||||
|
@ -5744,8 +5846,7 @@ SpriteSlice9Test9::SpriteSlice9Test9()
|
|||
addChild(s3);
|
||||
s3->setPosition(s.width/2+s.width/3, s.height/2);
|
||||
s3->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
s3->setCapInsetsNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s3->setCenterRectNormalized(Rect(1/3.f, 1/3.f, 1/3.f, 1/3.f));
|
||||
s3->setContentSize(s3->getContentSize()*1.5);
|
||||
s3->setFlippedY(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -802,15 +802,6 @@ protected:
|
|||
cocos2d::Vec3 rotation;
|
||||
};
|
||||
|
||||
class SpriteGetSpriteFrameTest : public SpriteTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(SpriteGetSpriteFrameTest);
|
||||
SpriteGetSpriteFrameTest();
|
||||
virtual std::string title() const override { return "getSpriteFrameTest()"; };
|
||||
virtual std::string subtitle() const override { return "Testing it"; }
|
||||
};
|
||||
|
||||
class SpriteSlice9Test1 : public SpriteTestDemo
|
||||
{
|
||||
public:
|
||||
|
@ -883,7 +874,7 @@ public:
|
|||
CREATE_FUNC(SpriteSlice9Test7);
|
||||
SpriteSlice9Test7();
|
||||
virtual std::string title() const override { return "Slice 9 Test #7"; };
|
||||
virtual std::string subtitle() const override { return "Testing offset"; }
|
||||
virtual std::string subtitle() const override { return "Offset"; }
|
||||
};
|
||||
|
||||
class SpriteSlice9Test8 : public SpriteTestDemo
|
||||
|
@ -892,7 +883,7 @@ public:
|
|||
CREATE_FUNC(SpriteSlice9Test8);
|
||||
SpriteSlice9Test8();
|
||||
virtual std::string title() const override { return "Slice 9 Test #8"; };
|
||||
virtual std::string subtitle() const override { return "Testing flipX, flipY"; }
|
||||
virtual std::string subtitle() const override { return "Flipped sprites"; }
|
||||
};
|
||||
|
||||
class SpriteSlice9Test9 : public SpriteTestDemo
|
||||
|
@ -901,7 +892,27 @@ public:
|
|||
CREATE_FUNC(SpriteSlice9Test9);
|
||||
SpriteSlice9Test9();
|
||||
virtual std::string title() const override { return "Slice 9 Test #9"; };
|
||||
virtual std::string subtitle() const override { return "Testing rotated frames"; }
|
||||
virtual std::string subtitle() const override { return "Flipped sprites #2"; }
|
||||
};
|
||||
|
||||
|
||||
class SpriteSlice9Test10 : public SpriteTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(SpriteSlice9Test10);
|
||||
SpriteSlice9Test10();
|
||||
virtual std::string title() const override { return "Slice 10 Test #9"; };
|
||||
virtual std::string subtitle() const override { return "Rotated Sprites"; }
|
||||
};
|
||||
|
||||
|
||||
class SpriteGetSpriteFrameTest : public SpriteTestDemo
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(SpriteGetSpriteFrameTest);
|
||||
SpriteGetSpriteFrameTest();
|
||||
virtual std::string title() const override { return "Sprite::getSpriteFrame(*)"; };
|
||||
virtual std::string subtitle() const override { return "setting after getting should be the same"; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue