Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate sound.group setter, avoiding recursion errors #3041

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ class FlxSound extends FlxBasic
public var length(get, never):Float;

/**
* The sound group this sound belongs to
* 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(default, set):FlxSoundGroup;

Expand Down Expand Up @@ -252,6 +253,10 @@ class FlxSound extends FlxBasic

override public function destroy():Void
{
// Prevents double destroy
if (group != null)
group.remove(this);

_transform = null;
exists = false;
active = false;
Expand Down Expand Up @@ -703,24 +708,20 @@ class FlxSound extends FlxBasic
}
#end

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

// New group must be set before removing sound to prevent infinite recursion
this.group = group;

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

if (group != null)
group.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 group;
return value;
}

inline function get_playing():Bool
Expand Down
17 changes: 13 additions & 4 deletions flixel/sound/FlxSoundGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ class FlxSoundGroup
}

/**
* Add a sound to this group
* Add a sound to this group, will remove the sound from any group it is currently in
* @param sound The sound to add to this group
* @return True if sound was successfully added, false otherwise
*/
public function add(sound:FlxSound):Bool
{
if (sounds.indexOf(sound) < 0)
if (!sounds.contains(sound))
{
// remove from prev group
if (sound.group != null)
sound.group.sounds.remove(sound);

sounds.push(sound);
@:bypassAccessor
sound.group = this;
sound.updateTransform();
return true;
}
return false;
Expand All @@ -47,10 +53,13 @@ class FlxSoundGroup
*/
public function remove(sound:FlxSound):Bool
{
if (sounds.indexOf(sound) >= 0)
if (sounds.contains(sound))
{
@:bypassAccessor
sound.group = null;
return sounds.remove(sound);
sounds.remove(sound);
sound.updateTransform();
return true;
}
return false;
}
Expand Down
29 changes: 13 additions & 16 deletions flixel/system/frontEnds/SoundFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class SoundFrontEnd
*/
public function playMusic(embeddedMusic:FlxSoundAsset, volume = 1.0, looped = true, ?group:FlxSoundGroup):Void
{
if (group == null)
group = defaultMusicGroup;

if (music == null)
{
music = new FlxSound();
Expand All @@ -114,11 +117,11 @@ class SoundFrontEnd
{
music.stop();
}

music.loadEmbedded(embeddedMusic, looped);
music.volume = volume;
music.persist = true;
music.group = (group == null) ? defaultMusicGroup : group;
group.add(music);
music.play();
}

Expand Down Expand Up @@ -180,14 +183,15 @@ class SoundFrontEnd

function loadHelper(sound:FlxSound, volume:Float, group:FlxSoundGroup, autoPlay = false):FlxSound
{
if (group == null)
group = defaultSoundGroup;

sound.volume = volume;

group.add(sound);

if (autoPlay)
{
sound.play();
}

sound.group = (group == null) ? defaultSoundGroup : group;

return sound;
}

Expand Down Expand Up @@ -308,26 +312,19 @@ class SoundFrontEnd
{
if (music != null && (forceDestroy || !music.persist))
{
destroySound(music);
music.destroy();
music = null;
}

for (sound in list.members)
{
if (sound != null && (forceDestroy || !sound.persist))
{
destroySound(sound);
sound.destroy();
}
}
}

function destroySound(sound:FlxSound):Void
{
defaultMusicGroup.remove(sound);
defaultSoundGroup.remove(sound);
sound.destroy();
}

/**
* Toggles muted, also activating the sound tray.
*/
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);
}
}
Loading