mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-28 17:34:18 +00:00
923cba8c2d
SelectionBoxPass now stores an array of selections and renders one quad per entry. GPURenderer gains setSelectedUnits(ids) — the single-unit setSelectedUnit becomes a wrapper. Position + color are rebuilt each frame from lastUnits; dead unit IDs get pruned in place. ClientGameRunner's UnitSelectionEvent listener forwards both single and multi to view.setSelectedUnits — no more single/multi split. UILayer drops everything canvas2D-related: the offscreen canvas + context, theme, selectionAnimTime, multiSelectionBoxCenters, SELECTION_BOX_SIZE, drawSelectionBoxMulti, paintSelectionBoxAt, clearSelectionBox, paintCell, clearCell, and renderLayer / redraw / shouldTransform. tick() now only prunes destroyed warships from the selection list; the layer is purely state + click handling. ~120 LOC gone. Tests: UILayer.test.ts updated — drops the canvas/redraw asserts, adds a multi-selection state assertion.
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { UILayer } from "../../../src/client/graphics/layers/UILayer";
|
|
import { UnitSelectionEvent } from "../../../src/client/InputHandler";
|
|
|
|
describe("UILayer", () => {
|
|
let game: any;
|
|
let eventBus: any;
|
|
let transformHandler: any;
|
|
|
|
beforeEach(() => {
|
|
game = {
|
|
width: () => 100,
|
|
height: () => 100,
|
|
config: () => ({
|
|
theme: () => ({
|
|
territoryColor: () => ({
|
|
lighten: () => ({ alpha: () => ({ toRgbString: () => "#fff" }) }),
|
|
}),
|
|
}),
|
|
}),
|
|
x: () => 10,
|
|
y: () => 10,
|
|
unitInfo: () => ({ maxHealth: 10, constructionDuration: 5 }),
|
|
myPlayer: () => ({ id: () => 1 }),
|
|
ticks: () => 1,
|
|
updatesSinceLastTick: () => undefined,
|
|
};
|
|
eventBus = { on: vi.fn() };
|
|
transformHandler = {};
|
|
});
|
|
|
|
it("tracks the selected unit on single-unit selection (rendering is WebGL)", () => {
|
|
const ui = new UILayer(game, eventBus, transformHandler);
|
|
const unit = {
|
|
type: () => "Warship",
|
|
isActive: () => true,
|
|
tile: () => ({}),
|
|
owner: () => ({}),
|
|
};
|
|
const event = { isSelected: true, unit };
|
|
ui["onUnitSelection"](event as UnitSelectionEvent);
|
|
// selectedUnit is held for game-logic callers (the click handlers). The
|
|
// visual selection box is drawn by WebGL SelectionBoxPass — wired from
|
|
// ClientGameRunner via view.setSelectedUnits([unit.id()]).
|
|
expect(ui["selectedUnit"]).toBe(unit);
|
|
});
|
|
|
|
it("clears selection on deselect", () => {
|
|
const ui = new UILayer(game, eventBus, transformHandler);
|
|
const unit = {
|
|
type: () => "Warship",
|
|
isActive: () => true,
|
|
tile: () => ({}),
|
|
owner: () => ({}),
|
|
};
|
|
ui["onUnitSelection"]({ isSelected: true, unit } as UnitSelectionEvent);
|
|
ui["onUnitSelection"]({
|
|
isSelected: false,
|
|
unit: null,
|
|
} as unknown as UnitSelectionEvent);
|
|
expect(ui["selectedUnit"]).toBeNull();
|
|
});
|
|
|
|
it("tracks multi-selection list", () => {
|
|
const ui = new UILayer(game, eventBus, transformHandler);
|
|
const units = [
|
|
{ id: () => 1, isActive: () => true },
|
|
{ id: () => 2, isActive: () => true },
|
|
];
|
|
ui["onUnitSelection"]({
|
|
isSelected: true,
|
|
unit: null,
|
|
units,
|
|
} as unknown as UnitSelectionEvent);
|
|
expect(ui["multiSelectedWarships"]).toEqual(units);
|
|
expect(ui["selectedUnit"]).toBeNull();
|
|
});
|
|
});
|