Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading