-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstractInteraction.ts
192 lines (172 loc) · 5 KB
/
abstractInteraction.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { v4 as uuidv4 } from 'uuid';
import type {
Cartesian2,
Cesium3DTileFeature,
Cesium3DTilePointFeature,
Entity,
Ray,
} from '@vcmap-cesium/engine';
import type { Coordinate } from 'ol/coordinate.js';
import type { Feature } from 'ol';
import {
EventType,
ModificationKeyType,
PointerEventType,
PointerKeyType,
} from './interactionType.js';
import type VcsMap from '../map/vcsMap.js';
export type MapEvent = {
pointerEvent: PointerEventType;
map: VcsMap;
windowPosition: Cartesian2;
key: ModificationKeyType;
pointer: PointerKeyType;
/**
* position in web mercator coordinates
*/
position?: Coordinate;
/**
* position in web mercator coordinates or image coordinates in ObliqueMap Map Mode
*/
positionOrPixel?: Coordinate;
multipleTouch?: boolean;
time?: number | null;
};
export type ObliqueParameters = {
/**
* the image pixel clicked
*/
pixel: import('ol/coordinate.js').Coordinate;
/**
* true if the terrain could not be taken into account
*/
estimate?: boolean;
};
export type EventFeature =
| Feature
| Cesium3DTileFeature
| Cesium3DTilePointFeature
| Entity;
export type InteractionEvent = MapEvent & {
type: EventType;
/**
* a potential feature at said location
*/
feature?: EventFeature;
/**
* if set to true, the event chain is interrupted
*/
stopPropagation?: boolean;
/**
* additional parameters from oblique if obliquemode is active
*/
obliqueParameters?: ObliqueParameters;
ray?: Ray;
/**
* whether the position is exact, eg with translucentDepthPicking on
*/
exactPosition?: boolean;
};
export type EventAfterEventHandler = Omit<
InteractionEvent,
'position' | 'positionOrPixel'
> & { position: Coordinate; positionOrPixel: Coordinate };
/**
* An abstract interface for all interactions
* @group Interaction
*/
class AbstractInteraction {
/**
* A unique identifier for this interaction
*/
id: string;
private _defaultActive: number;
/**
* The current active bitmask for {@link EventType}
*/
active: number;
private _defaultModificationKey: ModificationKeyType;
/**
* The current active {@link ModificationKeyType}
*/
modificationKey: ModificationKeyType;
private _defaultPointerKey: PointerKeyType;
/**
* The currently active {@link PointerKeyType}
*/
pointerKey: PointerKeyType;
/**
* @param [defaultActive=EventType.NONE] - A bitmask representing the default to listen to
* @param [defaultModificationKey=ModificationKeyType.NONE] - A bitmask representing the default keys to listen to
* @param [defaultPointerKey=PointerKeyType.LEFT] - A bitmask representing the pointer key to listen to
*/
constructor(
defaultActive: EventType = EventType.NONE,
defaultModificationKey: ModificationKeyType = ModificationKeyType.NONE,
defaultPointerKey: PointerKeyType = PointerKeyType.LEFT,
) {
this.id = uuidv4();
this._defaultActive = defaultActive;
this.active = this._defaultActive;
this._defaultModificationKey = defaultModificationKey;
this.modificationKey = this._defaultModificationKey;
this._defaultPointerKey = defaultPointerKey;
this.pointerKey = this._defaultPointerKey;
}
/**
* Main function, called when an event is raised for this interaction
*/
// eslint-disable-next-line class-methods-use-this
pipe(event: InteractionEvent): Promise<InteractionEvent> {
return Promise.resolve(event);
}
/**
* Called when the modifier keys have changed.
*/
// eslint-disable-next-line class-methods-use-this,no-unused-vars
modifierChanged(_modifier: ModificationKeyType): void {}
/**
* Sets the interaction active.
* Use boolean (true|false) to toggle default behavior.
* Pass it a bitmask of {@link EventType}
* to change the active state.
* Call without arguments to reset the default active, modification key and pointer Key behavior
*/
setActive(active?: boolean | number): void {
if (typeof active === 'undefined') {
this.active = this._defaultActive;
this.modificationKey = this._defaultModificationKey;
this.pointerKey = this._defaultPointerKey;
} else if (typeof active === 'boolean') {
this.active = active ? this._defaultActive : EventType.NONE;
} else {
this.active = active;
}
}
/**
* Sets the modification key to listen to or the default modification key if none is provided.
*/
setModification(mod?: ModificationKeyType): void {
if (mod) {
this.modificationKey = mod;
} else {
this.modificationKey = this._defaultModificationKey;
}
}
/**
* Sets the pointer key for this interaction or the default pointer if none is provided.
*/
setPointer(pointer?: PointerKeyType): void {
if (pointer) {
this.pointerKey = pointer;
} else {
this.pointerKey = this._defaultPointerKey;
}
}
/**
* destroys the implementation, removing any created resources
*/
// eslint-disable-next-line class-methods-use-this
destroy(): void {}
}
export default AbstractInteraction;