1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 19:27:52 +08:00
osu-lazer/osu.Game/Screens/Select/BeatmapInfoWedge.cs

606 lines
24 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
2018-04-13 17:19:50 +08:00
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 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Game.Configuration;
using osu.Game.Rulesets;
2019-04-08 17:32:05 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
2020-08-31 14:03:41 +08:00
using osu.Game.Screens.Ranking.Expanded;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select
{
public class BeatmapInfoWedge : VisibilityContainer
2018-04-13 17:19:50 +08:00
{
private const float shear_width = 36.75f;
private static readonly Vector2 wedged_container_shear = new Vector2(shear_width / SongSelect.WEDGE_HEIGHT, 0);
2018-04-13 17:19:50 +08:00
2021-02-25 21:01:53 +08:00
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
2021-04-20 05:41:51 +08:00
protected BufferedWedgeBackground Background;
protected WedgeInfoText Info;
2018-04-13 17:19:50 +08:00
public BeatmapInfoWedge()
{
Shear = wedged_container_shear;
Masking = true;
BorderColour = new Color4(221, 255, 255, 255);
BorderThickness = 2.5f;
Alpha = 0;
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Glow,
Colour = new Color4(130, 204, 255, 150),
Radius = 20,
Roundness = 15,
};
}
2021-02-25 21:09:41 +08:00
[BackgroundDependencyLoader]
private void load()
{
2021-02-25 21:09:41 +08:00
ruleset.BindValueChanged(_ => updateDisplay());
}
2018-04-13 17:19:50 +08:00
protected override void PopIn()
{
this.MoveToX(0, 800, Easing.OutQuint);
this.RotateTo(0, 800, Easing.OutQuint);
this.FadeIn(250);
}
protected override void PopOut()
{
this.MoveToX(-100, 800, Easing.In);
this.RotateTo(10, 800, Easing.In);
this.FadeOut(500, Easing.In);
}
private WorkingBeatmap beatmap;
public WorkingBeatmap Beatmap
{
get => beatmap;
set
{
if (beatmap == value) return;
beatmap = value;
updateDisplay();
}
}
2021-04-20 05:41:51 +08:00
public override bool IsPresent => base.IsPresent || Background == null; // Visibility is updated in the LoadComponentAsync callback
2021-04-20 05:41:51 +08:00
private BufferedWedgeBackground loadingInfo;
private void updateDisplay()
2018-04-13 17:19:50 +08:00
{
Scheduler.AddOnce(perform);
void perform()
{
void removeOldInfo()
{
State.Value = beatmap == null ? Visibility.Hidden : Visibility.Visible;
Info?.FadeOut(250);
Info?.Expire();
Info = null;
2021-04-20 05:41:51 +08:00
Background?.FadeOut(250);
Background?.Expire();
Background = null;
}
if (beatmap == null)
{
removeOldInfo();
return;
}
2021-04-20 05:41:51 +08:00
LoadComponentAsync(loadingInfo = new BufferedWedgeBackground(beatmap)
{
Shear = -Shear,
2021-04-20 05:41:51 +08:00
Depth = Background?.Depth + 1 ?? 0
}, loaded =>
{
// ensure we are the most recent loaded wedge.
if (loaded != loadingInfo) return;
removeOldInfo();
2021-04-20 05:41:51 +08:00
Add(Background = loaded);
Add(Info = new WedgeInfoText(beatmap, ruleset.Value)
2021-04-20 05:41:51 +08:00
{
Shear = -Shear
});
});
}
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
}
2021-04-20 05:41:51 +08:00
public class WedgeInfoText : Container
2018-04-13 17:19:50 +08:00
{
2021-04-20 05:41:51 +08:00
public FillFlowContainer MapperContainer { get; private set; }
2018-04-13 17:19:50 +08:00
public OsuSpriteText TitleLabel { get; private set; }
public OsuSpriteText ArtistLabel { get; private set; }
2021-04-20 05:41:51 +08:00
public OsuSpriteText VersionLabel { get; private set; }
public BeatmapSetOnlineStatusPill StatusPill { get; private set; }
2021-04-20 18:35:31 +08:00
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
private ILocalisedBindableString titleBinding;
private ILocalisedBindableString artistBinding;
private FillFlowContainer infoLabelContainer;
private Drawable starRatingDisplay;
private Container bpmLabelContainer;
2021-04-20 05:41:51 +08:00
private ModSettingChangeTracker settingChangeTracker;
private CancellationTokenSource cancellationTokenSource;
private IBindable<StarDifficulty?> starDifficulty;
2018-04-13 17:19:50 +08:00
private readonly WorkingBeatmap beatmap;
private readonly RulesetInfo ruleset;
public WedgeInfoText(WorkingBeatmap beatmap, RulesetInfo userRuleset)
2018-04-13 17:19:50 +08:00
{
this.beatmap = beatmap;
ruleset = userRuleset ?? beatmap.BeatmapInfo.Ruleset;
2018-04-13 17:19:50 +08:00
}
2018-05-11 13:13:52 +08:00
[BackgroundDependencyLoader]
private void load(LocalisationManager localisation, BeatmapDifficultyCache difficultyCache)
2018-04-13 17:19:50 +08:00
{
var beatmapInfo = beatmap.BeatmapInfo;
var metadata = beatmapInfo.Metadata ?? beatmap.BeatmapSetInfo?.Metadata ?? new BeatmapMetadata();
2018-04-13 17:19:50 +08:00
cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
starDifficulty?.UnbindAll();
starDifficulty = difficultyCache.GetBindableDifficulty(beatmapInfo, cancellationTokenSource.Token);
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.Both;
titleBinding = localisation.GetLocalisedString(new RomanisableString(metadata.TitleUnicode, metadata.Title));
artistBinding = localisation.GetLocalisedString(new RomanisableString(metadata.ArtistUnicode, metadata.Artist));
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new DifficultyColourBar(beatmapInfo)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Y,
Width = 20,
},
new FillFlowContainer
{
Name = "Topleft-aligned metadata",
2018-04-13 17:19:50 +08:00
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Direction = FillDirection.Vertical,
2019-09-25 07:28:40 +08:00
Padding = new MarginPadding { Top = 10, Left = 25, Right = shear_width * 2.5f },
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
VersionLabel = new OsuSpriteText
{
Text = beatmapInfo.Version,
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 24, italics: true),
RelativeSizeAxes = Axes.X,
Truncate = true,
2018-04-13 17:19:50 +08:00
},
}
},
new FillFlowContainer
{
Name = "Topright-aligned metadata",
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Direction = FillDirection.Vertical,
2019-09-25 07:28:40 +08:00
Padding = new MarginPadding { Top = 14, Right = shear_width / 2 },
AutoSizeAxes = Axes.Both,
2020-09-01 01:31:47 +08:00
Shear = wedged_container_shear,
Children = new[]
{
starRatingDisplay = new StarRatingDisplay(beatmapInfo)
2020-08-31 14:03:41 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Shear = -wedged_container_shear,
Margin = new MarginPadding { Bottom = 5 }
},
StatusPill = new BeatmapSetOnlineStatusPill
{
2020-09-01 01:23:19 +08:00
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2020-09-01 01:31:47 +08:00
Shear = -wedged_container_shear,
TextSize = 11,
TextPadding = new MarginPadding { Horizontal = 8, Vertical = 2 },
Status = beatmapInfo.Status,
}
}
},
new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Name = "Centre-aligned metadata",
Anchor = Anchor.CentreLeft,
Origin = Anchor.TopLeft,
2019-09-25 07:28:40 +08:00
Y = -7,
2018-04-13 17:19:50 +08:00
Direction = FillDirection.Vertical,
2019-09-25 07:28:40 +08:00
Padding = new MarginPadding { Left = 25, Right = shear_width },
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
TitleLabel = new OsuSpriteText
{
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 28, italics: true),
RelativeSizeAxes = Axes.X,
Truncate = true,
2018-04-13 17:19:50 +08:00
},
ArtistLabel = new OsuSpriteText
{
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 17, italics: true),
RelativeSizeAxes = Axes.X,
Truncate = true,
2018-04-13 17:19:50 +08:00
},
MapperContainer = new FillFlowContainer
{
Margin = new MarginPadding { Top = 10 },
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Children = getMapper(metadata)
},
infoLabelContainer = new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Margin = new MarginPadding { Top = 20 },
Spacing = new Vector2(20, 0),
AutoSizeAxes = Axes.Both,
}
}
}
};
2021-04-20 18:00:04 +08:00
addInfoLabels();
titleBinding.BindValueChanged(_ => setMetadata(metadata.Source));
artistBinding.BindValueChanged(_ => setMetadata(metadata.Source), true);
2021-04-20 18:00:04 +08:00
starDifficulty.BindValueChanged(_ => setStarRatingDisplayVisibility(), true);
// no difficulty means it can't have a status to show
if (beatmapInfo.Version == null)
StatusPill.Hide();
2018-04-13 17:19:50 +08:00
}
private void setStarRatingDisplayVisibility()
{
if (starDifficulty.Value.HasValue && starDifficulty.Value.Value.Stars > 0)
starRatingDisplay.Show();
else
starRatingDisplay.Hide();
}
private InfoLabel[] getRulesetInfoLabels()
2018-04-13 17:19:50 +08:00
{
try
2018-04-13 17:19:50 +08:00
{
IBeatmap playableBeatmap;
2018-04-13 17:19:50 +08:00
try
{
// Try to get the beatmap with the user's ruleset
playableBeatmap = beatmap.GetPlayableBeatmap(ruleset, Array.Empty<Mod>());
}
catch (BeatmapInvalidForRulesetException)
{
// Can't be converted to the user's ruleset, so use the beatmap's own ruleset
playableBeatmap = beatmap.GetPlayableBeatmap(beatmap.BeatmapInfo.Ruleset, Array.Empty<Mod>());
}
return playableBeatmap.GetStatistics().Select(s => new InfoLabel(s)).ToArray();
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap successfully!");
2018-04-13 17:19:50 +08:00
}
return Array.Empty<InfoLabel>();
2018-04-13 17:19:50 +08:00
}
2021-04-20 18:00:04 +08:00
private void refreshBPMLabel(IReadOnlyList<Mod> mods)
2018-04-13 17:19:50 +08:00
{
var b = beatmap.Beatmap;
if (b == null)
return;
// this doesn't consider mods which apply variable rates, yet.
double rate = 1;
foreach (var mod in mods.OfType<IApplicableToRate>())
rate = mod.ApplyToRate(0, rate);
double bpmMax = b.ControlPointInfo.BPMMaximum * rate;
double bpmMin = b.ControlPointInfo.BPMMinimum * rate;
double mostCommonBPM = 60000 / b.GetMostCommonBeatLength() * rate;
2018-04-13 17:19:50 +08:00
string labelText = Precision.AlmostEquals(bpmMin, bpmMax)
? $"{bpmMin:0}"
: $"{bpmMin:0}-{bpmMax:0} (mostly {mostCommonBPM:0})";
2018-04-13 17:19:50 +08:00
bpmLabelContainer.Child = new InfoLabel(new BeatmapStatistic
{
Name = "BPM",
CreateIcon = () => new BeatmapStatisticIcon(BeatmapStatisticsIconType.Bpm),
Content = labelText
});
2021-04-20 05:41:51 +08:00
}
2018-04-13 17:19:50 +08:00
2021-04-20 05:41:51 +08:00
private void setMetadata(string source)
{
ArtistLabel.Text = artistBinding.Value;
TitleLabel.Text = string.IsNullOrEmpty(source) ? titleBinding.Value : source + " — " + titleBinding.Value;
}
private void addInfoLabels()
{
if (beatmap.Beatmap?.HitObjects?.Any() != true)
return;
infoLabelContainer.Children = new Drawable[]
{
new InfoLabel(new BeatmapStatistic
{
Name = "Length",
CreateIcon = () => new BeatmapStatisticIcon(BeatmapStatisticsIconType.Length),
Content = TimeSpan.FromMilliseconds(beatmap.BeatmapInfo.Length).ToString(@"m\:ss"),
}),
bpmLabelContainer = new Container
{
AutoSizeAxes = Axes.Both,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(20, 0),
Children = getRulesetInfoLabels()
}
};
2021-04-20 18:00:04 +08:00
mods.BindValueChanged(mods => refreshModInformation(mods), true);
}
2021-04-20 05:41:51 +08:00
2021-04-20 18:00:04 +08:00
private void refreshModInformation(ValueChangedEvent<IReadOnlyList<Mod>> modsChangedEvent)
{
settingChangeTracker?.Dispose();
settingChangeTracker = new ModSettingChangeTracker(modsChangedEvent.NewValue);
settingChangeTracker.SettingChanged += _ => refreshBPMLabel(modsChangedEvent.NewValue);
2021-04-20 18:35:31 +08:00
refreshBPMLabel(modsChangedEvent.NewValue);
2018-04-13 17:19:50 +08:00
}
private OsuSpriteText[] getMapper(BeatmapMetadata metadata)
{
if (string.IsNullOrEmpty(metadata.Author?.Username))
return Array.Empty<OsuSpriteText>();
return new[]
{
new OsuSpriteText
{
Text = "mapped by ",
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(size: 15),
2018-04-13 17:19:50 +08:00
},
new OsuSpriteText
{
Text = metadata.Author.Username,
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 15),
2018-04-13 17:19:50 +08:00
}
};
}
2021-04-20 05:41:51 +08:00
private class DifficultyColourBar : Container
{
[Resolved]
private OsuColour colours { get; set; }
private Box solidDifficultyBox;
private Box transparentDifficultyBox;
private CancellationTokenSource cancellationTokenSource;
private IBindable<StarDifficulty?> starDifficulty;
2021-04-20 05:41:51 +08:00
private readonly BeatmapInfo beatmapInfo;
public DifficultyColourBar(BeatmapInfo beatmapInfo)
2021-04-20 05:41:51 +08:00
{
this.beatmapInfo = beatmapInfo;
2021-04-20 05:41:51 +08:00
}
[BackgroundDependencyLoader]
private void load(BeatmapDifficultyCache difficultyCache)
2021-04-20 05:41:51 +08:00
{
const float full_opacity_ratio = 0.7f;
cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
starDifficulty?.UnbindAll();
starDifficulty = difficultyCache.GetBindableDifficulty(beatmapInfo, cancellationTokenSource.Token);
2021-04-20 05:41:51 +08:00
Children = new Drawable[]
{
solidDifficultyBox = new Box
2021-04-20 05:41:51 +08:00
{
RelativeSizeAxes = Axes.Both,
Width = full_opacity_ratio,
},
transparentDifficultyBox = new Box
2021-04-20 05:41:51 +08:00
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
Alpha = 0.5f,
X = full_opacity_ratio,
Width = 1 - full_opacity_ratio,
}
};
starDifficulty.BindValueChanged(valueChangedEvent => setColour(valueChangedEvent), true);
}
private void setColour(ValueChangedEvent<StarDifficulty?> valueChanged)
{
var difficultyColour = colours.ForDifficultyRating(valueChanged.NewValue?.DifficultyRating ?? (new StarDifficulty()).DifficultyRating);
solidDifficultyBox.Colour = difficultyColour;
transparentDifficultyBox.Colour = difficultyColour;
2021-04-20 05:41:51 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
cancellationTokenSource?.Cancel();
}
}
2018-04-13 17:19:50 +08:00
public class InfoLabel : Container, IHasTooltip
{
public string TooltipText { get; }
2018-04-13 17:19:50 +08:00
public InfoLabel(BeatmapStatistic statistic)
{
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 18:55:24 +08:00
Icon = FontAwesome.Solid.Square,
2018-04-13 17:19:50 +08: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.8f)
2018-04-13 17:19:50 +08: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.64f);
}),
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
Margin = new MarginPadding { Left = 30 },
Text = statistic.Content,
}
};
}
}
2021-04-20 05:41:51 +08:00
protected override void Dispose(bool isDisposing)
2018-04-13 17:19:50 +08:00
{
2021-04-20 05:41:51 +08:00
base.Dispose(isDisposing);
settingChangeTracker?.Dispose();
}
}
2021-04-20 05:41:51 +08:00
public class BufferedWedgeBackground : BufferedContainer
{
private readonly WorkingBeatmap beatmap;
2018-04-13 17:19:50 +08:00
2021-04-20 05:41:51 +08:00
public BufferedWedgeBackground(WorkingBeatmap beatmap)
: base(pixelSnapping: true)
{
this.beatmap = beatmap;
}
2018-04-13 17:19:50 +08:00
2021-04-20 05:41:51 +08:00
[BackgroundDependencyLoader]
private void load(LocalisationManager localisation)
{
CacheDrawnFrameBuffer = true;
RelativeSizeAxes = Axes.Both;
2021-04-20 05:41:51 +08:00
Children = new Drawable[]
{
// We will create the white-to-black gradient by modulating transparency and having
// a black backdrop. This results in an sRGB-space gradient and not linear space,
// transitioning from white to black more perceptually uniformly.
new Box
2018-04-13 17:19:50 +08:00
{
2021-04-20 05:41:51 +08:00
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
// We use a container, such that we can set the colour gradient to go across the
// vertices of the masked container instead of the vertices of the (larger) sprite.
new Container
{
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientVertical(Color4.White, Color4.White.Opacity(0.3f)),
Children = new[]
2018-04-13 17:19:50 +08:00
{
2021-04-20 05:41:51 +08:00
// Zoomed-in and cropped beatmap background
new BeatmapBackgroundSprite(beatmap)
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
},
2018-04-13 17:19:50 +08:00
},
2021-04-20 05:41:51 +08:00
},
2021-04-20 20:29:53 +08:00
};
2018-04-13 17:19:50 +08:00
}
}
}
}