2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
2021-10-03 16:44:23 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2021-10-03 16:44:23 +00:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2019-12-26 06:14:19 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-06-25 17:10:04 +00:00
|
|
|
|
using osu.Framework.Localisation;
|
2021-07-01 11:45:29 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2021-10-03 16:44:23 +00:00
|
|
|
|
using osu.Game.Overlays;
|
2021-11-12 22:07:24 +00:00
|
|
|
|
using osu.Game.Utils;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
|
|
|
|
public class OsuSliderBar<T> : SliderBar<T>, IHasTooltip, IHasAccentColour
|
2019-10-23 05:04:06 +00:00
|
|
|
|
where T : struct, IEquatable<T>, IComparable<T>, IConvertible
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maximum number of decimal digits to be displayed in the tooltip.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int max_decimal_digits = 5;
|
|
|
|
|
|
2021-01-19 08:11:40 +00:00
|
|
|
|
private Sample sample;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private double lastSampleTime;
|
|
|
|
|
private T lastSampleValue;
|
|
|
|
|
|
|
|
|
|
protected readonly Nub Nub;
|
|
|
|
|
private readonly Box leftBox;
|
|
|
|
|
private readonly Box rightBox;
|
2019-12-26 06:14:19 +00:00
|
|
|
|
private readonly Container nubContainer;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-06-25 17:10:04 +00:00
|
|
|
|
public virtual LocalisableString TooltipText { get; private set; }
|
2020-01-28 10:04:00 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to format the tooltip as a percentage or the actual value.
|
|
|
|
|
/// </summary>
|
2020-01-28 04:32:30 +00:00
|
|
|
|
public bool DisplayAsPercentage { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
private Color4 accentColour;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public Color4 AccentColour
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => accentColour;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
accentColour = value;
|
|
|
|
|
leftBox.Colour = value;
|
2021-10-03 16:44:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Colour4 backgroundColour;
|
|
|
|
|
|
|
|
|
|
public Color4 BackgroundColour
|
|
|
|
|
{
|
|
|
|
|
get => backgroundColour;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
backgroundColour = value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
rightBox.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OsuSliderBar()
|
|
|
|
|
{
|
2021-10-03 16:44:23 +00:00
|
|
|
|
Height = Nub.HEIGHT;
|
|
|
|
|
RangePadding = Nub.EXPANDED_SIZE / 2;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2021-10-03 16:44:23 +00:00
|
|
|
|
new Container
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-10-03 16:44:23 +00:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
2021-10-03 16:44:23 +00:00
|
|
|
|
Padding = new MarginPadding { Horizontal = 2 },
|
|
|
|
|
Child = new CircularContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
Masking = true,
|
|
|
|
|
CornerRadius = 5f,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
leftBox = new Box
|
|
|
|
|
{
|
|
|
|
|
Height = 5,
|
|
|
|
|
EdgeSmoothness = new Vector2(0, 0.5f),
|
|
|
|
|
RelativeSizeAxes = Axes.None,
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
},
|
|
|
|
|
rightBox = new Box
|
|
|
|
|
{
|
|
|
|
|
Height = 5,
|
|
|
|
|
EdgeSmoothness = new Vector2(0, 0.5f),
|
|
|
|
|
RelativeSizeAxes = Axes.None,
|
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
Alpha = 0.5f,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
2019-12-26 06:14:19 +00:00
|
|
|
|
nubContainer = new Container
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-12-26 06:14:19 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Child = Nub = new Nub
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativePositionAxes = Axes.X,
|
2021-10-03 16:44:23 +00:00
|
|
|
|
Current = { Value = true }
|
2019-12-26 06:14:19 +00:00
|
|
|
|
},
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
|
|
|
|
new HoverClickSounds()
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-28 04:31:40 +00:00
|
|
|
|
Current.DisabledChanged += disabled => { Alpha = disabled ? 0.3f : 1; };
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 16:44:23 +00:00
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(AudioManager audio, [CanBeNull] OverlayColourProvider colourProvider, OsuColour colours)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-07-01 11:45:29 +00:00
|
|
|
|
sample = audio.Samples.Get(@"UI/notch-tick");
|
2021-10-03 16:44:23 +00:00
|
|
|
|
AccentColour = colourProvider?.Highlight1 ?? colours.Pink;
|
|
|
|
|
BackgroundColour = colourProvider?.Background5 ?? colours.Pink.Opacity(0.5f);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 06:14:19 +00:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
|
|
nubContainer.Padding = new MarginPadding { Horizontal = RangePadding };
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 08:03:53 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2019-02-22 11:13:38 +00:00
|
|
|
|
CurrentNumber.BindValueChanged(current => updateTooltipText(current.NewValue), true);
|
2019-01-17 08:03:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-10-15 02:51:08 +00:00
|
|
|
|
updateGlow();
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnHover(e);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-10-15 02:51:08 +00:00
|
|
|
|
updateGlow();
|
2018-10-02 03:02:47 +00:00
|
|
|
|
base.OnHoverLost(e);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 02:51:08 +00:00
|
|
|
|
protected override void OnDragEnd(DragEndEvent e)
|
|
|
|
|
{
|
|
|
|
|
updateGlow();
|
|
|
|
|
base.OnDragEnd(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateGlow()
|
|
|
|
|
{
|
|
|
|
|
Nub.Glowing = IsHovered || IsDragged;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 07:07:54 +00:00
|
|
|
|
protected override void OnUserChange(T value)
|
|
|
|
|
{
|
|
|
|
|
base.OnUserChange(value);
|
|
|
|
|
playSample(value);
|
2019-01-23 02:21:09 +00:00
|
|
|
|
updateTooltipText(value);
|
2019-01-08 07:07:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void playSample(T value)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-07-01 11:45:29 +00:00
|
|
|
|
if (Clock == null || Clock.CurrentTime - lastSampleTime <= 30)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-01-08 07:07:54 +00:00
|
|
|
|
if (value.Equals(lastSampleValue))
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-01-08 07:07:54 +00:00
|
|
|
|
lastSampleValue = value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
lastSampleTime = Clock.CurrentTime;
|
|
|
|
|
|
2021-07-02 06:52:20 +00:00
|
|
|
|
var channel = sample.GetChannel();
|
2021-01-19 08:11:40 +00:00
|
|
|
|
|
2021-07-02 06:39:43 +00:00
|
|
|
|
channel.Frequency.Value = 0.99f + RNG.NextDouble(0.02f) + NormalizedValue * 0.2f;
|
|
|
|
|
|
2021-07-02 06:55:20 +00:00
|
|
|
|
// intentionally pitched down, even when hitting max.
|
2021-07-02 06:39:43 +00:00
|
|
|
|
if (NormalizedValue == 0 || NormalizedValue == 1)
|
2021-07-01 11:45:29 +00:00
|
|
|
|
channel.Frequency.Value -= 0.5f;
|
2021-07-02 06:52:20 +00:00
|
|
|
|
|
|
|
|
|
channel.Play();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 07:07:54 +00:00
|
|
|
|
private void updateTooltipText(T value)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-01-08 07:07:54 +00:00
|
|
|
|
if (CurrentNumber.IsInteger)
|
2019-12-03 12:49:41 +00:00
|
|
|
|
TooltipText = value.ToInt32(NumberFormatInfo.InvariantInfo).ToString("N0");
|
2019-01-08 07:07:54 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-03 12:49:41 +00:00
|
|
|
|
double floatValue = value.ToDouble(NumberFormatInfo.InvariantInfo);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-01-28 04:32:30 +00:00
|
|
|
|
if (DisplayAsPercentage)
|
|
|
|
|
{
|
2020-01-29 10:51:24 +00:00
|
|
|
|
TooltipText = floatValue.ToString("0%");
|
2020-01-28 04:32:30 +00:00
|
|
|
|
}
|
2019-01-08 07:07:54 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
decimal decimalPrecision = normalise(CurrentNumber.Precision.ToDecimal(NumberFormatInfo.InvariantInfo), max_decimal_digits);
|
2019-01-08 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
// Find the number of significant digits (we could have less than 5 after normalize())
|
2021-11-12 22:07:24 +00:00
|
|
|
|
int significantDigits = FormatUtils.FindPrecision(decimalPrecision);
|
2019-01-08 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
TooltipText = floatValue.ToString($"N{significantDigits}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
2019-11-20 12:19:49 +00:00
|
|
|
|
leftBox.Scale = new Vector2(Math.Clamp(
|
2019-12-26 06:14:19 +00:00
|
|
|
|
RangePadding + Nub.DrawPosition.X - Nub.DrawWidth / 2, 0, DrawWidth), 1);
|
2019-11-20 12:19:49 +00:00
|
|
|
|
rightBox.Scale = new Vector2(Math.Clamp(
|
2019-12-26 06:14:19 +00:00
|
|
|
|
DrawWidth - Nub.DrawPosition.X - RangePadding - Nub.DrawWidth / 2, 0, DrawWidth), 1);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateValue(float value)
|
|
|
|
|
{
|
2019-12-26 06:14:19 +00:00
|
|
|
|
Nub.MoveToX(value, 250, Easing.OutQuint);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes all non-significant digits, keeping at most a requested number of decimal digits.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="d">The decimal to normalize.</param>
|
|
|
|
|
/// <param name="sd">The maximum number of decimal digits to keep. The final result may have fewer decimal digits than this value.</param>
|
|
|
|
|
/// <returns>The normalised decimal.</returns>
|
|
|
|
|
private decimal normalise(decimal d, int sd)
|
|
|
|
|
=> decimal.Parse(Math.Round(d, sd).ToString(string.Concat("0.", new string('#', sd)), CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
}
|