/
TileRequestDecoration.ts
99 lines (81 loc) · 3.37 KB
/
TileRequestDecoration.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import { ColorDef, LinePixels } from "@itwin/core-common";
import { DecorateContext, GraphicBuilder, GraphicType, IModelApp, TileTree, Tool, Viewport } from "@itwin/core-frontend";
import { parseToggle } from "./parseToggle";
class TileRequestDecoration {
private static _instance?: TileRequestDecoration;
private _removeDecorator?: () => void;
private readonly _targetVp: Viewport;
private constructor(vp: Viewport) {
this._targetVp = vp;
this._removeDecorator = IModelApp.viewManager.addDecorator(this);
}
private stop(): void {
if (this._removeDecorator) {
this._removeDecorator();
this._removeDecorator = undefined;
}
}
/** This will allow the render system to cache and reuse the decorations created by this decorator's decorate() method. */
public readonly useCachedDecorations = true;
public decorate(context: DecorateContext): void {
const tiles = IModelApp.tileAdmin.getRequestsForUser(this._targetVp);
if (undefined === tiles)
return;
const map = new Map<TileTree, GraphicBuilder>();
for (const tile of tiles) {
let builder = map.get(tile.tree);
if (undefined === builder) {
builder = context.createGraphicBuilder(GraphicType.WorldDecoration, tile.tree.iModelTransform);
map.set(tile.tree, builder);
}
let color = ColorDef.white;
if (undefined !== tile.request)
color = tile.request.isQueued ? ColorDef.green : ColorDef.red;
builder.setSymbology(color, color, 1, LinePixels.Solid);
builder.addRangeBox(tile.range);
}
for (const builder of map.values())
context.addDecorationFromBuilder(builder);
}
public static toggle(vp: Viewport, enabled?: boolean): void {
const instance = TileRequestDecoration._instance;
if (undefined !== enabled) {
if ((undefined !== instance) === enabled)
return;
}
if (undefined === instance) {
TileRequestDecoration._instance = new TileRequestDecoration(vp);
} else {
instance.stop();
TileRequestDecoration._instance = undefined;
}
}
}
/** Display in every viewport a range graphic for every tile currently being requested for the viewport that was initially selected when the decorator was installed.
* Green indicates queued (http request not yet sent), red indicates active (http request sent). White indicates unexpected state.
* @beta
*/
export class ToggleTileRequestDecorationTool extends Tool {
public static override toolId = "ToggleTileRequestDecoration";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
public override async run(enable?: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
TileRequestDecoration.toggle(vp, enable);
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
const enable = parseToggle(args[0]);
if (typeof enable !== "string")
await this.run(enable);
return true;
}
}