-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cube.tsx
171 lines (161 loc) · 4.7 KB
/
Cube.tsx
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Cube
*/
import "./Cube.scss";
import classnames from "classnames";
import * as React from "react";
import { Matrix3d } from "@itwin/core-geometry";
import type { CommonProps } from "@itwin/core-react";
/** Cube Face enumeration
* @public
*/
export enum Face {
None = "",
Left = "left",
Right = "right",
Back = "back",
Front = "front",
Bottom = "bottom",
Top = "top",
}
/** Properties for the [[Cube]] React component
* @public
*/
export interface CubeProps
extends React.AllHTMLAttributes<HTMLDivElement>,
CommonProps {
faces?: { [key: string]: React.ReactNode };
rotMatrix: Matrix3d;
}
/** Cube React component used by the 3d Cube Navigation Aid
* @public
*/
export class Cube extends React.PureComponent<CubeProps> {
public override render(): React.ReactNode {
const { faces, rotMatrix, className, ...props } = this.props;
return (
<div
className={classnames("components-cube-css3d", className)}
data-testid="components-cube"
{...props}
>
{[
Face.Front,
Face.Back,
Face.Right,
Face.Left,
Face.Top,
Face.Bottom,
].map((face: Face) => {
const content = faces && faces[face];
return (
<CubeFace key={face} rotMatrix={rotMatrix} face={face}>
{content}
</CubeFace>
);
})}
</div>
);
}
}
/** @internal */
export interface CubeFaceProps extends React.AllHTMLAttributes<HTMLDivElement> {
rotMatrix: Matrix3d;
face: Face;
}
/** @internal */
export class CubeFace extends React.Component<CubeFaceProps> {
private _faceWidth: number = 0;
public override render(): React.ReactNode {
const { rotMatrix, face, style, children, ...props } = this.props;
if (face === Face.None) return null;
const classes = classnames("face", this.getCSSClassNameFromFace(face));
// orient face (flip because of y axis reversal, rotate as necessary)
let reorient: Matrix3d = Matrix3d.createRowValues(
1,
0,
0,
0,
-1,
0,
0,
0,
1
);
// Position face correctly (applies to rotation, as well as translation)
let reposition: Matrix3d = Matrix3d.createIdentity();
switch (this.props.face) {
case Face.Bottom:
reposition = Matrix3d.createRowValues(-1, 0, 0, 0, 1, 0, 0, 0, -1);
reorient = Matrix3d.createRowValues(-1, 0, 0, 0, 1, 0, 0, 0, 1);
break;
case Face.Right:
reposition = Matrix3d.createRowValues(0, 0, 1, 0, 1, 0, -1, 0, 0);
reorient = Matrix3d.createRowValues(0, 1, 0, 1, 0, 0, 0, 0, 1);
break;
case Face.Left:
reposition = Matrix3d.createRowValues(0, 0, -1, 0, 1, 0, 1, 0, 0);
reorient = Matrix3d.createRowValues(0, -1, 0, -1, 0, 0, 0, 0, 1);
break;
case Face.Back:
reposition = Matrix3d.createRowValues(1, 0, 0, 0, 0, 1, 0, -1, 0);
reorient = Matrix3d.createRowValues(-1, 0, 0, 0, 1, 0, 0, 0, 1);
break;
case Face.Front:
reposition = Matrix3d.createRowValues(1, 0, 0, 0, 0, -1, 0, 1, 0);
reorient = Matrix3d.createRowValues(1, 0, 0, 0, -1, 0, 0, 0, 1);
break;
}
const repositioned = rotMatrix.multiplyMatrixMatrix(reposition);
const vect = repositioned.multiplyXYZ(0, 0, this._faceWidth);
const m = repositioned.multiplyMatrixMatrix(reorient);
const list = [
m.at(0, 0),
-m.at(1, 0),
m.at(2, 0),
0,
m.at(0, 1),
-m.at(1, 1),
m.at(2, 1),
0,
m.at(0, 2),
-m.at(1, 2),
m.at(2, 2),
0,
vect.at(0),
-vect.at(1),
vect.at(2) -
this._faceWidth /* move back faceWidth so face is on screen level */,
1,
];
const transform = `matrix3d(${list.join(", ")})`;
const s: React.CSSProperties = {
transform,
WebkitTransform: transform,
...style,
};
return (
<div
style={s}
data-testid={`components-cube-face-${face}`}
className={classes}
ref={(e) => {
this._faceWidth = (e && e.clientWidth / 2) || 0;
}}
{...props}
>
{children}
</div>
);
}
private getCSSClassNameFromFace(face: Face): string {
let className = "";
// istanbul ignore else
if (face !== Face.None) className = `cube-${face}`;
return className;
}
}