1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-15 19:33:36 +00:00
Funkin/source/funkin/ui/options/FunkinSoundTray.hx

151 lines
3.4 KiB
Haxe
Raw Normal View History

2024-03-15 02:27:07 +00:00
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 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;
2024-04-29 01:22:11 +00:00
var alphaTarget:Float = 0;
2024-03-15 02:27:07 +00:00
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;
2024-03-15 02:40:07 +00:00
// makes an alpha'd version of all the bars (bar_10.png)
var backingBar:Bitmap = new Bitmap(Assets.getBitmapData(Paths.image("soundtray/bars_10")));
2024-04-29 01:22:11 +00:00
backingBar.x = 9;
2024-03-15 02:40:07 +00:00
backingBar.y = 5;
backingBar.scaleX = graphicScale;
backingBar.scaleY = graphicScale;
addChild(backingBar);
backingBar.alpha = 0.4;
2024-03-15 02:27:07 +00:00
// 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)));
2024-04-29 01:22:11 +00:00
bar.x = 9;
2024-03-15 02:27:07 +00:00
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);
2024-04-29 01:22:11 +00:00
alpha = MathUtil.coolLerp(alpha, alphaTarget, 0.25);
2024-03-15 02:27:07 +00:00
// Animate sound tray thing
if (_timer > 0)
{
_timer -= (MS / 1000);
2024-04-29 01:22:11 +00:00
alphaTarget = 1;
2024-03-15 02:27:07 +00:00
}
2024-04-30 11:54:42 +00:00
else if (y >= -height)
2024-03-15 02:27:07 +00:00
{
2024-03-15 02:40:07 +00:00
lerpYPos = -height - 10;
2024-04-29 01:22:11 +00:00
alphaTarget = 0;
2024-04-30 11:54:42 +00:00
}
if (y <= -height)
{
visible = false;
active = false;
2024-03-15 02:27:07 +00:00
2024-04-30 11:54:42 +00:00
#if FLX_SAVE
// Save sound preferences
if (FlxG.save.isBound)
2024-03-15 02:27:07 +00:00
{
2024-04-30 11:54:42 +00:00
FlxG.save.data.mute = FlxG.sound.muted;
FlxG.save.data.volume = FlxG.sound.volume;
FlxG.save.flush();
2024-03-15 02:27:07 +00:00
}
2024-04-30 11:54:42 +00:00
#end
2024-03-15 02:27:07 +00:00
}
}
/**
* Makes the little volume tray slide out.
*
* @param up Whether the volume is increasing.
*/
override public function show(up:Bool = false):Void
{
_timer = 1;
2024-03-15 02:40:07 +00:00
lerpYPos = 10;
2024-03-15 02:27:07 +00:00
visible = true;
active = true;
2024-09-09 17:01:19 +00:00
var globalVolume:Int = Math.round(FlxG.sound.logToLinear(FlxG.sound.volume) * 10);
2024-03-15 02:27:07 +00:00
if (FlxG.sound.muted || FlxG.sound.volume == 0)
2024-03-15 02:27:07 +00:00
{
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;
}
}
}
}