The game view always stretched the camera picture to fit the pane.
Pixel art came out blurred at odd factors, and there was no way to
see the game at its real size. The view now has a header with a
scale drop-down, and 100 % is the default.
- The header of the new GamePane offers Auto, Fit, 25 %, 50 %,
100 %, 200 % and 400 %. Auto and Fit show the factor they land
on, e.g. "Auto · 100%", and follow the pane when it resizes.
- 100 % means one screen pixel per pixel of the camera resolution
(width x height). The camera zoom scales the world on top of
that, so a zoomed camera still fills the same frame.
- Auto takes the largest fixed step that fits the whole picture
and falls back to 25 % when nothing fits. Fit keeps the old
behaviour.
- A picture larger than the pane is cropped around the camera
centre. Fixed steps start on a whole pixel so pixel art stays
sharp at 100 %.
- The choice is kept in the browser like "Play starts". Nothing
is written into the project.
Found on the way: the first draft applied the step to the world
instead of the camera resolution. The camera zoom then had no
effect at 100 %, which the existing check 12c caught (zoom 1.37
measured as scale 1).
The CI smokes were not run locally, because that would reseed
projects the user works in. All three smoke cameras are 640x360
and fit the 670x383 harness view at 100 %.
Tests:
- ./check.sh: 156 files, 2298 tests, CHECK EXIT 0 (before 2290).
- 8 unit tests in tests/viewport/view.test.ts for whole-pixel
steps, cropping, Auto and Fit with fallback, and zoomed cameras.
- 7 e2e checks in 03g. e2e 1-17,28,30,38-40: ALL PASSED, 727
checks.
- Counter-checks: no cropping, no rounding, no factor reported to
the header, and no zoom share each fail their own tests.
- Check total: 1367 (CI at c1a702b) + 7 = 1374, computed only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.3 KiB
TypeScript
111 lines
4.3 KiB
TypeScript
/** The viewport transforms: plain geometry, so pinned here rather than through the browser. */
|
||
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
cameraFactor,
|
||
cameraView,
|
||
freeView,
|
||
GAME_SCALE_STEPS,
|
||
gameScaleFactor,
|
||
isInside,
|
||
screenToWorld,
|
||
worldBounds,
|
||
worldToScreen,
|
||
} from '../../src/viewport/view';
|
||
|
||
describe('freeView', () => {
|
||
it('puts the view centre in the middle of the widget', () => {
|
||
const view = freeView(50, -20, 2, 800, 600);
|
||
expect(worldToScreen(view, 50, -20)).toEqual({ x: 400, y: 300 });
|
||
expect(screenToWorld(view, 400, 300)).toEqual({ x: 50, y: -20 });
|
||
});
|
||
|
||
it('scales distances by the zoom', () => {
|
||
const view = freeView(0, 0, 2, 800, 600);
|
||
expect(worldToScreen(view, 100, 0)).toEqual({ x: 600, y: 300 });
|
||
});
|
||
|
||
it('round-trips any point', () => {
|
||
const view = freeView(13, -7, 0.35, 640, 480);
|
||
const back = screenToWorld(view, ...(Object.values(worldToScreen(view, 123, -456)) as [number, number]));
|
||
expect(back.x).toBeCloseTo(123, 9);
|
||
expect(back.y).toBeCloseTo(-456, 9);
|
||
});
|
||
|
||
it('covers the widget in world units', () => {
|
||
const bounds = worldBounds(freeView(0, 0, 2, 800, 600));
|
||
expect(bounds).toEqual({ left: -200, top: -150, right: 200, bottom: 150 });
|
||
});
|
||
});
|
||
|
||
describe('cameraView', () => {
|
||
const camera = { x: 10, y: 20, props: { width: 640, height: 360 } };
|
||
|
||
it('fits the camera frame into the widget and centres it', () => {
|
||
// 640×360 into 800×600: width is the tighter fit (1.25 vs 1.67), so bars top and bottom.
|
||
const view = cameraView(camera, 800, 600);
|
||
expect(view.scale).toBeCloseTo(1.25, 9);
|
||
expect(view.rect).toEqual({ x: 0, y: 75, w: 800, h: 450 });
|
||
// The camera position sits at the centre of the frame, not of the widget.
|
||
expect(worldToScreen(view, 10, 20)).toEqual({ x: 400, y: 300 });
|
||
});
|
||
|
||
it('reports the letterbox as outside', () => {
|
||
const view = cameraView(camera, 800, 600);
|
||
expect(isInside(view, 400, 300)).toBe(true);
|
||
expect(isInside(view, 400, 20)).toBe(false); // top bar
|
||
expect(isInside(view, 400, 580)).toBe(false); // bottom bar
|
||
});
|
||
|
||
it('draws a fixed step at exactly that many pixels per unit, starting on a whole pixel', () => {
|
||
const view = cameraView(camera, 801, 601, 0.5);
|
||
expect(view.scale).toBe(0.5);
|
||
expect(view.rect).toEqual({ x: 241, y: 211, w: 320, h: 180 });
|
||
});
|
||
|
||
it('cuts a frame larger than the view to the view, keeping the camera in the middle', () => {
|
||
const view = cameraView(camera, 800, 600, 2);
|
||
expect(view.scale).toBe(2);
|
||
expect(view.rect).toEqual({ x: 0, y: 0, w: 800, h: 600 });
|
||
expect(worldToScreen(view, 10, 20)).toEqual({ x: 400, y: 300 });
|
||
expect(worldBounds(view)).toEqual({ left: -190, top: -130, right: 210, bottom: 170 });
|
||
expect(isInside(view, 1, 1)).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe('cameraView with a zoomed camera', () => {
|
||
const zoomed = { x: 0, y: 0, props: { width: 640, height: 360, zoom: 2 } };
|
||
|
||
it('100% shows the camera picture pixel for pixel, the zoom magnifies the world on top', () => {
|
||
const view = cameraView(zoomed, 800, 600, 1);
|
||
expect(view.scale).toBe(2);
|
||
expect(view.rect).toEqual({ x: 80, y: 120, w: 640, h: 360 });
|
||
});
|
||
|
||
it('fit and auto size the picture, not the world region', () => {
|
||
expect(cameraFactor(zoomed.props, 800, 600, 'fit')).toBeCloseTo(1.25, 9);
|
||
expect(cameraView(zoomed, 800, 600, 'fit').scale).toBeCloseTo(2.5, 9);
|
||
expect(cameraFactor(zoomed.props, 800, 600, 'auto')).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe('gameScaleFactor', () => {
|
||
it('fit is the exact factor that fills the tighter side', () => {
|
||
expect(gameScaleFactor('fit', 640, 360, 800, 600)).toBeCloseTo(1.25, 9);
|
||
});
|
||
|
||
it('auto takes the largest step that still fits', () => {
|
||
expect(gameScaleFactor('auto', 640, 360, 800, 600)).toBe(1);
|
||
expect(gameScaleFactor('auto', 640, 360, 1280, 720)).toBe(2);
|
||
expect(gameScaleFactor('auto', 640, 360, 639, 600)).toBe(0.5);
|
||
expect(gameScaleFactor('auto', 320, 180, 2000, 2000)).toBe(4);
|
||
});
|
||
|
||
it('auto falls back to the smallest step when not even that fits', () => {
|
||
expect(gameScaleFactor('auto', 4000, 4000, 800, 600)).toBe(0.25);
|
||
});
|
||
|
||
it('a fixed step ignores the view size', () => {
|
||
for (const step of GAME_SCALE_STEPS) expect(gameScaleFactor(step, 640, 360, 10, 10)).toBe(step);
|
||
});
|
||
});
|