Skip to content

Commit 524a31b

Browse files
authored
FlxCallbackPoint: Fix duplicate calls and call xy when x or y is set directly (#3524)
* fix double callbacks, ensure xy calls on x/y direct sets * doc fixes * add missing scale overload * check operators
1 parent 9f6e768 commit 524a31b

File tree

2 files changed

+417
-91
lines changed

2 files changed

+417
-91
lines changed

flixel/math/FlxPoint.hx

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ import openfl.geom.Point;
310310
*/
311311
public inline function set(x:Float = 0, y:Float = 0):FlxPoint
312312
{
313-
this.x = x;
314-
this.y = y;
315-
return this;
313+
return this.set(x, y);
316314
}
317315

318316
/**
@@ -324,9 +322,7 @@ import openfl.geom.Point;
324322
*/
325323
public overload extern inline function add(x:Float = 0, y:Float = 0):FlxPoint
326324
{
327-
this.x += x;
328-
this.y += y;
329-
return this;
325+
return set(this.x + x, this.y + y);
330326
}
331327

332328
/**
@@ -352,10 +348,7 @@ import openfl.geom.Point;
352348
*/
353349
public overload inline extern function add(p:Point):FlxPoint
354350
{
355-
x += p.x;
356-
y += p.y;
357-
358-
return this;
351+
return add(p.x, p.y);
359352
}
360353

361354
/**
@@ -379,9 +372,7 @@ import openfl.geom.Point;
379372
*/
380373
public overload inline extern function subtract(x:Float = 0, y:Float = 0):FlxPoint
381374
{
382-
this.x -= x;
383-
this.y -= y;
384-
return this;
375+
return set(this.x - x, this.y - y);
385376
}
386377

387378
/**
@@ -434,9 +425,7 @@ import openfl.geom.Point;
434425
*/
435426
public overload inline extern function scale(x:Float, y:Float):FlxPoint
436427
{
437-
this.x *= x;
438-
this.y *= y;
439-
return this;
428+
return set(this.x * x, this.y * y);
440429
}
441430

442431
/**
@@ -448,9 +437,19 @@ import openfl.geom.Point;
448437
*/
449438
public overload inline extern function scale(amount:Float):FlxPoint
450439
{
451-
this.x *= amount;
452-
this.y *= amount;
453-
return this;
440+
return scale(amount, amount);
441+
}
442+
443+
/**
444+
* Scale this point by another point.
445+
* @since 6.0.0
446+
*
447+
* @param point The x and y scale coefficient
448+
* @return this point
449+
*/
450+
public overload inline extern function scale(point:FlxPoint):FlxPoint
451+
{
452+
return scale(point.x, point.y);
454453
}
455454

456455
/**
@@ -462,8 +461,7 @@ import openfl.geom.Point;
462461
*/
463462
public overload inline extern function scale(point:Point):FlxPoint
464463
{
465-
scale(point.x, point.y);
466-
return this;
464+
return scale(point.x, point.y);
467465
}
468466

469467
/**
@@ -535,7 +533,7 @@ import openfl.geom.Point;
535533
*/
536534
public overload inline extern function copyFrom(p:Point):FlxPoint
537535
{
538-
return this.set(p.x, p.y);
536+
return set(p.x, p.y);
539537
}
540538

541539
/**
@@ -547,14 +545,14 @@ import openfl.geom.Point;
547545
// @:deprecated("copyFromFlash is deprecated, use copyFrom, instead")// 6.0.0
548546
public inline function copyFromFlash(p:Point):FlxPoint
549547
{
550-
return this.set(p.x, p.y);
548+
return set(p.x, p.y);
551549
}
552550

553551
/**
554552
* Helper function, just copies the values from this point to the specified point.
555553
*
556-
* @param p optional point to copy this point to
557-
* @return copy of this point
554+
* @param p An optional point to copy this point to
555+
* @return The new point
558556
*/
559557
public overload inline extern function copyTo(?p:FlxPoint):FlxPoint
560558
{
@@ -569,8 +567,8 @@ import openfl.geom.Point;
569567
* Helper function, just copies the values from this point to the specified Flash point.
570568
* @since 6.0.0
571569
*
572-
* @param p Any Point.
573-
* @return A reference to the altered point parameter.
570+
* @param p The point to copy this point to
571+
* @return The new point
574572
*/
575573
public overload inline extern function copyTo(p:Point):Point
576574
{
@@ -624,29 +622,23 @@ import openfl.geom.Point;
624622
*/
625623
public inline function floor():FlxPoint
626624
{
627-
x = Math.floor(x);
628-
y = Math.floor(y);
629-
return this;
625+
return set(Math.floor(x), Math.floor(y));
630626
}
631627

632628
/**
633629
* Rounds x and y using Math.ceil()
634630
*/
635631
public inline function ceil():FlxPoint
636632
{
637-
x = Math.ceil(x);
638-
y = Math.ceil(y);
639-
return this;
633+
return set(Math.ceil(x), Math.ceil(y));
640634
}
641635

642636
/**
643637
* Rounds x and y using Math.round()
644638
*/
645639
public inline function round():FlxPoint
646640
{
647-
x = Math.round(x);
648-
y = Math.round(y);
649-
return this;
641+
return set(Math.round(x), Math.round(y));
650642
}
651643

652644
/**
@@ -945,8 +937,7 @@ import openfl.geom.Point;
945937
*/
946938
public inline function zero():FlxPoint
947939
{
948-
x = y = 0;
949-
return this;
940+
return set(0, 0);
950941
}
951942

952943
/**
@@ -979,12 +970,7 @@ import openfl.geom.Point;
979970
{
980971
var s:Float = Math.sin(rads);
981972
var c:Float = Math.cos(rads);
982-
var tempX:Float = x;
983-
984-
x = tempX * c - y * s;
985-
y = tempX * s + y * c;
986-
987-
return this;
973+
return set(x * c - y * s, x * s + y * c);
988974
}
989975

990976
/**
@@ -1007,10 +993,7 @@ import openfl.geom.Point;
1007993
*/
1008994
public inline function rotateWithTrig(sin:Float, cos:Float):FlxPoint
1009995
{
1010-
var tempX:Float = x;
1011-
x = tempX * cos - y * sin;
1012-
y = tempX * sin + y * cos;
1013-
return this;
996+
return set(x * cos - y * sin, x * sin + y * cos);
1014997
}
1015998

1016999
/**
@@ -1024,9 +1007,7 @@ import openfl.geom.Point;
10241007
*/
10251008
public function setPolarRadians(length:Float, radians:Float):FlxPoint
10261009
{
1027-
x = length * Math.cos(radians);
1028-
y = length * Math.sin(radians);
1029-
return this;
1010+
return set(length * Math.cos(radians), length * Math.sin(radians));
10301011
}
10311012

10321013
/**
@@ -1074,9 +1055,7 @@ import openfl.geom.Point;
10741055
*/
10751056
public inline function negate():FlxPoint
10761057
{
1077-
x *= -1;
1078-
y *= -1;
1079-
return this;
1058+
return set(x * -1, y * -1);
10801059
}
10811060

10821061
public inline function negateNew():FlxPoint
@@ -1360,8 +1339,7 @@ import openfl.geom.Point;
13601339
public inline function bounce(normal:FlxPoint, bounceCoeff:Float = 1):FlxPoint
13611340
{
13621341
var d:Float = (1 + bounceCoeff) * dotProductWeak(normal);
1363-
x -= d * normal.x;
1364-
y -= d * normal.y;
1342+
set(x - d * normal.x, y - d * normal.y);
13651343
normal.putWeak();
13661344
return this;
13671345
}
@@ -1382,10 +1360,9 @@ import openfl.geom.Point;
13821360
var bounceY:Float = -p2.y;
13831361
var frictionX:Float = p1.x;
13841362
var frictionY:Float = p1.y;
1385-
x = bounceX * bounceCoeff + frictionX * friction;
1386-
y = bounceY * bounceCoeff + frictionY * friction;
13871363
normal.putWeak();
1388-
return this;
1364+
1365+
return set(bounceX * bounceCoeff + frictionX * friction, bounceY * bounceCoeff + frictionY * friction);
13891366
}
13901367

13911368
/**
@@ -1401,8 +1378,8 @@ import openfl.geom.Point;
14011378
/**
14021379
* Copies this point.
14031380
*
1404-
* @param p optional point to copy this point to
1405-
* @return copy of this point
1381+
* @param p An optional point to copy this point to
1382+
* @return The new point
14061383
*/
14071384
public inline function clone(?p:FlxPoint):FlxPoint
14081385
{
@@ -1455,8 +1432,7 @@ import openfl.geom.Point;
14551432
if (!isZero())
14561433
{
14571434
var a:Float = radians;
1458-
x = l * Math.cos(a);
1459-
y = l * Math.sin(a);
1435+
set(l * Math.cos(a), l * Math.sin(a));
14601436
}
14611437
return l;
14621438
}
@@ -1486,8 +1462,7 @@ import openfl.geom.Point;
14861462
{
14871463
var len:Float = length;
14881464

1489-
x = len * Math.cos(rads);
1490-
y = len * Math.sin(rads);
1465+
set(len * Math.cos(rads), len * Math.sin(rads));
14911466
return rads;
14921467
}
14931468

@@ -1726,8 +1701,8 @@ abstract FlxReadOnlyPoint(FlxPoint) from FlxPoint
17261701
class FlxCallbackPoint extends FlxBasePoint
17271702
{
17281703
var _setXCallback:FlxPoint->Void;
1729-
var _setYCallback:FlxPoint->Void;
1730-
var _setXYCallback:FlxPoint->Void;
1704+
var _setYCallback:Null<FlxPoint->Void>;
1705+
var _setXYCallback:Null<FlxPoint->Void>;
17311706

17321707
/**
17331708
* If you only specify one callback function, then the remaining two will use the same.
@@ -1739,41 +1714,60 @@ class FlxCallbackPoint extends FlxBasePoint
17391714
public function new(setXCallback:FlxPoint->Void, ?setYCallback:FlxPoint->Void, ?setXYCallback:FlxPoint->Void)
17401715
{
17411716
super();
1742-
1743-
_setXCallback = setXCallback;
1744-
_setYCallback = setXYCallback;
1745-
_setXYCallback = setXYCallback;
1746-
1747-
if (_setXCallback != null)
1717+
1718+
// TODO: operator overloading?
1719+
if (setXCallback != null && setYCallback == null && setXYCallback == null)
1720+
{
1721+
_setXYCallback = setXCallback;
1722+
}
1723+
else
17481724
{
1749-
if (_setYCallback == null)
1750-
_setYCallback = setXCallback;
1751-
if (_setXYCallback == null)
1752-
_setXYCallback = setXCallback;
1725+
_setXCallback = setXCallback;
1726+
_setYCallback = setYCallback;
1727+
_setXYCallback = setXYCallback;
17531728
}
17541729
}
17551730

17561731
override public function set(x:Float = 0, y:Float = 0):FlxCallbackPoint
17571732
{
1758-
super.set(x, y);
1733+
@:bypassAccessor this.x = x;
1734+
@:bypassAccessor this.y = y;
1735+
1736+
if (_setXCallback != null)
1737+
_setXCallback(this);
1738+
1739+
if (_setYCallback != null)
1740+
_setYCallback(this);
1741+
17591742
if (_setXYCallback != null)
17601743
_setXYCallback(this);
1744+
17611745
return this;
17621746
}
17631747

17641748
override function set_x(value:Float):Float
17651749
{
17661750
super.set_x(value);
1751+
17671752
if (_setXCallback != null)
17681753
_setXCallback(this);
1754+
1755+
if (_setXYCallback != null)
1756+
_setXYCallback(this);
1757+
17691758
return value;
17701759
}
17711760

17721761
override function set_y(value:Float):Float
17731762
{
17741763
super.set_y(value);
1764+
17751765
if (_setYCallback != null)
17761766
_setYCallback(this);
1767+
1768+
if (_setXYCallback != null)
1769+
_setXYCallback(this);
1770+
17771771
return value;
17781772
}
17791773

0 commit comments

Comments
 (0)