1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 11:22:55 +08:00
osu-lazer/osu.Game/Rulesets/UI/ModIcon.cs

136 lines
3.9 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
2022-06-17 15:37:17 +08:00
#nullable disable
2018-04-13 17:19:50 +08:00
using System;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
2020-01-14 21:22:00 +08:00
using osu.Game.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Mods;
2018-11-20 15:51:59 +08:00
using osuTK;
2019-08-07 13:42:43 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Localisation;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.UI
{
2021-01-26 18:11:19 +08:00
/// <summary>
/// Display the specified mod at a fixed size.
/// </summary>
2022-11-24 13:32:20 +08:00
public partial class ModIcon : Container, IHasTooltip
2018-04-13 17:19:50 +08:00
{
2019-11-22 01:34:19 +08:00
public readonly BindableBool Selected = new BindableBool();
2019-08-07 13:42:43 +08:00
2018-04-13 17:19:50 +08:00
private readonly SpriteIcon modIcon;
2020-01-14 21:22:00 +08:00
private readonly SpriteText modAcronym;
2018-04-13 17:19:50 +08:00
private readonly SpriteIcon background;
private const float size = 80;
public virtual LocalisableString TooltipText => showTooltip ? ((mod as Mod)?.IconTooltip ?? mod.Name) : null;
2018-04-13 17:19:50 +08:00
private IMod mod;
2021-01-26 18:11:19 +08:00
private readonly bool showTooltip;
2020-01-14 20:11:32 +08:00
public IMod Mod
2020-01-14 20:11:32 +08:00
{
get => mod;
2020-01-14 21:22:00 +08:00
set
{
mod = value;
if (IsLoaded)
updateMod(value);
2020-01-14 21:22:00 +08:00
}
2020-01-14 20:11:32 +08:00
}
[Resolved]
private OsuColour colours { get; set; }
private Color4 backgroundColour;
2021-01-26 18:11:19 +08:00
/// <summary>
/// Construct a new instance.
/// </summary>
/// <param name="mod">The mod to be displayed</param>
/// <param name="showTooltip">Whether a tooltip describing the mod should display on hover.</param>
public ModIcon(IMod mod, bool showTooltip = true)
2018-04-13 17:19:50 +08:00
{
2020-01-14 21:22:00 +08:00
this.mod = mod ?? throw new ArgumentNullException(nameof(mod));
2021-01-26 18:11:19 +08:00
this.showTooltip = showTooltip;
2018-04-13 17:19:50 +08:00
Size = new Vector2(size);
Children = new Drawable[]
{
background = new SpriteIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Size = new Vector2(size),
Icon = OsuIcon.ModBg,
2018-04-13 17:19:50 +08:00
Shadow = true,
},
2020-01-14 21:22:00 +08:00
modAcronym = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Colour = OsuColour.Gray(84),
Alpha = 0,
2020-01-16 17:58:47 +08:00
Font = OsuFont.Numeric.With(null, 22f),
2020-01-14 21:22:00 +08:00
UseFullGlyphHeight = false,
Text = mod.Acronym
},
2018-04-13 17:19:50 +08:00
modIcon = new SpriteIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Colour = OsuColour.Gray(84),
2020-01-16 17:58:47 +08:00
Size = new Vector2(45),
2020-01-14 21:22:00 +08:00
Icon = FontAwesome.Solid.Question
2018-04-13 17:19:50 +08:00
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Selected.BindValueChanged(_ => updateColour());
2020-01-14 21:22:00 +08:00
updateMod(mod);
}
private void updateMod(IMod value)
2020-01-14 21:22:00 +08:00
{
modAcronym.Text = value.Acronym;
modIcon.Icon = value.Icon ?? FontAwesome.Solid.Question;
if (value.Icon is null)
{
modIcon.FadeOut();
modAcronym.FadeIn();
}
else
{
modIcon.FadeIn();
modAcronym.FadeOut();
2020-01-14 21:22:00 +08:00
}
backgroundColour = colours.ForModType(value.Type);
updateColour();
2018-04-13 17:19:50 +08:00
}
private void updateColour()
2018-04-13 17:19:50 +08:00
{
background.Colour = Selected.Value ? backgroundColour.Lighten(0.2f) : backgroundColour;
2018-04-13 17:19:50 +08:00
}
}
}