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

Inactive volume #359

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions source/funkin/backend/system/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Main extends Sprite

public static var time:Int = 0;

public static var isFocused:Bool = true;

// You can pretty much ignore everything from here on - your code should go in your states.

#if ALLOW_MULTITHREADING
Expand Down Expand Up @@ -101,6 +103,8 @@ class Main extends Sprite
return time = Lib.getTimer();
}

private static var lastVolume:Float = 0;

public static function loadGameSettings() {
WindowUtils.init();
SaveWarning.init();
Expand Down Expand Up @@ -155,6 +159,21 @@ class Main extends Sprite
FlxG.signals.preStateSwitch.add(onStateSwitch);
FlxG.signals.postStateSwitch.add(onStateSwitchPost);

FlxG.signals.focusLost.add(function()
{
isFocused = false;
lastVolume = FlxG.sound.volume;
Options.volume = FlxG.sound.volume;
if(Options.inactiveVolumeEnabled && !Options.autoPause)
FlxG.sound.volume = Options.inactiveVolume / 100;
});

FlxG.signals.focusGained.add(function()
{
isFocused = true;
FlxG.sound.volume = lastVolume;
});

FlxG.mouse.useSystemCursor = true;

ModsFolder.init();
Expand Down
5 changes: 4 additions & 1 deletion source/funkin/options/Options.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Options
public static var autoPause:Bool = true;
public static var antialiasing:Bool = true;
public static var volume:Float = 1;
public static var inactiveVolume:Float = 2;
public static var inactiveVolumeEnabled:Bool = true;
public static var week6PixelPerfect:Bool = true;
public static var gameplayShaders:Bool = true;
public static var colorHealthBar:Bool = true;
Expand Down Expand Up @@ -149,7 +151,8 @@ class Options
}

public static function save() {
volume = FlxG.sound.volume;
if (Main.isFocused)
volume = FlxG.sound.volume;
__flush();
}
}
12 changes: 12 additions & 0 deletions source/funkin/options/categories/AppearanceOptions.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ class AppearanceOptions extends OptionsScreen {
"Auto Pause",
"If checked, switching windows will pause the game.",
"autoPause"));
add(new Checkbox(
"Use inactive volume",
"If checked, the games volume will turn down when the window is inactive.",
"inactiveVolumeEnabled"));
add(new NumOption(
"Inactive Volume",
"Volume percentage the window will change too when the window is inactive.",
0,
100,
2,
"inactiveVolume",
null));
}

private function __changeFPS(change:Float) {
Expand Down