Files
feuerwerk/tests/viewport/render-layers.test.ts
T
adminandClaude Opus 5.5 3a52f2290f
game-maker / gate (push) Successful in 3m36s
game-maker / suite (push) Failing after 14m30s
Mix sounds through buses and a master (#325)
Until now every sound went straight to the speakers. There was no
point where all of them came together, so nothing could turn the
music down against the effects, and a compressor or a shared echo
over everything had nowhere to sit. The bus technique of #346 was
there already, only the end of the chain was missing.

- Bus asset (Audio): Volume, Sound FX and Into. Into is empty for
  the master and never offers the bus itself nor a bus that already
  goes into it. The list is worked out when the menu opens, so a
  change made meanwhile counts. A loop that is in a project anyway
  sends the buses on it into the master.
- The master is no asset. Its volume and sound FX are two new rows
  under Master in Project settings, one undo step each. A project
  written before has 1 and none, which is how it sounded.
- Sound, instrument, pattern and song have a row Bus, empty for the
  master. Play sound, Play note, Play pattern and Play song have an
  input bus: From the asset, Master or a bus. Inside a song or a
  pattern only the bus of the song or pattern counts.
- Set bus volume (bus, volume, time, easing) and Bus volume. An
  empty bus is the master. The volume set holds for the whole run,
  across scenes, over that of the asset. GameRun resets it at start
  and end. Set sound FX parameters without a voice now reaches the
  sound FX of the bus picked in its new bus input.
- SoundLibrary: the master is the bus "mix", each bus "mix:<id>".
  Every bus, the playing masters of #346 included, ends there, and
  only the master is heard. setMix sets them up once a context
  exists, so an editor nobody listened to opens no audio device. The
  same mix again changes nothing, which lets the editor hand it over
  on every change of the project.
- The tabs play as the game does: Sound, Sound FX, Instrument,
  Pattern and Song tab through the bus of their asset and the master.
- Pause game does nothing to the buses (the user's answer 4b). A
  game that wants quiet in its pause menu sets a bus volume itself.
- Streaming long music files is #349, split off as asked.

Finding on the way: the Into menu first worked from a list made when
the inspector row was built. After another bus was sent into the
open one, e2e 34 saw it still offered. It is now asked on open.

The SoundLibrary tests expected voices to end at the destination.
They now end in the master, which adds its input, level and pan to
every wiring and two gains to every list. Those expectations are
updated, nothing else in them changed.

Tests:
- Unit buses (new): 15 green. Ten counter-checks, each red on its
  own: loop check, bus picked, reset at run start, bus of Set sound
  FX parameters, master volume in the settings, and in SoundLibrary
  default into the master, voices into the master, run volume over
  the mix, no context before a play, a playing on its bus.
- Unit sound-library: 90 green (5 new). Node catalog: 416 green
  (9 new cases).
- e2e 34 (melektaus-atx): ALL PASSED, 75 checks (3 new). The voice
  of the Sound tab was heard on mix:<bus>, that bus into the other,
  that one at 0.5 into the master. Master volume 0.6 via the dialog,
  undo back to 1.
- Gate (melektaus-atx): CHECK EXIT 0, 262 files, 4072 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 19:13:32 +02:00

260 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { CreateAssetCommand } from '../../src/core/commands/assetCommands';
import { SetLayerNamesCommand } from '../../src/core/commands/projectCommands';
import { PlaceAssetCommand, SetOverrideCommand } from '../../src/core/commands/sceneObjectCommands';
import type { GraphNode } from '../../src/core/graph';
import { OBJECT_TYPE_PARAM } from '../../src/core/graph/propertyPorts';
import type { PropertyBag } from '../../src/core/model/Properties';
import {
cameraSees,
INHERIT_LAYER,
OVERLAY_KEY,
ownRenderLayer,
RENDER_LAYER_KEY,
renderLayersOf,
SEES_KEY,
} from '../../src/core/model/renderLayers';
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
import { settingsChanges } from '../../src/editor/ProjectSettingsDialog';
import {
cameraOfItems,
cameraOfWorld,
overlaysOfItems,
overlaysOfWorld,
} from '../../src/viewport/activeCamera';
import { collectHolderItems, collectWorldItems } from '../../src/viewport/items';
import { cameraView, overlayView } from '../../src/viewport/view';
import { type GraphEditor, graphEditor, makeBehavior, makeSpriteScene, place, startWorld } from '../helpers';
function sceneWithCamera() {
const fixture = makeSpriteScene();
const camera = new CreateAssetCommand('camera', 'Cam');
fixture.stack.execute(camera);
const container = new CreateAssetCommand('container', 'Hud');
fixture.stack.execute(container);
return {
...fixture,
cameraId: camera.asset.id,
containerId: container.asset.id,
set: (objectId: string, key: string, value: number | boolean) =>
fixture.stack.execute(new SetOverrideCommand(fixture.sceneId, objectId, key, value)),
items: () => collectHolderItems(fixture.project, fixture.sceneOf(), 0).items,
};
}
describe('render layers (#302)', () => {
it('reads the names like the collision layers, "Default" for a project from before', () => {
expect(renderLayersOf(null)).toEqual(['Default']);
expect(renderLayersOf({ renderLayers: ['World', '', 'UI'] })).toEqual(['World', 'Layer 1', 'UI']);
const f = sceneWithCamera();
const stored = serializeProject(f.project) as Record<string, unknown>;
delete stored.renderLayers;
expect(deserializeProject(stored).renderLayers).toEqual(['Default']);
});
it('reads an own choice, and anything unreadable as none', () => {
for (const value of [undefined, null, '', ' ', 'x', -1, -5, 16, true])
expect(ownRenderLayer(value)).toBe(INHERIT_LAYER);
expect(ownRenderLayer(0)).toBe(0);
expect(ownRenderLayer(3)).toBe(3);
expect(ownRenderLayer('2')).toBe(2);
});
it('puts an object without a choice on its parent’s layer, and the top on layer 0', () => {
const f = sceneWithCamera();
const hud = place(f, f.containerId, { x: 0, y: 0 });
const inside = place(f, f.spriteId, { x: 0, y: 0 }, hud);
const own = place(f, f.spriteId, { x: 0, y: 0 }, hud);
const loose = place(f, f.spriteId, { x: 0, y: 0 });
f.set(hud, RENDER_LAYER_KEY, 1);
f.set(own, RENDER_LAYER_KEY, 2);
const layerOf = (id: string) => f.items().find((item) => item.id === id)!.renderLayer;
expect([layerOf(hud), layerOf(inside), layerOf(own), layerOf(loose)]).toEqual([1, 1, 2, 0]);
const { world } = startWorld(f);
const running = (id: string) =>
collectWorldItems(world).items.find((item) => item.id === id)!.renderLayer;
expect([running(inside), running(own), running(loose)]).toEqual([1, 2, 0]);
});
it('lets a camera without `Sees` see every layer, and one with it only those', () => {
expect(cameraSees({}, 0)).toBe(true);
expect(cameraSees({}, 15)).toBe(true);
expect(cameraSees({ [SEES_KEY]: 0b10 }, 0)).toBe(false);
expect(cameraSees({ [SEES_KEY]: 0b10 }, 1)).toBe(true);
});
it('keeps the first camera without Overlay as the main one, the overlays apart in hierarchy order', () => {
const f = sceneWithCamera();
const over = place(f, f.cameraId, { x: 0, y: 0 });
const main = place(f, f.cameraId, { x: 100, y: 0 });
const plain = place(f, f.cameraId, { x: 200, y: 0 });
const over2 = place(f, f.cameraId, { x: 300, y: 0 });
f.set(over, OVERLAY_KEY, true);
f.set(over2, OVERLAY_KEY, true);
const { world } = startWorld(f);
expect(world.activeCamera()?.objectId).toBe(main);
expect(world.overlayCameras().map((c) => c.objectId)).toEqual([over, over2]);
expect(overlaysOfWorld(world).map((c) => c.x)).toEqual([0, 300]);
expect(cameraOfItems(f.items())?.id).toBe(main);
expect(overlaysOfItems(f.items()).map((c) => c.id)).toEqual([over, over2]);
// Two plain cameras on draw as before: through the first only, the second is no overlay.
expect(world.overlayCameras().map((c) => c.objectId)).not.toContain(plain);
f.set(over2, 'enabled', false);
world.syncFromModel();
expect(world.overlayCameras().map((c) => c.objectId)).toEqual([over]);
});
it('fits an overlay into the main picture, and gives the same camera the very same view', () => {
const props = { width: 640, height: 360, zoom: 1 };
const main = cameraView({ x: 0, y: 0, props }, 1280, 720, 2);
expect(overlayView({ x: 0, y: 0, props }, main, { w: 640, h: 360 })).toEqual(main);
// Narrower and zoomed: the frame is 160x180 world units, fitted to the 720 px height.
const narrow = overlayView({ x: 50, y: 7, props: { width: 320, height: 360, zoom: 2 } }, main, {
w: 640,
h: 360,
});
expect(narrow).toEqual({ x: 50, y: 7, scale: 4, rect: { x: 320, y: 0, w: 640, h: 720 } });
});
it('keeps the render layers an undo step and a change of the settings of their own', () => {
const f = sceneWithCamera();
f.stack.execute(new SetLayerNamesCommand('renderLayers', ['World', 'UI']));
expect(f.project.renderLayers).toEqual(['World', 'UI']);
expect(f.project.collisionLayers).toEqual(['Default']);
f.stack.undo();
expect(f.project.renderLayers).toEqual(['Default']);
const before = {
name: 'P',
playFrom: 'start' as const,
whilePlaying: 'live' as const,
gameLinkZoom: 'auto' as const,
collisionLayers: ['Default'],
renderLayers: ['Default'],
symbolScale: 1,
masterVolume: 1,
masterFx: '',
};
expect(settingsChanges(before, { ...before, renderLayers: ['Default'] })).toEqual({});
expect(settingsChanges(before, { ...before, renderLayers: ['Default', 'UI'] })).toEqual({
renderLayers: ['Default', 'UI'],
});
});
});
/**
* A coin on `Default` and a HUD container with a sprite in it, a main camera that sees `Default` and
* an overlay that sees `UI`. Behaviors change the layers with Properties nodes, the way a graph does
* it (#305): the coin goes to `UI` on `u` and back to Inherit on `i`, the HUD to `UI` on `h`, and the
* main camera sees both layers on `s`. On `l` the HUD's sprite asks `Render layer of` of itself, of
* the HUD and of its behavior, which is not drawn.
*/
function layerSwitchScene() {
const f = sceneWithCamera();
f.stack.execute(new SetLayerNamesCommand('renderLayers', ['Default', 'UI']));
const coin = place(f, f.spriteId, { x: 0, y: 0 });
const hud = place(f, f.containerId, { x: 0, y: 0 });
const inside = place(f, f.spriteId, { x: 0, y: 0 }, hud);
const main = place(f, f.cameraId, { x: 0, y: 0 });
const over = place(f, f.cameraId, { x: 0, y: 0 });
f.set(main, SEES_KEY, 0b01);
f.set(over, SEES_KEY, 0b10);
f.set(over, OVERLAY_KEY, true);
const behaviorOn = (objectId: string, name: string) => {
const behavior = makeBehavior(f, name);
f.stack.execute(new PlaceAssetCommand(f.sceneId, behavior.id, { x: 0, y: 0 }, objectId));
return graphEditor(f, behavior);
};
const setOnKey = (edit: GraphEditor, key: string, type: string, port: string, value: number) => {
const set = edit.properties([{ key: port, kind: 'number', write: true }], {
[OBJECT_TYPE_PARAM]: type,
[port]: value,
});
edit.connect(edit.addNode('Owner'), 'object', set, 'object');
edit.connect(edit.addNode('OnKeyDown', { key }), 'exec', set, 'exec');
};
const onCoin = behaviorOn(coin, 'Coin');
setOnKey(onCoin, 'u', 'sprite', RENDER_LAYER_KEY, 1);
setOnKey(onCoin, 'i', 'sprite', RENDER_LAYER_KEY, INHERIT_LAYER);
const onHud = behaviorOn(hud, 'Hud');
setOnKey(onHud, 'h', 'container', RENDER_LAYER_KEY, 1);
const onCamera = behaviorOn(main, 'Cam');
setOnKey(onCamera, 's', 'camera', SEES_KEY, 0b11);
const onInside = behaviorOn(inside, 'Inside');
let from = onInside.addNode('OnKeyDown', { key: 'l' });
const ask = (source: GraphNode, port: string) => {
const of = onInside.addNode('RenderLayerOf');
onInside.connect(source, port, of, 'object');
for (const output of ['name', 'number']) {
const log = onInside.addNode('Log');
onInside.connect(of, output, log, 'message');
onInside.connect(from, 'exec', log, 'exec');
from = log;
}
};
const owner = onInside.addNode('Owner');
const parent = onInside.addNode('Parent');
onInside.connect(owner, 'object', parent, 'object');
ask(owner, 'object');
ask(parent, 'parent');
ask(onInside.addNode('Self'), 'object');
return { ...f, coin, hud, inside };
}
describe('a graph changes render layers and what a camera sees during the run (#305)', () => {
const start = () => {
const s = layerSwitchScene();
const started = startWorld(s, { behaviors: true });
const press = (key: string) => {
started.input.press(key);
started.frame();
started.input.release(key);
};
const seenBy = (camera: { props: PropertyBag } | null) => {
const items = collectWorldItems(started.world).items.filter((item) => item.type === 'sprite');
const seen = items.filter((item) => camera && cameraSees(camera.props, item.renderLayer));
return seen.map((item) => (item.id === s.coin ? 'coin' : item.id === s.inside ? 'inside' : item.id));
};
const main = () => seenBy(cameraOfWorld(started.world));
const overlay = () => seenBy(overlaysOfWorld(started.world)[0]!);
started.frame();
return { ...s, ...started, press, main, overlay };
};
it('moves the coin into the overlay with Set, and back with Inherit', () => {
const r = start();
expect([r.main(), r.overlay()]).toEqual([['coin', 'inside'], []]);
r.press('u');
expect([r.main(), r.overlay()]).toEqual([['inside'], ['coin']]);
r.press('i');
expect([r.main(), r.overlay()]).toEqual([['coin', 'inside'], []]);
});
it('takes the HUD container’s sprite along when the container changes', () => {
const r = start();
r.press('h');
expect([r.main(), r.overlay()]).toEqual([['coin'], ['inside']]);
});
it('lets the main camera see a second layer once its Sees is set', () => {
const r = start();
r.press('h');
r.press('s');
expect(r.main()).toEqual(['coin', 'inside']);
});
it('names the layer that holds, the inherited one too, and none for what is not drawn', () => {
const r = start();
r.press('l');
expect(r.world.log).toEqual(['Default', '0', 'Default', '0', '', '-1']);
r.press('h');
r.press('l');
expect(r.world.log.slice(6)).toEqual(['UI', '1', 'UI', '1', '', '-1']);
});
});