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

Fix: issue with local bounds cache not being invalidated when a child is removed #10488

Merged
merged 1 commit into from
Apr 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/scene/container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,9 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE
/**
* A value that increments each time the container is modified
* the first 12 bits represent the container changes (eg transform, alpha, visible etc)
* the second 12 bits represent the view changes (eg texture swap, geometry change etc)
* the second 12 bits represent:
* - for view changes (eg texture swap, geometry change etc)
* - containers changes (eg children added, removed etc)
*
* view container
* [000000000000][00000000000]
Expand Down Expand Up @@ -658,6 +660,8 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE
this.emit('childAdded', child, this, this.children.length - 1);
child.emit('added', this);

this._didChangeId += 1 << 12;

if (child._zIndex !== 0)
{
child.depthOfChildModified();
Expand Down Expand Up @@ -691,6 +695,8 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE

if (index > -1)
{
this._didChangeId += 1 << 12;

this.children.splice(index, 1);

if (this.renderGroup)
Expand Down
50 changes: 50 additions & 0 deletions tests/scene/getLocalBounds.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,54 @@ describe('getLocalBounds', () =>

expect(child._localBoundsCacheData.didChange).toEqual(true);
});

it('should measure correctly if a child has been added and removed', async () =>
{
const container = new Container({ label: 'container' });

const bounds1 = container.getLocalBounds();

expect(bounds1).toMatchObject({ minX: 0, minY: 0, maxX: 0, maxY: 0 });

const child = new DummyView({ label: 'child' });

container.addChild(child);

const bounds2 = container.getLocalBounds();

expect(bounds2).toMatchObject({ minX: 0, minY: 0, maxX: 100, maxY: 100 });

container.removeChild(child);

const bounds3 = container.getLocalBounds();

expect(bounds3).toMatchObject({ minX: 0, minY: 0, maxX: 0, maxY: 0 });
});

it('should measure correctly if a nested child has been added and removed', async () =>
{
const container = new Container({ label: 'container' });

const container2 = new Container({ label: 'container2' });

container.addChild(container2);

const bounds1 = container.getLocalBounds();

expect(bounds1).toMatchObject({ minX: 0, minY: 0, maxX: 0, maxY: 0 });

const child = new DummyView({ label: 'child' });

container2.addChild(child);

const bounds2 = container.getLocalBounds();

expect(bounds2).toMatchObject({ minX: 0, minY: 0, maxX: 100, maxY: 100 });

container2.removeChild(child);

const bounds3 = container.getLocalBounds();

expect(bounds3).toMatchObject({ minX: 0, minY: 0, maxX: 0, maxY: 0 });
});
});