72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
/**
|
||
* Which tilemap the painting panel and the scene view's brush work on (#142): exactly one placed
|
||
* map, selected in the active scene. Everything else – nothing, two objects, the asset itself, a
|
||
* map in a scene that is not showing – leaves nothing to paint.
|
||
*/
|
||
import { describe, expect, it } from 'vitest';
|
||
import { CreateAssetCommand } from '../../src/core/commands/assetCommands';
|
||
import { PlaceAssetCommand } from '../../src/core/commands/sceneObjectCommands';
|
||
import { createEmptyProject } from '../../src/core/model/Project';
|
||
import { paintedTilemap } from '../../src/editor/panels/TilemapPanel';
|
||
import { EditorState, objectSelection } from '../../src/editor/state';
|
||
|
||
function setup() {
|
||
const state = new EditorState();
|
||
state.loadProject(createEmptyProject('p', 'P'), 1);
|
||
const create = (type: 'tilemap' | 'sprite' | 'scene', name: string) => {
|
||
const command = new CreateAssetCommand(type, name);
|
||
expect(state.execute(command)).toBe(true);
|
||
return command.asset.id;
|
||
};
|
||
// The first scene there is becomes the active one by itself.
|
||
const sceneId = create('scene', 'Main');
|
||
expect(state.activeScene?.id).toBe(sceneId);
|
||
const place = (holderId: string, assetId: string) => {
|
||
const command = new PlaceAssetCommand(holderId, assetId, { x: 0, y: 0 });
|
||
expect(state.execute(command)).toBe(true);
|
||
return command.objectId;
|
||
};
|
||
const tilemapId = create('tilemap', 'Ground');
|
||
return { state, sceneId, tilemapId, create, place };
|
||
}
|
||
|
||
describe('the painted tilemap', () => {
|
||
it('is the one placed map selected in the active scene', () => {
|
||
const { state, sceneId, tilemapId, place } = setup();
|
||
const map = place(sceneId, tilemapId);
|
||
state.select(objectSelection(sceneId, [map]));
|
||
expect(paintedTilemap(state)).toMatchObject({ objectId: map, asset: { id: tilemapId } });
|
||
});
|
||
|
||
it('is none without a selection, for another object and for the asset itself', () => {
|
||
const { state, sceneId, tilemapId, create, place } = setup();
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
const sprite = place(sceneId, create('sprite', 'Box'));
|
||
state.select(objectSelection(sceneId, [sprite]));
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
state.select({ kind: 'asset', assetId: tilemapId });
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
});
|
||
|
||
it('is none while a second object is selected with it, map or not', () => {
|
||
const { state, sceneId, tilemapId, create, place } = setup();
|
||
const map = place(sceneId, tilemapId);
|
||
const other = place(sceneId, tilemapId);
|
||
const sprite = place(sceneId, create('sprite', 'Box'));
|
||
state.select(objectSelection(sceneId, [map, other], map));
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
state.select(objectSelection(sceneId, [map, sprite], map));
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
state.select(objectSelection(sceneId, [map]));
|
||
expect(paintedTilemap(state)?.objectId).toBe(map);
|
||
});
|
||
|
||
it('is none for a map in a scene that is not the one showing', () => {
|
||
const { state, tilemapId, create, place } = setup();
|
||
const otherScene = create('scene', 'Elsewhere');
|
||
const map = place(otherScene, tilemapId);
|
||
state.select(objectSelection(otherScene, [map]));
|
||
expect(paintedTilemap(state)).toBeNull();
|
||
});
|
||
});
|