1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 23:37:26 +08:00
osu-lazer/osu.Game/Screens/Select/Details/AdvancedStats.cs

278 lines
10 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
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
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;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Localisation;
using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Rulesets;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select.Details
{
public class AdvancedStats : Container
{
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; }
protected readonly StatisticRow FirstValue, HpDrain, Accuracy, ApproachRate;
private readonly StatisticRow starDifficulty;
2018-04-13 17:19:50 +08:00
private BeatmapInfo beatmap;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public BeatmapInfo Beatmap
{
get => beatmap;
2018-04-13 17:19:50 +08:00
set
{
if (value == beatmap) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
beatmap = value;
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 = "HP Drain" },
Accuracy = new StatisticRow { Title = "Accuracy" },
ApproachRate = new StatisticRow { Title = "Approach Rate" },
2018-04-13 17:19:50 +08:00
starDifficulty = new StatisticRow(10, true) { Title = "Star Difficulty" },
},
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
2018-04-13 17:19:50 +08:00
{
starDifficulty.AccentColour = colours.Yellow;
}
protected override void LoadComplete()
{
base.LoadComplete();
ruleset.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);
modSettingChangeTracker.SettingChanged += m =>
{
if (!(m is IApplicableToDifficulty))
return;
debouncedStatisticsUpdate?.Cancel();
debouncedStatisticsUpdate = Scheduler.AddDelayed(updateStatistics, 100);
};
updateStatistics();
}
private void updateStatistics()
{
BeatmapDifficulty baseDifficulty = Beatmap?.BaseDifficulty;
BeatmapDifficulty adjustedDifficulty = null;
2019-12-18 16:00:35 +08:00
if (baseDifficulty != null && mods.Value.Any(m => m is IApplicableToDifficulty))
{
adjustedDifficulty = baseDifficulty.Clone();
foreach (var mod in mods.Value.OfType<IApplicableToDifficulty>())
2019-12-18 16:00:35 +08:00
mod.ApplyToDifficulty(adjustedDifficulty);
}
switch (Beatmap?.Ruleset?.ID ?? 0)
{
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
FirstValue.Title = "Key Count";
FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, null);
break;
default:
FirstValue.Title = "Circle Size";
FirstValue.Value = (baseDifficulty?.CircleSize ?? 0, adjustedDifficulty?.CircleSize);
break;
}
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;
private void updateStarDifficulty()
{
starDifficultyCancellationSource?.Cancel();
if (Beatmap == null)
return;
2020-07-24 15:11:28 +08:00
starDifficultyCancellationSource = new CancellationTokenSource();
var normalStarDifficulty = difficultyCache.GetDifficultyAsync(Beatmap, ruleset.Value, null, starDifficultyCancellationSource.Token);
var moddedStarDifficulty = difficultyCache.GetDifficultyAsync(Beatmap, ruleset.Value, mods.Value, starDifficultyCancellationSource.Token);
2020-07-21 22:13:04 +08:00
Task.WhenAll(normalStarDifficulty, moddedStarDifficulty).ContinueWith(_ => Schedule(() =>
{
starDifficulty.Value = ((float)normalStarDifficulty.Result.Stars, (float)moddedStarDifficulty.Result.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 class StatisticRow : Container, IHasAccentColour
2018-04-13 17:19:50 +08:00
{
private const float value_width = 25;
private const float name_width = 70;
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
2018-04-13 17:19:50 +08:00
{
get => name.Text;
set => name.Text = value;
2018-04-13 17:19:50 +08:00
}
private (float baseValue, float? adjustedValue) value;
public (float baseValue, float? adjustedValue) Value
2018-04-13 17:19:50 +08:00
{
get => value;
2018-04-13 17:19:50 +08:00
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;
}
}
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => bar.AccentColour;
set => bar.AccentColour = value;
2018-04-13 17:19:50 +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 },
2018-04-13 17:19:50 +08:00
Child = name = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 12)
2018-04-13 17:19:50 +08:00
},
},
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 },
},
2018-04-13 17:19:50 +08:00
new Container
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Width = value_width,
RelativeSizeAxes = Axes.Y,
Child = valueText = new OsuSpriteText
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 12)
2018-04-13 17:19:50 +08:00
},
},
};
}
}
}
}