Skip to content

Commit

Permalink
remove _group proxy, for better backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Feb 23, 2024
1 parent 8423970 commit ac1fd09
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
35 changes: 9 additions & 26 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,7 @@ class FlxSound extends FlxBasic
* The sound group this sound belongs to, can only be in one group.
* NOTE: This setter is deprecated, use `group.add(sound)` or `group.remove(sound)`.
*/
public var group(get, set):FlxSoundGroup;

/**
* The sound group this sound belongs to. This is a temporary proxy for group, until it is removed
*/
@:allow(flixel.sound.FlxSoundGroup)
var _group:FlxSoundGroup;
public var group(default, set):FlxSoundGroup;

/**
* Whether or not this sound should loop.
Expand Down Expand Up @@ -714,29 +708,18 @@ class FlxSound extends FlxBasic
}
#end

// Will be removed in a major version, and become a simple `(default,null)` var
inline function get_group():FlxSoundGroup
{
return _group;
}

@:deprecated("sound.group = myGroup is deprecated, use myGroup.add(sound)") // 5.7.0
function set_group(value:FlxSoundGroup):FlxSoundGroup
{
if (_group != value)
if (value != null)
{
final oldGroup = _group;

// New group must be set before removing sound to prevent infinite recursion
_group = value;

if (oldGroup != null)
oldGroup.remove(this);

if (value != null)
value.add(this);

updateTransform();
// add to new group, also removes from prev and calls updateTransform
value.add(this);
}
else
{
// remove from prev group, also calls updateTransform
group.remove(this);
}
return value;
}
Expand Down
11 changes: 7 additions & 4 deletions flixel/sound/FlxSoundGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class FlxSoundGroup
{
if (!sounds.contains(sound))
{
if (sound._group != null)
sound._group.sounds.remove(sound);
// remove from prev group
if (sound.group != null)
sound.group.sounds.remove(sound);

sounds.push(sound);
sound._group = this;
@:bypassAccessor
sound.group = this;
sound.updateTransform();
return true;
}
Expand All @@ -53,7 +55,8 @@ class FlxSoundGroup
{
if (sounds.contains(sound))
{
sound._group = null;
@:bypassAccessor
sound.group = null;
sounds.remove(sound);
sound.updateTransform();
return true;
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/src/flixel/sound/FlxSoundGroupTest.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flixel.sound;

import flixel.FlxG;
import massive.munit.Assert;

class FlxSoundGroupTest
Expand All @@ -8,7 +9,23 @@ class FlxSoundGroupTest
function testLengthAfterAdd()
{
var group = new FlxSoundGroup();
group.add(new FlxSound());
var sound = new FlxSound();
group.add(sound);
Assert.areEqual(1, group.sounds.length);
FlxG.sound.defaultSoundGroup.add(sound);
Assert.areEqual(0, group.sounds.length);
Assert.areEqual(1, FlxG.sound.defaultSoundGroup.sounds.length);
}

@:haxe.warning("-WDeprecated")
function testSetter()
{
var group = new FlxSoundGroup();
var sound = new FlxSound();
sound.group = group;
Assert.areEqual(1, group.sounds.length);
sound.group = FlxG.sound.defaultSoundGroup;
Assert.areEqual(0, group.sounds.length);
Assert.areEqual(1, FlxG.sound.defaultSoundGroup.sounds.length);
}
}

0 comments on commit ac1fd09

Please sign in to comment.