Skip to content

Commit 6cd83e7

Browse files
itulauGeokureli
andauthored
Add disabled status to FlxButton (HaxeFlixel#3087)
* Add disabled status to FlxButton (HaxeFlixel#3083) * create FlxButtonState enum * fix unit tests --------- Co-authored-by: George FunBook <gkurelic@gmail.com>
1 parent 5ede2bd commit 6cd83e7

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

assets/images/ui/button.png

29 Bytes
Loading

flixel/ui/FlxButton.hx

+59-27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ import flixel.util.FlxDestroyUtil;
1818
import flixel.input.touch.FlxTouch;
1919
#end
2020

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+
2136
/**
2237
* A simple button class that calls a function when clicked by the mouse.
2338
*/
@@ -26,17 +41,30 @@ class FlxButton extends FlxTypedButton<FlxText>
2641
/**
2742
* Used with public variable status, means not highlighted or pressed.
2843
*/
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;
3047

3148
/**
3249
* Used with public variable status, means highlighted (usually from mouse over).
3350
*/
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;
3554

3655
/**
3756
* Used with public variable status, means pressed (usually from mouse click).
3857
*/
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;
4068

4169
/**
4270
* Shortcut to setting label.text
@@ -80,7 +108,7 @@ class FlxButton extends FlxTypedButton<FlxText>
80108
{
81109
if (Text != null)
82110
{
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);
84112
label.setFormat(null, 8, 0x333333, "center");
85113
label.alpha = labelAlphas[status];
86114
label.drawFrame(true);
@@ -122,19 +150,19 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
122150
/**
123151
* What offsets the `label` should have for each status.
124152
*/
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()];
126154

127155
/**
128156
* What alpha value the label should have for each status. Default is `[0.8, 1.0, 0.5]`.
129157
* Multiplied with the button's `alpha`.
130158
*/
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];
132160

133161
/**
134162
* What animation should be played for each status.
135163
* Default is ["normal", "highlight", "pressed"].
136164
*/
137-
public var statusAnimations:Array<String> = ["normal", "highlight", "pressed"];
165+
public var statusAnimations:Array<String> = ["normal", "highlight", "pressed", "disabled"];
138166

139167
/**
140168
* 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
157185
public var maxInputMovement:Float = Math.POSITIVE_INFINITY;
158186

159187
/**
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`.
162190
*/
163-
public var status(default, set):Int;
191+
public var status(default, set):FlxButtonState;
164192

165193
/**
166194
* The properties of this button's `onUp` event (callback function, sound).
@@ -222,7 +250,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
222250
onOver = new FlxButtonEvent();
223251
onOut = new FlxButtonEvent();
224252

225-
status = FlxButton.NORMAL;
253+
status = NORMAL;
226254

227255
// Since this is a UI element, the default scrollFactor is (0, 0)
228256
scrollFactor.set();
@@ -232,8 +260,8 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
232260
#end
233261

234262
#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;
237265
#end
238266

239267
input = new FlxInput(0);
@@ -243,9 +271,10 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
243271
{
244272
super.graphicLoaded();
245273

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);
249278
}
250279

251280
function loadDefaultGraphic():Void
@@ -384,6 +413,9 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
384413
*/
385414
function updateButton():Void
386415
{
416+
// Prevent interactions with this input if it's currently disabled
417+
if (status == DISABLED)
418+
return;
387419
// We're looking for any touch / mouse overlaps with this button
388420
var overlapFound = checkMouseOverlap();
389421
if (!overlapFound)
@@ -394,7 +426,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
394426
onUpHandler();
395427
}
396428

