2023-12-28 02:43:14 +08: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.
|
|
|
|
|
2023-12-28 04:14:37 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2023-12-28 02:43:14 +08:00
|
|
|
using osu.Framework.Allocation;
|
2023-12-28 04:14:37 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2023-12-28 02:43:14 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2023-12-28 04:14:37 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2023-12-28 21:10:03 +08:00
|
|
|
using osu.Game.Configuration;
|
2023-12-28 04:14:37 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
using osu.Game.Localisation;
|
|
|
|
using osuTK;
|
2023-12-28 02:43:14 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Settings.Sections.Audio
|
|
|
|
{
|
|
|
|
public partial class AudioOffsetAdjustControl : SettingsItem<double>
|
|
|
|
{
|
2023-12-31 02:33:03 +08:00
|
|
|
public IBindable<double?> SuggestedOffset => ((AudioOffsetPreview)Control).SuggestedOffset;
|
|
|
|
|
2023-12-28 02:43:14 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2023-12-28 04:14:37 +08:00
|
|
|
LabelText = AudioSettingsStrings.AudioOffset;
|
2023-12-28 02:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override Drawable CreateControl() => new AudioOffsetPreview();
|
|
|
|
|
2023-12-28 04:14:37 +08:00
|
|
|
private partial class AudioOffsetPreview : CompositeDrawable, IHasCurrentValue<double>
|
2023-12-28 02:43:14 +08:00
|
|
|
{
|
2023-12-28 04:14:37 +08:00
|
|
|
public Bindable<double> Current
|
|
|
|
{
|
|
|
|
get => current.Current;
|
|
|
|
set => current.Current = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly BindableNumberWithCurrent<double> current = new BindableNumberWithCurrent<double>();
|
|
|
|
|
2023-12-29 19:20:14 +08:00
|
|
|
private readonly IBindableList<SessionAverageHitErrorTracker.DataPoint> averageHitErrorHistory = new BindableList<SessionAverageHitErrorTracker.DataPoint>();
|
2023-12-28 04:14:37 +08:00
|
|
|
|
2023-12-31 02:33:03 +08:00
|
|
|
public readonly Bindable<double?> SuggestedOffset = new Bindable<double?>();
|
2023-12-28 04:14:37 +08:00
|
|
|
|
|
|
|
private Container<Box> notchContainer = null!;
|
|
|
|
private TextFlowContainer hintText = null!;
|
|
|
|
private RoundedButton applySuggestion = null!;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(SessionAverageHitErrorTracker hitErrorTracker)
|
|
|
|
{
|
|
|
|
averageHitErrorHistory.BindTo(hitErrorTracker.AverageHitErrorHistory);
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
InternalChild = new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Spacing = new Vector2(10),
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new TimeSlider
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Current = { BindTarget = Current },
|
|
|
|
KeyboardStep = 1,
|
|
|
|
},
|
|
|
|
notchContainer = new Container<Box>
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 10,
|
|
|
|
Padding = new MarginPadding { Horizontal = Nub.DEFAULT_EXPANDED_SIZE / 2 },
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
},
|
|
|
|
hintText = new OsuTextFlowContainer(t => t.Font = OsuFont.Default.With(size: 16))
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
},
|
|
|
|
applySuggestion = new RoundedButton
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Text = "Apply suggested offset",
|
|
|
|
Action = () =>
|
|
|
|
{
|
2023-12-31 02:33:03 +08:00
|
|
|
if (SuggestedOffset.Value.HasValue)
|
|
|
|
current.Value = SuggestedOffset.Value.Value;
|
2023-12-28 04:14:37 +08:00
|
|
|
hitErrorTracker.ClearHistory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
averageHitErrorHistory.BindCollectionChanged(updateDisplay, true);
|
2023-12-31 02:33:03 +08:00
|
|
|
SuggestedOffset.BindValueChanged(_ => updateHintText(), true);
|
2023-12-28 04:14:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateDisplay(object? _, NotifyCollectionChangedEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2023-12-29 19:20:14 +08:00
|
|
|
foreach (SessionAverageHitErrorTracker.DataPoint dataPoint in e.NewItems!)
|
2023-12-28 04:14:37 +08:00
|
|
|
{
|
|
|
|
notchContainer.ForEach(n => n.Alpha *= 0.95f);
|
|
|
|
notchContainer.Add(new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Width = 2,
|
|
|
|
RelativePositionAxes = Axes.X,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
2023-12-29 19:20:14 +08:00
|
|
|
X = getXPositionForOffset(dataPoint.SuggestedGlobalAudioOffset)
|
2023-12-28 04:14:37 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
2023-12-29 19:20:14 +08:00
|
|
|
foreach (SessionAverageHitErrorTracker.DataPoint dataPoint in e.OldItems!)
|
2023-12-28 04:14:37 +08:00
|
|
|
{
|
2023-12-29 19:20:14 +08:00
|
|
|
var notch = notchContainer.FirstOrDefault(n => n.X == getXPositionForOffset(dataPoint.SuggestedGlobalAudioOffset));
|
2023-12-28 04:14:37 +08:00
|
|
|
Debug.Assert(notch != null);
|
|
|
|
notchContainer.Remove(notch, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Reset:
|
|
|
|
notchContainer.Clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-12-31 02:34:37 +08:00
|
|
|
SuggestedOffset.Value = averageHitErrorHistory.Any() ? averageHitErrorHistory.Average(dataPoint => dataPoint.SuggestedGlobalAudioOffset) : null;
|
2023-12-28 04:14:37 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 19:20:14 +08:00
|
|
|
private float getXPositionForOffset(double offset) => (float)(Math.Clamp(offset, current.MinValue, current.MaxValue) / (2 * current.MaxValue));
|
2023-12-28 04:14:37 +08:00
|
|
|
|
|
|
|
private void updateHintText()
|
|
|
|
{
|
2023-12-31 02:33:03 +08:00
|
|
|
hintText.Text = SuggestedOffset.Value == null
|
2023-12-28 04:14:37 +08:00
|
|
|
? @"Play a few beatmaps to receive a suggested offset!"
|
2023-12-31 02:33:03 +08:00
|
|
|
: $@"Based on the last {averageHitErrorHistory.Count} play(s), the suggested offset is {SuggestedOffset.Value:N0} ms.";
|
|
|
|
applySuggestion.Enabled.Value = SuggestedOffset.Value != null;
|
2023-12-28 04:14:37 +08:00
|
|
|
}
|
2023-12-28 02:43:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|