Skip to content

Commit

Permalink
deprecate sound.group setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Feb 22, 2024
1 parent 59f92ed commit 8423970
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
34 changes: 24 additions & 10 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,16 @@ 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;
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;

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

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

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

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

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

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

updateTransform();
}
return group;
return value;
}

inline function get_playing():Bool
Expand Down
18 changes: 12 additions & 6 deletions flixel/sound/FlxSoundGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ 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))
{
if (sound._group != null)
sound._group.sounds.remove(sound);

sounds.push(sound);
sound.group = this;
sound._group = this;
sound.updateTransform();
return true;
}
return false;
Expand All @@ -47,10 +51,12 @@ class FlxSoundGroup
*/
public function remove(sound:FlxSound):Bool
{
if (sounds.indexOf(sound) >= 0)
if (sounds.contains(sound))
{
sound.group = null;
return sounds.remove(sound);
sound._group = null;
sounds.remove(sound);
sound.updateTransform();
return true;
}
return false;
}
Expand Down
18 changes: 11 additions & 7 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

0 comments on commit 8423970

Please sign in to comment.