1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 17:22:54 +08:00
osu-lazer/osu.Game/Overlays/Mods/BeatmapAttributesDisplay.cs

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

189 lines
6.8 KiB
C#
Raw Normal View History

2023-08-29 04:16:33 +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.
2023-09-09 01:32:55 +08:00
using System.Collections.Generic;
using System.Linq;
2023-08-29 04:16:33 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2023-09-03 07:09:01 +08:00
using osu.Framework.Extensions.LocalisationExtensions;
2023-08-29 04:16:33 +08:00
using osu.Framework.Graphics;
2023-09-03 07:09:01 +08:00
using osu.Framework.Localisation;
2023-08-29 04:16:33 +08:00
using osu.Game.Beatmaps;
2023-09-03 07:09:01 +08:00
using osu.Game.Beatmaps.Drawables;
2023-08-29 04:16:33 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2023-09-09 01:32:55 +08:00
using osu.Game.Rulesets.Mods;
2023-08-29 04:16:33 +08:00
using osuTK;
2023-09-09 01:32:55 +08:00
using System.Threading;
using osu.Framework.Input.Events;
using osu.Game.Configuration;
2023-08-29 04:16:33 +08:00
namespace osu.Game.Overlays.Mods
{
/// <summary>
/// On the mod select overlay, this provides a local updating view of BPM, star rating and other
/// difficulty attributes so the user can have a better insight into what mods are changing.
/// </summary>
public partial class BeatmapAttributesDisplay : ModFooterInformationDisplay
2023-08-29 04:16:33 +08:00
{
2023-09-11 14:26:05 +08:00
private StarRatingDisplay starRatingDisplay = null!;
private BPMDisplay bpmDisplay = null!;
2023-09-03 07:09:01 +08:00
2023-09-11 14:26:05 +08:00
private VerticalAttributeDisplay circleSizeDisplay = null!;
private VerticalAttributeDisplay drainRateDisplay = null!;
private VerticalAttributeDisplay approachRateDisplay = null!;
private VerticalAttributeDisplay overallDifficultyDisplay = null!;
2023-08-29 04:16:33 +08:00
public Bindable<IBeatmapInfo?> BeatmapInfo { get; } = new Bindable<IBeatmapInfo?>();
2023-09-09 01:32:55 +08:00
[Resolved]
private Bindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
public BindableBool Collapsed { get; } = new BindableBool(true);
private ModSettingChangeTracker? modSettingChangeTracker;
2023-08-29 04:16:33 +08:00
[Resolved]
2023-09-09 01:32:55 +08:00
private BeatmapDifficultyCache difficultyCache { get; set; } = null!;
2023-08-29 04:16:33 +08:00
2023-09-11 14:26:05 +08:00
private CancellationTokenSource? cancellationSource;
2023-09-09 01:32:55 +08:00
private IBindable<StarDifficulty?> starDifficulty = null!;
private const float transition_duration = 250;
2023-09-11 14:26:05 +08:00
[BackgroundDependencyLoader]
private void load()
2023-09-03 07:09:01 +08:00
{
const float shear = ShearedOverlayContainer.SHEAR;
2023-08-29 04:16:33 +08:00
LeftContent.AddRange(new Drawable[]
2023-09-03 07:09:01 +08:00
{
starRatingDisplay = new StarRatingDisplay(default, animated: true)
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Shear = new Vector2(-shear, 0),
},
bpmDisplay = new BPMDisplay
2023-09-03 07:09:01 +08:00
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Shear = new Vector2(-shear, 0),
AutoSizeAxes = Axes.Y,
Width = 75,
2023-09-03 07:09:01 +08:00
}
});
RightContent.Alpha = 0;
RightContent.AddRange(new Drawable[]
{
circleSizeDisplay = new VerticalAttributeDisplay("CS") { Shear = new Vector2(-shear, 0), },
drainRateDisplay = new VerticalAttributeDisplay("HP") { Shear = new Vector2(-shear, 0), },
approachRateDisplay = new VerticalAttributeDisplay("AR") { Shear = new Vector2(-shear, 0), },
overallDifficultyDisplay = new VerticalAttributeDisplay("OD") { Shear = new Vector2(-shear, 0), },
});
2023-09-03 07:09:01 +08:00
}
2023-09-11 14:26:05 +08:00
2023-08-29 04:16:33 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2023-09-09 01:32:55 +08:00
mods.BindValueChanged(_ =>
{
modSettingChangeTracker?.Dispose();
modSettingChangeTracker = new ModSettingChangeTracker(mods.Value);
modSettingChangeTracker.SettingChanged += _ => updateValues();
updateValues();
2023-09-11 16:16:09 +08:00
}, true);
BeatmapInfo.BindValueChanged(_ => updateValues(), true);
Collapsed.BindValueChanged(_ =>
{
// Only start autosize animations on first collapse toggle. This avoids an ugly initial presentation.
startAnimating();
updateCollapsedState();
});
updateCollapsedState();
}
protected override bool OnHover(HoverEvent e)
{
startAnimating();
updateCollapsedState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateCollapsedState();
base.OnHoverLost(e);
}
protected override bool OnMouseDown(MouseDownEvent e) => true;
protected override bool OnClick(ClickEvent e) => true;
private void startAnimating()
{
Content.AutoSizeEasing = Easing.OutQuint;
Content.AutoSizeDuration = transition_duration;
}
private void updateCollapsedState()
{
RightContent.FadeTo(Collapsed.Value && !IsHovered ? 0 : 1, transition_duration, Easing.OutQuint);
2023-08-29 04:16:33 +08:00
}
2023-09-11 14:26:05 +08:00
private void updateValues() => Scheduler.AddOnce(() =>
2023-09-09 01:32:55 +08:00
{
if (BeatmapInfo.Value == null)
2023-09-11 14:26:05 +08:00
return;
cancellationSource?.Cancel();
starDifficulty = difficultyCache.GetBindableDifficulty(BeatmapInfo.Value, (cancellationSource = new CancellationTokenSource()).Token);
2023-09-09 01:32:55 +08:00
starDifficulty.BindValueChanged(s =>
{
starRatingDisplay.Current.Value = s.NewValue ?? default;
2023-08-29 04:16:33 +08:00
2023-09-09 01:32:55 +08:00
if (!starRatingDisplay.IsPresent)
starRatingDisplay.FinishTransforms(true);
});
double rate = 1;
foreach (var mod in mods.Value.OfType<IApplicableToRate>())
rate = mod.ApplyToRate(0, rate);
bpmDisplay.Current.Value = BeatmapInfo.Value.BPM * rate;
2023-09-03 07:09:01 +08:00
BeatmapDifficulty adjustedDifficulty = new BeatmapDifficulty(BeatmapInfo.Value.Difficulty);
2023-09-09 01:32:55 +08:00
foreach (var mod in mods.Value.OfType<IApplicableToDifficulty>())
mod.ApplyToDifficulty(adjustedDifficulty);
2023-09-03 07:09:01 +08:00
2023-09-09 01:32:55 +08:00
circleSizeDisplay.Current.Value = adjustedDifficulty.CircleSize;
drainRateDisplay.Current.Value = adjustedDifficulty.DrainRate;
approachRateDisplay.Current.Value = adjustedDifficulty.ApproachRate;
overallDifficultyDisplay.Current.Value = adjustedDifficulty.OverallDifficulty;
});
2023-09-03 07:09:01 +08:00
private partial class BPMDisplay : RollingCounter<double>
{
protected override double RollingDuration => 500;
protected override LocalisableString FormatCount(double count) => count.ToLocalisableString("0 BPM");
protected override OsuSpriteText CreateSpriteText() => new OsuSpriteText
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2023-09-11 15:37:25 +08:00
Font = OsuFont.Default.With(size: 20, weight: FontWeight.SemiBold),
UseFullGlyphHeight = false,
2023-09-03 07:09:01 +08:00
};
2023-08-29 04:16:33 +08:00
}
}
}