397-
if (status != FlxButton.NORMAL && (!overlapFound || (currentInput != null && currentInput.justReleased)))
429+
if (status != NORMAL && (!overlapFound || (currentInput != null && currentInput.justReleased)))
398430
{
399431
onOutHandler();
400432
}
@@ -464,7 +496,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
464496
currentInput = input;
465497
onDownHandler();
466498
}
467-
else if (status == FlxButton.NORMAL)
499+
else if (status == NORMAL)
468500
{
469501
// Allow "swiping" to press a button (dragging it over the button while pressed)
470502
if (allowSwiping && input.pressed)
@@ -489,7 +521,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
489521

490522
function updateLabelAlpha()
491523
{
492-
if (_spriteLabel != null && labelAlphas.length > status)
524+
if (_spriteLabel != null && labelAlphas.length > (status : Int))
493525
{
494526
_spriteLabel.alpha = alpha * labelAlphas[status];
495527
}
@@ -502,7 +534,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
502534
#if FLX_MOUSE
503535
function onUpEventListener(_):Void
504536
{
505-
if (visible && exists && active && status == FlxButton.PRESSED)
537+
if (visible && exists && active && status == PRESSED)
506538
{
507539
onUpHandler();
508540
}
@@ -514,7 +546,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
514546
*/
515547
function onUpHandler():Void
516548
{
517-
status = FlxButton.HIGHLIGHT;
549+
status = HIGHLIGHT;
518550
input.release();
519551
currentInput = null;
520552
// 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
526558
*/
527559
function onDownHandler():Void
528560
{
529-
status = FlxButton.PRESSED;
561+
status = PRESSED;
530562
input.press();
531563
// Order matters here, because onDown.fire() could cause a state change and destroy this object.
532564
onDown.fire();
@@ -542,11 +574,11 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
542574
// by remaining in the normal state (until mouse input is re-enabled).
543575
if (!FlxG.mouse.enabled)
544576
{
545-
status = FlxButton.NORMAL;
577+
status = NORMAL;
546578
return;
547579
}
548580
#end
549-
status = FlxButton.HIGHLIGHT;
581+
status = HIGHLIGHT;
550582
// Order matters here, because onOver.fire() could cause a state change and destroy this object.
551583
onOver.fire();
552584
}
@@ -556,7 +588,7 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
556588
*/
557589
function onOutHandler():Void
558590
{
559-
status = FlxButton.NORMAL;
591+
status = NORMAL;
560592
input.release();
561593
// Order matters here, because onOut.fire() could cause a state change and destroy this object.
562594
onOut.fire();
@@ -579,9 +611,9 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
579611
return Value;
580612
}
581613

582-
function set_status(Value:Int):Int
614+
function set_status(value:FlxButtonState):FlxButtonState
583615
{
584-
status = Value;
616+
status = value;
585617
updateLabelAlpha();
586618
return status;
587619
}

tests/unit/src/flixel/ui/FlxButtonTest.hx

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package flixel.ui;
22

3-
import openfl.display.BitmapData;
43
import flixel.FlxSprite;
4+
import flixel.ui.FlxButton;
5+
import openfl.display.BitmapData;
56
import massive.munit.Assert;
67

78
class FlxButtonTest extends FlxTest
@@ -45,9 +46,9 @@ class FlxButtonTest extends FlxTest
4546

4647
function assertStatusAnimationsExist()
4748
{
48-
var normalName:String = button.statusAnimations[FlxButton.NORMAL];
49-
var highlightName:String = button.statusAnimations[FlxButton.HIGHLIGHT];
50-
var pressedName:String = button.statusAnimations[FlxButton.PRESSED];
49+
var normalName:String = button.statusAnimations[NORMAL];
50+
var highlightName:String = button.statusAnimations[HIGHLIGHT];
51+
var pressedName:String = button.statusAnimations[PRESSED];
5152

5253
Assert.isNotNull(button.animation.getByName(normalName));
5354
Assert.isNotNull(button.animation.getByName(highlightName));
@@ -74,7 +75,7 @@ class FlxButtonTest extends FlxTest
7475

7576
button.setPosition();
7677
step(1);
77-
Assert.areEqual(FlxButton.HIGHLIGHT, button.status);
78+
Assert.areEqual(HIGHLIGHT, button.status);
7879

7980
FlxG.state.remove(button);
8081
}
@@ -87,7 +88,7 @@ class FlxButtonTest extends FlxTest
8788
FlxG.state.add(button);
8889
step(2);
8990

90-
Assert.areEqual(FlxButton.NORMAL, button.status);
91+
Assert.areEqual(NORMAL, button.status);
9192
Assert.areEqual("normal", button.animation.curAnim.name);
9293
Assert.areEqual(false, button.animation.finished);
9394
step(10);

0 commit comments

Comments
 (0)