Skip to content

Commit 076f9cd

Browse files
authored
remove implicit int casts to/from directions (#3308)
* remove implicit int casts to/from directions * D'oh * D'oh! * deprecate operators and fix haxe 4.2.5 * order imports * remove casts, add explicit cast functions * fix not() and add tests * Doc * doc + tests
1 parent 2f5d24a commit 076f9cd

File tree

8 files changed

+150
-75
lines changed

8 files changed

+150
-75
lines changed

flixel/FlxObject.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,14 +1247,14 @@ class FlxObject extends FlxBasic
12471247
endDrawDebug(camera);
12481248
}
12491249

1250-
function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:Int, partial:Bool)
1250+
function drawDebugBoundingBox(gfx:Graphics, rect:FlxRect, allowCollisions:FlxDirectionFlags, partial:Bool)
12511251
{
12521252
// Find the color to use
12531253
final color = getDebugBoundingBoxColor(allowCollisions);
12541254
drawDebugBoundingBoxColor(gfx, rect, color);
12551255
}
12561256

1257-
function getDebugBoundingBoxColor(allowCollisions:Int)
1257+
function getDebugBoundingBoxColor(allowCollisions:FlxDirectionFlags)
12581258
{
12591259
if (debugBoundingBoxColor != null)
12601260
return debugBoundingBoxColor;

flixel/group/FlxSpriteGroup.hx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import flixel.math.FlxRect;
1313
import flixel.system.FlxAssets;
1414
import flixel.util.FlxColor;
1515
import flixel.util.FlxDestroyUtil;
16+
import flixel.util.FlxDirectionFlags;
1617
import flixel.util.FlxSort;
1718

1819
/**
@@ -753,7 +754,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
753754
return alpha = Value;
754755
}
755756

756-
override function set_facing(Value:Int):Int
757+
override function set_facing(Value:FlxDirectionFlags):FlxDirectionFlags
757758
{
758759
if (exists && facing != Value)
759760
transformChildren(facingTransform, Value);
@@ -1023,7 +1024,7 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
10231024
inline function directAlphaTransform(Sprite:FlxSprite, Alpha:Float)
10241025
Sprite.alpha = Alpha; // direct set
10251026

1026-
inline function facingTransform(Sprite:FlxSprite, Facing:Int)
1027+
inline function facingTransform(Sprite:FlxSprite, Facing:FlxDirectionFlags)
10271028
Sprite.facing = Facing;
10281029

10291030
inline function flipXTransform(Sprite:FlxSprite, FlipX:Bool)

flixel/system/FlxQuadTree.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class FlxQuadTree extends FlxRect
616616
{
617617
_iterator = iterator.next;
618618
}
619-
if (_object != null && _object.exists && _object.allowCollisions > 0 && _iterator != null && _iterator.object != null && overlapNode())
619+
if (_object != null && _object.exists && _object.allowCollisions != NONE && _iterator != null && _iterator.object != null && overlapNode())
620620
{
621621
overlapProcessed = true;
622622
}
@@ -666,7 +666,7 @@ class FlxQuadTree extends FlxRect
666666
while (_iterator != null)
667667
{
668668
checkObject = _iterator.object;
669-
if (_object == checkObject || !checkObject.exists || checkObject.allowCollisions <= 0)
669+
if (_object == checkObject || !checkObject.exists || checkObject.allowCollisions == NONE)
670670
{
671671
_iterator = _iterator.next;
672672
continue;

flixel/system/macros/FlxDefines.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ class FlxDefines
140140
checkOpenFLVersions();
141141
#end
142142

143-
#if (flixel_addons < version("3.0.2"))
144-
abortVersion("Flixel Addons", "3.0.2 or newer", "flixel-addons", (macro null).pos);
143+
#if (flixel_addons < version("3.3.0"))
144+
abortVersion("Flixel Addons", "3.3.0 or newer", "flixel-addons", (macro null).pos);
145145
#end
146-
#if (flixel_ui < version("2.4.0"))
147-
abortVersion("Flixel UI", "2.4.0 or newer", "flixel-addons", (macro null).pos);
146+
#if (flixel_ui < version("2.6.2"))
147+
abortVersion("Flixel UI", "2.6.2 or newer", "flixel_ui", (macro null).pos);
148148
#end
149149
}
150150

flixel/tile/FlxTilemap.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
15381538
return tileSprite;
15391539
}
15401540

1541-
override function set_allowCollisions(value:Int):Int
1541+
override function set_allowCollisions(value:FlxDirectionFlags):FlxDirectionFlags
15421542
{
15431543
for (tile in _tileObjects)
15441544
if (tile.index >= _collideIndex)

flixel/util/FlxDirection.hx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,61 @@ package flixel.util;
44
* Simple enum for orthogonal directions. Can be combined into `FlxDirectionFlags`.
55
* @since 4.10.0
66
*/
7-
enum abstract FlxDirection(Int) to Int
7+
enum abstract FlxDirection(Int)
88
{
99
var LEFT = 0x0001;
1010
var RIGHT = 0x0010;
1111
var UP = 0x0100;
1212
var DOWN = 0x1000;
13-
13+
14+
var self(get, never):FlxDirection;
15+
inline function get_self():FlxDirection
16+
{
17+
#if (haxe >= version("4.3.0"))
18+
return abstract;
19+
#else
20+
return cast this;
21+
#end
22+
}
23+
24+
inline function new(value:Int)
25+
{
26+
this = value;
27+
}
28+
1429
public function toString()
1530
{
16-
return switch (cast this : FlxDirection)
31+
return switch self
1732
{
1833
case LEFT: "L";
1934
case RIGHT: "R";
2035
case UP: "U";
2136
case DOWN: "D";
2237
}
2338
}
39+
40+
@:deprecated("implicit cast from FlxDirection to Int is deprecated, use toInt()")
41+
@:to
42+
inline function toIntImplicit()
43+
{
44+
return toInt();
45+
}
46+
47+
48+
inline public function toInt()
49+
{
50+
return this;
51+
}
52+
53+
@:deprecated("implicit cast from Int to FlxDirection is deprecated, use FlxDirection.fromInt")
54+
@:from
55+
inline static function fromIntImplicit(value:Int):FlxDirection
56+
{
57+
return fromInt(value);
58+
}
59+
60+
public inline static function fromInt(value:Int):FlxDirection
61+
{
62+
return new FlxDirection(value);
63+
}
2464
}

flixel/util/FlxDirectionFlags.hx

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import flixel.math.FlxAngle;
77
* many `FlxObject` features like `allowCollisions` and `touching`.
88
* @since 4.10.0
99
*/
10-
enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
10+
enum abstract FlxDirectionFlags(Int)
1111
{
1212
var LEFT = 0x0001; // FlxDirection.LEFT;
1313
var RIGHT = 0x0010; // FlxDirection.RIGHT;
@@ -32,7 +32,18 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
3232

3333
/** Special-case constant meaning any, or all directions. */
3434
var ANY = 0x1111; // LEFT | RIGHT | UP | DOWN;
35-
35+
36+
var self(get, never):FlxDirectionFlags;
37+
38+
inline function get_self():FlxDirectionFlags
39+
{
40+
#if (haxe >= version("4.3.0"))
41+
return abstract;
42+
#else
43+
return cast this;
44+
#end
45+
}
46+
3647
/**
3748
* Calculates the angle (in degrees) of the facing flags.
3849
* Returns 0 if two opposing flags are true.
@@ -41,7 +52,7 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
4152
public var degrees(get, never):Float;
4253
function get_degrees():Float
4354
{
44-
return switch (this)
55+
return switch self
4556
{
4657
case RIGHT: 0;
4758
case DOWN: 90;
@@ -82,42 +93,63 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
8293
public var right(get, never):Bool;
8394
inline function get_right() return has(RIGHT);
8495

85-
96+
inline function new(value:Int)
97+
{
98+
this = value;
99+
}
100+
86101
/**
87102
* Returns true if this contains **all** of the supplied flags.
88103
*/
89104
public inline function has(dir:FlxDirectionFlags):Bool
90105
{
91-
return this & dir == dir;
106+
return this & dir.toInt() == dir.toInt();
92107
}
93108

94109
/**
95110
* Returns true if this contains **any** of the supplied flags.
96111
*/
97112
public inline function hasAny(dir:FlxDirectionFlags):Bool
98113
{
99-
return this & dir > 0;
114+
return this & dir.toInt() > 0;
100115
}
101116

102117
/**
103118
* Creates a new `FlxDirections` that includes the supplied directions.
104119
*/
105120
public inline function with(dir:FlxDirectionFlags):FlxDirectionFlags
106121
{
107-
return this | dir;
122+
return fromInt(this | dir.toInt());
108123
}
109124

110125
/**
111126
* Creates a new `FlxDirections` that excludes the supplied directions.
112127
*/
113128
public inline function without(dir:FlxDirectionFlags):FlxDirectionFlags
114129
{
115-
return this & ~dir;
130+
return fromInt(this & ~dir.toInt());
131+
}
132+
133+
public inline function not():FlxDirectionFlags
134+
{
135+
return fromInt((~this & ANY.toInt()));
116136
}
117137

138+
@:deprecated("implicit cast from FlxDirectionFlags to Int is deprecated, use toInt")
139+
@:to
140+
inline function toIntImplicit()
141+
{
142+
return toInt();
143+
}
144+
145+
public inline function toInt():Int
146+
{
147+
return this;
148+
}
149+
118150
public function toString()
119151
{
120-
if (this == NONE)
152+
if (self == NONE)
121153
return "NONE";
122154

123155
var str = "";
@@ -146,16 +178,34 @@ enum abstract FlxDirectionFlags(Int) from Int from FlxDirection to Int
146178
| (down ? DOWN : NONE);
147179
}
148180

149-
// Expose int operators
150-
@:op(A & B) static function and(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;
181+
@:deprecated("implicit cast from Int to FlxDirectionFlags is deprecated, use FlxDirectionFlags.fromInt")
182+
@:from
183+
inline static function fromIntImplicit(value:Int):FlxDirectionFlags
184+
{
185+
return fromInt(value);
186+
}
187+
188+
public inline static function fromInt(value:Int):FlxDirectionFlags
189+
{
190+
return new FlxDirectionFlags(value);
191+
}
192+
193+
@:from
194+
inline static function fromDir(dir:FlxDirection):FlxDirectionFlags
195+
{
196+
return fromInt(dir.toInt());
197+
}
151198

199+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")// Expose int operators
200+
@:op(A & B) static function and(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;
201+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
152202
@:op(A | B) static function or(a:FlxDirectionFlags, b:FlxDirectionFlags):FlxDirectionFlags;
153-
203+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
154204
@:op(A > B) static function gt(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;
155-
205+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
156206
@:op(A < B) static function lt(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;
157-
207+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
158208
@:op(A >= B) static function gte(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;
159-
209+
@:deprecated("FlxDirectionFlags operators are deprecated, use has(), instead")
160210
@:op(A <= B) static function lte(a:FlxDirectionFlags, b:FlxDirectionFlags):Bool;
161211
}

0 commit comments

Comments
 (0)