2022-03-01 14:40:19 +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.
|
|
|
|
|
2022-03-08 18:36:08 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
using System;
|
2022-03-03 16:07:46 +08:00
|
|
|
using System.Diagnostics;
|
2022-03-01 18:41:51 +08:00
|
|
|
using System.Linq;
|
2022-03-01 17:28:53 +08:00
|
|
|
using System.Threading.Tasks;
|
2022-03-01 15:59:33 +08:00
|
|
|
using osu.Framework.Allocation;
|
2022-03-01 14:40:19 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-03-03 16:07:46 +08:00
|
|
|
using osu.Framework.Localisation;
|
2022-03-01 18:13:40 +08:00
|
|
|
using osu.Framework.Utils;
|
2022-03-01 15:59:33 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
2022-03-01 18:31:11 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
2022-03-01 14:40:19 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2022-03-02 13:42:08 +08:00
|
|
|
using osu.Game.Localisation;
|
2022-03-01 14:40:19 +08:00
|
|
|
using osu.Game.Overlays.Settings;
|
|
|
|
using osu.Game.Rulesets.Scoring;
|
2022-03-01 15:14:57 +08:00
|
|
|
using osu.Game.Scoring;
|
2022-03-01 14:40:19 +08:00
|
|
|
using osu.Game.Screens.Ranking.Statistics;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.PlayerSettings
|
|
|
|
{
|
|
|
|
public class BeatmapOffsetControl : CompositeDrawable
|
|
|
|
{
|
2022-03-01 15:14:57 +08:00
|
|
|
public Bindable<ScoreInfo> ReferenceScore { get; } = new Bindable<ScoreInfo>();
|
2022-03-01 14:40:19 +08:00
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
public BindableDouble Current { get; } = new BindableDouble
|
2022-03-01 14:40:19 +08:00
|
|
|
{
|
|
|
|
Default = 0,
|
|
|
|
Value = 0,
|
|
|
|
MinValue = -50,
|
|
|
|
MaxValue = 50,
|
|
|
|
Precision = 0.1,
|
|
|
|
};
|
|
|
|
|
2022-03-01 15:14:57 +08:00
|
|
|
private readonly FillFlowContainer referenceScoreContainer;
|
|
|
|
|
2022-03-01 18:57:09 +08:00
|
|
|
[Resolved]
|
2022-03-02 13:34:24 +08:00
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2022-03-01 18:57:09 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-03-02 13:34:24 +08:00
|
|
|
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
|
2022-03-01 18:57:09 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-03-02 13:34:24 +08:00
|
|
|
private OsuColour colours { get; set; } = null!;
|
|
|
|
|
|
|
|
private double lastPlayAverage;
|
2022-03-04 11:55:35 +08:00
|
|
|
private double lastPlayBeatmapOffset;
|
2022-03-04 12:09:19 +08:00
|
|
|
private HitEventTimingDistributionGraph? lastPlayGraph;
|
2022-03-02 13:34:24 +08:00
|
|
|
|
|
|
|
private SettingsButton? useAverageButton;
|
|
|
|
|
|
|
|
private IDisposable? beatmapOffsetSubscription;
|
|
|
|
|
|
|
|
private Task? realmWriteTask;
|
2022-03-01 18:57:09 +08:00
|
|
|
|
2022-03-01 15:14:57 +08:00
|
|
|
public BeatmapOffsetControl()
|
2022-03-01 14:40:19 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
2022-03-01 15:14:57 +08:00
|
|
|
InternalChild = new FillFlowContainer
|
2022-03-01 14:40:19 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(10),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2022-03-03 16:07:46 +08:00
|
|
|
new OffsetSliderBar
|
2022-03-01 14:40:19 +08:00
|
|
|
{
|
|
|
|
KeyboardStep = 5,
|
2022-03-01 18:33:30 +08:00
|
|
|
LabelText = BeatmapOffsetControlStrings.BeatmapOffset,
|
2022-03-01 14:40:19 +08:00
|
|
|
Current = Current,
|
|
|
|
},
|
2022-03-01 15:14:57 +08:00
|
|
|
referenceScoreContainer = new FillFlowContainer
|
2022-03-01 14:40:19 +08:00
|
|
|
{
|
2022-03-01 15:14:57 +08:00
|
|
|
Spacing = new Vector2(10),
|
|
|
|
Direction = FillDirection.Vertical,
|
2022-03-01 14:40:19 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
2022-03-01 15:14:57 +08:00
|
|
|
AutoSizeAxes = Axes.Y,
|
2022-03-01 14:40:19 +08:00
|
|
|
},
|
2022-03-01 15:14:57 +08:00
|
|
|
}
|
|
|
|
};
|
2022-03-01 15:59:33 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 16:07:46 +08:00
|
|
|
public class OffsetSliderBar : PlayerSliderBar<double>
|
|
|
|
{
|
|
|
|
protected override Drawable CreateControl() => new CustomSliderBar();
|
|
|
|
|
|
|
|
protected class CustomSliderBar : SliderBar
|
|
|
|
{
|
2022-03-05 21:46:13 +08:00
|
|
|
public override LocalisableString TooltipText =>
|
|
|
|
Current.Value == 0
|
|
|
|
? new TranslatableString("_", @"{0} ms", base.TooltipText)
|
|
|
|
: new TranslatableString("_", @"{0} ms {1}", base.TooltipText, getEarlyLateText(Current.Value));
|
2022-03-03 16:07:46 +08:00
|
|
|
|
|
|
|
private LocalisableString getEarlyLateText(double value)
|
|
|
|
{
|
|
|
|
Debug.Assert(value != 0);
|
|
|
|
|
|
|
|
return value > 0
|
2022-03-04 12:09:19 +08:00
|
|
|
? BeatmapOffsetControlStrings.HitObjectsAppearEarlier
|
|
|
|
: BeatmapOffsetControlStrings.HitObjectsAppearLater;
|
2022-03-03 16:07:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2022-03-01 15:14:57 +08:00
|
|
|
|
|
|
|
ReferenceScore.BindValueChanged(scoreChanged, true);
|
2022-03-01 14:40:19 +08:00
|
|
|
|
2022-03-03 16:42:50 +08:00
|
|
|
beatmapOffsetSubscription = realm.SubscribeToPropertyChanged(
|
2022-03-04 03:28:19 +08:00
|
|
|
r => r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings,
|
2022-03-03 16:42:50 +08:00
|
|
|
settings => settings.Offset,
|
2022-03-08 18:36:08 +08:00
|
|
|
val =>
|
|
|
|
{
|
|
|
|
// At the point we reach here, it's not guaranteed that all realm writes have taken place (there may be some in-flight).
|
|
|
|
// We are only aware of writes that originated from our own flow, so if we do see one that's active we can avoid handling the feedback value arriving.
|
|
|
|
if (realmWriteTask == null)
|
|
|
|
Current.Value = val;
|
|
|
|
|
|
|
|
// we can also mark any in-flight write that is managed locally as "seen" and start handling any incoming changes again.
|
|
|
|
realmWriteTask = null;
|
|
|
|
});
|
2022-03-01 18:13:40 +08:00
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
Current.BindValueChanged(currentChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void currentChanged(ValueChangedEvent<double> offset)
|
|
|
|
{
|
2022-03-01 17:28:53 +08:00
|
|
|
Scheduler.AddOnce(updateOffset);
|
|
|
|
|
|
|
|
void updateOffset()
|
2022-03-01 15:59:33 +08:00
|
|
|
{
|
2022-03-04 12:09:19 +08:00
|
|
|
// the last play graph is relative to the offset at the point of the last play, so we need to factor that out.
|
|
|
|
double adjustmentSinceLastPlay = lastPlayBeatmapOffset - Current.Value;
|
|
|
|
|
|
|
|
// Negative is applied here because the play graph is considering a hit offset, not track (as we currently use for clocks).
|
|
|
|
lastPlayGraph?.UpdateOffset(-adjustmentSinceLastPlay);
|
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
// ensure the previous write has completed. ignoring performance concerns, if we don't do this, the async writes could be out of sequence.
|
2022-03-02 13:34:24 +08:00
|
|
|
if (realmWriteTask?.IsCompleted == false)
|
2022-03-01 17:28:53 +08:00
|
|
|
{
|
|
|
|
Scheduler.AddOnce(updateOffset);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-01 15:59:33 +08:00
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
if (useAverageButton != null)
|
2022-03-04 12:09:19 +08:00
|
|
|
{
|
|
|
|
useAverageButton.Enabled.Value = !Precision.AlmostEquals(lastPlayAverage, adjustmentSinceLastPlay, Current.Precision / 2);
|
|
|
|
}
|
2022-03-01 18:13:40 +08:00
|
|
|
|
2022-03-02 13:34:24 +08:00
|
|
|
realmWriteTask = realm.WriteAsync(r =>
|
2022-03-01 17:28:53 +08:00
|
|
|
{
|
2022-03-01 18:20:18 +08:00
|
|
|
var settings = r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings;
|
|
|
|
|
|
|
|
if (settings == null) // only the case for tests.
|
|
|
|
return;
|
2022-03-01 17:28:53 +08:00
|
|
|
|
2022-03-08 18:36:23 +08:00
|
|
|
double val = Current.Value;
|
|
|
|
|
|
|
|
if (settings.Offset == val)
|
2022-03-01 18:13:40 +08:00
|
|
|
return;
|
|
|
|
|
2022-03-08 18:36:23 +08:00
|
|
|
settings.Offset = val;
|
2022-03-01 17:28:53 +08:00
|
|
|
});
|
|
|
|
}
|
2022-03-01 14:40:19 +08:00
|
|
|
}
|
2022-03-01 15:14:57 +08:00
|
|
|
|
|
|
|
private void scoreChanged(ValueChangedEvent<ScoreInfo> score)
|
|
|
|
{
|
2022-03-01 18:31:11 +08:00
|
|
|
referenceScoreContainer.Clear();
|
|
|
|
|
2022-03-01 18:41:51 +08:00
|
|
|
if (score.NewValue == null)
|
|
|
|
return;
|
|
|
|
|
2022-03-02 13:42:08 +08:00
|
|
|
if (score.NewValue.Mods.Any(m => !m.UserPlayable))
|
2022-03-01 18:41:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
var hitEvents = score.NewValue.HitEvents;
|
|
|
|
|
|
|
|
if (!(hitEvents.CalculateAverageHitError() is double average))
|
2022-03-01 18:31:11 +08:00
|
|
|
return;
|
2022-03-01 15:14:57 +08:00
|
|
|
|
|
|
|
referenceScoreContainer.Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
2022-03-01 18:33:30 +08:00
|
|
|
Text = BeatmapOffsetControlStrings.PreviousPlay
|
2022-03-01 15:14:57 +08:00
|
|
|
},
|
2022-03-01 18:31:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (hitEvents.Count < 10)
|
|
|
|
{
|
|
|
|
referenceScoreContainer.AddRange(new Drawable[]
|
|
|
|
{
|
|
|
|
new OsuTextFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Colour = colours.Red1,
|
2022-03-01 18:33:30 +08:00
|
|
|
Text = BeatmapOffsetControlStrings.PreviousPlayTooShortToUseForCalibration
|
2022-03-01 18:31:11 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPlayAverage = average;
|
2022-03-04 11:55:35 +08:00
|
|
|
lastPlayBeatmapOffset = Current.Value;
|
2022-03-01 18:31:11 +08:00
|
|
|
|
|
|
|
referenceScoreContainer.AddRange(new Drawable[]
|
|
|
|
{
|
2022-03-04 12:09:19 +08:00
|
|
|
lastPlayGraph = new HitEventTimingDistributionGraph(hitEvents)
|
2022-03-01 15:14:57 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 50,
|
|
|
|
},
|
2022-03-01 18:31:11 +08:00
|
|
|
new AverageHitError(hitEvents),
|
2022-03-01 15:14:57 +08:00
|
|
|
useAverageButton = new SettingsButton
|
|
|
|
{
|
2022-03-01 18:33:30 +08:00
|
|
|
Text = BeatmapOffsetControlStrings.CalibrateUsingLastPlay,
|
2022-03-04 11:55:35 +08:00
|
|
|
Action = () => Current.Value = lastPlayBeatmapOffset - lastPlayAverage
|
2022-03-01 15:14:57 +08:00
|
|
|
},
|
2022-03-01 18:31:11 +08:00
|
|
|
});
|
2022-03-01 15:14:57 +08:00
|
|
|
}
|
2022-03-01 18:13:40 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
beatmapOffsetSubscription?.Dispose();
|
|
|
|
}
|
2022-03-01 14:40:19 +08:00
|
|
|
}
|
|
|
|
}
|