-
Notifications
You must be signed in to change notification settings - Fork 2
/
CursorInformation.ts
197 lines (167 loc) · 5.96 KB
/
CursorInformation.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Cursor
*/
import type { XAndY } from "@itwin/core-geometry";
import { RelativePosition, UiEvent } from "@itwin/appui-abstract";
import { Point } from "@itwin/core-react";
/** Enum for Cursor Direction parts
* @public
*/
export enum CursorDirectionParts {
Top = 0x1000,
Left = 0x0100,
Right = 0x0010,
Bottom = 0x0001,
}
/** Enum for Cursor Direction
* @public
*/
export enum CursorDirection {
None = 0,
Top = CursorDirectionParts.Top,
Left = CursorDirectionParts.Left,
Right = CursorDirectionParts.Right,
Bottom = CursorDirectionParts.Bottom,
TopLeft = CursorDirectionParts.Top | CursorDirectionParts.Left,
TopRight = CursorDirectionParts.Top | CursorDirectionParts.Right,
BottomLeft = CursorDirectionParts.Bottom | CursorDirectionParts.Left,
BottomRight = CursorDirectionParts.Bottom | CursorDirectionParts.Right,
}
/** Cursor Updated Event Args interface.
* @public
* @deprecated in 4.13.x. Event args are inferred from a listener. If explicit type is needed use a type helper.
*/
export interface CursorUpdatedEventArgs {
oldPt: XAndY;
newPt: XAndY;
direction: CursorDirection;
}
/** Cursor Updated Event class.
* @public
* @deprecated in 4.13.x. This class should not be used by applications to instantiate objects.
*/
// eslint-disable-next-line deprecation/deprecation
export class CursorUpdatedEvent extends UiEvent<CursorUpdatedEventArgs> {}
/** Cursor Information class
* @public
*/
export class CursorInformation {
private static _cursorPosition: Point = new Point();
private static _cursorDirection: CursorDirection =
CursorDirection.BottomRight;
private static _cursorDirections = new Array<CursorDirection>();
/** The cursor position. */
public static get cursorPosition(): XAndY {
return this._cursorPosition;
}
public static set cursorPosition(pt: XAndY) {
this._cursorPosition = Point.create(pt);
}
/** Gets the cursor X position. */
public static get cursorX(): number {
return this._cursorPosition.x;
}
/** Gets the cursor Y position. */
public static get cursorY(): number {
return this._cursorPosition.y;
}
/** Gets the general cursor movement direction. */
public static get cursorDirection(): CursorDirection {
return this._cursorDirection;
}
/** Gets the [[CursorUpdatedEvent]]. */
// eslint-disable-next-line deprecation/deprecation
public static readonly onCursorUpdatedEvent = new CursorUpdatedEvent();
/** Handles the mouse movement. Sets the cursor position and direction and emits onCursorUpdatedEvent. */
public static handleMouseMove(point: XAndY): void {
const oldPt = CursorInformation.cursorPosition;
const direction = this._determineMostFrequentDirection(
this._cursorDirections,
this._cursorPosition,
Point.create(point)
);
this.cursorPosition = point;
this._cursorDirection = direction;
this.onCursorUpdatedEvent.emit({ oldPt, newPt: point, direction });
}
private static _determineMostFrequentDirection(
cursorDirections: CursorDirection[],
oldPt: Point,
newPt: Point
): CursorDirection {
const cursorDirection = this._determineCursorDirection(oldPt, newPt);
cursorDirections.push(cursorDirection);
if (cursorDirections.length > 10) cursorDirections.shift();
return this._mostFrequent(cursorDirections);
}
private static _mostFrequent(array: CursorDirection[]): CursorDirection {
const hashMap = new Map<CursorDirection, number>();
let maxCount = 0,
mostFrequent = CursorDirection.None;
array.forEach((value: CursorDirection) => {
const key = value;
let frequency = hashMap.get(key);
if (frequency !== undefined) frequency++;
else frequency = 1;
hashMap.set(key, frequency);
if (frequency > maxCount) {
mostFrequent = key;
maxCount = frequency;
}
});
return mostFrequent;
}
private static _determineCursorDirection(
oldPt: Point,
newPt: Point
): CursorDirection {
let directionParts: number = 0;
if (newPt.x < oldPt.x) directionParts |= CursorDirectionParts.Left;
else if (newPt.x > oldPt.x) directionParts |= CursorDirectionParts.Right;
if (newPt.y < oldPt.y) directionParts |= CursorDirectionParts.Top;
else if (newPt.y > oldPt.y) directionParts |= CursorDirectionParts.Bottom;
const direction = directionParts as CursorDirection;
return direction;
}
/** Gets the relative position based on the cursor direction. */
public static getRelativePositionFromCursorDirection(
cursorDirection: CursorDirection
): RelativePosition {
let relativePosition: RelativePosition = RelativePosition.BottomRight;
switch (cursorDirection) {
case CursorDirection.Top:
relativePosition = RelativePosition.Top;
break;
case CursorDirection.Left:
relativePosition = RelativePosition.Left;
break;
case CursorDirection.Right:
relativePosition = RelativePosition.Right;
break;
case CursorDirection.Bottom:
relativePosition = RelativePosition.Bottom;
break;
case CursorDirection.TopLeft:
relativePosition = RelativePosition.TopLeft;
break;
case CursorDirection.TopRight:
relativePosition = RelativePosition.TopRight;
break;
case CursorDirection.BottomLeft:
relativePosition = RelativePosition.BottomLeft;
break;
case CursorDirection.BottomRight:
relativePosition = RelativePosition.BottomRight;
break;
}
return relativePosition;
}
/** @internal - for testing */
public static clearCursorDirections(): void {
this._cursorDirections.length = 0;
}
}