Skip to content

Commit 0adbce1

Browse files
authored
FlxPoint: Deprecate old fields (#3526)
* add dot product helpers * add result arg to negateNew * deprecate old methods * use left/righthand fields * remove need for some static points * fix deprecateion warnings * fix warnings * fix haxe 4.2.5 * fix deprecation warning * doc
1 parent 16b3578 commit 0adbce1

File tree

8 files changed

+57
-34
lines changed

8 files changed

+57
-34
lines changed

flixel/FlxSprite.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ class FlxSprite extends FlxObject
10541054
/**
10551055
* Whether this sprite has a color transform, menaing any of the following: less than full
10561056
* `alpha`, a `color` tint, or a `colorTransform` whos values are not the default.
1057+
* @since 6.1.0
10571058
*/
10581059
@:haxe.warning("-WDeprecated")
10591060
public function hasColorTransform()

flixel/FlxStrip.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class FlxStrip extends FlxSprite
5656
if (!camera.visible || !camera.exists)
5757
continue;
5858

59-
getScreenPosition(_point, camera).subtractPoint(offset);
59+
getScreenPosition(_point, camera);
60+
_point -= offset;
6061
camera.drawTriangles(graphic, vertices, indices, uvtData, colors, _point, blend, repeat, antialiasing, colorTransform, shader);
6162
}
6263
}

flixel/graphics/frames/FlxTileFrames.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package flixel.graphics.frames;
22

3-
import openfl.display.BitmapData;
4-
import openfl.geom.Point;
53
import flixel.graphics.FlxGraphic;
64
import flixel.graphics.frames.FlxFramesCollection.FlxFrameCollectionType;
75
import flixel.math.FlxPoint;
@@ -10,6 +8,8 @@ import flixel.system.FlxAssets.FlxGraphicAsset;
108
import flixel.util.FlxBitmapDataUtil;
119
import flixel.util.FlxColor;
1210
import flixel.util.FlxDestroyUtil;
11+
import openfl.display.BitmapData;
12+
import openfl.geom.Point;
1313

