-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathInteractive.hx
433 lines (372 loc) · 13.5 KB
/
Interactive.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package h2d;
/**
A user input handler.
Hitbox area can be a rectangle, an ellipse or an arbitrary shape (`h2d.col.Collider`).
Note that Interactive does not reports its hitbox bounds in `Object.getBounds`
unless `Interactive.backgroundColor` is set, in which case `width` and `height` are reported.
By default, Interactive only reacts to primary (left) mouse button for actions, see `Interactive.enableRightButton` for details.
**/
@:allow(h2d.Scene)
class Interactive extends Object implements hxd.SceneEvents.Interactive {
/**
Width of the Interactive. Ignored if `Interactive.shape` is set.
**/
public var width : Float;
/**
Height of the Interactive. Ignored if `Interactive.shape` is set.
**/
public var height : Float;
/**
Cursor used when Interactive is under mouse cursor.
**/
public var cursor(default,set) : Null<hxd.Cursor> = Button;
/**
Performs an elliptic hit-test instead of rectangular one based on `Interactive.width` and `height`. Ignored if `Interactive.shape` is set.
**/
public var isEllipse : Bool;
/**
Set the default `hxd.Event.cancel` mode.
**/
public var cancelEvents : Bool = false;
/**
Set the default `hxd.Event.propagate` mode.
**/
public var propagateEvents : Bool = false;
/**
When enabled, interacting with secondary mouse buttons (right button/wheel) will cause `onPush`, `onClick`, `onRelease` and `onReleaseOutside` callbacks.
Otherwise those callbacks will only be triggered with primary mouse button (left button).
**/
public var enableRightButton : Bool = false;
/**
When enabled, allows to receive several onClick events the same frame.
**/
public var allowMultiClick : Bool = false;
/**
If set, Interactive will draw a `Tile` with `[width, height]` dimensions of specified color (including alpha).
**/
public var backgroundColor : Null<Int>;
var scene : Scene;
var mouseDownButton : Int = -1;
var lastClickFrame : Int = -1;
var invDet : Float;
var maskedBounds : h2d.col.Bounds;
/**
Detailed shape collider for Interactive.
If set, `width` and `height` properties are ignored for collision checks.
**/
public var shape : h2d.col.Collider;
/**
Detailed shape X offset from Interactive.
**/
public var shapeX : Float = 0;
/**
Detailed shape Y offset from Interactive.
**/
public var shapeY : Float = 0;
/**
Create a new Interactive with specified parameters. `width`, `height` and `parent` with optional detailed `shape`.
@param width The width of the Interactive hitbox.
@param height The height of the Interactive hitbox.
@param parent An optional parent `h2d.Object` instance to which Interactive adds itself if set.
@param shape An optional detailed Interactive hitbox.
**/
public function new( width, height, ?parent, ?shape ) {
super(parent);
this.width = width;
this.height = height;
this.shape = shape;
}
override function onAdd() {
this.scene = getScene();
if( scene != null ) scene.addEventTarget(this);
super.onAdd();
}
override function draw( ctx : RenderContext ) {
maskedBounds = ctx.getCurrentRenderZone(maskedBounds);
if( backgroundColor != null ) emitTile(ctx, h2d.Tile.fromColor(backgroundColor, Std.int(width), Std.int(height), (backgroundColor>>>24)/255 ));
}
override function getBoundsRec( relativeTo, out, forSize ) {
super.getBoundsRec(relativeTo, out, forSize);
if( backgroundColor != null || forSize ) addBounds(relativeTo, out, 0, 0, Std.int(width), Std.int(height));
}
override private function onHierarchyMoved(parentChanged:Bool)
{
super.onHierarchyMoved(parentChanged);
if( scene != null ) {
scene.removeEventTarget(this);
scene = getScene();
if( scene != null )
scene.addEventTarget(this);
}
}
override function onRemove() {
if( scene != null ) {
scene.removeEventTarget(this, true);
scene = null;
}
super.onRemove();
}
function checkBounds( e : hxd.Event ) {
return switch( e.kind ) {
case EOut, EFocus, EFocusLost, EReleaseOutside: false;
default: true;
}
}
/**
Reset current pressed state of the Interactive, preventing the `onClick` or `onReleaseOutside` being triggered when user releases mouse button.
**/
public function preventClick() {
mouseDownButton = -1;
}
@:dox(hide)
@:noCompletion public function getInteractiveScene() : hxd.SceneEvents.InteractiveScene {
return scene;
}
@:dox(hide)
@:noCompletion public function handleEvent( e : hxd.Event ) {
if( maskedBounds != null && checkBounds(e) ) {
var pt = new h2d.col.Point(e.relX, e.relY);
localToGlobal(pt);
if( pt.x < maskedBounds.xMin || pt.y < maskedBounds.yMin || pt.x > maskedBounds.xMax || pt.y > maskedBounds.yMax ) {
e.cancel = true;
return;
}
}
if(shape == null && isEllipse && checkBounds(e) ) {
var cx = width * 0.5, cy = height * 0.5;
var dx = (e.relX - cx) / cx;
var dy = (e.relY - cy) / cy;
if( dx * dx + dy * dy > 1 ) {
e.cancel = true;
return;
}
}
if( propagateEvents ) e.propagate = true;
if( cancelEvents ) e.cancel = true;
switch( e.kind ) {
case EMove:
onMove(e);
case EPush:
if( enableRightButton || e.button == 0 ) {
mouseDownButton = e.button;
onPush(e);
if( e.cancel ) mouseDownButton = -1;
}
case ERelease:
if( enableRightButton || e.button == 0 ) {
onRelease(e);
var frame = hxd.Timer.frameCount;
if( mouseDownButton == e.button && (lastClickFrame != frame || allowMultiClick) ) {
onClick(e);
lastClickFrame = frame;
}
}
mouseDownButton = -1;
case EReleaseOutside:
if( enableRightButton || e.button == 0 ) {
onRelease(e);
if ( mouseDownButton == e.button )
onReleaseOutside(e);
}
mouseDownButton = -1;
case EOver:
onOver(e);
case EOut:
onOut(e);
case EWheel:
e.propagate = true; // propagate wheel events by default
onWheel(e);
case EFocusLost:
onFocusLost(e);
case EFocus:
onFocus(e);
case EKeyUp:
onKeyUp(e);
case EKeyDown:
onKeyDown(e);
case ECheck:
onCheck(e);
case ETextInput:
onTextInput(e);
}
}
override private function calcAbsPos()
{
super.calcAbsPos();
invDet = 1 / (matA * matD - matB * matC);
}
function set_cursor(c) {
this.cursor = c;
if ( scene != null && scene.events != null )
scene.events.updateCursor(this);
return c;
}
function eventToLocal( e : hxd.Event ) {
// convert scene event to our local space
var i = this;
var dx = e.relX - i.absX;
var dy = e.relY - i.absY;
e.relX = ( dx * i.matD - dy * i.matC) * i.invDet;
e.relY = (-dx * i.matB + dy * i.matA) * i.invDet;
}
/**
Starts input events capture and redirects them to `callb` method until `Interactive.stopCapture` is called.
While the method name may imply that only mouse events would be captured: This is not the case,
as it will also capture all other input events, including keyboard events.
Starting event capture through `Interactive.startCapture` will convert `Event.relX` and `relY` to local coordinates
of the Interactive and will restore them after invoking `callb`.
In order to receive coordinates in scene coordinate space use `Scene.startCapture`.
@param callb A callback method that receives `hxd.Event` when input event happens.
Unless `callb` sets `Event.propagate` to `true`, event won't be sent to other Interactives.
@param onCancel An optional callback that is invoked when `Interactive.stopCapture` is called.
**/
public function startCapture(callb : hxd.Event -> Void, ?onCancel : Void -> Void, ?touchId : Int) {
scene.startCapture(function(event) {
var x = event.relX, y = event.relY;
eventToLocal(event);
callb(event);
event.relX = x;
event.relY = y;
}, onCancel, touchId);
}
/**
Stops current input event capture.
**/
public function stopCapture() {
scene.stopCapture();
}
@:deprecated("Renamed to startCapture") @:dox(hide)
public inline function startDrag(callb,?onCancel) {
startCapture(callb, onCancel);
}
@:deprecated("Renamed to stopCapture") @:dox(hide)
public inline function stopDrag() {
stopCapture();
}
/**
Sets focus on this `Interactive`.
If Interactive was not already focused and it receives focus - `onFocus` event is sent.
Interactive won't become focused if during `onFocus` call it will set `Event.cancel` to `true`.
**/
public function focus() {
if( scene == null || scene.events == null )
return;
scene.events.focus(this);
}
/**
Removes focus from interactive if it's focused.
If Interactive is currently focused - `onFocusLost` event will be sent.
Interactive won't lose focus if during `onFocusLost` call it will set `Event.cancel` to `true`.
**/
public function blur() {
if( hasFocus() ) scene.events.blur();
}
/**
Checks if Interactive is currently hovered by the mouse.
**/
public function isOver() {
return scene != null && scene.events != null && @:privateAccess scene.events.overList.indexOf(this) != -1;
}
/**
Checks if Interactive is currently focused.
**/
public function hasFocus() {
return scene != null && scene.events != null && @:privateAccess scene.events.currentFocus == this;
}
/**
Sent when mouse enters Interactive hitbox area.
`Event.propagate` and `Event.cancel` are ignored during `onOver`.
Propagation can be set with `onMove` event, as well as cancelling `onMove` will prevent `onOver`.
**/
public dynamic function onOver( e : hxd.Event ) {
}
/**
Sent when mouse exits Interactive hitbox area.
`Event.propagate` and `Event.cancel` are ignored during `onOut`.
**/
public dynamic function onOut( e : hxd.Event ) {
}
/**
Sent when Interactive is pressed by the user.
**/
public dynamic function onPush( e : hxd.Event ) {
}
/**
Sent when Interactive is unpressed under multiple circumstances.
* Always sent if user releases mouse while it is inside Interactive hitbox area.
This happens regardless if that Interactive was pressed prior or not,
and due to that it's not guaranteed that `Interactive.onPush` would precede this event.
`Event.kind` is set to `ERelease` during this event.
* Sent before `Interactive.onReleaseOutside` if this Interactive was pressed, but released outside its hitbox area.
`Event.kind` is set to `EReleaseOutside` during this event.
See `Interactive.onClick` and `Interactive.onReleaseOutside` methods for separate events that trigger only when user interacts with this particular Interactive.
**/
public dynamic function onRelease( e : hxd.Event ) {
}
/**
Sent when user presses the Interactive, moves the mouse outside its hitbox area and releases the mouse button.
Can be prevented to fire by calling `Interactive.preventClick` during or after `Interactive.onPush` event.
`Interactive.onRelease` is sent with `Event.kind` being `EReleaseOutside` just before this event.
**/
public dynamic function onReleaseOutside( e : hxd.Event ) {
}
/**
Sent when the Interactive is clicked by the user.
Can be prevented to fire by calling `Interactive.preventClick` during or after `Interactive.onPush` event.
`Interactive.onRelease` is sent with `Event.kind` being `ERelease` just before this event.
**/
public dynamic function onClick( e : hxd.Event ) {
}
/**
Sent when user moves within the Interactive hitbox area.
See `Interactive.onCheck` for event when user does not move the mouse.
Cancelling the `Event` will prevent interactive from becoming overed,
causing `Interactive.onOut` if it was overed previously.
Interactive would be treated as not overed as long as event is cancelled even if mouse is within the hitbox area.
**/
public dynamic function onMove( e : hxd.Event ) {
}
/**
Sent when user scrolls mouse wheel above the Interactive. Wheel delta can be obtained through the `Event.wheelDelta`.
**/
public dynamic function onWheel( e : hxd.Event ) {
}
/**
Sent when Interactive receives focus during `Interactive.focus` call.
Cancelling the `Event` will prevent the Interactive from becoming focused.
**/
public dynamic function onFocus( e : hxd.Event ) {
}
/**
Sent when Interactive lost focus either via `Interactive.blur` call or when user clicks on another Interactive/outside this Interactive hitbox area.
Cancelling the `Event` will prevent the Interactive from losing focus.
**/
public dynamic function onFocusLost( e : hxd.Event ) {
}
/**
Sent when this Interactive is focused and user unpressed a keyboard key.
Unpressed key can be accessed through `Event.keyCode`.
**/
public dynamic function onKeyUp( e : hxd.Event ) {
}
/**
Sent when this Interactive is focused and user pressed a keyboard key.
Pressed key can be accessed through `Event.keyCode`.
**/
public dynamic function onKeyDown( e : hxd.Event ) {
}
/**
Sent every frame when user hovers an Interactive but does not move the mouse.
See `Interactive.onMove` for event when user moves the mouse.
Cancelling the `Event` will prevent interactive from becoming overed,
causing `Interactive.onOut` if it was overed previously.
Interactive would be treated as not overed as long as event is cancelled even if mouse is within the hitbox area.
**/
public dynamic function onCheck( e : hxd.Event ) {
}
/**
Sent when this Interactive is focused and user inputs text. Character added can be accessed through `Event.charCode`.
**/
public dynamic function onTextInput( e : hxd.Event ) {
}
}