From 05f510cf5cddfa0751a851309df966cf405bfc83 Mon Sep 17 00:00:00 2001 From: ACrazyTown <47027981+ACrazyTown@users.noreply.github.com> Date: Tue, 21 May 2024 22:08:05 +0200 Subject: [PATCH] Add onVolumeChange to SoundFrontEnd (#3148) * 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 --- flixel/system/frontEnds/SoundFrontEnd.hx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/flixel/system/frontEnds/SoundFrontEnd.hx b/flixel/system/frontEnds/SoundFrontEnd.hx index 4abab4d04a..0a5e6bb07f 100644 --- a/flixel/system/frontEnds/SoundFrontEnd.hx +++ b/flixel/system/frontEnds/SoundFrontEnd.hx @@ -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") @@ -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):FlxTypedSignalVoid> = new FlxTypedSignalVoid>(); + #if FLX_KEYBOARD /** * The key codes used to increase volume (see FlxG.keys for the keys available). @@ -328,6 +335,7 @@ class SoundFrontEnd /** * Toggles muted, also activating the sound tray. */ + @:haxe.warning("-WDeprecated") public function toggleMuted():Void { muted = !muted; @@ -337,6 +345,8 @@ class SoundFrontEnd volumeHandler(muted ? 0 : volume); } + onVolumeChange.dispatch(muted ? 0 : volume); + showSoundTray(true); } @@ -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; } }