1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-24 23:21:19 +08:00
Files
osu-lazer/osu.Game/Screens/SelectV2/PanelBeatmapStandalone.cs
T
2025-04-23 16:57:41 +09:00

278 lines
11 KiB
C#

// 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.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Carousel;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osuTK;
namespace osu.Game.Screens.SelectV2
{
public partial class PanelBeatmapStandalone : Panel
{
public const float HEIGHT = CarouselItem.DEFAULT_HEIGHT * 1.7f;
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[Resolved]
private BeatmapManager beatmaps { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; } = null!;
private IBindable<StarDifficulty?>? starDifficultyBindable;
private CancellationTokenSource? starDifficultyCancellationSource;
private PanelSetBackground background = null!;
private OsuSpriteText titleText = null!;
private OsuSpriteText artistText = null!;
private PanelUpdateBeatmapButton updateButton = null!;
private BeatmapSetOnlineStatusPill statusPill = null!;
private ConstrainedIconContainer difficultyIcon = null!;
private FillFlowContainer difficultyLine = null!;
private StarRatingDisplay difficultyStarRating = null!;
private PanelLocalRankDisplay difficultyRank = null!;
private OsuSpriteText difficultyKeyCountText = null!;
private OsuSpriteText difficultyName = null!;
private OsuSpriteText difficultyAuthor = null!;
public PanelBeatmapStandalone()
{
PanelXOffset = 20;
}
[BackgroundDependencyLoader]
private void load()
{
Height = HEIGHT;
Icon = difficultyIcon = new ConstrainedIconContainer
{
Size = new Vector2(16),
Margin = new MarginPadding { Horizontal = 5f },
Colour = colourProvider.Background5,
};
Background = background = new PanelSetBackground
{
RelativeSizeAxes = Axes.Both,
};
Content.Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Padding = new MarginPadding { Top = 7.5f, Left = 15, Bottom = 5 },
Children = new Drawable[]
{
titleText = new OsuSpriteText
{
Font = OsuFont.Style.Heading1.With(typeface: Typeface.TorusAlternate),
},
artistText = new OsuSpriteText
{
Font = OsuFont.Style.Body.With(weight: FontWeight.SemiBold),
},
new FillFlowContainer
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
updateButton = new PanelUpdateBeatmapButton
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Right = 5f, Top = -2f },
},
statusPill = new BeatmapSetOnlineStatusPill
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
TextSize = OsuFont.Style.Caption2.Size,
Margin = new MarginPadding { Right = 5f },
},
difficultyLine = new FillFlowContainer
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
difficultyStarRating = new StarRatingDisplay(default, StarRatingDisplaySize.Small)
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Scale = new Vector2(0.875f),
Margin = new MarginPadding { Right = 5f },
},
difficultyRank = new PanelLocalRankDisplay
{
Scale = new Vector2(0.65f),
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Margin = new MarginPadding { Right = 5f },
},
difficultyKeyCountText = new OsuSpriteText
{
Font = OsuFont.Style.Heading2,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Alpha = 0,
Margin = new MarginPadding { Bottom = 2f },
},
difficultyName = new OsuSpriteText
{
Font = OsuFont.Style.Heading2,
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Margin = new MarginPadding { Right = 5f, Bottom = 2f },
},
difficultyAuthor = new OsuSpriteText
{
Colour = colourProvider.Content2,
Font = OsuFont.Style.Caption1.With(weight: FontWeight.SemiBold),
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Margin = new MarginPadding { Right = 5f, Bottom = 2f },
}
}
},
},
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
ruleset.BindValueChanged(_ =>
{
computeStarRating();
updateKeyCount();
});
mods.BindValueChanged(_ =>
{
computeStarRating();
updateKeyCount();
}, true);
Selected.BindValueChanged(s => Expanded.Value = s.NewValue, true);
}
protected override void PrepareForUse()
{
base.PrepareForUse();
Debug.Assert(Item != null);
var beatmap = (BeatmapInfo)Item.Model;
var beatmapSet = beatmap.BeatmapSet!;
// Choice of background image matches BSS implementation (always uses the lowest `beatmap_id` from the set).
background.Beatmap = beatmaps.GetWorkingBeatmap(beatmapSet.Beatmaps.MinBy(b => b.OnlineID));
titleText.Text = new RomanisableString(beatmapSet.Metadata.TitleUnicode, beatmapSet.Metadata.Title);
artistText.Text = new RomanisableString(beatmapSet.Metadata.ArtistUnicode, beatmapSet.Metadata.Artist);
updateButton.BeatmapSet = beatmapSet;
statusPill.Status = beatmapSet.Status;
difficultyIcon.Icon = beatmap.Ruleset.CreateInstance().CreateIcon();
difficultyIcon.Show();
difficultyRank.Beatmap = beatmap;
difficultyName.Text = beatmap.DifficultyName;
difficultyAuthor.Text = BeatmapsetsStrings.ShowDetailsMappedBy(beatmap.Metadata.Author.Username);
difficultyLine.Show();
computeStarRating();
}
protected override void FreeAfterUse()
{
base.FreeAfterUse();
background.Beatmap = null;
updateButton.BeatmapSet = null;
difficultyRank.Beatmap = null;
starDifficultyBindable = null;
}
private void computeStarRating()
{
starDifficultyCancellationSource?.Cancel();
starDifficultyCancellationSource = new CancellationTokenSource();
if (Item == null)
return;
var beatmap = (BeatmapInfo)Item.Model;
starDifficultyBindable = difficultyCache.GetBindableDifficulty(beatmap, starDifficultyCancellationSource.Token);
starDifficultyBindable.BindValueChanged(_ => updateDisplay(), true);
}
private void updateKeyCount()
{
if (Item == null)
return;
var beatmap = (BeatmapInfo)Item.Model;
if (ruleset.Value.OnlineID == 3)
{
// Account for mania differences locally for now.
// Eventually this should be handled in a more modular way, allowing rulesets to add more information to the panel.
ILegacyRuleset legacyRuleset = (ILegacyRuleset)ruleset.Value.CreateInstance();
int keyCount = legacyRuleset.GetKeyCount(beatmap, mods.Value);
difficultyKeyCountText.Alpha = 1;
difficultyKeyCountText.Text = $"[{keyCount}K] ";
}
else
difficultyKeyCountText.Alpha = 0;
}
private void updateDisplay()
{
const float duration = 500;
var starDifficulty = starDifficultyBindable?.Value ?? default;
AccentColour = colours.ForStarDifficulty(starDifficulty.Stars);
difficultyIcon.FadeColour(starDifficulty.Stars > OsuColour.STAR_DIFFICULTY_DEFINED_COLOUR_CUTOFF ? colours.Orange1 : colourProvider.Background5, duration, Easing.OutQuint);
difficultyStarRating.Current.Value = starDifficulty;
}
}
}