1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-12 02:27:28 +08:00
osu-lazer/osu.Game/Screens/SelectV2/PanelBeatmapStandalone.cs

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

315 lines
13 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.
using System;
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.Graphics.Pooling;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
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 : PoolableDrawable, ICarouselPanel
{
public const float HEIGHT = CarouselItem.DEFAULT_HEIGHT * 1.6f;
// todo: this should be replaced with information from CarouselItem about how deep is PanelBeatmap in the carousel
2025-02-05 22:14:38 -05:00
// (i.e. whether it's under a beatmap set that's under a group, or just under a top-level beatmap set).
private const float standalone_x_offset = 20f; // constant X offset for beatmap set/standalone panels specifically.
private const float duration = 500;
[Resolved]
private BeatmapCarousel? carousel { get; set; }
[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 CarouselPanelPiece panel = null!;
private BeatmapSetPanelBackground background = null!;
private OsuSpriteText titleText = null!;
private OsuSpriteText artistText = null!;
2025-02-12 19:22:57 +09:00
private UpdateBeatmapSetButton updateButton = null!;
private BeatmapSetOnlineStatusPill statusPill = null!;
private ConstrainedIconContainer difficultyIcon = null!;
private FillFlowContainer difficultyLine = null!;
private StarRatingDisplay difficultyStarRating = null!;
2025-02-12 19:22:57 +09:00
private TopLocalRank difficultyRank = null!;
private OsuSpriteText difficultyKeyCountText = null!;
private OsuSpriteText difficultyName = null!;
private OsuSpriteText difficultyAuthor = null!;
[BackgroundDependencyLoader]
private void load()
{
Anchor = Anchor.TopRight;
Origin = Anchor.TopRight;
RelativeSizeAxes = Axes.X;
Width = 1f;
Height = HEIGHT;
InternalChild = panel = new CarouselPanelPiece(standalone_x_offset)
{
Action = () => carousel?.Activate(Item!),
Icon = difficultyIcon = new ConstrainedIconContainer
{
Size = new Vector2(20),
Margin = new MarginPadding { Horizontal = 5f },
Colour = colourProvider.Background5,
},
Background = background = new BeatmapSetPanelBackground
{
RelativeSizeAxes = Axes.Both,
},
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.GetFont(weight: FontWeight.Bold, size: 22, italics: true),
Shadow = true,
},
artistText = new OsuSpriteText
{
Font = OsuFont.GetFont(weight: FontWeight.SemiBold, size: 17, italics: true),
Shadow = true,
},
new FillFlowContainer
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Margin = new MarginPadding { Top = 5f },
Children = new Drawable[]
{
2025-02-12 19:22:57 +09:00
updateButton = new UpdateBeatmapSetButton
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Margin = new MarginPadding { Right = 5f, Top = -2f },
},
statusPill = new BeatmapSetOnlineStatusPill
{
AutoSizeAxes = Axes.Both,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
TextSize = 11,
TextPadding = new MarginPadding { Horizontal = 8, Vertical = 2 },
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(8f / 9f),
Margin = new MarginPadding { Right = 5f },
},
2025-02-12 19:22:57 +09:00
difficultyRank = new TopLocalRank
{
Scale = new Vector2(8f / 11),
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Margin = new MarginPadding { Right = 5f },
},
difficultyKeyCountText = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 18, weight: FontWeight.SemiBold),
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Alpha = 0,
Margin = new MarginPadding { Bottom = 2f },
},
difficultyName = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 18, weight: FontWeight.SemiBold),
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Margin = new MarginPadding { Right = 5f, Bottom = 2f },
},
difficultyAuthor = new OsuSpriteText
{
Colour = colourProvider.Content2,
Font = OsuFont.GetFont(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 => panel.Active.Value = s.NewValue, true);
KeyboardSelected.BindValueChanged(k => panel.KeyboardActive.Value = k.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();
FinishTransforms(true);
this.FadeInFromZero(duration, Easing.OutQuint);
}
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()
{
var starDifficulty = starDifficultyBindable?.Value ?? default;
panel.AccentColour = colours.ForStarDifficulty(starDifficulty.Stars);
difficultyIcon.FadeColour(starDifficulty.Stars > 6.5f ? colours.Orange1 : colourProvider.Background5, duration, Easing.OutQuint);
difficultyStarRating.Current.Value = starDifficulty;
}
#region ICarouselPanel
public CarouselItem? Item { get; set; }
public BindableBool Selected { get; } = new BindableBool();
public BindableBool Expanded { get; } = new BindableBool();
public BindableBool KeyboardSelected { get; } = new BindableBool();
public double DrawYPosition { get; set; }
public void Activated()
{
// sets should never be activated.
throw new InvalidOperationException();
}
#endregion
}
}