Skip to content

Commit

Permalink
Add support for ensuring sprite content size does not change on new t…
Browse files Browse the repository at this point in the history
…exture (#1897)

* Add support for ensuring sprite content size does not change on new texture

* Use better method name for enabling sprite dynamic sizing
  • Loading branch information
rh101 authored May 16, 2024
1 parent 99866d9 commit 49bd2bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/2d/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ void Sprite::setTextureRect(const Rect& rect, bool rotated, const Vec2& untrimme
{
_rectRotated = rotated;

Node::setContentSize(untrimmedSize);
if (_autoSizeEnabled || _contentSize == Vec2::ZERO)
{
Node::setContentSize(untrimmedSize);
}
_originalContentSize = untrimmedSize;

setVertexRect(rect);
Expand Down
10 changes: 10 additions & 0 deletions core/2d/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ class AX_DLL Sprite : public Node, public TextureProtocol

void setAutoUpdatePS(bool bVal) { _autoUpdatePS = bVal; }

/**
* Indicate if the sprite content size can change if new textures are applied to
* the sprite.
*
* @param enabled True if the sprite can change size on new frames/textures
*/
void setAutoSize(bool enabled) { _autoSizeEnabled = enabled; }

protected:
virtual void updateColor() override;
virtual void setTextureCoords(const Rect& rect);
Expand Down Expand Up @@ -703,6 +711,8 @@ class AX_DLL Sprite : public Node, public TextureProtocol
bool _stretchEnabled = true;
bool _autoUpdatePS = true;

bool _autoSizeEnabled = true;

private:
AX_DISALLOW_COPY_AND_ASSIGN(Sprite);
};
Expand Down

0 comments on commit 49bd2bc

Please sign in to comment.