1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:52:57 +08:00
osu-lazer/osu.Game/Overlays/Volume/VolumeMeter.cs

194 lines
6.4 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2018-02-28 23:14:52 +08:00
using System.Globalization;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
2018-03-04 02:25:34 +08:00
using osu.Framework.Extensions.Color4Extensions;
2018-02-28 23:14:52 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings;
using osu.Framework.MathUtils;
2018-02-28 23:14:52 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Overlays.Volume
{
public class VolumeMeter : Container, IKeyBindingHandler<GlobalAction>
{
private CircularProgress volumeCircle;
2018-03-07 13:38:41 +08:00
public BindableDouble Bindable { get; } = new BindableDouble { MinValue = 0, MaxValue = 1 };
2018-02-28 23:14:52 +08:00
private readonly float circleSize;
private readonly Color4 meterColour;
private readonly string name;
2018-03-07 13:50:50 +08:00
private OsuSpriteText text;
private BufferedContainer maxGlow;
2018-02-28 23:14:52 +08:00
public VolumeMeter(string name, float circleSize, Color4 meterColour)
{
this.circleSize = circleSize;
this.meterColour = meterColour;
this.name = name;
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Add(new Container
{
Size = new Vector2(120, 20),
CornerRadius = 10,
Masking = true,
Margin = new MarginPadding { Left = circleSize + 10 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
Alpha = 0.9f,
},
new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = "Exo2.0-Bold",
Text = name
}
}
});
CircularProgress bgProgress;
Add(new CircularContainer
{
Masking = true,
Size = new Vector2(circleSize),
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1,
Alpha = 0.9f,
},
bgProgress = new CircularProgress
{
RelativeSizeAxes = Axes.Both,
InnerRadius = 0.05f,
Rotation = 180,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = colours.Gray2,
Size = new Vector2(0.8f)
},
new Container
2018-02-28 23:14:52 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.8f),
Padding = new MarginPadding(-Blur.KernelSize(5)),
Rotation = 180,
Child = (volumeCircle = new CircularProgress
{
RelativeSizeAxes = Axes.Both,
InnerRadius = 0.05f,
}).WithEffect(new GlowEffect
{
Colour = meterColour,
Strength = 2,
PadExtent = true
}),
},
2018-03-04 02:25:34 +08:00
maxGlow = (text = new OsuSpriteText
2018-02-28 23:14:52 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = "Venera",
TextSize = 0.16f * circleSize
2018-03-04 02:25:34 +08:00
}).WithEffect(new GlowEffect
2018-02-28 23:14:52 +08:00
{
2018-03-04 02:25:34 +08:00
Colour = Color4.Transparent,
2018-02-28 23:14:52 +08:00
PadExtent = true,
2018-03-04 02:25:34 +08:00
})
2018-02-28 23:14:52 +08:00
}
});
2018-03-07 13:50:50 +08:00
Bindable.ValueChanged += newVolume => { this.TransformTo("DisplayVolume", newVolume, 400, Easing.OutQuint); };
bgProgress.Current.Value = 0.75f;
}
protected override void LoadComplete()
{
base.LoadComplete();
Bindable.TriggerChange();
}
2018-03-07 13:50:50 +08:00
private double displayVolume;
protected double DisplayVolume
{
get => displayVolume;
set
2018-02-28 23:14:52 +08:00
{
2018-03-07 13:50:50 +08:00
displayVolume = value;
if (displayVolume > 0.99f)
2018-02-28 23:14:52 +08:00
{
2018-03-04 02:25:34 +08:00
text.Text = "MAX";
maxGlow.EffectColour = meterColour.Opacity(2f);
2018-02-28 23:14:52 +08:00
}
else
{
2018-03-07 13:50:50 +08:00
maxGlow.EffectColour = Color4.Transparent;
text.Text = Math.Round(displayVolume * 100).ToString(CultureInfo.CurrentCulture);
2018-02-28 23:14:52 +08:00
}
2018-03-07 13:50:50 +08:00
volumeCircle.Current.Value = displayVolume * 0.75f;
}
2018-02-28 23:14:52 +08:00
}
public double Volume
{
get => Bindable;
private set => Bindable.Value = value;
}
2018-03-07 13:54:54 +08:00
public void Increase() => Volume += 0.05f;
2018-02-28 23:14:52 +08:00
2018-03-07 13:54:54 +08:00
public void Decrease() => Volume -= 0.05f;
2018-02-28 23:14:52 +08:00
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;
}
}