1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 01:27:35 +08:00
osu-lazer/osu.Game/Screens/Play/PlayerSettings/BeatmapOffsetControl.cs

178 lines
5.7 KiB
C#
Raw Normal View History

// 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.
using System;
using System.ComponentModel;
using System.Threading.Tasks;
2022-03-01 15:59:33 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Utils;
2022-03-01 15:59:33 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Ranking.Statistics;
using osuTK;
namespace osu.Game.Screens.Play.PlayerSettings
{
public class BeatmapOffsetControl : CompositeDrawable
{
public Bindable<ScoreInfo> ReferenceScore { get; } = new Bindable<ScoreInfo>();
public BindableDouble Current { get; } = new BindableDouble
{
Default = 0,
Value = 0,
MinValue = -50,
MaxValue = 50,
Precision = 0.1,
};
private SettingsButton useAverageButton;
private double lastPlayAverage;
private readonly FillFlowContainer referenceScoreContainer;
public BeatmapOffsetControl()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(10),
Children = new Drawable[]
{
new PlayerSliderBar<double>
{
KeyboardStep = 5,
LabelText = "Beatmap offset",
Current = Current,
},
referenceScoreContainer = new FillFlowContainer
{
Spacing = new Vector2(10),
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
};
2022-03-01 15:59:33 +08:00
}
[Resolved]
private RealmAccess realm { get; set; }
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
private IDisposable beatmapOffsetSubscription;
2022-03-01 15:59:33 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
ReferenceScore.BindValueChanged(scoreChanged, true);
beatmapOffsetSubscription = realm.RegisterCustomSubscription(r =>
{
var userSettings = r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings;
if (userSettings == null) // only the case for tests.
return null;
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);
}
private Task realmWrite;
2022-03-01 15:59:33 +08:00
private void currentChanged(ValueChangedEvent<double> offset)
{
Scheduler.AddOnce(updateOffset);
void updateOffset()
2022-03-01 15:59:33 +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.
if (realmWrite?.IsCompleted == false)
{
Scheduler.AddOnce(updateOffset);
return;
}
2022-03-01 15:59:33 +08:00
if (useAverageButton != null)
useAverageButton.Enabled.Value = !Precision.AlmostEquals(lastPlayAverage, Current.Value, Current.Precision);
realmWrite = realm.WriteAsync(r =>
{
var settings = r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings;
if (settings == null) // only the case for tests.
return;
if (Precision.AlmostEquals(settings.Offset, Current.Value))
return;
settings.Offset = Current.Value;
});
}
}
private void scoreChanged(ValueChangedEvent<ScoreInfo> score)
{
if (!(score.NewValue?.HitEvents.CalculateAverageHitError() is double average))
{
referenceScoreContainer.Clear();
return;
}
lastPlayAverage = average;
referenceScoreContainer.Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Last play:"
},
new HitEventTimingDistributionGraph(score.NewValue.HitEvents)
{
RelativeSizeAxes = Axes.X,
Height = 50,
},
new AverageHitError(score.NewValue.HitEvents),
useAverageButton = new SettingsButton
{
Text = "Calibrate using last play",
Action = () => Current.Value = lastPlayAverage
},
};
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmapOffsetSubscription?.Dispose();
}
}
}