-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Mouse.hx
326 lines (281 loc) · 9.68 KB
/
Mouse.hx
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
package kha.input;
import kha.netsync.Controller;
/** See `Mouse.setWheelEventBlockBehavior` */
enum MouseEventBlockBehavior {
Full;
None;
Custom(func: (event: Dynamic) -> Bool);
}
enum MouseCursor {
Default;
Pointer;
Text;
EastWestResize;
NorthSouthResize;
NorthEastResize;
SouthEastResize;
NorthWestResize;
SouthWestResize;
Grab;
Grabbing;
NotAllowed;
Wait;
Crosshair;
Custom(image: kha.Image);
}
@:allow(kha.SystemImpl)
@:expose
class Mouse extends Controller {
static var wheelEventBlockBehavior = MouseEventBlockBehavior.Full;
/**
* Get current Mouse.
* @param num (optional) mouse id (0 by default).
*/
public static function get(num: Int = 0): Mouse {
return SystemImpl.getMouse(num);
}
/**
* Allows fine grained control of mouse wheel browser default actions (html5 only).
* @param behavior can be:
* Full - block wheel events.
* None - do not block wheel events.
* Custom(func:(event:WheelEvent)->Bool) - set custom handler for wheel event (should return true if wheel event blocked).
*/
public static function setWheelEventBlockBehavior(behavior: MouseEventBlockBehavior): Void {
wheelEventBlockBehavior = behavior;
}
/**
* Creates event handlers from passed functions.
* @param downListener (optional) function with `button:Int`,`x:Int`,`y:Int` arguments, fired when a mouse is pressed down. `button:Int` is `0` for left button, `1` for right and `2` for middle.
* @param upListener (optional) function with `button:Int`,`x:Int`,`y:Int` arguments, fired when a mouse is released.
* @param moveListener (optional) function with `x:Int`,`y:Int`,`moveX:Int`,`moveY:Int` arguments, fired when a mouse is moved. `moveX`/`moveY` is the difference between the current coordinates and the last position of the mouse.
* @param wheelListener (optional) function with `delta:Int` argument, fired when the wheel rotates. It can have a value of `1` or `-1` depending on the rotation.
* @param leaveListener (optional) function without` arguments, when fired mouse leave canvas.
*/
public function notify(?downListener: (button: Int, x: Int, y: Int) -> Void, ?upListener: (button: Int, x: Int, y: Int) -> Void,
?moveListener: (x: Int, y: Int, moveX: Int, moveY: Int) -> Void, ?wheelListener: (delta: Int) -> Void, ?leaveListener: () -> Void = null): Void {
notifyWindowed(0, downListener, upListener, moveListener, wheelListener, leaveListener);
}
/**
* Removes event handlers from the passed functions that were passed to `notify` function.
*/
public function remove(?downListener: (button: Int, x: Int, y: Int) -> Void, ?upListener: (button: Int, x: Int, y: Int) -> Void,
?moveListener: (x: Int, y: Int, moveX: Int, moveY: Int) -> Void, ?wheelListener: (delta: Int) -> Void, ?leaveListener: () -> Void = null): Void {
removeWindowed(0, downListener, upListener, moveListener, wheelListener, leaveListener);
}
/**
* Creates event handlers from passed functions like `notify` function, but only for window with `windowId:Int` id argument. The windows are not supported by all the targets.
*/
public function notifyWindowed(windowId: Int, ?downListener: Int->Int->Int->Void, ?upListener: Int->Int->Int->Void,
?moveListener: Int->Int->Int->Int->Void, ?wheelListener: Int->Void, ?leaveListener: Void->Void = null): Void {
if (downListener != null) {
if (windowDownListeners == null) {
windowDownListeners = new Array();
}
while (windowDownListeners.length <= windowId) {
windowDownListeners.push(new Array());
}
windowDownListeners[windowId].push(downListener);
}
if (upListener != null) {
if (windowUpListeners == null) {
windowUpListeners = new Array();
}
while (windowUpListeners.length <= windowId) {
windowUpListeners.push(new Array());
}
windowUpListeners[windowId].push(upListener);
}
if (moveListener != null) {
if (windowMoveListeners == null) {
windowMoveListeners = new Array();
}
while (windowMoveListeners.length <= windowId) {
windowMoveListeners.push(new Array());
}
windowMoveListeners[windowId].push(moveListener);
}
if (wheelListener != null) {
if (windowWheelListeners == null) {
windowWheelListeners = new Array();
}
while (windowWheelListeners.length <= windowId) {
windowWheelListeners.push(new Array());
}
windowWheelListeners[windowId].push(wheelListener);
}
if (leaveListener != null) {
if (windowLeaveListeners == null) {
windowLeaveListeners = new Array();
}
while (windowLeaveListeners.length <= windowId) {
windowLeaveListeners.push(new Array());
}
windowLeaveListeners[windowId].push(leaveListener);
}
}
/**
* Removes event handlers for `windowId:Int` from the passed functions that were passed to `notifyWindowed` function.
*/
public function removeWindowed(windowId: Int, ?downListener: Int->Int->Int->Void, ?upListener: Int->Int->Int->Void,
?moveListener: Int->Int->Int->Int->Void, ?wheelListener: Int->Void, ?leaveListener: Void->Void = null): Void {
if (downListener != null) {
if (windowDownListeners != null) {
if (windowId < windowDownListeners.length) {
windowDownListeners[windowId].remove(downListener);
}
else {
trace('no downListeners for window "${windowId}" are registered');
}
}
else {
trace('no downListeners were ever registered');
}
}
if (upListener != null) {
if (windowUpListeners != null) {
if (windowId < windowUpListeners.length) {
windowUpListeners[windowId].remove(upListener);
}
else {
trace('no upListeners for window "${windowId}" are registered');
}
}
else {
trace('no upListeners were ever registered');
}
}
if (moveListener != null) {
if (windowMoveListeners != null) {
if (windowId < windowMoveListeners.length) {
windowMoveListeners[windowId].remove(moveListener);
}
else {
trace('no moveListeners for window "${windowId}" are registered');
}
}
else {
trace('no moveListeners were ever registered');
}
}
if (wheelListener != null) {
if (windowWheelListeners != null) {
if (windowId < windowWheelListeners.length) {
windowWheelListeners[windowId].remove(wheelListener);
}
else {
trace('no wheelListeners for window "${windowId}" are registered');
}
}
else {
trace('no wheelListeners were ever registered');
}
}
if (leaveListener != null) {
if (windowLeaveListeners != null) {
if (windowId < windowLeaveListeners.length) {
windowLeaveListeners[windowId].remove(leaveListener);
}
else {
trace('no leaveListeners for window "${windowId}" are registered');
}
}
else {
trace('no leaveListeners were ever registered');
}
}
}
/**
* Locks the cursor position and hides it. For catching movements, use the `moveX`/`moveY` arguments of your `moveListener` handler.
*/
public function lock(): Void {}
/**
* Unlock the cursor position and hides it. For catching movements, use the `moveX`/`moveY` arguments of your `moveListener` handler.
*/
public function unlock(): Void {}
/**
* Unlocks the cursor position and displays it.
*/
public function canLock(): Bool {
return false;
}
/**
* Returns the status of the cursor lock
*/
public function isLocked(): Bool {
return false;
}
/**
* Creates event handlers from passed functions.
* @param change function fired when the lock is turned on / off.
* @param error function fired when a toggle error occurs.
*/
public function notifyOnLockChange(change: Void->Void, error: Void->Void): Void {}
/**
* Removes event handlers from the passed functions that were passed to `notifyOnLockChange` function.
*/
public function removeFromLockChange(change: Void->Void, error: Void->Void): Void {}
/**
* Hides the system cursor (without locking)
*/
public function hideSystemCursor(): Void {}
/**
* Show the system cursor
*/
public function showSystemCursor(): Void {}
/**
* Set the native system cursor
* @param cursor The native cursor to show.
*/
public function setSystemCursor(cursor: MouseCursor): Void {}
static var instance: Mouse;
var windowDownListeners: Array<Array<Int->Int->Int->Void>>;
var windowUpListeners: Array<Array<Int->Int->Int->Void>>;
var windowMoveListeners: Array<Array<Int->Int->Int->Int->Void>>;
var windowWheelListeners: Array<Array<Int->Void>>;
var windowLeaveListeners: Array<Array<Void->Void>>;
function new() {
super();
instance = this;
}
@input
function sendLeaveEvent(windowId: Int): Void {
if (windowLeaveListeners != null) {
for (listener in windowLeaveListeners[windowId]) {
listener();
}
}
}
@input
function sendDownEvent(windowId: Int, button: Int, x: Int, y: Int): Void {
if (windowDownListeners != null) {
for (listener in windowDownListeners[windowId]) {
listener(button, x, y);
}
}
}
@input
function sendUpEvent(windowId: Int, button: Int, x: Int, y: Int): Void {
if (windowUpListeners != null) {
for (listener in windowUpListeners[windowId]) {
listener(button, x, y);
}
}
}
@input
function sendMoveEvent(windowId: Int, x: Int, y: Int, movementX: Int, movementY: Int): Void {
if (windowMoveListeners != null) {
for (listener in windowMoveListeners[windowId]) {
listener(x, y, movementX, movementY);
}
}
}
@input
function sendWheelEvent(windowId: Int, delta: Int): Void {
if (windowWheelListeners != null) {
for (listener in windowWheelListeners[windowId]) {
listener(delta);
}
}
}
}