1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 05:50:05 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/DifficultyIcon.cs

182 lines
6.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
using System;
using osu.Framework.Allocation;
2018-07-13 16:15:06 +08:00
using osu.Framework.Extensions.Color4Extensions;
2018-04-13 17:19:50 +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.Game.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Beatmaps.Drawables
{
public class DifficultyIcon : CompositeDrawable, IHasCustomTooltip
2018-04-13 17:19:50 +08:00
{
private readonly BeatmapInfo beatmap;
private readonly RulesetInfo ruleset;
2018-04-13 17:19:50 +08:00
private readonly Container iconContainer;
/// <summary>
/// Size of this difficulty icon.
/// </summary>
public new Vector2 Size
2018-04-13 17:19:50 +08:00
{
get => iconContainer.Size;
set => iconContainer.Size = value;
}
2018-04-13 17:19:50 +08:00
public DifficultyIcon(BeatmapInfo beatmap, RulesetInfo ruleset = null, bool shouldShowTooltip = true)
{
this.beatmap = beatmap ?? throw new ArgumentNullException(nameof(beatmap));
this.ruleset = ruleset ?? beatmap.Ruleset;
2019-08-17 14:20:27 +08:00
if (shouldShowTooltip)
TooltipContent = beatmap;
AutoSizeAxes = Axes.Both;
InternalChild = iconContainer = new Container { Size = new Vector2(20f) };
2018-04-13 17:19:50 +08:00
}
2019-08-17 14:20:27 +08:00
public ITooltip GetCustomTooltip() => new DifficultyIconTooltip();
2020-01-06 18:12:10 +08:00
public object TooltipContent { get; }
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,
Scale = new Vector2(0.84f),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Colour = Color4.Black.Opacity(0.08f),
Type = EdgeEffectType.Shadow,
Radius = 5,
},
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.ForDifficultyRating(beatmap.DifficultyRating),
2019-08-15 11:11:54 +08:00
},
},
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 = ruleset?.CreateInstance()?.CreateIcon() ?? new SpriteIcon { Icon = FontAwesome.Regular.QuestionCircle }
2019-08-15 11:11:54 +08:00
}
};
}
private class DifficultyIconTooltip : VisibilityContainer, ITooltip
{
private readonly OsuSpriteText difficultyName, starRating;
private readonly Box background;
2019-08-17 14:20:27 +08:00
private readonly FillFlowContainer difficultyFlow;
2019-08-17 14:20:27 +08:00
public DifficultyIconTooltip()
{
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 5;
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
2019-08-17 14:29:39 +08:00
AutoSizeDuration = 200,
AutoSizeEasing = Easing.OutQuint,
Direction = FillDirection.Vertical,
Padding = new MarginPadding(10),
Children = new Drawable[]
{
difficultyName = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Bold),
},
2019-08-17 14:20:27 +08:00
difficultyFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
starRating = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 16, weight: FontWeight.Regular),
},
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Margin = new MarginPadding { Left = 4 },
Icon = FontAwesome.Solid.Star,
Size = new Vector2(12),
},
}
}
}
}
};
}
2020-02-14 21:27:21 +08:00
[Resolved]
private OsuColour colours { get; set; }
2019-08-17 14:20:27 +08:00
[BackgroundDependencyLoader]
2020-02-14 21:27:21 +08:00
private void load()
{
background.Colour = colours.Gray3;
}
2019-08-17 14:20:27 +08:00
public bool SetContent(object content)
{
if (!(content is BeatmapInfo beatmap))
return false;
difficultyName.Text = beatmap.Version;
starRating.Text = $"{beatmap.StarDifficulty:0.##}";
difficultyFlow.Colour = colours.ForDifficultyRating(beatmap.DifficultyRating, true);
2019-08-17 14:20:27 +08:00
return true;
}
public void Move(Vector2 pos) => Position = pos;
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
}
}