-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selectable.ts
115 lines (96 loc) · 3.56 KB
/
Selectable.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
import type { Board } from "canvas/board/Board";
import { Group } from "canvas/board/objects/foundation/Group";
import { RotateContainer } from "canvas/board/objects/foundation/RotateContainer";
import type { GeometricObject } from "canvas/board/objects/GeometricObject";
import type { BoardItem } from "canvas/board/objects/items/BoardItem";
import { allPositionings } from "canvas/board/objects/ui/selectable/ResizeFrame";
import type { ResizeHandlePositioning } from "canvas/board/objects/ui/selectable/ResizeHandle";
import { SelectionOverlay } from "canvas/board/objects/ui/selectable/SelectionOverlay";
import type { Optional } from "canvas/primitives/types";
export class SelectionOptions {
constructor(
readonly canMove: boolean = false,
readonly canResize: boolean = false,
readonly canRotate: boolean = false,
readonly resizeHandles: ResizeHandlePositioning[] = allPositionings,
) {}
}
export class Selectable extends Group {
readonly isSelectable = true;
readonly rotatedContent: Optional<RotateContainer<BoardItem>>;
private overlay: SelectionOverlay;
private overlayWrapper: GeometricObject;
private _isSelected = false;
constructor(
readonly content: BoardItem,
readonly options: SelectionOptions = content.selectionOptions ||
new SelectionOptions(),
) {
super();
this.overlay = new SelectionOverlay(this, this.options);
this.rotatedContent = new RotateContainer(this.content, this.content);
this.overlayWrapper = new RotateContainer(this.overlay, this.content);
this.children = [this.rotatedContent];
}
public get isSelected(): boolean {
return this._isSelected;
}
public set isSelected(isSelected: boolean) {
if (this._isSelected != isSelected) {
this._isSelected = isSelected;
if (this._isSelected) {
this.board.addObjectsAbove(
[this.overlayWrapper],
this.board.controller.minOverlayMarker,
);
} else {
this.board.removeObjects([this.overlayWrapper]);
}
}
this.overlay.isSelected = isSelected;
}
public onSpawn(board: Board): void {
super.onSpawn(board);
const select = board.controller.select;
const mouse = this.board.controller.mouse;
this.subscriptions.push(
mouse.onMouseDown.listen(this, e => {
select.onObjectMouseDown(e);
}),
);
this.subscriptions.push(
mouse.onMouseClick.listen(this, e => {
select.onObjectClick(e);
}),
);
this.subscriptions.push(
select.onSelect.listen(undefined, e => {
this.isSelected = this.isSelected || e.target == this.content;
}),
);
this.subscriptions.push(
select.onDeselect.listen(undefined, e => {
this.isSelected = this.isSelected && e.target != this.content;
}),
);
}
public onDespawn(board: Board): void {
if (this._isSelected) {
this.board.removeObjects([this.overlayWrapper]);
}
super.onDespawn(board);
}
}
export function getTopSelectableOnEventStack<T>(
eventStack: Optional<T[]>,
): Optional<Selectable> {
if (eventStack === undefined) {
return undefined;
}
for (const item of eventStack) {
if ("isSelectable" in item) {
return <Selectable>(<unknown>item);
}
}
return undefined;
}