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

Refactor isRenderable detection #479

Merged
merged 3 commits into from
Jan 10, 2025
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
188 changes: 146 additions & 42 deletions src/core/CoreNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,73 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { CoreNode, type CoreNodeProps, UpdateType } from './CoreNode.js';
import { Stage } from './Stage.js';
import { mock } from 'vitest-mock-extended';
import { type TextureOptions } from './CoreTextureManager.js';
import { type BaseShaderController } from '../main-api/ShaderController';
import { createBound } from './lib/utils.js';
import { ImageTexture } from './textures/ImageTexture.js';

describe('set color()', () => {
const defaultProps: CoreNodeProps = {
alpha: 0,
autosize: false,
clipping: false,
color: 0,
colorBl: 0,
colorBottom: 0,
colorBr: 0,
colorLeft: 0,
colorRight: 0,
colorTl: 0,
colorTop: 0,
colorTr: 0,
height: 0,
mount: 0,
mountX: 0,
mountY: 0,
parent: null,
pivot: 0,
pivotX: 0,
pivotY: 0,
rotation: 0,
rtt: false,
scale: 0,
scaleX: 0,
scaleY: 0,
shader: mock<BaseShaderController>(),
src: '',
texture: null,
textureOptions: {} as TextureOptions,
width: 0,
x: 0,
y: 0,
zIndex: 0,
zIndexLocked: 0,
preventCleanup: false,
strictBounds: false,
};
const defaultProps: CoreNodeProps = {
alpha: 0,
autosize: false,
clipping: false,
color: 0,
colorBl: 0,
colorBottom: 0,
colorBr: 0,
colorLeft: 0,
colorRight: 0,
colorTl: 0,
colorTop: 0,
colorTr: 0,
height: 0,
mount: 0,
mountX: 0,
mountY: 0,
parent: null,
pivot: 0,
pivotX: 0,
pivotY: 0,
rotation: 0,
rtt: false,
scale: 0,
scaleX: 0,
scaleY: 0,
shader: mock<BaseShaderController>(),
src: '',
texture: null,
textureOptions: {} as TextureOptions,
width: 0,
x: 0,
y: 0,
zIndex: 0,
zIndexLocked: 0,
preventCleanup: false,
strictBounds: false,
};

const clippingRect = {
x: 0,
y: 0,
width: 200,
height: 200,
valid: false,
};

const stage = mock<Stage>({
strictBound: createBound(0, 0, 200, 200),
preloadBound: createBound(0, 0, 200, 200),
defaultTexture: {
state: 'loaded',
},
});

describe('set color()', () => {
it('should set all color subcomponents.', () => {
const node = new CoreNode(mock<Stage>(), defaultProps);
const node = new CoreNode(stage, defaultProps);
node.colorBl = 0x99aabbff;
node.colorBr = 0xaabbccff;
node.colorTl = 0xbbcceeff;
Expand All @@ -85,11 +103,97 @@ describe('set color()', () => {
});

it('should set update type.', () => {
const node = new CoreNode(mock<Stage>(), defaultProps);
const node = new CoreNode(stage, defaultProps);
node.updateType = 0;

node.color = 0xffffffff;

expect(node.updateType).toBe(UpdateType.PremultipliedColors);
});
});

describe('isRenderable checks', () => {
it('should return false if node is not renderable', () => {
const node = new CoreNode(stage, defaultProps);
expect(node.isRenderable).toBe(false);
});

it('visible node that is a color texture', () => {
const node = new CoreNode(stage, defaultProps);
node.alpha = 1;
node.x = 0;
node.y = 0;
node.width = 100;
node.height = 100;
node.color = 0xffffffff;

node.update(0, clippingRect);
expect(node.isRenderable).toBe(true);
});

it('visible node that is a texture', () => {
const node = new CoreNode(stage, defaultProps);
node.alpha = 1;
node.x = 0;
node.y = 0;
node.width = 100;
node.height = 100;
node.texture = mock<ImageTexture>({
state: 'initial',
});

node.update(0, clippingRect);
expect(node.isRenderable).toBe(false);

node.texture.state = 'loaded';
node.setUpdateType(UpdateType.IsRenderable);
node.update(1, clippingRect);

expect(node.isRenderable).toBe(true);
});

it('a node with a texture with alpha 0 should not be renderable', () => {
const node = new CoreNode(stage, defaultProps);
node.alpha = 0;
node.x = 0;
node.y = 0;
node.width = 100;
node.height = 100;
node.texture = mock<ImageTexture>({
state: 'loaded',
});

node.update(0, clippingRect);
expect(node.isRenderable).toBe(false);
});

it('a node with a texture that is OutOfBounds should not be renderable', () => {
const node = new CoreNode(stage, defaultProps);
node.alpha = 1;
node.x = 300;
node.y = 300;
node.width = 100;
node.height = 100;
node.texture = mock<ImageTexture>({
state: 'loaded',
});

node.update(0, clippingRect);
expect(node.isRenderable).toBe(false);
});

it('a node with a freed texture should not be renderable', () => {
const node = new CoreNode(stage, defaultProps);
node.alpha = 1;
node.x = 0;
node.y = 0;
node.width = 100;
node.height = 100;
node.texture = mock<ImageTexture>({
state: 'freed',
});

node.update(0, clippingRect);
expect(node.isRenderable).toBe(false);
});
});
Loading
Loading