@@ -18,6 +18,21 @@ import flixel.util.FlxDestroyUtil;
18
18
import flixel .input .touch .FlxTouch ;
19
19
#end
20
20
21
+ enum abstract FlxButtonState (Int ) to Int
22
+ {
23
+ /** The button is not highlighted or pressed */
24
+ var NORMAL = 0 ;
25
+
26
+ /** The button is selected, usually meaning the mouse is hovering over it */
27
+ var HIGHLIGHT = 1 ;
28
+
29
+ /** The button is being pressed usually by a mouse */
30
+ var PRESSED = 2 ;
31
+
32
+ /** The button is not interactible */
33
+ var DISABLED = 3 ;
34
+ }
35
+
21
36
/**
22
37
* A simple button class that calls a function when clicked by the mouse.
23
38
*/
@@ -26,17 +41,30 @@ class FlxButton extends FlxTypedButton<FlxText>
26
41
/**
27
42
* Used with public variable status, means not highlighted or pressed.
28
43
*/
29
- public static inline var NORMAL : Int = 0 ;
44
+ @:dox (hide ) @:noCompletion
45
+ @:deprecated (" FlxButton.NORMAL is deprecated, use FlxButtonState.NORMAL" )
46
+ public static inline var NORMAL = FlxButtonState .NORMAL ;
30
47
31
48
/**
32
49
* Used with public variable status, means highlighted (usually from mouse over).
33
50
*/
34
- public static inline var HIGHLIGHT : Int = 1 ;
51
+ @:dox (hide ) @:noCompletion
52
+ @:deprecated (" FlxButton.HIGHLIGHT is deprecated, use FlxButtonState.HIGHLIGHT" )
53
+ public static inline var HIGHLIGHT = FlxButtonState .HIGHLIGHT ;
35
54
36
55
/**
37
56
* Used with public variable status, means pressed (usually from mouse click).
38
57
*/
39
- public static inline var PRESSED : Int = 2 ;
58
+ @:dox (hide ) @:noCompletion
59
+ @:deprecated (" FlxButton.PRESSED is deprecated, use FlxButtonState.PRESSED" )
60
+ public static inline var PRESSED = FlxButtonState .PRESSED ;
61
+
62
+ /**
63
+ * Used with public variable status, means non interactible.
64
+ */
65
+ @:dox (hide ) @:noCompletion
66
+ @:deprecated (" FlxButton.DISABLED is deprecated, use FlxButtonState.DISABLED" )
67
+ public static inline var DISABLED = FlxButtonState .DISABLED ;
40
68
41
69
/**
42
70
* Shortcut to setting label.text
@@ -80,7 +108,7 @@ class FlxButton extends FlxTypedButton<FlxText>
80
108
{
81
109
if (Text != null )
82
110
{
83
- label = new FlxText (x + labelOffsets [NORMAL ].x , y + labelOffsets [NORMAL ].y , 80 , Text );
111
+ label = new FlxText (x + labelOffsets [FlxButtonState . NORMAL ].x , y + labelOffsets [FlxButtonState . NORMAL ].y , 80 , Text );
84
112
label .setFormat (null , 8 , 0x333333 , " center" );
85
113
label .alpha = labelAlphas [status ];
86
114
label .drawFrame (true );
@@ -122,19 +150,19 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
122
150
/**
123
151
* What offsets the `label` should have for each status.
124
152
*/
125
- public var labelOffsets : Array <FlxPoint > = [FlxPoint .get (), FlxPoint .get (), FlxPoint .get (0 , 1 )];
153
+ public var labelOffsets : Array <FlxPoint > = [FlxPoint .get (), FlxPoint .get (), FlxPoint .get (0 , 1 ), FlxPoint . get () ];
126
154
127
155
/**
128
156
* What alpha value the label should have for each status. Default is `[0.8, 1.0, 0.5]`.
129
157
* Multiplied with the button's `alpha`.
130
158
*/
131
- public var labelAlphas : Array <Float > = [0.8 , 1.0 , 0.5 ];
159
+ public var labelAlphas : Array <Float > = [0.8 , 1.0 , 0.5 , 0.3 ];
132
160
133
161
/**
134
162
* What animation should be played for each status.
135
163
* Default is ["normal", "highlight", "pressed"].
136
164
*/
137
- public var statusAnimations : Array <String > = [" normal" , " highlight" , " pressed" ];
165
+ public var statusAnimations : Array <String > = [" normal" , " highlight" , " pressed" , " disabled " ];
138
166
139
167
/**
140
168
* Whether you can press the button simply by releasing the touch / mouse button over it (default).
@@ -157,10 +185,10 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
157
185
public var maxInputMovement : Float = Math .POSITIVE_INFINITY ;
158
186
159
187
/**
160
- * Shows the current state of the button, either `FlxButton. NORMAL`,
161
- * `FlxButton. HIGHLIGHT` or `FlxButton. PRESSED`.
188
+ * Shows the current state of the button, either `NORMAL`,
189
+ * `HIGHLIGHT` or `PRESSED`.
162
190
*/
163
- public var status (default , set ): Int ;
191
+ public var status (default , set ): FlxButtonState ;
164
192
165
193
/**
166
194
* The properties of this button's `onUp` event (callback function, sound).
@@ -222,7 +250,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
222
250
onOver = new FlxButtonEvent ();
223
251
onOut = new FlxButtonEvent ();
224
252
225
- status = FlxButton . NORMAL ;
253
+ status = NORMAL ;
226
254
227
255
// Since this is a UI element, the default scrollFactor is (0, 0)
228
256
scrollFactor .set ();
@@ -232,8 +260,8 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
232
260
#end
233
261
234
262
#if FLX_NO_MOUSE // no need for highlight frame without mouse input
235
- statusAnimations [FlxButton . HIGHLIGHT ] = " normal" ;
236
- labelAlphas [FlxButton . HIGHLIGHT ] = 1 ;
263
+ statusAnimations [HIGHLIGHT ] = " normal" ;
264
+ labelAlphas [HIGHLIGHT ] = 1 ;
237
265
#end
238
266
239
267
input = new FlxInput (0 );
@@ -243,9 +271,10 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
243
271
{
244
272
super .graphicLoaded ();
245
273
246
- setupAnimation (" normal" , FlxButton .NORMAL );
247
- setupAnimation (" highlight" , FlxButton .HIGHLIGHT );
248
- setupAnimation (" pressed" , FlxButton .PRESSED );
274
+ setupAnimation (" normal" , NORMAL );
275
+ setupAnimation (" highlight" , HIGHLIGHT );
276
+ setupAnimation (" pressed" , PRESSED );
277
+ setupAnimation (" disabled" , DISABLED );
249
278
}
250
279
251
280
function loadDefaultGraphic (): Void
@@ -384,6 +413,9 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
384
413
*/
385
414
function updateButton (): Void
386
415
{
416
+ // Prevent interactions with this input if it's currently disabled
417
+ if (status == DISABLED )
418
+ return ;
387
419
// We're looking for any touch / mouse overlaps with this button
388
420
var overlapFound = checkMouseOverlap ();
389
421
if (! overlapFound )
@@ -394,7 +426,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
394
426
onUpHandler ();
395
427
}
396
428
397
- if (status != FlxButton . NORMAL && (! overlapFound || (currentInput != null && currentInput .justReleased )))
429
+ if (status != NORMAL && (! overlapFound || (currentInput != null && currentInput .justReleased )))
398
430
{
399
431
onOutHandler ();
400
432
}
@@ -464,7 +496,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
464
496
currentInput = input ;
465
497
onDownHandler ();
466
498
}
467
- else if (status == FlxButton . NORMAL )
499
+ else if (status == NORMAL )
468
500
{
469
501
// Allow "swiping" to press a button (dragging it over the button while pressed)
470
502
if (allowSwiping && input .pressed )
@@ -489,7 +521,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
489
521
490
522
function updateLabelAlpha ()
491
523
{
492
- if (_spriteLabel != null && labelAlphas .length > status )
524
+ if (_spriteLabel != null && labelAlphas .length > ( status : Int ) )
493
525
{
494
526
_spriteLabel .alpha = alpha * labelAlphas [status ];
495
527
}
@@ -502,7 +534,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
502
534
#if FLX_MOUSE
503
535
function onUpEventListener (_ ): Void
504
536
{
505
- if (visible && exists && active && status == FlxButton . PRESSED )
537
+ if (visible && exists && active && status == PRESSED )
506
538
{
507
539
onUpHandler ();
508
540
}
@@ -514,7 +546,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
514
546
*/
515
547
function onUpHandler (): Void
516
548
{
517
- status = FlxButton . HIGHLIGHT ;
549
+ status = HIGHLIGHT ;
518
550
input .release ();
519
551
currentInput = null ;
520
552
// Order matters here, because onUp.fire() could cause a state change and destroy this object.
@@ -526,7 +558,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
526
558
*/
527
559
function onDownHandler (): Void
528
560
{
529
- status = FlxButton . PRESSED ;
561
+ status = PRESSED ;
530
562
input .press ();
531
563
// Order matters here, because onDown.fire() could cause a state change and destroy this object.
532
564
onDown .fire ();
@@ -542,11 +574,11 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
542
574
// by remaining in the normal state (until mouse input is re-enabled).
543
575
if (! FlxG .mouse .enabled )
544
576
{
545
- status = FlxButton . NORMAL ;
577
+ status = NORMAL ;
546
578
return ;
547
579
}
548
580
#end
549
- status = FlxButton . HIGHLIGHT ;
581
+ status = HIGHLIGHT ;
550
582
// Order matters here, because onOver.fire() could cause a state change and destroy this object.
551
583
onOver .fire ();
552
584
}
@@ -556,7 +588,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
556
588
*/
557
589
function onOutHandler (): Void
558
590
{
559
- status = FlxButton . NORMAL ;
591
+ status = NORMAL ;
560
592
input .release ();
561
593
// Order matters here, because onOut.fire() could cause a state change and destroy this object.
562
594
onOut .fire ();
@@ -579,9 +611,9 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
579
611
return Value ;
580
612
}
581
613
582
- function set_status (Value : Int ) : Int
614
+ function set_status (value : FlxButtonState ) : FlxButtonState
583
615
{
584
- status = Value ;
616
+ status = value ;
585
617
updateLabelAlpha ();
586
618
return status ;
587
619
}
0 commit comments