-
Notifications
You must be signed in to change notification settings - Fork 63
/
FlxUICursor.hx
1456 lines (1322 loc) · 33.2 KB
/
FlxUICursor.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package flixel.addons.ui;
import flash.events.MouseEvent;
import flixel.addons.ui.Anchor;
import flixel.addons.ui.FlxUIAssets;
import flixel.addons.ui.FlxUICursor.WidgetList;
import flixel.addons.ui.FlxUISprite;
import flixel.addons.ui.interfaces.ICursorPointable;
import flixel.addons.ui.interfaces.IFlxUIWidget;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.input.gamepad.FlxGamepadInputID;
import flixel.input.gamepad.FlxGamepad;
import flixel.input.mouse.FlxMouse;
import flixel.math.FlxMath;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.util.FlxArrayUtil;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
/**
* Cursor object that you can use to "click" on interface elements using a keyboard or gamepad
* TODO: need to support gamepad and/or deal with absence of mouse
*/
class FlxUICursor extends FlxUISprite
{
public var callback:String->IFlxUIWidget->Void; // callback to notify whoever's listening that I did something(presumably a FlxUI object)
public var wrap:Bool = true; // when cycling through values, loop from back to front or stop at "edges?"
public var location(default, set):Int = -1; // which object the cursor is pointing to (-1 means nothing)
public var listIndex(default, set):Int = 0; // which group is my location pointing to?
/**
* Exactly what it sounds like. The next input that would trigger a jump doesn't happen, then this flag is reset.
*/
public var ignoreNextInput:Bool;
/**
* Same as setting .location, but lets you specificy what to do if that object is invisible
* @param loc the location you want to set the cursor to
* @param forwardIfInvisible if object @ loc is invisible, keep adding 1 until we find a good state. If false, subtract 1.
* @param wrap if true, wrap around if we reach the end of the list. If false, don't.
*/
public function findVisibleLocation(loc:Int, forwardIfInvisible:Bool = true, wrap:Bool = true):Void
{
location = loc;
if (location == -1)
return;
var wrapped = false;
while (_widgets[location] == null || _widgets[location].visible == false)
{
if (forwardIfInvisible)
{
if (location == _widgets.length - 1)
{
if (wrap)
{
if (!wrapped)
{
wrapped = true;
location = 0;
}
else
{
location = -1;
return;
}
}
else
{
location = -1;
return;
}
}
else
{
location++;
}
}
else
{
if (location == 0)
{
if (wrap)
{
if (!wrapped)
{
wrapped = true;
location = _widgets.length - 1;
}
else
{
location = -1;
return;
}
}
else
{
location = -1;
return;
}
}
else
{
location--;
}
}
}
}
/**
* Returns the current widget the cursor is pointing to, if any
* @since 2.1.0
*/
public function getCurrentWidget():IFlxUIWidget
{
if (_widgets != null && location >= 0 && location < _widgets.length)
{
return _widgets[location];
}
return null;
}
private function set_listIndex(i:Int):Int
{
if (i >= _lists.length)
{
i = _lists.length - 1;
}
else if (i < 0)
{
i = 0;
}
listIndex = i;
location = 0;
_updateCursor();
return listIndex;
}
private override function set_visible(b:Bool):Bool
{
b = super.set_visible(b);
return b;
}
private function set_location(i:Int):Int
{
if (i >= _widgets.length)
{
i = _widgets.length - 1;
}
location = i;
_updateCursor();
return location;
}
/**
* If a gamepad's connection is suddenly lost, what should be done?
*/
public var gamepadAutoConnect:GamepadAutoConnectPreference = FirstActive;
public var gamepad(get, set):FlxGamepad;
private function set_gamepad(g:FlxGamepad):FlxGamepad
{
_gamepad = g;
setDefaultKeys(_defaultCode);
var arr = [keysUp, keysDown, keysLeft, keysRight, keysClick];
for (list in arr)
{
if (list != null)
{
for (keys in list)
{
if ((keys is FlxMultiGamepad))
{
var fmg:FlxMultiGamepad = cast keys;
fmg.gamepad = _gamepad;
}
}
}
}
return g;
}
private function get_gamepad():FlxGamepad
{
return _gamepad;
}
private var _gamepad:FlxGamepad;
// Key configurations, you can set easily with setDefaultKeys(KEYS_TAB), for instance.
public var keysUp:Array<FlxBaseMultiInput>; // List of keys (ie, tab) and/or key combinations (ie, shift+tab) that indicate intent to go "up"
public var keysDown:Array<FlxBaseMultiInput>;
public var keysLeft:Array<FlxBaseMultiInput>;
public var keysRight:Array<FlxBaseMultiInput>;
public var keysClick:Array<FlxBaseMultiInput>; // intent to "click" or select
// Various default key configurations:
public static inline var KEYS_TAB:Int = 0x00000001; // tab to go "right", shift+tab to go "left", enter to click
public static inline var KEYS_WASD:Int = 0x00000010; // WASD to go up/left/down/right, enter to click
public static inline var KEYS_ARROWS:Int = 0x00000100; // Arrows to go up/left/down/right, enter to click
public static inline var KEYS_NUMPAD:Int = 0x00001000; // Numpad numbers to go up/left/down/right, enter to click
public static inline var GAMEPAD_DPAD:Int = 0x00010000; // DPAD to go up/left/down/right, A to click
public static inline var GAMEPAD_LEFT_STICK:Int = 0x00100000; // Left STICK to go up/left/down/right, A to click
public static inline var GAMEPAD_RIGHT_STICK:Int = 0x01000000; // Right STICK to go up/left/down/right, A to click
public static inline var GAMEPAD_SHOULDER_BUTTONS:Int = 0x10000000; // Left / Right shoulder buttons to go left/right, A to click
// Determines how the cursor attaches itself to the widget it's pointing to
public var anchor:Anchor;
public var dispatchEvents:Bool = true; // set to false if you just want to rely on callbacks rather than low-level events
// TODO: make this work
public var inputMethod:Int = 0x00; // simple bitmask for storing what input methods can move the cursor
public static inline var INPUT_NONE:Int = 0x00; // No cursor input what
public static inline var INPUT_KEYS:Int = 0x01; // Use keyboard to control the cursor
public static inline var INPUT_GAMEPAD:Int = 0x10; // Use gamepad to control the cursor
/*********************************/
/**
* Creates a cursor that can be controlled with the keyboard or gamepad
* @param Callback callback to notify listener about when something happens
* @param InputMethod bit-flag, accepts INPUT_KEYS, INPUT_GAMEPAD, or both using "|" operator
* @param DefaultKeys default hotkey layouts, accepts KEYS_TAB, ..._WASD, etc, combine using "|" operator
* @param Asset visual asset for the cursor. If not supplied, uses default
*/
public function new(Callback:String->IFlxUIWidget->Void, InputMethod:Int = INPUT_KEYS, DefaultKeys:Int = KEYS_TAB, ?Asset:Dynamic)
{
if (Asset == null)
{ // No asset detected? Guess based on game's resolution
if (FlxG.height < 400)
{
Asset = FlxUIAssets.IMG_FINGER_SMALL; // 16x16 pixel finger
}
else
{
Asset = FlxUIAssets.IMG_FINGER_BIG; // 32x32 pixel finger
}
}
super(0, 0, Asset);
inputMethod = InputMethod;
_lists = [
{
x: 0,
y: 0,
width: 0,
height: 0,
widgets: []
}
];
_widgets = _lists[0].widgets;
anchor = new Anchor(-2, 0, Anchor.LEFT, Anchor.CENTER, Anchor.RIGHT, Anchor.CENTER);
setDefaultKeys(DefaultKeys);
callback = Callback;
scrollFactor.set(0, 0);
#if FLX_MOUSE
if (FlxG.mouse != null && (FlxG.mouse is FlxUIMouse) == false)
{
_newMouse = new FlxUIMouse(FlxG.mouse.cursorContainer);
FlxG.mouse = _newMouse;
}
else
{
_newMouse = cast FlxG.mouse;
}
#end
}
public override function destroy():Void
{
super.destroy();
#if FLX_MOUSE
if (FlxG.mouse == _newMouse)
{
// remove the local pointer, but allow the replaced mouse object to carry on, it won't hurt anything
_newMouse = null;
}
#end
keysUp = FlxDestroyUtil.destroyArray(keysUp);
keysDown = FlxDestroyUtil.destroyArray(keysDown);
keysLeft = FlxDestroyUtil.destroyArray(keysLeft);
keysRight = FlxDestroyUtil.destroyArray(keysRight);
keysClick = FlxDestroyUtil.destroyArray(keysClick);
anchor = FlxDestroyUtil.destroy(anchor);
for (l in _lists)
{
U.clearArraySoft(l.widgets);
}
U.clearArraySoft(_lists);
_widgets = null;
}
public override function update(elapsed:Float):Void
{
#if FLX_GAMEPAD
if (gamepad == null)
{
var g = getGamepad(false);
if (g != null)
{
gamepad = g;
}
}
#end
#if FLX_MOUSE
if (lastMouseX != FlxG.mouse.x || lastMouseY != FlxG.mouse.y)
{
var oldVis = visible;
// Ad hoc fix to avoid world coordinates on UI elements
if (scrollFactor.x == 0 && scrollFactor.y == 0)
jumpToXY(FlxG.mouse.screenX, FlxG.mouse.screenY);
else
jumpToXY(FlxG.mouse.x, FlxG.mouse.y);
visible = oldVis;
lastMouseX = FlxG.mouse.x;
lastMouseY = FlxG.mouse.y;
}
#end
_checkKeys();
_clickTime += elapsed;
super.update(elapsed);
}
public function addWidgetsFromUI(ui:FlxUI)
{
if (ui.cursorLists != null)
{
for (list in ui.cursorLists)
{
addWidgetList(list);
}
_widgets = _lists[0].widgets;
location = 0;
listIndex = 0;
}
else
{
for (widget in ui.members)
{
if ((widget is ICursorPointable) || (widget is FlxUIGroup)) // if it's directly pointable or a group
{
addWidget(cast widget); // add it
}
}
}
}
/**
* Forces the cursor to change its location to point to this widget, if the widget is in its list
* @param widget
* @return whether the widget was found or not
*/
public function jumpTo(widget:IFlxUIWidget):Bool
{
var listi:Int = 0;
var i:Int = 0;
if (_lists != null)
{
for (list in _lists)
{
i = list.widgets.indexOf(widget);
if (i != -1)
{
listIndex = listi;
location = i;
return true;
}
listi++;
}
}
else
{
i = _widgets.indexOf(widget);
location = i;
return true;
}
return false;
}
/**
* Forces the cursor to to point to whatever widget's center is closest to X,Y -- IF the widget is in its list
* @param X
* @param Y
* @return
*/
public function jumpToXY(X:Float, Y:Float):Bool
{
var listi:Int = 0;
var bestd2 = Math.POSITIVE_INFINITY;
var bestli = -1;
var besti = -1;
if (_lists != null)
{
for (list in _lists)
{
for (i in 0...list.widgets.length)
{
var w:IFlxUIWidget = list.widgets[i];
if (w.visible == true && X >= w.x && Y >= w.y && X <= w.x + w.width && Y <= w.y + w.height)
{
var dx = ((w.x + w.width / 2) - X);
var dy = ((w.y + w.height / 2) - Y);
var d2 = dx * dx + dy * dy;
if (d2 < bestd2)
{
bestd2 = d2;
bestli = listi;
besti = i;
}
}
}
listi++;
}
if (bestli != -1 && besti != -1)
{
listIndex = bestli;
location = besti;
return true;
}
}
else
{
for (i in 0..._widgets.length)
{
var w:IFlxUIWidget = _widgets[i];
if (w.visible == true && X >= w.x && Y >= w.y && X <= w.x + w.width && Y <= w.y + w.height)
{
var dx = ((w.x + w.width / 2) - X);
var dy = ((w.y + w.height / 2) - Y);
var d2 = dx * dx + dy * dy;
if (d2 < bestd2)
{
bestd2 = d2;
besti = i;
}
}
}
if (besti != -1)
{
location = besti;
return true;
}
}
return false;
}
public function addWidgetList(list:Array<IFlxUIWidget>):Void
{
for (l in _lists)
{
if (FlxArrayUtil.equals(l.widgets, list))
{
return;
}
}
var x1 = Math.POSITIVE_INFINITY;
var y1 = Math.POSITIVE_INFINITY;
var x2 = Math.NEGATIVE_INFINITY;
var y2 = Math.NEGATIVE_INFINITY;
for (w in list)
{
if (w.x < x1)
x1 = w.x;
if (w.y < y1)
y1 = w.y;
if (w.x + w.width > x2)
x2 = w.x;
if (w.y + w.height > y2)
y2 = w.y;
}
var theList:WidgetList = null;
if (_lists.length == 1 && _lists[0].widgets != null && _lists[0].widgets.length == 0)
{
_lists[0].widgets = [];
_lists[0].x = Std.int(x1);
_lists[0].y = Std.int(y1);
_lists[0].width = Std.int(x2 - x1);
_lists[0].height = Std.int(y2 - y1);
theList = _lists[0];
}
else
{
_lists.push({
x: Std.int(x1),
y: Std.int(y1),
width: Std.int(x2 - x1),
height: Std.int(y2 - y1),
widgets: []
});
theList = _lists[_lists.length - 1];
}
var oldWidgets = _widgets;
_widgets = theList.widgets;
for (ifw in list)
{
addWidget(ifw);
}
_widgets = oldWidgets;
_lists.sort(_sortXYWidgetList);
for (widgetList in _lists)
{
widgetList.widgets.sort(_sortXYVisible);
}
}
public function addWidget(widget:IFlxUIWidget):Void
{
if ((widget is ICursorPointable)) // directly pointable? add it
{
_widgets.push(widget);
}
else if ((widget is FlxUIGroup)) // it's a group?
{
var g:FlxUIGroup = cast widget;
for (member in g.members)
{
if ((member is IFlxUIWidget))
{
addWidget(cast member); // add each member individually
}
}
}
_widgets.sort(_sortXYVisible);
}
public function sortWidgets(method:SortMethod, ?list:Array<IFlxUIWidget>):Void
{
if (list == null)
{
list = _widgets;
}
switch (method)
{
case SortMethod.XY:
list.sort(_sortXYVisible);
case SortMethod.ID:
list.sort(_sortIDVisible);
}
}
/** @since 2.1.0 */
public function clearWidgets():Void
{
FlxArrayUtil.clearArray(_widgets);
}
public function removeWidget(widget:IFlxUIWidget, ?list:Array<IFlxUIWidget>):Bool
{
if (list == null)
{
list = _widgets;
}
var value:Bool = false;
if (list != null)
{
if (list.indexOf(widget) != -1)
{
value = list.remove(widget);
list.sort(_sortXYVisible);
}
}
return value;
}
/**
* Set the default key layout quickly using a constant.
* @param code KEYS_TAB, ..._WASD, etc, combine with "|" operator
*/
public function setDefaultKeys(code:Int):Void
{
_defaultCode = code;
_clearKeys();
_newKeys();
if (code & KEYS_TAB == KEYS_TAB)
{
_addToKeys(keysRight, new FlxMultiKey(TAB, null, [SHIFT])); // Tab, (but NOT Shift+Tab!)
_addToKeys(keysLeft, new FlxMultiKey(TAB, [SHIFT])); // Shift+Tab
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}
if (code & KEYS_ARROWS == KEYS_ARROWS)
{
_addToKeys(keysRight, new FlxMultiKey(RIGHT));
_addToKeys(keysLeft, new FlxMultiKey(LEFT));
_addToKeys(keysDown, new FlxMultiKey(DOWN));
_addToKeys(keysUp, new FlxMultiKey(UP));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}
if (code & KEYS_WASD == KEYS_WASD)
{
_addToKeys(keysRight, new FlxMultiKey(D));
_addToKeys(keysLeft, new FlxMultiKey(A));
_addToKeys(keysDown, new FlxMultiKey(S));
_addToKeys(keysUp, new FlxMultiKey(W));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}
if (code & KEYS_NUMPAD == KEYS_NUMPAD)
{
_addToKeys(keysRight, new FlxMultiKey(NUMPADSIX));
_addToKeys(keysLeft, new FlxMultiKey(NUMPADFOUR));
_addToKeys(keysDown, new FlxMultiKey(NUMPADTWO));
_addToKeys(keysUp, new FlxMultiKey(NUMPADEIGHT));
_addToKeys(keysClick, new FlxMultiKey(ENTER));
}
#if FLX_GAMEPAD
if (gamepad == null)
{
_gamepad = getGamepad(); // set _gamepad to avoid a stack overflow loop
}
if (code & GAMEPAD_DPAD == GAMEPAD_DPAD)
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_LEFT));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_RIGHT));
_addToKeys(keysDown, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_DOWN));
_addToKeys(keysUp, new FlxMultiGamepad(gamepad, FlxGamepadInputID.DPAD_UP));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code & GAMEPAD_SHOULDER_BUTTONS == GAMEPAD_SHOULDER_BUTTONS)
{
_addToKeys(keysLeft, new FlxMultiGamepad(gamepad, FlxGamepadInputID.LEFT_SHOULDER));
_addToKeys(keysRight, new FlxMultiGamepad(gamepad, FlxGamepadInputID.RIGHT_SHOULDER));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code & GAMEPAD_LEFT_STICK == GAMEPAD_LEFT_STICK)
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: LEFT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
if (code & GAMEPAD_RIGHT_STICK == GAMEPAD_RIGHT_STICK)
{
_addToKeys(keysLeft, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: false}));
_addToKeys(keysRight, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: X, positive: true}));
_addToKeys(keysUp, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: false}));
_addToKeys(keysDown, new FlxMultiGamepadAnalogStick(gamepad, {id: RIGHT_ANALOG_STICK, axis: Y, positive: true}));
_addToKeys(keysClick, new FlxMultiGamepad(gamepad, FlxGamepadInputID.A));
}
#end
}
/****PRIVATE****/
private var _lists:Array<WidgetList>; // list of lists of widgets with boundary metadata
private var _widgets:Array<IFlxUIWidget>; // list of widgets under cursor's control
#if FLX_MOUSE
private var _newMouse:FlxUIMouse;
private var lastMouseX:Float = 0;
private var lastMouseY:Float = 0;
#end
private var _clickPressed:Bool = false;
private var _defaultCode:Int;
private var _rightAnchor:Anchor;
private var _topAnchor:Anchor;
private var _leftAnchor:Anchor;
private var _bottomAnchor:Anchor;
private var _clickTime:Float = 0;
#if FLX_GAMEPAD
private function getGamepad(exhaustive:Bool = true):FlxGamepad
{
var gamepad = switch (gamepadAutoConnect)
{
case Never: null;
case FirstActive: FlxG.gamepads.getFirstActiveGamepad();
case LastActive: FlxG.gamepads.lastActive;
case GamepadID(i): FlxG.gamepads.getByID(i);
}
if (gamepad == null && exhaustive)
{
for (i in 0...FlxG.gamepads.numActiveGamepads)
{
gamepad = FlxG.gamepads.getByID(i);
if (gamepad != null)
{
return gamepad;
}
}
}
return gamepad;
}
#end
private function _sortIDVisible(a:IFlxUIWidget, b:IFlxUIWidget):Int
{
if (a.visible && !b.visible)
return -1;
if (b.visible && !a.visible)
return 1;
if (a.ID < b.ID)
return -1;
if (a.ID > b.ID)
return 1;
return 0;
}
private function _sortXYWidgetList(a:WidgetList, b:WidgetList):Int
{
if (a.y < b.y)
return -1;
if (a.y > b.y)
return 1;
if (a.x < b.x)
return -1;
if (a.x > b.x)
return 1;
return 0;
}
private function _sortXYVisible(a:IFlxUIWidget, b:IFlxUIWidget):Int
{
if (a.visible && !b.visible)
return -1;
if (b.visible && !a.visible)
return 1;
if (a.y < b.y)
return -1;
if (a.y > b.y)
return 1;
if (a.x < b.x)
return -1;
if (a.x > b.x)
return 1;
return 0;
}
private function _addToKeys(keys:Array<FlxBaseMultiInput>, m:FlxBaseMultiInput)
{
var exists:Bool = false;
for (mk in keys)
{
if (m.equals(mk))
{
exists = true;
break;
}
}
if (!exists)
{
keys.push(m);
}
}
private function _clearKeys():Void
{
U.clearArray(keysUp);
keysUp = null;
U.clearArray(keysDown);
keysDown = null;
U.clearArray(keysLeft);
keysLeft = null;
U.clearArray(keysRight);
keysRight = null;
U.clearArray(keysClick);
keysClick = null;
}
private function _newKeys():Void
{
keysUp = [];
keysDown = [];
keysLeft = [];
keysRight = [];
keysClick = [];
}
private function _checkKeys():Void
{
var wasInvisible = (visible == false);
var lastLocation = location;
for (key in keysUp)
{
if (key.justPressed())
{
_doInput(0, -1);
break;
}
}
for (key in keysDown)
{
if (key.justPressed())
{
_doInput(0, 1);
break;
}
}
for (key in keysLeft)
{
if (key.justPressed())
{
_doInput(-1, 0);
break;
}
}
for (key in keysRight)
{
if (key.justPressed())
{
_doInput(1, 0);
break;
}
}
if (wasInvisible && visible && lastLocation != -1)
{
location = lastLocation;
}
if (_clickKeysJustPressed()) // JUST PRESSED: send a press event only the first time it's pressed
{
if (!ignoreNextInput)
{
_clickPressed = true;
_clickTime = 0;
_doPress();
}
else
{
ignoreNextInput = false;
}
}
if (_clickKeysPressed()) // STILL PRESSED: keep the cursor in that position while the key is down
{
_clickPressed = true;
_doMouseMove();
}
else if (_clickTime > 0) // NOT PRESSED and not exact same frame as when it was just pressed
{
if (_clickPressed) // if we were previously just pressed...
{
_doRelease(); // do the release action
_clickPressed = false; // count this as "just released"
}
}
}
private function _clickKeysJustPressed():Bool
{
for (key in keysClick)
{
if (key.justPressed())
{
return true;
}
}
return false;
}
private function _clickKeysPressed():Bool
{
for (key in keysClick)
{
if (key.pressed())
{
return true;
}
}
return false;
}
private function _getWidgetPoint(?Camera:FlxCamera):FlxPoint
{
if (Camera == null)
Camera = FlxG.camera;
// get the widget;
var currWidget:IFlxUIWidget = _widgets[location];
if (currWidget == null)
{
return null;
}
var fo:FlxObject;
var widgetPoint:FlxPoint = null;
// Try to convert to FlxObject if possible
if ((currWidget is FlxObject))
{
fo = cast currWidget;
// success! Get ScreenXY, to deal with any possible scrolling/camera craziness
widgetPoint = fo.getScreenPosition();
}
widgetPoint.x *= Camera.totalScaleX;
widgetPoint.y *= Camera.totalScaleY;
if (widgetPoint == null)
{
// otherwise just make your best guess from current raw position
widgetPoint = FlxPoint.get(currWidget.x, currWidget.y);
}
// get center point of object
widgetPoint.x += currWidget.width / 2;
widgetPoint.y += currWidget.height / 2;
return widgetPoint;
}
private function _doMouseMove(?pt:FlxPoint):Void
{
var dispose:Bool = false;
if (pt == null)
{
pt = _getWidgetPoint();
if (pt == null)
{
return;
}
dispose = true;
}
if (dispatchEvents)
{
#if FLX_MOUSE
// REALLY force it to this location
FlxG.mouse.setGlobalScreenPositionUnsafe(pt.x, pt.y);
if (_newMouse != null)
{
_newMouse.updateGlobalScreenPosition = false; // don't low-level-update the mouse while I'm overriding the mouse position
}
#if FLX_KEYBOARD
FlxG.stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE, true, false, pt.x, pt.y, FlxG.stage, FlxG.keys.pressed.CONTROL,
FlxG.keys.pressed.ALT, FlxG.keys.pressed.SHIFT));
#end
#end
}
if (dispose)
{
pt.put();
}
}
private function _doPress(?pt:FlxPoint):Void
{
var currWidget:IFlxUIWidget = _widgets[location];
if (currWidget == null)
{
return;
}
var dispose:Bool = false;
if (pt == null)
{
pt = _getWidgetPoint();
if (pt == null)
{
return;
}
dispose = true;
}
#if FLX_MOUSE
if (dispatchEvents)
{
var rawMouseX:Float = pt.x * FlxG.camera.zoom;
var rawMouseY:Float = pt.y * FlxG.camera.zoom;
#if FLX_KEYBOARD
FlxG.stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, rawMouseX, rawMouseY, FlxG.stage, FlxG.keys.pressed.CONTROL,
FlxG.keys.pressed.ALT, FlxG.keys.pressed.SHIFT));
#else
FlxG.stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, rawMouseX, rawMouseY, FlxG.stage));
#end
}