Skip to content

Commit

Permalink
change sound tray stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Dec 18, 2023
1 parent 760b42d commit cb7d4fe
Showing 1 changed file with 207 additions and 0 deletions.
207 changes: 207 additions & 0 deletions source/flixel/system/ui/FlxSoundTray.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package flixel.system.ui;

#if FLX_SOUND_SYSTEM
import flixel.FlxG;
import flixel.system.FlxAssets;
import flixel.util.FlxColor;
import openfl.Lib;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Sprite;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFormatAlign;
#if flash
import openfl.text.AntiAliasType;
import openfl.text.GridFitType;
#end

/**
* The flixel sound tray, the little volume meter that pops down sometimes.
* Accessed via `FlxG.game.soundTray` or `FlxG.sound.soundTray`.
*/
class FlxSoundTray extends Sprite
{
/**
* Because reading any data from DisplayObject is insanely expensive in hxcpp, keep track of whether we need to update it or not.
*/
public var active:Bool;

/**
* Helps us auto-hide the sound tray after a volume change.
*/
var _timer:Float;

/**
* Helps display the volume bars on the sound tray.
*/
var _bars:Array<Bitmap>;

/**
* How wide the sound tray background is.
*/
var _width:Int = 80;

var _defaultScale:Float = 2.0;

/**The sound used when increasing the volume.**/
public var volumeUpSound:String = "flixel/sounds/beep";

/**The sound used when decreasing the volume.**/
public var volumeDownSound:String = 'flixel/sounds/beep';

/**Whether or not changing the volume should make noise.**/
public var silent:Bool = false;

var percentText:TextField;

/**
* Sets up the "sound tray", the little volume meter that pops down sometimes.
*/
@:keep
public function new()
{
super();

visible = false;
scaleX = _defaultScale;
scaleY = _defaultScale;
var tmp:Bitmap = new Bitmap(new BitmapData(_width, 50, true, 0x7F000000));
screenCenter();
addChild(tmp);

var text:TextField = new TextField();
text.width = tmp.width;
text.height = tmp.height;
text.multiline = true;
text.wordWrap = true;
text.selectable = false;

percentText = new TextField();
percentText.width = tmp.width;
percentText.height = tmp.height;
percentText.multiline = true;
percentText.wordWrap = true;
percentText.selectable = false;

#if flash
text.embedFonts = true;
text.antiAliasType = AntiAliasType.NORMAL;
text.gridFitType = GridFitType.PIXEL;
percentText.embedFonts = true;
percentText.antiAliasType = AntiAliasType.NORMAL;
percentText.gridFitType = GridFitType.PIXEL;
#else
#end
var dtf:TextFormat = new TextFormat(FlxAssets.FONT_DEFAULT, 10, 0xffffff);
dtf.align = TextFormatAlign.CENTER;
text.defaultTextFormat = dtf;
addChild(text);
text.text = "VOLUME";
text.y = 16;

percentText.defaultTextFormat = dtf;
addChild(percentText);
percentText.text = Math.round(FlxG.sound.volume * 10) + "%";
percentText.y = 30;

var bx:Int = 10;
var by:Int = 14;
_bars = new Array();

for (i in 0...10)
{
tmp = new Bitmap(new BitmapData(4, i + 1, false, FlxColor.WHITE));
tmp.x = bx;
tmp.y = by;
addChild(tmp);
_bars.push(tmp);
bx += 6;
by--;
}

y = -height;
visible = false;
}

/**
* This function updates the soundtray object.
*/
public function update(MS:Float):Void
{
// Animate sound tray thing
if (_timer > 0)
{
_timer -= (MS / 1000);
}
else if (y > -height)
{
y -= (MS / 1000) * height * 0.5;

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.
*/
public function show(up:Bool = false):Void
{
if (!silent)
{
var sound = FlxAssets.getSound(up ? volumeUpSound : volumeDownSound);
if (sound != null)
FlxG.sound.load(sound).play();
}

_timer = 1;
y = 0;
visible = true;
active = true;
var globalVolume:Int = Math.round(FlxG.sound.volume * 10);

if (FlxG.sound.muted)
{
globalVolume = 0;
}

percentText.text = globalVolume * 10 + "%";

for (i in 0..._bars.length)
{
if (i < globalVolume)
{
_bars[i].alpha = 1;
}
else
{
_bars[i].alpha = 0.5;
}
}
}

public function screenCenter():Void
{
scaleX = _defaultScale;
scaleY = _defaultScale;

x = (0.5 * (Lib.current.stage.stageWidth - _width * _defaultScale) - FlxG.game.x);
}
}
#end

0 comments on commit cb7d4fe

Please sign in to comment.