Skip to content

Commit b7ff71e

Browse files
committed
rrvolumehh
1 parent 5326c40 commit b7ff71e

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

project.hxp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ class Project extends HXProject
293293
*/
294294
static final FEATURE_DISCORD_RPC:FeatureFlag = "FEATURE_DISCORD_RPC";
295295

296+
/**
297+
* `-DFEATURE_LOST_FOCUS_VOLUME`
298+
* If this flag is enabled, the game will reduce application volume, when lost focus.
299+
* Allowed only on Desktop targets.
300+
*/
301+
static final FEATURE_LOST_FOCUS_VOLUME:FeatureFlag = "FEATURE_LOST_FOCUS_VOLUME";
302+
296303
/**
297304
* `-DFEATURE_FILE_DROP`
298305
* If this flag is enabled, the game will support dragging and dropping files onto it for various features.
@@ -857,6 +864,8 @@ class Project extends HXProject
857864
// We don't want testers to accidentally leak songs to their Discord friends!
858865
FEATURE_DISCORD_RPC.apply(this, isDesktop() && !FEATURE_DEBUG_FUNCTIONS.isEnabled(this));
859866

867+
FEATURE_LOST_FOCUS_VOLUME.apply(this, isDesktop());
868+
860869
// Newgrounds features
861870
FEATURE_NEWGROUNDS.apply(this, !isMobile());
862871
FEATURE_NEWGROUNDS_DEBUG.apply(this, false);

source/funkin/InitState.hx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ import funkin.api.discord.DiscordClient;
4444
#if FEATURE_NEWGROUNDS
4545
import funkin.api.newgrounds.NewgroundsClient;
4646
#end
47+
#if FEATURE_LOST_FOCUS_VOLUME
48+
import flixel.tweens.FlxTween;
49+
import flixel.tweens.FlxEase;
50+
#end
4751

4852
/**
4953
* A core class which performs initialization of the game.
@@ -212,6 +216,10 @@ class InitState extends FlxState
212216
});
213217
#end
214218

219+
#if FEATURE_LOST_FOCUS_VOLUME
220+
FlxG.signals.focusLost.add(onLostFocus);
221+
FlxG.signals.focusGained.add(onGainFocus);
222+
#end
215223
//
216224
// ANDROID SETUP
217225
//
@@ -287,6 +295,50 @@ class InitState extends FlxState
287295
#end
288296
}
289297

298+
#if FEATURE_LOST_FOCUS_VOLUME
299+
@:noCompletion var _lastFocusVolume:Null<Float>;
300+
@:noCompletion var _lostFocusVolumeTween:Null<FlxTween>;
301+
302+
function onLostFocus()
303+
{
304+
if (FlxG.sound.muted || FlxG.sound.volume == 0 || Preferences.lostVolumeFocusMode == "Off" || FlxG.autoPause) return;
305+
_lostFocusVolumeTween?.cancel();
306+
_lastFocusVolume = FlxG.sound.volume;
307+
308+
if (Preferences.lostVolumeFocusMode == "Instant")
309+
{
310+
FlxG.sound.volume *= Constants.LOST_FOCUS_VOLUME_MULTIPLIER;
311+
}
312+
else if (Preferences.lostVolumeFocusMode == "Delayed")
313+
{
314+
_lostFocusVolumeTween = FlxTween.num(FlxG.sound.volume, FlxG.sound.volume * Constants.LOST_FOCUS_VOLUME_MULTIPLIER, Constants.LOST_FOCUS_VOLUME_DURATION,
315+
{
316+
startDelay: Constants.LOST_FOCUS_VOLUME_DELAY,
317+
ease: FlxEase.smootherStepOut
318+
}, (volume:Float) -> FlxG.sound.volume = volume);
319+
}
320+
}
321+
322+
function onGainFocus()
323+
{
324+
if (FlxG.sound.muted || Preferences.lostVolumeFocusMode == "Off" || FlxG.autoPause) return;
325+
_lostFocusVolumeTween?.cancel();
326+
327+
if (_lastFocusVolume != null)
328+
{
329+
if (Preferences.lostVolumeFocusMode == "Instant")
330+
{
331+
FlxG.sound.volume = _lastFocusVolume;
332+
}
333+
else if (Preferences.lostVolumeFocusMode == "Delayed")
334+
{
335+
_lostFocusVolumeTween = FlxTween.num(FlxG.sound.volume, _lastFocusVolume, Math.max(0.15, FlxG.sound.volume / _lastFocusVolume * .5),
336+
{ease: FlxEase.smootherStepInOut}, (volume:Float) -> FlxG.sound.volume = volume);
337+
}
338+
}
339+
}
340+
#end
341+
290342
/**
291343
* Start the game.
292344
*

source/funkin/util/Constants.hx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,23 @@ class Constants
570570
public static final GHOST_TAP_DELAY:Float = 3 / 8;
571571
#end
572572

573+
#if FEATURE_LOST_FOCUS_VOLUME
574+
/**
575+
* How much volume should be reduced on Application Focus Lost.
576+
*/
577+
public static final LOST_FOCUS_VOLUME_MULTIPLIER:Float = 0.15;
578+
579+
/**
580+
* Duration, in seconds, how long should volume reduce.
581+
*/
582+
public static final LOST_FOCUS_VOLUME_DURATION:Float = 0.85;
583+
584+
/**
585+
* Delay how long should game wait before fading volume with `Delayed` volume fade option.
586+
*/
587+
public static final LOST_FOCUS_VOLUME_DELAY:Float = 0.15;
588+
#end
589+
573590
/**
574591
* Otherwise known as "The FuckCunt Variable"
575592
*/

0 commit comments

Comments
 (0)