/
ViewportComponentEvents.ts
189 lines (165 loc) · 5.34 KB
/
ViewportComponentEvents.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Viewport
*/
import { Matrix3d, Point3d, Vector3d } from "@itwin/core-geometry";
import type {
SelectedViewportChangedArgs,
StandardViewId,
Viewport,
} from "@itwin/core-frontend";
import { IModelApp } from "@itwin/core-frontend";
import { UiEvent } from "@itwin/appui-abstract";
import { Face } from "../navigationaids/Cube";
/** Arguments for [[DrawingViewportChangeEvent]]
* @public
*/
export interface DrawingViewportChangeEventArgs {
rotation: Matrix3d;
origin: Point3d;
complete: boolean;
}
/** Drawing View Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class DrawingViewportChangeEvent extends UiEvent<DrawingViewportChangeEventArgs> {}
/** Arguments for [[CubeRotationChangeEvent]]
* @public
*/
export interface CubeRotationChangeEventArgs {
rotMatrix: Matrix3d;
face: Face;
complete?: boolean;
}
/** 3d Cube Rotation Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class CubeRotationChangeEvent extends UiEvent<CubeRotationChangeEventArgs> {}
/** Arguments for [[StandardRotationChangeEvent]]
* @public
*/
export interface StandardRotationChangeEventArgs {
standardRotation: StandardViewId;
}
/** Standard Rotation Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class StandardRotationChangeEvent extends UiEvent<StandardRotationChangeEventArgs> {}
/** Arguments for [[ViewRotationChangeEvent]]
* @public
*/
export interface ViewRotationChangeEventArgs {
viewport: Viewport;
animationTime?: number;
}
/** View Rotation Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class ViewRotationChangeEvent extends UiEvent<ViewRotationChangeEventArgs> {}
/** Arguments for [[ViewClassFullNameChangedEvent]]
* @public
*/
export interface ViewClassFullNameChangedEventArgs {
viewport: Viewport;
oldName: string;
newName: string;
}
/** View Class Full Name Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class ViewClassFullNameChangedEvent extends UiEvent<ViewClassFullNameChangedEventArgs> {}
/** Arguments for [[ViewIdChangedEvent]]
* @public
*/
export interface ViewIdChangedEventArgs {
viewport: Viewport;
oldId: string;
newId: string;
}
/** View Id Change event
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export class ViewIdChangedEvent extends UiEvent<ViewIdChangedEventArgs> {}
/** Viewport Rotation events and methods
* @public
*/
export class ViewportComponentEvents {
private static _removeListener?: () => void;
public static initialize() {
if (undefined !== this._removeListener) return;
this._removeListener =
IModelApp.viewManager.onSelectedViewportChanged.addListener(
ViewportComponentEvents.handleSelectedViewportChanged
);
}
/** @internal - for unit testing */
public static terminate() {
if (this._removeListener) {
this._removeListener();
this._removeListener = undefined;
}
}
public static face = Face.None;
public static readonly origin = Point3d.createZero();
public static readonly extents = Vector3d.createZero();
public static readonly rotationMatrix = Matrix3d.createIdentity();
public static readonly onDrawingViewportChangeEvent =
new DrawingViewportChangeEvent();
public static readonly onCubeRotationChangeEvent =
new CubeRotationChangeEvent();
public static readonly onStandardRotationChangeEvent =
new StandardRotationChangeEvent();
public static readonly onViewRotationChangeEvent =
new ViewRotationChangeEvent();
public static readonly onViewClassFullNameChangedEvent =
new ViewClassFullNameChangedEvent();
public static readonly onViewIdChangedEvent = new ViewIdChangedEvent();
private static handleSelectedViewportChanged(
args: SelectedViewportChangedArgs
): void {
if (args.current) ViewportComponentEvents.setViewMatrix(args.current);
}
public static setCubeMatrix(
rotMatrix: Matrix3d,
face = Face.None,
complete: boolean = false
): void {
this.rotationMatrix.setFrom(rotMatrix);
this.face = face;
this.onCubeRotationChangeEvent.emit({ rotMatrix, complete, face });
}
public static setDrawingViewportState(
origin: Point3d,
rotation: Matrix3d,
complete: boolean = false
): void {
this.onDrawingViewportChangeEvent.emit({ origin, rotation, complete });
}
public static setStandardRotation(standardRotation: StandardViewId): void {
this.onStandardRotationChangeEvent.emit({ standardRotation });
}
public static setViewMatrix(
viewport: Viewport,
animationTime?: number
): void {
// When handling onViewChanged, use setTimeout
setTimeout(() => {
// istanbul ignore next
if (viewport.view) {
this.origin.setFrom(viewport.view.getOrigin());
this.extents.setFrom(viewport.view.getExtents());
}
this.rotationMatrix.setFrom(viewport.rotation);
this.onViewRotationChangeEvent.emit({ viewport, animationTime });
});
}
}