MCPcopy Create free account
hub / github.com/esengine/esengine / RectangleTool

Class RectangleTool

packages/tilemap-editor/src/tools/RectangleTool.ts:7–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import type { ITilemapTool, ToolContext } from './ITilemapTool';
6
7export class RectangleTool implements ITilemapTool {
8 readonly id = 'rectangle';
9 readonly name = 'Rectangle';
10 readonly icon = 'Square';
11 readonly cursor = 'crosshair';
12
13 private _isDrawing = false;
14 private _startX = -1;
15 private _startY = -1;
16 private _currentX = -1;
17 private _currentY = -1;
18
19 onMouseDown(tileX: number, tileY: number, ctx: ToolContext): void {
20 if (ctx.layerLocked && !ctx.editingCollision) return;
21 this._isDrawing = true;
22 this._startX = tileX;
23 this._startY = tileY;
24 this._currentX = tileX;
25 this._currentY = tileY;
26 }
27
28 onMouseMove(tileX: number, tileY: number, _ctx: ToolContext): void {
29 if (!this._isDrawing) return;
30 this._currentX = tileX;
31 this._currentY = tileY;
32 }
33
34 onMouseUp(tileX: number, tileY: number, ctx: ToolContext): void {
35 if (!this._isDrawing) return;
36 if (ctx.layerLocked && !ctx.editingCollision) {
37 this.reset();
38 return;
39 }
40
41 this._currentX = tileX;
42 this._currentY = tileY;
43 this.fillRectangle(ctx);
44 this.reset();
45 }
46
47 getPreviewTiles(tileX: number, tileY: number, _ctx: ToolContext): { x: number; y: number }[] {
48 if (!this._isDrawing) {
49 return [{ x: tileX, y: tileY }];
50 }
51
52 const tiles: { x: number; y: number }[] = [];
53 const minX = Math.min(this._startX, tileX);
54 const maxX = Math.max(this._startX, tileX);
55 const minY = Math.min(this._startY, tileY);
56 const maxY = Math.max(this._startY, tileY);
57
58 for (let y = minY; y <= maxY; y++) {
59 for (let x = minX; x <= maxX; x++) {
60 tiles.push({ x, y });
61 }
62 }
63
64 return tiles;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected