1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 06:07:28 +08:00
osu-lazer/osu.Game/Screens/Select/Details/AdvancedStats.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

331 lines
13 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2023-11-05 03:55:46 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Beatmaps;
using osu.Framework.Bindables;
using System.Collections.Generic;
using osu.Game.Rulesets.Mods;
2023-11-05 03:57:42 +08:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Extensions;
using osu.Framework.Localisation;
using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Rulesets;
2023-11-24 05:30:18 +08:00
using osu.Game.Overlays.Mods;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select.Details
{
2023-11-24 05:30:18 +08:00
public partial class AdvancedStats : Container, IHasCustomTooltip
{
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; }
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved]
private OsuGameBase game { get; set; }
private IBindable<RulesetInfo> gameRuleset;
protected readonly StatisticRow FirstValue, HpDrain, Accuracy, ApproachRate;
private readonly StatisticRow starDifficulty;
2018-04-13 17:19:50 +08:00
2023-11-24 05:30:18 +08:00
private AdjustedAttributesTooltip rateAdjustTooltip;
public ITooltip GetCustomTooltip() => rateAdjustTooltip;
public object TooltipContent => this;
2023-11-05 03:55:46 +08:00
private IBeatmapInfo beatmapInfo;
2019-02-28 12:31:40 +08:00
public IBeatmapInfo BeatmapInfo
{
get => beatmapInfo;
set
{
if (value == beatmapInfo) return;
2019-02-28 12:31:40 +08:00
beatmapInfo = value;
2018-04-13 17:19:50 +08:00
updateStatistics();
}
}
2018-04-13 17:19:50 +08:00
public AdvancedStats()
{
Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new[]
{
2020-05-05 09:31:11 +08:00
FirstValue = new StatisticRow(), // circle size/key amount
HpDrain = new StatisticRow { Title = BeatmapsetsStrings.ShowStatsDrain },
Accuracy = new StatisticRow { Title = BeatmapsetsStrings.ShowStatsAccuracy },
ApproachRate = new StatisticRow { Title = BeatmapsetsStrings.ShowStatsAr },
starDifficulty = new StatisticRow(10, true) { Title = BeatmapsetsStrings.ShowStatsStars },
},
};
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
starDifficulty.AccentColour = colours.Yellow;
2023-11-24 05:30:18 +08:00
rateAdjustTooltip = new AdjustedAttributesTooltip();
}
protected override void LoadComplete()
{
base.LoadComplete();
// the cached ruleset bindable might be a decoupled bindable provided by SongSelect,
// which we can't rely on in combination with the game-wide selected mods list,
// since mods could be updated to the new ruleset instances while the decoupled bindable is held behind,
// therefore resulting in performing difficulty calculation with invalid states.
gameRuleset = game.Ruleset.GetBoundCopy();
gameRuleset.BindValueChanged(_ => updateStatistics());
mods.BindValueChanged(modsChanged, true);
}
private ModSettingChangeTracker modSettingChangeTracker;
private ScheduledDelegate debouncedStatisticsUpdate;
private void modsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{
modSettingChangeTracker?.Dispose();
modSettingChangeTracker = new ModSettingChangeTracker(mods.NewValue);
2022-06-24 20:25:23 +08:00
modSettingChangeTracker.SettingChanged += _ =>
{
debouncedStatisticsUpdate?.Cancel();
debouncedStatisticsUpdate = Scheduler.AddDelayed(updateStatistics, 100);
};
updateStatistics();
}
private void updateStatistics()
{
IBeatmapDifficultyInfo baseDifficulty = BeatmapInfo?.Difficulty;
2023-11-24 05:30:18 +08:00
BeatmapDifficulty adjustedDifficulty = null;
2023-12-13 15:42:20 +08:00
IRulesetInfo ruleset = gameRuleset?.Value ?? beatmapInfo.Ruleset;
if (baseDifficulty != null)
{
BeatmapDifficulty originalDifficulty = new BeatmapDifficulty(baseDifficulty);
2023-11-04 23:25:09 +08:00
foreach (var mod in mods.Value.OfType<IApplicableToDifficulty>())
mod.ApplyToDifficulty(originalDifficulty);
2023-11-04 23:25:09 +08:00
2023-11-24 05:30:18 +08:00
adjustedDifficulty = originalDifficulty;
if (gameRuleset != null)
{
double rate = 1;
foreach (var mod in mods.Value.OfType<IApplicableToRate>())
rate = mod.ApplyToRate(0, rate);
2023-11-04 23:25:09 +08:00
2023-12-13 15:42:20 +08:00
adjustedDifficulty = ruleset.CreateInstance().GetRateAdjustedDisplayDifficulty(originalDifficulty, rate);
2023-11-24 05:30:18 +08:00
2023-12-13 16:33:39 +08:00
rateAdjustTooltip.UpdateAttributes(originalDifficulty, adjustedDifficulty);
}
}
2023-12-13 12:32:27 +08:00
switch (ruleset.OnlineID)
{
case 3:
// Account for mania differences locally for now.
// Eventually this should be handled in a more modular way, allowing rulesets to return arbitrary difficulty attributes.
2023-12-13 12:32:27 +08:00
ILegacyRuleset legacyRuleset = (ILegacyRuleset)ruleset.CreateInstance();
// For the time being, the key count is static no matter what, because:
// a) The method doesn't have knowledge of the active keymods. Doing so may require considerations for filtering.
// b) Using the difficulty adjustment mod to adjust OD doesn't have an effect on conversion.
int keyCount = baseDifficulty == null ? 0 : legacyRuleset.GetKeyCount(BeatmapInfo);
FirstValue.Title = BeatmapsetsStrings.ShowStatsCsMania;
FirstValue.Value = (keyCount, keyCount);
break;
default:
FirstValue.Title = BeatmapsetsStrings.ShowStatsCs;
2023-11-04 23:25:09 +08:00
FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, adjustedDifficulty?.CircleSize);
break;
}
2023-11-04 23:25:09 +08:00
HpDrain.Value = (baseDifficulty?.DrainRate ?? 0, adjustedDifficulty?.DrainRate);
Accuracy.Value = (baseDifficulty?.OverallDifficulty ?? 0, adjustedDifficulty?.OverallDifficulty);
ApproachRate.Value = (baseDifficulty?.ApproachRate ?? 0, adjustedDifficulty?.ApproachRate);
updateStarDifficulty();
}
private CancellationTokenSource starDifficultyCancellationSource;
/// <summary>
/// Updates the displayed star difficulty statistics with the values provided by the currently-selected beatmap, ruleset, and selected mods.
/// </summary>
/// <remarks>
/// This is scheduled to avoid scenarios wherein a ruleset changes first before selected mods do,
/// potentially resulting in failure during difficulty calculation due to incomplete bindable state updates.
/// </remarks>
private void updateStarDifficulty() => Scheduler.AddOnce(() =>
{
starDifficultyCancellationSource?.Cancel();
if (BeatmapInfo == null)
return;
2020-07-24 15:11:28 +08:00
starDifficultyCancellationSource = new CancellationTokenSource();
var normalStarDifficultyTask = difficultyCache.GetDifficultyAsync(BeatmapInfo, gameRuleset.Value, null, starDifficultyCancellationSource.Token);
var moddedStarDifficultyTask = difficultyCache.GetDifficultyAsync(BeatmapInfo, gameRuleset.Value, mods.Value, starDifficultyCancellationSource.Token);
2020-07-21 22:13:04 +08:00
Task.WhenAll(normalStarDifficultyTask, moddedStarDifficultyTask).ContinueWith(_ => Schedule(() =>
{
2022-01-06 21:54:43 +08:00
var normalDifficulty = normalStarDifficultyTask.GetResultSafely();
2022-01-07 16:33:38 +08:00
var moddedDifficulty = moddedStarDifficultyTask.GetResultSafely();
2022-01-07 16:33:38 +08:00
if (normalDifficulty == null || moddedDifficulty == null)
return;
2023-11-04 23:25:09 +08:00
starDifficulty.Value = ((float)normalDifficulty.Value.Stars, (float)moddedDifficulty.Value.Stars);
}), starDifficultyCancellationSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);
});
2020-07-21 22:13:04 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
modSettingChangeTracker?.Dispose();
2020-07-21 22:13:04 +08:00
starDifficultyCancellationSource?.Cancel();
}
2018-04-13 17:19:50 +08:00
public partial class StatisticRow : Container, IHasAccentColour
{
private const float value_width = 25;
private const float name_width = 70;
2018-04-13 17:19:50 +08:00
private readonly float maxValue;
private readonly bool forceDecimalPlaces;
private readonly OsuSpriteText name, valueText;
private readonly Bar bar;
public readonly Bar ModBar;
[Resolved]
private OsuColour colours { get; set; }
2018-04-13 17:19:50 +08:00
public LocalisableString Title
{
get => name.Text;
set => name.Text = value;
}
2018-04-13 17:19:50 +08:00
2023-11-04 23:25:09 +08:00
private (float baseValue, float? adjustedValue)? value;
2023-11-04 23:25:09 +08:00
public (float baseValue, float? adjustedValue) Value
{
2023-11-04 23:25:09 +08:00
get => value ?? (0, null);
set
{
if (value == this.value)
return;
2018-04-13 17:19:50 +08:00
this.value = value;
bar.Length = value.baseValue / maxValue;
valueText.Text = (value.adjustedValue ?? value.baseValue).ToString(forceDecimalPlaces ? "0.00" : "0.##");
ModBar.Length = (value.adjustedValue ?? 0) / maxValue;
if (Precision.AlmostEquals(value.baseValue, value.adjustedValue ?? value.baseValue, 0.05f))
ModBar.AccentColour = valueText.Colour = Color4.White;
else if (value.adjustedValue > value.baseValue)
ModBar.AccentColour = valueText.Colour = colours.Red;
else if (value.adjustedValue < value.baseValue)
ModBar.AccentColour = valueText.Colour = colours.BlueDark;
}
}
public Color4 AccentColour
{
get => bar.AccentColour;
set => bar.AccentColour = value;
}
2018-04-13 17:19:50 +08:00
2017-09-08 02:21:18 +08:00
public StatisticRow(float maxValue = 10, bool forceDecimalPlaces = false)
{
this.maxValue = maxValue;
this.forceDecimalPlaces = forceDecimalPlaces;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Vertical = 2.5f };
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new Container
{
Width = name_width,
AutoSizeAxes = Axes.Y,
// osu-web uses 1.25 line-height, which at 12px font size makes the element 14px tall - this compentates that difference
Padding = new MarginPadding { Vertical = 1 },
2017-09-08 02:38:23 +08:00
Child = name = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 12)
},
},
bar = new Bar
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
Height = 5,
BackgroundColour = Color4.White.Opacity(0.5f),
Padding = new MarginPadding { Left = name_width + 10, Right = value_width + 10 },
},
ModBar = new Bar
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
Alpha = 0.5f,
Height = 5,
Padding = new MarginPadding { Left = name_width + 10, Right = value_width + 10 },
},
new Container
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Width = value_width,
RelativeSizeAxes = Axes.Y,
Child = valueText = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12)
},
},
};
}
}
}
}