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-01 18:13:40 +08:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
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-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;
|
|
|
|
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;
|
2022-03-01 18:33:30 +08:00
|
|
|
using osu.Game.Localisation;
|
2022-03-01 18:41:51 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2022-03-01 14:40:19 +08:00
|
|
|
|
|
|
|
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 SettingsButton useAverageButton;
|
|
|
|
|
|
|
|
private double lastPlayAverage;
|
|
|
|
|
|
|
|
private readonly FillFlowContainer referenceScoreContainer;
|
|
|
|
|
|
|
|
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[]
|
|
|
|
{
|
|
|
|
new PlayerSliderBar<double>
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RealmAccess realm { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IBindable<WorkingBeatmap> beatmap { get; set; }
|
|
|
|
|
2022-03-01 18:31:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
private IDisposable beatmapOffsetSubscription;
|
|
|
|
|
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-01 18:13:40 +08:00
|
|
|
beatmapOffsetSubscription = realm.RegisterCustomSubscription(r =>
|
|
|
|
{
|
2022-03-01 18:20:18 +08:00
|
|
|
var userSettings = r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings;
|
|
|
|
|
|
|
|
if (userSettings == null) // only the case for tests.
|
|
|
|
return null;
|
2022-03-01 18:13:40 +08:00
|
|
|
|
|
|
|
Current.Value = userSettings.Offset;
|
|
|
|
userSettings.PropertyChanged += onUserSettingsOnPropertyChanged;
|
|
|
|
|
|
|
|
return new InvokeOnDisposal(() => userSettings.PropertyChanged -= onUserSettingsOnPropertyChanged);
|
|
|
|
|
|
|
|
void onUserSettingsOnPropertyChanged(object sender, PropertyChangedEventArgs args)
|
|
|
|
{
|
|
|
|
if (args.PropertyName == nameof(BeatmapUserSettings.Offset))
|
|
|
|
Current.Value = userSettings.Offset;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
Current.BindValueChanged(currentChanged);
|
|
|
|
}
|
|
|
|
|
2022-03-01 17:28:53 +08:00
|
|
|
private Task realmWrite;
|
|
|
|
|
2022-03-01 15:59:33 +08:00
|
|
|
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-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-01 17:28:53 +08:00
|
|
|
if (realmWrite?.IsCompleted == false)
|
|
|
|
{
|
|
|
|
Scheduler.AddOnce(updateOffset);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-01 15:59:33 +08:00
|
|
|
|
2022-03-01 18:13:40 +08:00
|
|
|
if (useAverageButton != null)
|
|
|
|
useAverageButton.Enabled.Value = !Precision.AlmostEquals(lastPlayAverage, Current.Value, Current.Precision);
|
|
|
|
|
2022-03-01 17:28:53 +08:00
|
|
|
realmWrite = realm.WriteAsync(r =>
|
|
|
|
{
|
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-01 18:13:40 +08:00
|
|
|
if (Precision.AlmostEquals(settings.Offset, Current.Value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
settings.Offset = Current.Value;
|
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;
|
|
|
|
|
|
|
|
if (score.NewValue.Mods.Any(m => m is ModAutoplay))
|
|
|
|
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;
|
|
|
|
|
|
|
|
referenceScoreContainer.AddRange(new Drawable[]
|
|
|
|
{
|
|
|
|
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-01 15:14:57 +08:00
|
|
|
Action = () => Current.Value = lastPlayAverage
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
}
|