Skip to content

Commit

Permalink
Merge branch 'dev' into video-frame-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
orange4glace authored Sep 19, 2023
2 parents d489448 + 10e3bea commit 3797e32
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 71 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pixi.js-monorepo",
"private": true,
"version": "7.3.0-rc.2",
"version": "7.3.0",
"workspaces": [
"bundles/*",
"packages/*",
Expand Down
4 changes: 2 additions & 2 deletions packages/assets/src/loader/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ export class Loader
{
const loadedAsset = await loadPromise.promise;

loadPromise.parser?.unload?.(loadedAsset, asset, this);

delete this.promiseCache[url];

loadPromise.parser?.unload?.(loadedAsset, asset, this);
}
});

Expand Down
24 changes: 21 additions & 3 deletions packages/assets/src/loader/parsers/textures/utils/createTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,34 @@ export function createTexture(base: BaseTexture, loader: Loader, url: string)
base.resource.internal = true;

const texture = new Texture(base);

// remove the promise from the loader and the url from the cache when the texture is destroyed
texture.baseTexture.once('destroyed', () =>
const unload = () =>
{
delete loader.promiseCache[url];

if (Cache.has(url))
{
Cache.remove(url);
}
};

// remove the promise from the loader and the url from the cache when the texture is destroyed
texture.baseTexture.once('destroyed', () =>
{
if (url in loader.promiseCache)
{
console.warn('[Assets] A BaseTexture managed by Assets was destroyed instead of unloaded! '
+ 'Use Assets.unload() instead of destroying the BaseTexture.');
unload();
}
});
texture.once('destroyed', () =>
{
if (!base.destroyed)
{
console.warn('[Assets] A Texture managed by Assets was destroyed instead of unloaded! '
+ 'Use Assets.unload() instead of destroying the Texture.');
unload();
}
});

return texture;
Expand Down
31 changes: 28 additions & 3 deletions packages/assets/test/assets.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('Assets', () =>
expect(loadTextures.config.preferWorkers).toBe(true);
});

