1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 02:12:56 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs

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

157 lines
5.7 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
2017-02-07 12:52:19 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-07-13 16:15:06 +08:00
using osu.Framework.Extensions.Color4Extensions;
2017-02-07 12:52:19 +08:00
using osu.Framework.Graphics;
2018-07-13 16:15:06 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-07-13 16:15:06 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2016-11-23 10:59:50 +08:00
namespace osu.Game.Beatmaps.Drawables
2016-10-27 10:55:55 +08:00
{
public partial class DifficultyIcon : CompositeDrawable, IHasCustomTooltip<DifficultyIconTooltipContent>, IHasCurrentValue<StarDifficulty>
2017-02-07 12:52:19 +08:00
{
/// <summary>
/// Size of this difficulty icon.
/// </summary>
public new Vector2 Size
2017-02-07 12:52:19 +08:00
{
get => iconContainer.Size;
set => iconContainer.Size = value;
}
2018-04-13 17:19:50 +08:00
2022-06-23 17:53:21 +08:00
/// <summary>
/// Which type of tooltip to show. Only works if a beatmap was provided at construction time.
2022-06-23 17:53:21 +08:00
/// </summary>
public DifficultyIconTooltipType TooltipType { get; set; } = DifficultyIconTooltipType.StarRating;
private readonly IBeatmapInfo? beatmap;
private readonly IRulesetInfo ruleset;
private readonly Mod[]? mods;
private Drawable background = null!;
private readonly Container iconContainer;
private readonly BindableWithCurrent<StarDifficulty> difficulty = new BindableWithCurrent<StarDifficulty>();
public virtual Bindable<StarDifficulty> Current
{
get => difficulty.Current;
set => difficulty.Current = value;
}
2022-06-23 15:55:22 +08:00
[Resolved]
private IRulesetStore rulesets { get; set; } = null!;
2022-06-23 15:55:22 +08:00
/// <summary>
/// Creates a new <see cref="DifficultyIcon"/>. Will use provided beatmap's <see cref="BeatmapInfo.StarRating"/> for initial value.
/// </summary>
/// <param name="beatmap">The beatmap to be displayed in the tooltip, and to be used for the initial star rating value.</param>
/// <param name="mods">An array of mods to account for in the calculations</param>
/// <param name="ruleset">An optional ruleset to be used for the icon display, in place of the beatmap's ruleset.</param>
public DifficultyIcon(IBeatmapInfo beatmap, IRulesetInfo? ruleset = null, Mod[]? mods = null)
: this(ruleset ?? beatmap.Ruleset)
{
this.beatmap = beatmap;
this.mods = mods;
Current.Value = new StarDifficulty(beatmap.StarRating, 0);
}
/// <summary>
/// Creates a new <see cref="DifficultyIcon"/> without an associated beatmap.
/// </summary>
/// <param name="ruleset">The ruleset to be used for the icon display.</param>
public DifficultyIcon(IRulesetInfo ruleset)
{
this.ruleset = ruleset;
AutoSizeAxes = Axes.Both;
InternalChild = iconContainer = new Container { Size = new Vector2(20f) };
2017-02-07 12:52:19 +08:00
}
2018-04-13 17:19:50 +08:00
2019-08-15 11:11:54 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
2019-08-15 11:11:54 +08:00
{
iconContainer.Children = new Drawable[]
2019-08-15 11:11:54 +08:00
{
new CircularContainer
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
2021-08-06 17:00:12 +08:00
Colour = Color4.Black.Opacity(0.06f),
2019-08-15 11:11:54 +08:00
Type = EdgeEffectType.Shadow,
2021-08-06 17:00:12 +08:00
Radius = 3,
2019-08-15 11:11:54 +08:00
},
Child = background = new Box
2019-08-15 11:11:54 +08:00
{
RelativeSizeAxes = Axes.Both,
},
},
new ConstrainedIconContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
// the null coalesce here is only present to make unit tests work (ruleset dlls aren't copied correctly for testing at the moment)
Icon = getRulesetIcon()
},
2019-08-15 11:11:54 +08:00
};
Current.BindValueChanged(difficulty => background.Colour = colours.ForStarDifficulty(difficulty.NewValue.Stars), true);
}
private Drawable getRulesetIcon()
{
int? onlineID = ruleset.OnlineID;
if (onlineID >= 0 && rulesets.GetRuleset(onlineID.Value)?.CreateInstance() is Ruleset rulesetInstance)
return rulesetInstance.CreateIcon();
return new SpriteIcon { Icon = FontAwesome.Regular.QuestionCircle };
}
ITooltip<DifficultyIconTooltipContent> IHasCustomTooltip<DifficultyIconTooltipContent>.
GetCustomTooltip() => new DifficultyIconTooltip();
DifficultyIconTooltipContent IHasCustomTooltip<DifficultyIconTooltipContent>.
TooltipContent => (TooltipType != DifficultyIconTooltipType.None && beatmap != null ? new DifficultyIconTooltipContent(beatmap, Current, ruleset, mods, TooltipType) : null)!;
}
public enum DifficultyIconTooltipType
{
/// <summary>
/// No tooltip.
/// </summary>
None,
/// <summary>
/// Star rating only.
/// </summary>
StarRating,
/// <summary>
/// Star rating, OD, HP, CS, AR, length, BPM, and max combo.
/// </summary>
Extended,
2016-10-27 10:55:55 +08:00
}
}