-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
130 lines (108 loc) · 5.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { ActionHistory } from 'app/canvas/ActionHistory';
import { Camera } from 'app/canvas/Camera';
import { CanvasDragListener } from 'app/canvas/CanvasDragListener';
import { CollisionDetector } from 'app/geometry/collision/CollisionDetector';
import { SeparatingAxisCollisionDetector } from 'app/geometry/collision/SeparatingAxisCollisionDetector';
import { Dimensions } from 'app/geometry/Dimensions';
import { GridLayerPainter } from 'app/canvas/GridLayerPainter';
import { PanDownHotkey } from 'app/canvas/hotkeys/PanDownHotkey';
import { PanLeftHotkey } from 'app/canvas/hotkeys/PanLeftHotkey';
import { PanRightHotkey } from 'app/canvas/hotkeys/PanRightHotkey';
import { PanUpHotkey } from 'app/canvas/hotkeys/PanUpHotkey';
import { RedoHotkey } from 'app/canvas/hotkeys/RedoHotkey';
import { UndoHotkey } from 'app/canvas/hotkeys/UndoHotkey';
import { KeyboardEventRouter } from 'app/canvas/KeyboardEventRouter';
import { MouseEventRouter } from 'app/canvas/MouseEventRouter';
import { PolygonDragListener } from 'app/canvas/PolygonDragListener';
import { PolygonDragTransactionFactory } from 'app/canvas/PolygonDragTransactionFactory';
import { PolygonLayerPainter } from 'app/canvas/PolygonLayerPainter';
import { PolygonMover } from 'app/canvas/PolygonMover';
import { PolygonPainter } from 'app/canvas/PolygonPainter';
import { PolygonRepository } from 'app/canvas/PolygonRepository';
import { PolygonSelectListener } from 'app/canvas/PolygonSelectListener';
import { PolygonSelector } from 'app/canvas/PolygonSelector';
import { RandomPolygonSpawner } from 'app/canvas/RandomPolygonSpawner';
import { RenderingContext } from 'app/canvas/RenderingContext';
import { RotationAnchorCollisionDetector } from 'app/canvas/RotationAnchorCollisionDetector';
import { RotationAnchorDragListener } from 'app/canvas/RotationAnchorDragListener';
import { RotationAnchorLayerPainter } from 'app/canvas/RotationAnchorLayerPainter';
import { RotationAnchorPainter } from 'app/canvas/RotationAnchorPainter';
import { RotationAnchorRepository } from 'app/canvas/RotationAnchorRepository';
import { ScrollListener } from 'app/canvas/ScrollListener';
import { VertexAnchorDragListener } from 'app/canvas/VertexAnchorDragListener';
import { VertexAnchorLayerPainter } from 'app/canvas/VertexAnchorLayerPainter';
import { VertexAnchorPainter } from 'app/canvas/VertexAnchorPainter';
import { VertexAnchorRepository } from 'app/canvas/VertexAnchorRepository';
import { WindowResizeListener } from 'app/canvas/WindowResizeListener';
import { WorldPainter } from 'app/canvas/WorldPainter';
import { VertexAnchorFactory } from 'app/canvas/VertexAnchorFactory';
const canvas = document.createElement('canvas');
const camera = new Camera();
const renderingContext = new RenderingContext(canvas.getContext('2d'));
const gridLayerPainter = new GridLayerPainter(renderingContext, camera);
const polygonPainter = new PolygonPainter(renderingContext);
const polygonRepository = new PolygonRepository();
const polygonLayerPainter = new PolygonLayerPainter(polygonRepository, polygonPainter);
const actionHistory = new ActionHistory();
const separatingAxisCollisionDetector = new SeparatingAxisCollisionDetector();
const polygonMover = new PolygonMover(polygonRepository, separatingAxisCollisionDetector);
const polygonDragTransactionFactory = new PolygonDragTransactionFactory(polygonMover, actionHistory);
const rotationAnchorPainter = new RotationAnchorPainter(renderingContext);
const collisionDetector = new CollisionDetector();
const rotationAnchorCollisionDetector = new RotationAnchorCollisionDetector(
collisionDetector,
new Dimensions(15, 0, 15),
);
const rotationAnchorRepository = new RotationAnchorRepository(rotationAnchorCollisionDetector, polygonMover);
const rotationAnchorLayerPainter = new RotationAnchorLayerPainter(rotationAnchorRepository, rotationAnchorPainter);
const vertexAnchorFactory = new VertexAnchorFactory();
const vertexAnchorRepository = new VertexAnchorRepository();
const vertexAnchorPainter = new VertexAnchorPainter(renderingContext);
const vertexAnchorLayerPainter = new VertexAnchorLayerPainter(vertexAnchorRepository, vertexAnchorPainter);
const polygonSpawner = new RandomPolygonSpawner();
const polygons = polygonSpawner.spawnMany(20);
for (const polygon of polygons) {
polygonRepository.push(polygon);
}
const polygonSelector = new PolygonSelector(
rotationAnchorRepository,
vertexAnchorFactory,
vertexAnchorRepository,
);
const mouseEventRouter = new MouseEventRouter([
new RotationAnchorDragListener(rotationAnchorRepository, actionHistory),
new VertexAnchorDragListener(vertexAnchorRepository, actionHistory),
new PolygonSelectListener(polygonRepository, polygonSelector),
new PolygonDragListener(polygonRepository, polygonDragTransactionFactory),
new CanvasDragListener(camera),
], camera);
mouseEventRouter.register(document);
const layerPainters = [
gridLayerPainter,
polygonLayerPainter,
rotationAnchorLayerPainter,
vertexAnchorLayerPainter,
];
const worldPainter = new WorldPainter(renderingContext, camera, layerPainters);
const keyboardEventRouter = new KeyboardEventRouter({
'z': new UndoHotkey(actionHistory),
'x': new RedoHotkey(actionHistory),
'ArrowLeft': new PanLeftHotkey(camera),
'ArrowRight': new PanRightHotkey(camera),
'ArrowUp': new PanUpHotkey(camera),
'ArrowDown': new PanDownHotkey(camera),
});
keyboardEventRouter.register(document);
const scrollListener = new ScrollListener(camera);
scrollListener.register(document);
const windowResizeListener = new WindowResizeListener(renderingContext);
windowResizeListener.register(window);
const ticksPerSecond = 120;
const msPerTick = 1000 / ticksPerSecond;
document.body.appendChild(canvas);
renderingContext.fitToScreen();
setInterval(() => {
requestAnimationFrame(() => {
worldPainter.paint();
});
}, msPerTick);