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

Support for fixed sprite content size regardless of new texture content #1896

Closed
rh101 opened this issue May 15, 2024 · 4 comments · Fixed by #1897
Closed

Support for fixed sprite content size regardless of new texture content #1896

rh101 opened this issue May 15, 2024 · 4 comments · Fixed by #1897

Comments

@rh101
Copy link
Contributor

rh101 commented May 15, 2024

This isn't really a bug, since the current implementation is correct. Sprite frames can have different sizes, and a sprite should change size depending on the texture size of the frame.

It would be nice to have the ability to lock the sprite size so that it ignores the new frame texture size. This would typically be used with stretch mode enabled.

Use grossini.plist sprite sheet and texture from cpp-tests.

auto spriteCache = SpriteFrameCache::getInstance();
spriteCache->addSpriteFramesWithFile("grossini.plist");

ax::Vector<ax::SpriteFrame*> animFrames;
for (int i = 1; i < 15; i++)
{
    auto frameName = fmt::format("grossini_dance_{:02}.png", i);
    animFrames.pushBack(spriteCache->getSpriteFrameByName(frameName));
}

auto* animation = ax::Animation::createWithSpriteFrames(animFrames, 1 / 25.f, -1);
auto* animationAction = ax::Animate::create(animation);
auto animatingSprite  = ax::Sprite::createWithSpriteFrame(animFrames.front());
animatingSprite->setPosition(visibleSize / 2);
animatingSprite->setStretchEnabled(true);
animatingSprite->setContentSize(ax::Vec2(200, 200));
animatingSprite->runAction(animationAction);
this->addChild(animatingSprite);

Expected output when animating (size of 200, 200 due to stretch factor):
image

Actual output while animating:
image

@rh101
Copy link
Contributor Author

rh101 commented May 15, 2024

Not sure about the best way to add this feature.

Can add a new FixedSizeAnimate action is added that always sets the same content size after each new frame on a sprite, but that means calculations inside the Sprite will be done twice, once in the chain called from setSpriteFrame -> setTextyureRect etc., and then another by the extra setContentSize. They all end up calling updateStretchFactor(); and updatePoly();, so they'll be called twice for every frame.

Another option would be to add something to Sprite to indicate that the content size is fixed, and should not change on new textures/frames (as long as it's not a content size of 0, 0).

@rh101 rh101 changed the title Sprite content size should not change on newly applied frame if stretch mode is enabled Support for fixed sprite content size regardless of new texture content May 15, 2024
@rh101
Copy link
Contributor Author

rh101 commented May 15, 2024

With PR #1897 a simple call to sprite->setContentSizeDynamic(false); sprite->setAutoSize(false); would enable the functionality described above.

@halx99
Copy link
Collaborator

halx99 commented May 15, 2024

API: change to setAutoSize?

@rh101
Copy link
Contributor Author

rh101 commented May 16, 2024

API: change to setAutoSize?

Yes! That makes way more sense. Will update the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants