1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 13:47:24 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/Volume/VolumeMeter.cs

126 lines
3.7 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
using osu.Framework.Configuration;
2016-10-10 22:19:05 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Game.Input.Bindings;
namespace osu.Game.Graphics.UserInterface.Volume
{
public class VolumeMeter : Container, IKeyBindingHandler<GlobalAction>
{
private readonly Box meterFill;
2017-03-07 09:59:19 +08:00
public BindableDouble Bindable { get; } = new BindableDouble();
2016-10-10 22:19:05 +08:00
2018-01-17 00:46:54 +08:00
private double lastVolume;
public bool IsMuted { get; private set; }
public VolumeMeter(string meterName)
{
Size = new Vector2(40, 180);
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both
},
new Container
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.5f, 0.9f),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
{
new Box
{
Colour = Color4.DarkGray,
RelativeSizeAxes = Axes.Both
},
meterFill = new Box
{
Colour = Color4.White,
Scale = new Vector2(1, 0),
RelativeSizeAxes = Axes.Both,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre
}
}
},
new OsuSpriteText
2016-10-14 00:11:51 +08:00
{
Text = meterName,
Anchor = Anchor.BottomCentre,
Origin = Anchor.TopCentre
2016-10-14 00:11:51 +08:00
}
};
Bindable.ValueChanged += delegate { updateFill(); };
}
2016-10-10 22:19:05 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
updateFill();
}
2016-10-13 03:17:53 +08:00
public double Volume
2016-10-10 22:19:05 +08:00
{
2018-01-17 00:46:54 +08:00
get => Bindable.Value;
2016-10-13 03:17:53 +08:00
private set
{
Bindable.Value = value;
2018-01-17 00:46:54 +08:00
if (value > 0)
IsMuted = false;
2016-10-13 03:17:53 +08:00
}
2016-10-10 22:19:05 +08:00
}
public void Increase()
2016-10-10 22:19:05 +08:00
{
Volume += 0.05f;
2018-01-17 00:46:54 +08:00
IsMuted = false;
}
public void Decrease()
{
Volume -= 0.05f;
2016-10-10 22:19:05 +08:00
}
2018-01-17 21:36:33 +08:00
public void ToggleMute()
2018-01-17 00:46:54 +08:00
{
IsMuted = !IsMuted;
if (IsMuted)
lastVolume = Volume;
Volume = IsMuted ? 0.0 : lastVolume;
}
2017-07-23 02:50:25 +08:00
private void updateFill() => meterFill.ScaleTo(new Vector2(1, (float)Volume), 300, Easing.OutQuint);
public bool OnPressed(GlobalAction action)
{
if (!IsHovered) return false;
switch (action)
{
case GlobalAction.DecreaseVolume:
Decrease();
return true;
case GlobalAction.IncreaseVolume:
Increase();
return true;
}
return false;
}
public bool OnReleased(GlobalAction action) => false;
}
}