1414
/**
1515
* Spritesheet frame collection. It is used for tilemaps and animated sprites.
@@ -91,7 +91,7 @@ class FlxTileFrames extends FlxFramesCollection
9191
borderY = Std.int(tileBorder.y);
9292
}
9393

94-
var tileFrames:FlxTileFrames = FlxTileFrames.fromGraphic(result, FlxPoint.get().addPoint(tileSize).add(2 * borderX, 2 * borderY), null, tileSpacing);
94+
var tileFrames:FlxTileFrames = FlxTileFrames.fromGraphic(result, FlxPoint.get().add(tileSize).add(2 * borderX, 2 * borderY), null, tileSpacing);
9595

9696
if (tileBorder == null)
9797
return tileFrames;

flixel/input/FlxPointer.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class FlxPointer
7575
camera = FlxG.camera;
7676

7777
result = getViewPosition(camera, result);
78-
result.addPoint(camera.scroll);
78+
result.add(camera.scroll);
7979
return result;
8080
}
8181

flixel/math/FlxPoint.hx

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ import openfl.geom.Point;
7171
*/
7272
@:forward abstract FlxPoint(FlxBasePoint) to FlxBasePoint from FlxBasePoint
7373
{
74+
75+
/**
76+
* Vector components less than this are considered zero, to account for rounding errors
77+
*/
7478
public static inline var EPSILON:Float = 0.0000001;
7579
public static inline var EPSILON_SQUARED:Float = EPSILON * EPSILON;
80+
81+
/**
82+
* Vector lengths less than this are considered zero, to account for rounding errors
83+
*/
84+
public static inline var EPSILON_LENGTH:Float = EPSILON * FlxMath.SQUARE_ROOT_OF_TWO;
7685

7786
static var _point1 = new FlxPoint();
78-
static var _point2 = new FlxPoint();
79-
static var _point3 = new FlxPoint();
8087

8188
/**
8289
* Recycle or create new FlxPoint.
@@ -162,7 +169,7 @@ import openfl.geom.Point;
162169
@:op(A += B)
163170
static inline function plusEqualOp(a:FlxPoint, b:FlxPoint):FlxPoint
164171
{
165-
return a.addPoint(b);
172+
return a.add(b);
166173
}
167174

168175
/**
@@ -172,7 +179,7 @@ import openfl.geom.Point;
172179
@:op(A -= B)
173180
static inline function minusEqualOp(a:FlxPoint, b:FlxPoint):FlxPoint
174181
{
175-
return a.subtractPoint(b);
182+
return a.subtract(b);
176183
}
177184

178185
/**
@@ -357,7 +364,7 @@ import openfl.geom.Point;
357364
* @param point The point to add to this point
358365
* @return This point.
359366
*/
360-
// @:deprecated("addPoint is deprecated, use add(point), instead")// 6.0.0
367+
@:deprecated("addPoint is deprecated, use add(point), instead")// 6.1.2
361368
public inline function addPoint(point:FlxPoint):FlxPoint
362369
{
363370
return add(point);
@@ -408,7 +415,7 @@ import openfl.geom.Point;
408415
* @param point The point to subtract from this point
409416
* @return This point.
410417
*/
411-
// @:deprecated("subtractPoint is deprecated, use subtract(point), instead")// 6.0.0
418+
@:deprecated("subtractPoint is deprecated, use subtract(point), instead")// 6.1.2
412419
public inline function subtractPoint(point:FlxPoint):FlxPoint
413420
{
414421
subtract(point.x, point.y);
@@ -470,7 +477,7 @@ import openfl.geom.Point;
470477
* @param point The x and y scale coefficient
471478
* @return scaled point
472479
*/
473-
// @:deprecated("scalePoint is deprecated, use scale(point), instead")// 6.0.0
480+
@:deprecated("scalePoint is deprecated, use scale(point), instead")// 6.1.2
474481
public inline function scalePoint(point:FlxPoint):FlxPoint
475482
{
476483
scale(point.x, point.y);
@@ -497,7 +504,7 @@ import openfl.geom.Point;
497504
*/
498505
public inline function addNew(p:FlxPoint):FlxPoint
499506
{
500-
return clone().addPoint(p);
507+
return clone().add(p);
501508
}
502509

503510
/**
@@ -508,7 +515,7 @@ import openfl.geom.Point;
508515
*/
509516
public inline function subtractNew(p:FlxPoint):FlxPoint
510517
{
511-
return clone().subtractPoint(p);
518+
return clone().subtract(p);
512519
}
513520

514521
/**
@@ -542,7 +549,7 @@ import openfl.geom.Point;
542549
* @param p Any Point.
543550
* @return A reference to itself.
544551
*/
545-
// @:deprecated("copyFromFlash is deprecated, use copyFrom, instead")// 6.0.0
552+
@:deprecated("copyFromFlash is deprecated, use copyFrom, instead")// 6.1.2
546553
public inline function copyFromFlash(p:Point):FlxPoint
547554
{
548555
return set(p.x, p.y);
@@ -583,7 +590,7 @@ import openfl.geom.Point;
583590
* @param p Any Point.
584591
* @return A reference to the altered point parameter.
585592
*/
586-
// @:deprecated("copyToFlash is deprecated, use copyTo, instead")// 6.0.0
593+
@:deprecated("copyToFlash is deprecated, use copyTo, instead")// 6.1.2
587594
public inline function copyToFlash(?p:Point):Point
588595
{
589596
return copyTo(p != null ? p : new Point());
@@ -677,7 +684,7 @@ import openfl.geom.Point;
677684
*/
678685
public function pivotRadians(pivot:FlxPoint, radians:Float):FlxPoint
679686
{
680-
_point1.copyFrom(this).subtractPoint(pivot);
687+
_point1.copyFrom(this).subtract(pivot);
681688
_point1.radians += radians;
682689
set(_point1.x + pivot.x, _point1.y + pivot.y);
683690
pivot.putWeak();
@@ -845,7 +852,12 @@ import openfl.geom.Point;
845852
*/
846853
inline function dotProductWeak(p:FlxPoint):Float
847854
{
848-
return x * p.x + y * p.y;
855+
return dotProductXY(p.x, p.y);
856+
}
857+
858+
inline function dotProductXY(x:Float, y:Float):Float
859+
{
860+
return this.x * x + this.y * y;
849861
}
850862

851863
/**
@@ -856,9 +868,10 @@ import openfl.geom.Point;
856868
*/
857869
public inline function dotProdWithNormalizing(p:FlxPoint):Float
858870
{
859-
var normalized:FlxPoint = p.clone(_point1).normalize();
871+
final length = p.length;
872+
final result = length < EPSILON_LENGTH ? 0 : dotProductXY(p.x / length, p.y / length);
860873
p.putWeak();
861-
return dotProductWeak(normalized);
874+
return result;
862875
}
863876

864877
/**
@@ -929,7 +942,8 @@ import openfl.geom.Point;
929942
*/
930943
public inline function isZero():Bool
931944
{
932-
return Math.abs(x) < EPSILON && Math.abs(y) < EPSILON;
945+
// i.e: x*x < EPSILON_SQUARED && y*y < EPSILON_SQUARED;
946+
return lengthSquared < 2 * EPSILON_SQUARED;
933947
}
934948

935949
/**
@@ -1033,7 +1047,7 @@ import openfl.geom.Point;
10331047
{
10341048
p = get();
10351049
}
1036-
p.set(-y, x);
1050+
p.set(rx, ry);
10371051
return p;
10381052
}
10391053

@@ -1046,7 +1060,7 @@ import openfl.geom.Point;
10461060
{
10471061
p = get();
10481062
}
1049-
p.set(y, -x);
1063+
p.set(lx, ly);
10501064
return p;
10511065
}
10521066

@@ -1058,9 +1072,9 @@ import openfl.geom.Point;
10581072
return set(x * -1, y * -1);
10591073
}
10601074

1061-
public inline function negateNew():FlxPoint
1075+
public inline function negateNew(?result:FlxPoint):FlxPoint
10621076
{
1063-
return clone().negate();
1077+
return clone(result).negate();
10641078
}
10651079

10661080
/**
@@ -1136,7 +1150,12 @@ import openfl.geom.Point;
11361150
*/
11371151
inline function perpProductWeak(p:FlxPoint):Float
11381152
{
1139-
return lx * p.x + ly * p.y;
1153+
return perpProductXY(p.x, p.y);
1154+
}
1155+
1156+
inline function perpProductXY(x:Float, y:Float):Float
1157+
{
1158+
return lx * x + ly * y;
11401159
}
11411160

11421161
/**
@@ -1354,12 +1373,13 @@ import openfl.geom.Point;
13541373
*/
13551374
public inline function bounceWithFriction(normal:FlxPoint, bounceCoeff:Float = 1, friction:Float = 0):FlxPoint
13561375
{
1357-
var p1:FlxPoint = projectToNormalizedWeak(normal.rightNormal(_point3), _point1);
1358-
var p2:FlxPoint = projectToNormalizedWeak(normal, _point2);
1359-
var bounceX:Float = -p2.x;
1360-
var bounceY:Float = -p2.y;
1361-
var frictionX:Float = p1.x;
1362-
var frictionY:Float = p1.y;
1376+
final dp = dotProductWeak(normal);
1377+
final bounceX = -normal.x * dp;
1378+
final bounceY = -normal.y * dp;
1379+
final pp = perpProductWeak(normal);
1380+
final frictionX = normal.rx * pp;
1381+
final frictionY = normal.ry * pp;
1382+
13631383
normal.putWeak();
13641384

13651385
return set(bounceX * bounceCoeff + frictionX * friction, bounceY * bounceCoeff + frictionY * friction);

flixel/text/FlxBitmapText.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class FlxBitmapText extends FlxSprite
374374
continue;
375375
}
376376

377-
getScreenPosition(screenPos, camera).subtractPoint(offset);
377+
getScreenPosition(screenPos, camera).subtract(offset);
378378

379379
if (isPixelPerfectRender(camera))
380380
{

flixel/tile/FlxTilemap.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ class FlxTypedTilemap<Tile:FlxTile> extends FlxBaseTilemap<Tile>
12201220
}
12211221
else
12221222
{
1223-
getScreenPosition(_point, camera).subtractPoint(offset).copyTo(_helperPoint);
1223+
getScreenPosition(_point, camera).subtract(offset).copyTo(_helperPoint);
12241224

12251225
_helperPoint.x = isPixelPerfectRender(camera) ? Math.floor(_helperPoint.x) : _helperPoint.x;
12261226
_helperPoint.y = isPixelPerfectRender(camera) ? Math.floor(_helperPoint.y) : _helperPoint.y;

tests/unit/src/flixel/math/FlxPointTest.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class FlxPointTest extends FlxTest
168168
}
169169

170170
@Test
171+
@:haxe.warning("-WDeprecated")
171172
function testOperators()
172173
{
173174
point1.set(1, 2);

0 commit comments

Comments
 (0)