-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CanvasEngine.ts
133 lines (113 loc) · 3.5 KB
/
CanvasEngine.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
131
132
133
import { CanvasModel } from './entities/canvas/CanvasModel';
import { FactoryBank } from './core/FactoryBank';
import { AbstractReactFactory } from './core/AbstractReactFactory';
import { LayerModel } from './entities/layer/LayerModel';
import { BaseListener, BaseObserver } from './core/BaseObserver';
import { MouseEvent } from 'react';
import { BaseModel } from './core-models/BaseModel';
import { Point } from '@projectstorm/geometry';
import { ActionEventBus } from './core-actions/ActionEventBus';
import { ZoomCanvasAction } from './actions/ZoomCanvasAction';
import { DeleteItemsAction } from './actions/DeleteItemsAction';
import { StateMachine } from './core-state/StateMachine';
export interface CanvasEngineListener extends BaseListener {
canvasReady?(): void;
repaintCanvas?(): void;
rendered?(): void;
}
export class CanvasEngine<
L extends CanvasEngineListener = CanvasEngineListener,
M extends CanvasModel = CanvasModel
> extends BaseObserver<L> {
protected model: M;
protected layerFactories: FactoryBank<AbstractReactFactory<LayerModel>>;
protected canvas: HTMLDivElement;
protected eventBus: ActionEventBus;
protected stateMachine: StateMachine;
constructor() {
super();
this.model = null;
this.eventBus = new ActionEventBus(this);
this.stateMachine = new StateMachine(this);
this.layerFactories = new FactoryBank();
this.registerFactoryBank(this.layerFactories);
this.eventBus.registerAction(new ZoomCanvasAction());
this.eventBus.registerAction(new DeleteItemsAction());
}
getStateMachine() {
return this.stateMachine;
}
getRelativeMousePoint(event: { clientX: number; clientY: number }): Point {
const point = this.getRelativePoint(event.clientX, event.clientY);
return new Point(
(point.x - this.model.getOffsetX()) / (this.model.getZoomLevel() / 100.0),
(point.y - this.model.getOffsetY()) / (this.model.getZoomLevel() / 100.0)
);
}
getRelativePoint(x, y): Point {
const canvasRect = this.canvas.getBoundingClientRect();
return new Point(x - canvasRect.left, y - canvasRect.top);
}
registerFactoryBank(factory: FactoryBank) {
factory.registerListener({
factoryAdded: event => {
event.factory.setDiagramEngine(this);
},
factoryRemoved: event => {
event.factory.setDiagramEngine(null);
}
});
}
getActionEventBus() {
return this.eventBus;
}
getLayerFactories() {
return this.layerFactories;
}
getFactoryForLayer<F extends AbstractReactFactory<LayerModel>>(layer: LayerModel | string) {
if (typeof layer === 'string') {
return this.layerFactories.getFactory(layer);
}
return this.layerFactories.getFactory(layer.getType());
}
setModel(model: M) {
this.model = model;
if (this.canvas) {
requestAnimationFrame(() => {
this.repaintCanvas();
});
}
}
getModel(): M {
return this.model;
}
repaintCanvas() {
this.iterateListeners(listener => {
if (listener.repaintCanvas) {
listener.repaintCanvas();
}
});
}
setCanvas(canvas?: HTMLDivElement) {
if (this.canvas !== canvas) {
this.canvas = canvas;
if (canvas) {
this.fireEvent({}, 'canvasReady');
}
}
}
getCanvas() {
return this.canvas;
}
getMouseElement(event: MouseEvent): BaseModel {
return null;
}
zoomToFit() {
const xFactor = this.canvas.clientWidth / this.canvas.scrollWidth;
const yFactor = this.canvas.clientHeight / this.canvas.scrollHeight;
const zoomFactor = xFactor < yFactor ? xFactor : yFactor;
this.model.setZoomLevel(this.model.getZoomLevel() * zoomFactor);
this.model.setOffset(0, 0);
this.repaintCanvas();
}
}