-
Notifications
You must be signed in to change notification settings - Fork 5
/
TrackObject.ts
329 lines (257 loc) · 11 KB
/
TrackObject.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import Animator from "../Animator";
import UsageCache from "engine/ds/UsageCache";
import Rect from "engine/ui/Rect";
import Text from "engine/ui/Text";
import { InternalDataSource } from "../data-source/InternalDataSource";
import { OpenSansRegular } from "../ui/font/Fonts";
import { Tile, TileLoader, TileState } from "./TileLoader";
import { TrackModel } from "./TrackModel";
import { Scalar } from "engine/math/Scalar";
import { StyleProxy } from "../ui/util/StyleProxy";
import { TrackEvent } from "./TrackEvent";
export class TrackObject<
ModelType extends TrackModel = TrackModel,
TileLoaderType extends TileLoader<any, any> = TileLoader<any, any>
> extends Rect {
protected displayLoadingIndicator = false;
protected _pixelRatio: number = window.devicePixelRatio || 1;
get pixelRatio() { return this._pixelRatio; }
set pixelRatio(v: number) {
this._pixelRatio = v;
this.displayNeedUpdate = true;
}
protected dataSource: InternalDataSource;
protected contig: string | undefined;
protected x0: number;
protected x1: number;
protected defaultCursor = 'crosshair';
protected axisPointers: { [id: string]: AxisPointer } = {};
protected activeAxisPointerColor = [1, 1, 1, 0.8];
protected secondaryAxisPointerColor = [0.2, 0.2, 0.2, 1];
protected focusRegionRectLeft: Rect;
protected focusRegionRectRight: Rect;
protected loadingIndicator: LoadingIndicator;
protected displayNeedUpdate = true;
protected loadingIndicatorPadding = 0.1;
constructor(protected readonly model: ModelType) {
super(0, 0);
// set default background color
this.color = [0.1, 0.1, 0.1, 1];
this.cursorStyle = this.defaultCursor;
this.addInteractionListener('pointerdown', () => this.cursorStyle = 'pointer');
this.addInteractionListener('pointerup', () => this.cursorStyle = this.defaultCursor);
this.addInteractionListener('dragend', () => this.cursorStyle = this.defaultCursor);
this.loadingIndicator = new LoadingIndicator();
this.loadingIndicator.cursorStyle = 'pointer';
this.loadingIndicator.originY = -1;
this.loadingIndicator.originX = -1;
this.loadingIndicator.relativeX = 1;
this.loadingIndicator.relativeY = 1;
this.loadingIndicator.x = -20;
this.loadingIndicator.y = -10;
this.loadingIndicator.mask = this;
this.add(this.loadingIndicator);
// @! depth-box, should be at top, maybe relativeZ = 1
// - be careful to avoid conflict with cursor
this.toggleLoadingIndicator(false, false);
let focusRegionColor = [.1, .1, .1, 1.0];
let focusRegionOpacity = 0.7;
this.focusRegionRectLeft = new Rect(0, 0, focusRegionColor);
this.focusRegionRectRight = new Rect(0, 0, focusRegionColor);
this.focusRegionRectLeft.opacity = this.focusRegionRectRight.opacity = focusRegionOpacity;
this.focusRegionRectLeft.relativeH = this.focusRegionRectRight.relativeH = 1.0;
this.focusRegionRectLeft.z = this.focusRegionRectRight.z = 10.0;
this.add(this.focusRegionRectLeft);
this.add(this.focusRegionRectRight);
// disabled by default
this.clearFocusRegion();
}
setDataSource(dataSource: InternalDataSource) {
this.dataSource = dataSource;
this.displayNeedUpdate = true;
}
setContig(contig: string) {
this.contig = contig;
this.displayNeedUpdate = true;
}
setRange(x0: number, x1: number) {
this.x0 = x0;
this.x1 = x1;
this.displayNeedUpdate = true;
}
setAxisPointer(id: string, fractionX: number, style: AxisPointerStyle) {
let withinBounds = fractionX >= 0 && fractionX <= 1;
let axisPointer = this.axisPointers[id];
if (axisPointer === undefined) {
// !withinBounds means do not draw, so we don't need to create the object
if (!withinBounds) return;
// create axis pointer
axisPointer = new AxisPointer(style, this.activeAxisPointerColor, this.secondaryAxisPointerColor, 'x');
axisPointer.z = 2;
this.add(axisPointer);
this.axisPointers[id] = axisPointer;
}
axisPointer.render = withinBounds;
if (withinBounds) {
axisPointer.relativeX = fractionX;
}
if (axisPointer.style !== style) {
axisPointer.setStyle(style);
}
}
removeAxisPointer(id: string) {
let axisPointer = this.axisPointers[id];
if (axisPointer === undefined) {
return;
}
this.remove(axisPointer);
delete this.axisPointers[id];
}
setFocusRegion(x0_fractional: number, x1_fractional: number) {
this.focusRegionRectLeft.relativeX = 0;
this.focusRegionRectLeft.relativeW = Math.max(Math.min(x0_fractional, x1_fractional), 0);
this.focusRegionRectLeft.render = true;
this.focusRegionRectRight.relativeX = Math.max(x0_fractional, x1_fractional);
this.focusRegionRectRight.relativeW = Math.max(1.0 - this.focusRegionRectRight.relativeX, 0);
this.focusRegionRectRight.render = true;
}
clearFocusRegion() {
this.focusRegionRectLeft.render = false;
this.focusRegionRectRight.render = false;
}
private _lastComputedWidth: number;
applyTransformToSubNodes(root?: boolean) {
// update tiles if we need to
if ((this._lastComputedWidth !== this.getComputedWidth()) || this.displayNeedUpdate) {
this.triggerDisplayUpdate();
this._lastComputedWidth = this.getComputedWidth();
}
super.applyTransformToSubNodes(root);
}
currentSamplingDensity() {
const span = this.x1 - this.x0;
const widthPx = this.getComputedWidth();
let basePairsPerDOMPixel = (span / widthPx);
let samplingDensity = basePairsPerDOMPixel / this.pixelRatio;
return samplingDensity;
}
applyStyle(styleProxy: StyleProxy) {
this.color = styleProxy.getColor('background-color');
this.loadingIndicator.color = styleProxy.getColor('--loading-indicator') || this.loadingIndicator.color;
this.activeAxisPointerColor = styleProxy.getColor('--cursor') || this.activeAxisPointerColor;
this.secondaryAxisPointerColor = styleProxy.getColor('--secondary-cursor') || this.secondaryAxisPointerColor;
}
emitTrackEvent(eventData: TrackEvent) {
this.emit('track-event', eventData);
}
/**
* Override to handle drawing
*/
updateDisplay(samplingDensity: number, continuousLodLevel: number, span: number, widthPx: number) {
}
getTileLoader(): TileLoaderType {
return this.dataSource.getTileLoader(this.model, this.contig) as any;
}
protected _loadingTiles = new UsageCache<Tile<any>>(
null,
(tile) => this.removeTileLoadingDependency(tile),
);
protected triggerDisplayUpdate() {
this._loadingTiles.markAllUnused();
this.displayNeedUpdate = false;
const span = this.x1 - this.x0;
const widthPx = this.getComputedWidth();
let samplingDensity = this.currentSamplingDensity();
let continuousLodLevel = Scalar.log2(Math.max(samplingDensity, 1));
let lodLevel = Math.floor(continuousLodLevel);
let tileLoader = this.getTileLoader();
tileLoader._lowestTouchedLod = Infinity;
this.updateDisplay(samplingDensity, continuousLodLevel, span, widthPx);
// display loading indicator if any tiles in the current range are loading
let topLod = tileLoader.topTouchedLod();
let lowestVisibleLod = tileLoader._lowestTouchedLod;
let _lastMappedLod = -1;
for (let l = lowestVisibleLod; l <= topLod; l++) {
let mappedLod = tileLoader.mapLodLevel(l);
if (_lastMappedLod != mappedLod) {
let lodLevelCovered = true;
tileLoader.forEachTileAtLod(this.x0, this.x1, mappedLod, false, (tile) => {
if (tile.state === TileState.Loading) {
this._loadingTiles.get(tile.key, () => this.createTileLoadingDependency(tile));
}
if (tile.state !== TileState.Complete) {
lodLevelCovered = false;
}
});
// if a level has been covered complete we assume we don't care about the higher lods
if (lodLevelCovered) break;
}
_lastMappedLod = mappedLod;
}
this._loadingTiles.removeUnused();
this.toggleLoadingIndicator((this._loadingTiles.count > 0) || this.displayLoadingIndicator, true);
}
/**
* Show or hide the loading indicator via animation
* This function can be safely called repeatedly without accounting for the current state of the indicator
*/
protected toggleLoadingIndicator(visible: boolean, animate: boolean) {
// we want a little bit of delay before the animation, for this we use a negative opacity when invisible
let targetOpacity = visible ? 1.0 : -this.loadingIndicatorPadding;
if (animate) {
Animator.springTo(this.loadingIndicator, { 'opacity': targetOpacity }, 50);
} else {
Animator.stop(this.loadingIndicator, ['opacity']);
this.loadingIndicator.opacity = targetOpacity;
}
}
// when we're waiting on data from a tile we add a complete listener to update the annotation when the data arrives
protected createTileLoadingDependency = (tile: Tile<any>) => {
tile.addEventListener('complete', this.onDependentTileComplete);
return tile;
}
protected removeTileLoadingDependency = (tile: Tile<any>) => {
tile.removeEventListener('complete', this.onDependentTileComplete);
}
protected onDependentTileComplete = () => {
this.triggerDisplayUpdate();
}
}
export enum AxisPointerStyle {
Active = 0,
Secondary = 1,
}
export class AxisPointer extends Rect {
readonly style: AxisPointerStyle;
constructor(style: AxisPointerStyle, public activeColor: ArrayLike<number>, public secondaryColor: ArrayLike<number>, axis: 'x' | 'y') {
super(0, 0);
if (axis === 'y') {
this.originY = -0.5;
this.relativeW = 1;
this.h = 1;
} else {
this.originX = -0.5;
this.relativeH = 1;
this.w = 1;
}
this.transparent = true;
this.setStyle(style);
}
setStyle(style: AxisPointerStyle) {
switch (style) {
case AxisPointerStyle.Active:
this.color = this.activeColor;
break;
case AxisPointerStyle.Secondary:
this.color = this.secondaryColor;
break;
}
(this.style as any) = style;
}
}
class LoadingIndicator extends Text {
constructor() {
super(OpenSansRegular, 'Loading', 12, [1, 1, 1, 1]);
}
}
export default TrackObject;