Merge pull request #392 from FunkinCrew/soundtray

funkin soundtray
This commit is contained in:
Eric 2024-03-21 21:49:27 -04:00 committed by GitHub
commit 72d7c1273d
2 changed files with 155 additions and 1 deletions

View file

@ -100,8 +100,15 @@ class Main extends Sprite
// George recommends binding the save before FlxGame is created. // George recommends binding the save before FlxGame is created.
Save.load(); Save.load();
var game:FlxGame = new FlxGame(gameWidth, gameHeight, initialState, framerate, framerate, skipSplash, startFullscreen);
addChild(new FlxGame(gameWidth, gameHeight, initialState, framerate, framerate, skipSplash, startFullscreen)); // FlxG.game._customSoundTray wants just the class, it calls new from
// create() in there, which gets called when it's added to stage
// which is why it needs to be added before addChild(game) here
@:privateAccess
game._customSoundTray = funkin.ui.options.FunkinSoundTray;
addChild(game);
#if hxcpp_debug_server #if hxcpp_debug_server
trace('hxcpp_debug_server is enabled! You can now connect to the game with a debugger.'); trace('hxcpp_debug_server is enabled! You can now connect to the game with a debugger.');

View file

@ -0,0 +1,147 @@
package funkin.ui.options;
import flixel.system.ui.FlxSoundTray;
import flixel.tweens.FlxTween;
import flixel.system.FlxAssets;
import flixel.tweens.FlxEase;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.utils.Assets;
import funkin.util.MathUtil;
/**
* Extends the default flixel soundtray, but with some art
* and lil polish!
*
* Gets added to the game in Main.hx, right after FlxGame is new'd
* since it's a Sprite rather than Flixel related object
*/
class FunkinSoundTray extends FlxSoundTray
{
var graphicScale:Float = 0.30;
var lerpYPos:Float = 0;
var volumeMaxSound:String;
public function new()
{
// calls super, then removes all children to add our own
// graphics
super();
removeChildren();
var bg:Bitmap = new Bitmap(Assets.getBitmapData(Paths.image("soundtray/volumebox")));
bg.scaleX = graphicScale;
bg.scaleY = graphicScale;
addChild(bg);
y = -height;
visible = false;
// makes an alpha'd version of all the bars (bar_10.png)
var backingBar:Bitmap = new Bitmap(Assets.getBitmapData(Paths.image("soundtray/bars_10")));
backingBar.x = 10;
backingBar.y = 5;
backingBar.scaleX = graphicScale;
backingBar.scaleY = graphicScale;
addChild(backingBar);
backingBar.alpha = 0.4;
// clear the bars array entirely, it was initialized
// in the super class
_bars = [];
// 1...11 due to how block named the assets,
// we are trying to get assets bars_1-10
for (i in 1...11)
{
var bar:Bitmap = new Bitmap(Assets.getBitmapData(Paths.image("soundtray/bars_" + i)));
bar.x = 10;
bar.y = 5;
bar.scaleX = graphicScale;
bar.scaleY = graphicScale;
addChild(bar);
_bars.push(bar);
}
y = -height;
screenCenter();
volumeUpSound = Paths.sound("soundtray/Volup");
volumeDownSound = Paths.sound("soundtray/Voldown");
volumeMaxSound = Paths.sound("soundtray/VolMAX");
trace("Custom tray added!");
}
override public function update(MS:Float):Void
{
y = MathUtil.coolLerp(y, lerpYPos, 0.1);
// Animate sound tray thing
if (_timer > 0)
{
_timer -= (MS / 1000);
}
else if (y > -height)
{
lerpYPos = -height - 10;
if (y <= -height)
{
visible = false;
active = false;
#if FLX_SAVE
// Save sound preferences
if (FlxG.save.isBound)
{
FlxG.save.data.mute = FlxG.sound.muted;
FlxG.save.data.volume = FlxG.sound.volume;
FlxG.save.flush();
}
#end
}
}
}
/**
* Makes the little volume tray slide out.
*
* @param up Whether the volume is increasing.
*/
override public function show(up:Bool = false):Void
{
_timer = 1;
lerpYPos = 10;
visible = true;
active = true;
var globalVolume:Int = Math.round(FlxG.sound.volume * 10);
if (FlxG.sound.muted)
{
globalVolume = 0;
}
if (!silent)
{
var sound = up ? volumeUpSound : volumeDownSound;
if (globalVolume == 10) sound = volumeMaxSound;
if (sound != null) FlxG.sound.load(sound).play();
}
for (i in 0..._bars.length)
{
if (i < globalVolume)
{
_bars[i].visible = true;
}
else
{
_bars[i].visible = false;
}
}
}
}