Skip to content

Commit

Permalink
fix: simplify masking (#10531)
Browse files Browse the repository at this point in the history
* simplify masking

* tidy
  • Loading branch information
GoodBoyDigital committed May 14, 2024
1 parent c31ba5a commit cb418be
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/events/EventBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ export class EventBoundary
* Checks whether the container or any of its children cannot pass the hit test at all.
*
* {@link EventBoundary}'s implementation uses the {@link Container.hitArea hitArea}
* and {@link Container._mask} for pruning.
* and {@link Container._maskEffect} for pruning.
* @param container - The container to prune.
* @param location - The location to test for overlap.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/rendering/mask/MaskEffectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface MaskConversionTest
maskClass: new (item: any) => Effect & PoolItem;
}

export type MaskEffect = {mask: unknown} & Effect;

/**
* A class that manages the conversion of masks to mask effects.
* @memberof rendering
Expand Down Expand Up @@ -44,7 +46,7 @@ export class MaskEffectManagerClass
this._tests.push(test);
}

public getMaskEffect(item: any): Effect
public getMaskEffect(item: any): MaskEffect
{
if (!this._initialized) this.init();

Expand All @@ -54,7 +56,7 @@ export class MaskEffectManagerClass

if (test.test(item))
{
return BigPool.get(test.maskClass as PoolItemConstructor<Effect & PoolItem>, item);
return BigPool.get(test.maskClass as PoolItemConstructor<MaskEffect & PoolItem>, item);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scene/container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ export class Container<C extends ContainerChild = ContainerChild> extends EventE

this.removeFromParent();
this.parent = null;
this._mask = null;
this._maskEffect = null;
this._filterEffect = null;
this.effects = null;
this._position = null;
Expand Down
29 changes: 13 additions & 16 deletions src/scene/container/container-mixins/effectsMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MaskEffectManager } from '../../../rendering/mask/MaskEffectManager';

import type { Filter } from '../../../filters/Filter';
import type { Rectangle } from '../../../maths/shapes/Rectangle';
import type { MaskEffect } from '../../../rendering/mask/MaskEffectManager';
import type { Container } from '../Container';
import type { Effect } from '../Effect';

Expand All @@ -13,16 +14,18 @@ export interface EffectsMixinConstructor
}
export interface EffectsMixin extends Required<EffectsMixinConstructor>
{
_mask?: {mask: unknown, effect: Effect};
_maskEffect?: MaskEffect;
_filterEffect?: FilterEffect,

filterArea?: Rectangle,
effects?: Effect[];

addEffect(effect: Effect): void;
removeEffect(effect: Effect): void;
}

export const effectsMixin: Partial<Container> = {
_mask: null,
_maskEffect: null,
_filterEffect: null,

/**
Expand Down Expand Up @@ -79,28 +82,22 @@ export const effectsMixin: Partial<Container> = {

set mask(value: number | Container | null)
{
this._mask ||= { mask: null, effect: null };
const effect = this._maskEffect;

if (this._mask.mask === value) return;
if (effect?.mask === value) return;

if (this._mask.effect)
if (effect)
{
this.removeEffect(this._mask.effect);

MaskEffectManager.returnMaskEffect(this._mask.effect);
this.removeEffect(effect);

this._mask.effect = null;
MaskEffectManager.returnMaskEffect(effect);
}

this._mask.mask = value;

if (value === null || value === undefined) return;

const effect = MaskEffectManager.getMaskEffect(value);

this._mask.effect = effect;
this._maskEffect = MaskEffectManager.getMaskEffect(value);

this.addEffect(effect);
this.addEffect(this._maskEffect);
},

/**
Expand All @@ -126,7 +123,7 @@ export const effectsMixin: Partial<Container> = {
*/
get mask(): unknown
{
return this._mask?.mask;
return this._maskEffect?.mask;
},

set filters(value: Filter | Filter[] | null | undefined)
Expand Down

0 comments on commit cb418be

Please sign in to comment.