osu/osu.Game/Screens/Select/BeatmapInfoWedge.cs

520 lines
20 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 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
2018-04-13 09:19:50 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Cursor;
2019-04-02 05:51:28 +00:00
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Sprites;
2018-04-13 09:19:50 +00:00
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Rulesets;
2019-04-08 09:32:05 +00:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
2021-04-19 14:53:44 +00:00
using osu.Game.Graphics.Containers;
using osu.Game.Resources.Localisation.Web;
2024-02-22 09:27:37 +00:00
using osu.Game.Utils;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Screens.Select
{
2022-11-24 05:32:20 +00:00
public partial class BeatmapInfoWedge : VisibilityContainer
2018-04-13 09:19:50 +00:00
{
2021-08-13 13:29:22 +00:00
public const float BORDER_THICKNESS = 2.5f;
private const float shear_width = 36.75f;
private const float transition_duration = 250;
private static readonly Vector2 wedged_container_shear = new Vector2(shear_width / SongSelect.WEDGE_HEIGHT, 0);
2018-04-13 09:19:50 +00:00
2021-02-25 13:01:53 +00:00
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
protected Container DisplayedContent { get; private set; }
2021-05-05 17:14:04 +00:00
protected WedgeInfoText Info { get; private set; }
2018-04-13 09:19:50 +00:00
public BeatmapInfoWedge()
{
Shear = wedged_container_shear;
Masking = true;
BorderColour = new Color4(221, 255, 255, 255);
2021-08-13 13:29:22 +00:00
BorderThickness = BORDER_THICKNESS;
2018-04-13 09:19:50 +00:00
Alpha = 0;
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Glow,
Colour = new Color4(130, 204, 255, 150),
2023-12-19 10:18:36 +00:00
Radius = 15,
2018-04-13 09:19:50 +00:00
Roundness = 15,
};
}
2021-02-25 13:09:41 +00:00
[BackgroundDependencyLoader]
private void load()
{
2021-02-25 13:09:41 +00:00
ruleset.BindValueChanged(_ => updateDisplay());
}
2021-08-19 10:19:46 +00:00
private const double animation_duration = 800;
2018-04-13 09:19:50 +00:00
protected override void PopIn()
{
2021-08-19 10:19:46 +00:00
this.MoveToX(0, animation_duration, Easing.OutQuint);
this.FadeIn(transition_duration);
2018-04-13 09:19:50 +00:00
}
protected override void PopOut()
{
2021-08-19 10:19:46 +00:00
this.MoveToX(-100, animation_duration, Easing.In);
this.FadeOut(transition_duration * 2, Easing.In);
2018-04-13 09:19:50 +00:00
}
private WorkingBeatmap beatmap;
public WorkingBeatmap Beatmap
{
get => beatmap;
set
{
if (beatmap == value) return;
beatmap = value;
updateDisplay();
}
}
2021-05-05 17:14:04 +00:00
public override bool IsPresent => base.IsPresent || DisplayedContent == null; // Visibility is updated in the LoadComponentAsync callback
2021-05-05 16:50:49 +00:00
private Container loadingInfo;
private void updateDisplay()
2018-04-13 09:19:50 +00:00
{
Scheduler.AddOnce(perform);
void perform()
{
void removeOldInfo()
{
State.Value = beatmap == null ? Visibility.Hidden : Visibility.Visible;
DisplayedContent?.FadeOut(transition_duration);
2021-05-05 17:14:04 +00:00
DisplayedContent?.Expire();
DisplayedContent = null;
}
if (beatmap == null)
{
removeOldInfo();
return;
}
2021-05-05 16:50:49 +00:00
LoadComponentAsync(loadingInfo = new Container
{
2021-05-05 16:50:49 +00:00
RelativeSizeAxes = Axes.Both,
Shear = -Shear,
2021-05-05 17:14:04 +00:00
Depth = DisplayedContent?.Depth + 1 ?? 0,
2021-05-05 16:50:49 +00:00
Children = new Drawable[]
{
new BeatmapInfoWedgeBackground(beatmap),
Info = new WedgeInfoText(beatmap, ruleset.Value),
2021-05-05 16:50:49 +00:00
}
}, loaded =>
{
// ensure we are the most recent loaded wedge.
if (loaded != loadingInfo) return;
removeOldInfo();
2021-05-05 17:14:04 +00:00
Add(DisplayedContent = loaded);
});
}
2018-04-13 09:19:50 +00:00
}
2022-11-24 05:32:20 +00:00
public partial class WedgeInfoText : Container
2018-04-13 09:19:50 +00:00
{
public OsuSpriteText VersionLabel { get; private set; }
public OsuSpriteText TitleLabel { get; private set; }
public OsuSpriteText ArtistLabel { get; private set; }
public FillFlowContainer MapperContainer { get; private set; }
private Container difficultyColourBar;
private StarRatingDisplay starRatingDisplay;
private ILocalisedBindableString titleBinding;
private ILocalisedBindableString artistBinding;
private FillFlowContainer infoLabelContainer;
private Container bpmLabelContainer;
private Container lengthLabelContainer;
2018-04-13 09:19:50 +00:00
private readonly WorkingBeatmap working;
private readonly RulesetInfo ruleset;
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private ModSettingChangeTracker settingChangeTracker;
public WedgeInfoText(WorkingBeatmap working, RulesetInfo userRuleset)
2018-04-13 09:19:50 +00:00
{
this.working = working;
ruleset = userRuleset ?? working.BeatmapInfo.Ruleset;
2018-04-13 09:19:50 +00:00
}
private CancellationTokenSource cancellationSource;
private IBindable<StarDifficulty?> starDifficulty;
2018-05-11 05:13:52 +00:00
[BackgroundDependencyLoader]
private void load(LocalisationManager localisation)
2018-04-13 09:19:50 +00:00
{
var beatmapInfo = working.BeatmapInfo;
var metadata = beatmapInfo.Metadata;
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both;
titleBinding = localisation.GetLocalisedBindableString(new RomanisableString(metadata.TitleUnicode, metadata.Title));
artistBinding = localisation.GetLocalisedBindableString(new RomanisableString(metadata.ArtistUnicode, metadata.Artist));
2018-04-13 09:19:50 +00:00
2021-08-19 10:19:46 +00:00
const float top_height = 0.7f;
2018-04-13 09:19:50 +00:00
Children = new Drawable[]
{
difficultyColourBar = new Container
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Y,
Width = 20f,
Children = new[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
2021-08-19 10:19:46 +00:00
Width = top_height,
},
new Box
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
Alpha = 0.5f,
2021-08-19 10:19:46 +00:00
X = top_height,
Width = 1 - top_height,
}
}
2018-04-13 09:19:50 +00:00
},
new FillFlowContainer
{
Name = "Topleft-aligned metadata",
2018-04-13 09:19:50 +00:00
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Direction = FillDirection.Vertical,
2019-09-24 23:28:40 +00:00
Padding = new MarginPadding { Top = 10, Left = 25, Right = shear_width * 2.5f },
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2018-04-13 09:19:50 +00:00
Children = new Drawable[]
{
2023-06-09 00:11:17 +00:00
VersionLabel = new TruncatingSpriteText
2018-04-13 09:19:50 +00:00
{
Text = beatmapInfo.DifficultyName,
2019-02-20 07:52:36 +00:00
Font = OsuFont.GetFont(size: 24, italics: true),
RelativeSizeAxes = Axes.X,
2018-04-13 09:19:50 +00:00
},
}
},
new FillFlowContainer
{
Name = "Topright-aligned metadata",
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Direction = FillDirection.Vertical,
2019-09-24 23:28:40 +00:00
Padding = new MarginPadding { Top = 14, Right = shear_width / 2 },
AutoSizeAxes = Axes.Both,
2020-08-31 17:31:47 +00:00
Shear = wedged_container_shear,
Spacing = new Vector2(0f, 5f),
Children = new Drawable[]
{
starRatingDisplay = new StarRatingDisplay(default, animated: true)
2020-08-31 06:03:41 +00:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Shear = -wedged_container_shear,
Alpha = 0f,
},
new BeatmapSetOnlineStatusPill
{
AutoSizeAxes = Axes.Both,
2020-08-31 17:23:19 +00:00
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2020-08-31 17:31:47 +00:00
Shear = -wedged_container_shear,
TextSize = 11,
TextPadding = new MarginPadding { Horizontal = 8, Vertical = 2 },
Status = beatmapInfo.Status,
Alpha = string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? 0 : 1
}
}
},
new FillFlowContainer
2018-04-13 09:19:50 +00:00
{
Name = "Centre-aligned metadata",
Anchor = Anchor.CentreLeft,
Origin = Anchor.TopLeft,
2019-09-24 23:28:40 +00:00
Y = -7,
2018-04-13 09:19:50 +00:00
Direction = FillDirection.Vertical,
2019-09-24 23:28:40 +00:00
Padding = new MarginPadding { Left = 25, Right = shear_width },
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2018-04-13 09:19:50 +00:00
Children = new Drawable[]
{
2023-06-09 00:11:17 +00:00
TitleLabel = new TruncatingSpriteText
2018-04-13 09:19:50 +00:00
{
2022-03-30 08:59:45 +00:00
Current = { BindTarget = titleBinding },
2019-02-20 07:52:36 +00:00
Font = OsuFont.GetFont(size: 28, italics: true),
RelativeSizeAxes = Axes.X,
2018-04-13 09:19:50 +00:00
},
2023-06-09 00:11:17 +00:00
ArtistLabel = new TruncatingSpriteText
2018-04-13 09:19:50 +00:00
{
2022-03-30 08:59:45 +00:00
Current = { BindTarget = artistBinding },
2019-02-20 07:52:36 +00:00
Font = OsuFont.GetFont(size: 17, italics: true),
RelativeSizeAxes = Axes.X,
2018-04-13 09:19:50 +00:00
},
MapperContainer = new FillFlowContainer
{
Margin = new MarginPadding { Top = 10 },
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
2021-04-19 17:24:46 +00:00
Child = getMapper(metadata),
2018-04-13 09:19:50 +00:00
},
infoLabelContainer = new FillFlowContainer
2018-04-13 09:19:50 +00:00
{
Margin = new MarginPadding { Top = 8 },
2018-04-13 09:19:50 +00:00
Spacing = new Vector2(20, 0),
AutoSizeAxes = Axes.Both,
}
}
}
};
addInfoLabels();
}
protected override void LoadComplete()
{
base.LoadComplete();
starRatingDisplay.DisplayedStars.BindValueChanged(s =>
{
difficultyColourBar.Colour = colours.ForStarDifficulty(s.NewValue);
}, true);
starDifficulty = difficultyCache.GetBindableDifficulty(working.BeatmapInfo, (cancellationSource = new CancellationTokenSource()).Token);
starDifficulty.BindValueChanged(s =>
{
starRatingDisplay.Current.Value = s.NewValue ?? default;
// Don't roll the counter on initial display (but still allow it to roll on applying mods etc.)
if (!starRatingDisplay.IsPresent)
starRatingDisplay.FinishTransforms(true);
starRatingDisplay.FadeIn(transition_duration);
});
mods.BindValueChanged(m =>
{
settingChangeTracker?.Dispose();
refreshBPMAndLengthLabel();
settingChangeTracker = new ModSettingChangeTracker(m.NewValue);
settingChangeTracker.SettingChanged += _ => refreshBPMAndLengthLabel();
}, true);
}
private void addInfoLabels()
2018-04-13 09:19:50 +00:00
{
2023-06-24 14:07:01 +00:00
if (working.Beatmap?.HitObjects.Any() != true)
return;
2018-04-13 09:19:50 +00:00
try
2018-04-13 09:19:50 +00:00
{
IBeatmap playableBeatmap;
2018-04-13 09:19:50 +00:00
try
{
// Try to get the beatmap with the user's ruleset
playableBeatmap = working.GetPlayableBeatmap(ruleset, Array.Empty<Mod>());
}
catch (BeatmapInvalidForRulesetException)
{
// Can't be converted to the user's ruleset, so use the beatmap's own ruleset
playableBeatmap = working.GetPlayableBeatmap(working.BeatmapInfo.Ruleset, Array.Empty<Mod>());
}
infoLabelContainer.Children = new Drawable[]
{
lengthLabelContainer = new Container
{
AutoSizeAxes = Axes.Both,
},
bpmLabelContainer = new Container
{
AutoSizeAxes = Axes.Both,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(20, 0),
Children = playableBeatmap.GetStatistics().Select(s => new InfoLabel(s)).ToArray()
}
};
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap successfully!");
2018-04-13 09:19:50 +00:00
}
}
private void refreshBPMAndLengthLabel()
2018-04-13 09:19:50 +00:00
{
var beatmap = working.Beatmap;
if (beatmap == null || bpmLabelContainer == null)
return;
// this doesn't consider mods which apply variable rates, yet.
2024-06-06 15:59:15 +00:00
double rate = ModUtils.CalculateRateWithMods(mods.Value);
2024-02-22 09:27:37 +00:00
int bpmMax = FormatUtils.RoundBPM(beatmap.ControlPointInfo.BPMMaximum, rate);
int bpmMin = FormatUtils.RoundBPM(beatmap.ControlPointInfo.BPMMinimum, rate);
int mostCommonBPM = FormatUtils.RoundBPM(60000 / beatmap.GetMostCommonBeatLength(), rate);
2018-04-13 09:19:50 +00:00
string labelText = bpmMin == bpmMax
? $"{bpmMin}"
: $"{bpmMin}-{bpmMax} (mostly {mostCommonBPM})";
2018-04-13 09:19:50 +00:00
bpmLabelContainer.Child = new InfoLabel(new BeatmapStatistic
{
Name = BeatmapsetsStrings.ShowStatsBpm,
CreateIcon = () => new BeatmapStatisticIcon(BeatmapStatisticsIconType.Bpm),
Content = labelText
});
double drainLength = Math.Round(beatmap.CalculateDrainLength() / rate);
double hitLength = Math.Round(beatmap.BeatmapInfo.Length / rate);
lengthLabelContainer.Child = new InfoLabel(new BeatmapStatistic
{
Name = BeatmapsetsStrings.ShowStatsTotalLength(drainLength.ToFormattedDuration()),
CreateIcon = () => new BeatmapStatisticIcon(BeatmapStatisticsIconType.Length),
Content = hitLength.ToFormattedDuration().ToString(),
});
2018-04-13 09:19:50 +00:00
}
2021-04-19 17:24:46 +00:00
private Drawable getMapper(BeatmapMetadata metadata)
2018-04-13 09:19:50 +00:00
{
if (string.IsNullOrEmpty(metadata.Author.Username))
2021-04-19 17:24:46 +00:00
return Empty();
2018-04-13 09:19:50 +00:00
2021-04-19 14:53:44 +00:00
return new LinkFlowContainer(s =>
2018-04-13 09:19:50 +00:00
{
2021-04-19 14:53:44 +00:00
s.Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 15);
}).With(d =>
{
d.AutoSizeAxes = Axes.Both;
d.AddText("mapped by ");
d.AddUserLink(metadata.Author);
});
2018-04-13 09:19:50 +00:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
settingChangeTracker?.Dispose();
cancellationSource?.Cancel();
}
2022-11-24 05:32:20 +00:00
public partial class InfoLabel : Container, IHasTooltip
2018-04-13 09:19:50 +00:00
{
public LocalisableString TooltipText { get; }
2018-04-13 09:19:50 +00:00
2021-08-22 17:01:26 +00:00
internal BeatmapStatistic Statistic { get; }
2018-04-13 09:19:50 +00:00
public InfoLabel(BeatmapStatistic statistic)
{
2021-08-22 17:01:26 +00:00
Statistic = statistic;
2018-04-13 09:19:50 +00:00
TooltipText = statistic.Name;
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
new Container
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Size = new Vector2(20),
Children = new[]
{
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"441288"),
2019-04-02 10:55:24 +00:00
Icon = FontAwesome.Solid.Square,
2018-04-13 09:19:50 +00:00
Rotation = 45,
},
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"f7dd55"),
Icon = FontAwesome.Regular.Circle,
Size = new Vector2(0.7f)
2018-04-13 09:19:50 +00:00
},
statistic.CreateIcon().With(i =>
{
i.Anchor = Anchor.Centre;
i.Origin = Anchor.Centre;
i.RelativeSizeAxes = Axes.Both;
i.Colour = Color4Extensions.FromHex(@"f7dd55");
i.Size = new Vector2(0.6f);
}),
2018-04-13 09:19:50 +00:00
}
},
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Colour = new Color4(255, 221, 85, 255),
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 17),
2018-04-13 09:19:50 +00:00
Margin = new MarginPadding { Left = 30 },
Text = statistic.Content,
}
};
}
}
}
}
}