Skip to content

Commit

Permalink
Add onVolumeChange to SoundFrontEnd (HaxeFlixel#3148)
Browse files Browse the repository at this point in the history
* Add onVolumeChange to SoundFrontEnd

* Move import inside conditional

* deprecate old func

* simplify import

* remove deprecation warnings

* fix typo in @:deprecated

---------

Co-authored-by: George Kurelic <[email protected]>
  • Loading branch information
ACrazyTown and Geokureli committed May 21, 2024
1 parent d2f3ae2 commit 05f510c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions flixel/system/frontEnds/SoundFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import flixel.system.FlxAssets;
import flixel.sound.FlxSound;
import flixel.sound.FlxSoundGroup;
import flixel.system.ui.FlxSoundTray;
import flixel.util.FlxSignal;
import openfl.Assets;
import openfl.media.Sound;
#if (openfl >= "8.0.0")
Expand All @@ -35,8 +36,14 @@ class SoundFrontEnd
* Set this hook to get a callback whenever the volume changes.
* Function should take the form myVolumeHandler(volume:Float).
*/
@:deprecated("volumeHandler is deprecated, use onVolumeChange, instead")
public var volumeHandler:Float->Void;

/**
* A signal that gets dispatched whenever the volume changes.
*/
public var onVolumeChange(default, null):FlxTypedSignal<Float->Void> = new FlxTypedSignal<Float->Void>();

#if FLX_KEYBOARD
/**
* The key codes used to increase volume (see FlxG.keys for the keys available).
Expand Down Expand Up @@ -328,6 +335,7 @@ class SoundFrontEnd
/**
* Toggles muted, also activating the sound tray.
*/
@:haxe.warning("-WDeprecated")
public function toggleMuted():Void
{
muted = !muted;
Expand All @@ -337,6 +345,8 @@ class SoundFrontEnd
volumeHandler(muted ? 0 : volume);
}

onVolumeChange.dispatch(muted ? 0 : volume);

showSoundTray(true);
}

Expand Down Expand Up @@ -448,15 +458,18 @@ class SoundFrontEnd
}
#end

@:haxe.warning("-WDeprecated")
function set_volume(Volume:Float):Float
{
Volume = FlxMath.bound(Volume, 0, 1);

if (volumeHandler != null)
{
var param:Float = muted ? 0 : Volume;
volumeHandler(param);
volumeHandler(muted ? 0 : Volume);
}

onVolumeChange.dispatch(muted ? 0 : Volume);

return volume = Volume;
}
}
Expand Down

0 comments on commit 05f510c

Please sign in to comment.