it('should remove the asset from the cache when destroyed', async () =>
it('should remove the asset from the cache when the texture is destroyed', async () =>
{
await Assets.init({
basePath,
Expand All @@ -509,7 +509,7 @@ describe('Assets', () =>

expect(Assets.cache.has(url)).toBeTrue();

texture1.destroy(true);
texture1.destroy();

expect(Assets.cache.has(url)).toBeFalse();

Expand All @@ -518,7 +518,32 @@ describe('Assets', () =>
expect(Assets.cache.has(url)).toBeTrue();
expect(texture2).not.toBe(texture1);

texture2.destroy(true);
texture2.destroy();

expect(Assets.cache.has(url)).toBeFalse();
});

it('should remove the asset from the cache when the base texture is destroyed', async () =>
{
await Assets.init({
basePath,
});

const url = 'textures/bunny.png';
const texture1 = await Assets.load(url);

expect(Assets.cache.has(url)).toBeTrue();

texture1.baseTexture.destroy();

expect(Assets.cache.has(url)).toBeFalse();

const texture2 = await Assets.load(url);

expect(Assets.cache.has(url)).toBeTrue();
expect(texture2).not.toBe(texture1);

texture2.baseTexture.destroy();

expect(Assets.cache.has(url)).toBeFalse();
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/textures/BaseTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ export class BaseTexture<R extends Resource = Resource, RO = IAutoDetectOptions>

this.destroyed = true;
this.emit('destroyed', this);
this.removeAllListeners();
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/textures/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
/** This will let the renderer know if the texture is valid. If it's not then it cannot be rendered. */
public valid: boolean;

/**
* Has the texture been destroyed?
* @readonly
*/
public destroyed: boolean;

/**
* Does this Texture have any frame data assigned to it?
*
Expand Down Expand Up @@ -190,6 +196,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
this._frame = frame;
this.trim = trim;
this.valid = false;
this.destroyed = false;
this._uvs = DEFAULT_UVS;
this.uvMatrix = null;
this.orig = orig || frame;// new Rectangle(0, 0, 1, 1);
Expand Down Expand Up @@ -283,6 +290,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
/**
* Destroys this texture
* @param [destroyBase=false] - Whether to destroy the base texture as well
* @fires PIXI.Texture#destroyed
*/
destroy(destroyBase?: boolean): void
{
Expand Down Expand Up @@ -317,6 +325,10 @@ export class Texture<R extends Resource = Resource> extends EventEmitter

Texture.removeFromCache(this);
this.textureCacheIds = null;

this.destroyed = true;
this.emit('destroyed', this);
this.removeAllListeners();
}

/**
Expand Down
43 changes: 14 additions & 29 deletions packages/events/src/EventSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ export class EventSystem implements ISystem<EventSystemOptions>
if (!this.features.click) return;
this.rootBoundary.rootTarget = this.renderer.lastObjectRendered as DisplayObject;

// if we support touch events, then only use those for touch events, not pointer events
if (this.supportsTouchEvents && ['touch', 'pen'].includes((nativeEvent as PointerEvent).pointerType)) return;

const events = this.normalizeToPointerData(nativeEvent);

/*
Expand Down Expand Up @@ -388,9 +385,6 @@ export class EventSystem implements ISystem<EventSystemOptions>
if (!this.features.move) return;
this.rootBoundary.rootTarget = this.renderer.lastObjectRendered as DisplayObject;

// if we support touch events, then only use those for touch events, not pointer events
if (this.supportsTouchEvents && ['touch', 'pen'].includes((nativeEvent as PointerEvent).pointerType)) return;

EventsTicker.pointerMoved();

const normalizedEvents = this.normalizeToPointerData(nativeEvent);
Expand All @@ -414,9 +408,6 @@ export class EventSystem implements ISystem<EventSystemOptions>
if (!this.features.click) return;
this.rootBoundary.rootTarget = this.renderer.lastObjectRendered as DisplayObject;

// if we support touch events, then only use those for touch events, not pointer events
if (this.supportsTouchEvents && ['touch', 'pen'].includes((nativeEvent as PointerEvent).pointerType)) return;

let target = nativeEvent.target;

// if in shadow DOM use composedPath to access target
Expand Down Expand Up @@ -449,9 +440,6 @@ export class EventSystem implements ISystem<EventSystemOptions>
if (!this.features.click) return;
this.rootBoundary.rootTarget = this.renderer.lastObjectRendered as DisplayObject;

// if we support touch events, then only use those for touch events, not pointer events
if (this.supportsTouchEvents && (nativeEvent as PointerEvent).pointerType === 'touch') return;

const normalizedEvents = this.normalizeToPointerData(nativeEvent);

for (let i = 0, j = normalizedEvents.length; i < j; i++)
Expand Down Expand Up @@ -539,17 +527,14 @@ export class EventSystem implements ISystem<EventSystemOptions>
this.domElement.addEventListener('mouseout', this.onPointerOverOut, true);
this.domElement.addEventListener('mouseover', this.onPointerOverOut, true);
globalThis.addEventListener('mouseup', this.onPointerUp, true);
}

// Always look directly for touch events so that we can provide original data
// In a future version we should change this to being just a fallback and rely solely on
// PointerEvents whenever available
if (this.supportsTouchEvents)
{
this.domElement.addEventListener('touchstart', this.onPointerDown, true);
// this.domElement.addEventListener('touchcancel', this.onPointerCancel, true);
this.domElement.addEventListener('touchend', this.onPointerUp, true);
this.domElement.addEventListener('touchmove', this.onPointerMove, true);
if (this.supportsTouchEvents)
{
this.domElement.addEventListener('touchstart', this.onPointerDown, true);
// this.domElement.addEventListener('touchcancel', this.onPointerCancel, true);
this.domElement.addEventListener('touchend', this.onPointerUp, true);
this.domElement.addEventListener('touchmove', this.onPointerMove, true);
}
}

this.domElement.addEventListener('wheel', this.onWheel, {
Expand Down Expand Up @@ -598,14 +583,14 @@ export class EventSystem implements ISystem<EventSystemOptions>
this.domElement.removeEventListener('mouseout', this.onPointerOverOut, true);
this.domElement.removeEventListener('mouseover', this.onPointerOverOut, true);
globalThis.removeEventListener('mouseup', this.onPointerUp, true);
}

if (this.supportsTouchEvents)
{
this.domElement.removeEventListener('touchstart', this.onPointerDown, true);
// this.domElement.removeEventListener('touchcancel', this.onPointerCancel, true);
this.domElement.removeEventListener('touchend', this.onPointerUp, true);
this.domElement.removeEventListener('touchmove', this.onPointerMove, true);
if (this.supportsTouchEvents)
{
this.domElement.removeEventListener('touchstart', this.onPointerDown, true);
// this.domElement.removeEventListener('touchcancel', this.onPointerCancel, true);
this.domElement.removeEventListener('touchend', this.onPointerUp, true);
this.domElement.removeEventListener('touchmove', this.onPointerMove, true);
}
}

this.domElement.removeEventListener('wheel', this.onWheel, true);
Expand Down
2 changes: 1 addition & 1 deletion packages/text-bitmap/src/formats/TextFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TextFormat
* @param data
* @returns - True if resource could be treated as font data, false otherwise.
*/
static test(data: unknown): boolean
static test(data: string | XMLDocument | BitmapFontData): boolean
{
return typeof data === 'string' && data.startsWith('info face=');
}
Expand Down
5 changes: 3 additions & 2 deletions packages/text-bitmap/src/formats/XMLFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export class XMLFormat
* @param data
* @returns - True if resource could be treated as font data, false otherwise.
*/
static test(data: unknown): boolean
static test(data: string | XMLDocument | BitmapFontData): boolean
{
const xml = data as Document;

return 'getElementsByTagName' in xml
return typeof data !== 'string'
&& 'getElementsByTagName' in data
&& xml.getElementsByTagName('page').length
&& xml.getElementsByTagName('info')[0].getAttribute('face') !== null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/text-bitmap/src/formats/XMLStringFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class XMLStringFormat
* @param data
* @returns - True if resource could be treated as font data, false otherwise.
*/
static test(data: unknown): boolean
static test(data: string | XMLDocument | BitmapFontData): boolean
{
if (typeof data === 'string' && data.includes('<font>'))
{
Expand Down
4 changes: 3 additions & 1 deletion packages/text-bitmap/src/formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { TextFormat } from './TextFormat';
import { XMLFormat } from './XMLFormat';
import { XMLStringFormat } from './XMLStringFormat';

import type { BitmapFontData } from '../BitmapFontData';

// Registered formats, maybe make this extensible in the future?
const formats = [
TextFormat,
Expand All @@ -15,7 +17,7 @@ const formats = [
* @param {any} data - Data to detect format
* @returns {any} Format or null
*/
export function autoDetectFormat(data: unknown): typeof formats[number] | null
export function autoDetectFormat(data: string | XMLDocument | BitmapFontData): typeof formats[number] | null
{
for (let i = 0; i < formats.length; i++)
{
Expand Down
Loading

0 comments on commit 3797e32

Please sign in to comment.