// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using osuTK.Graphics; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Sprites; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Game.Rulesets.Mods; using osuTK; using osu.Framework.Bindables; using osu.Framework.Localisation; namespace osu.Game.Rulesets.UI { /// /// Display the specified mod at a fixed size. /// public class ModIcon : Container, IHasTooltip { public readonly BindableBool Selected = new BindableBool(); private readonly SpriteIcon modIcon; private readonly SpriteText modAcronym; private readonly SpriteIcon background; private const float size = 80; public virtual LocalisableString TooltipText => showTooltip ? ((mod as Mod)?.IconTooltip ?? mod.Name) : null; private IMod mod; private readonly bool showTooltip; public IMod Mod { get => mod; set { mod = value; if (IsLoaded) updateMod(value); } } [Resolved] private OsuColour colours { get; set; } private Color4 backgroundColour; private Color4 highlightedColour; /// /// Construct a new instance. /// /// The mod to be displayed /// Whether a tooltip describing the mod should display on hover. public ModIcon(IMod mod, bool showTooltip = true) { this.mod = mod ?? throw new ArgumentNullException(nameof(mod)); this.showTooltip = showTooltip; Size = new Vector2(size); Children = new Drawable[] { background = new SpriteIcon { Origin = Anchor.Centre, Anchor = Anchor.Centre, Size = new Vector2(size), Icon = OsuIcon.ModBg, Shadow = true, }, modAcronym = new OsuSpriteText { Origin = Anchor.Centre, Anchor = Anchor.Centre, Colour = OsuColour.Gray(84), Alpha = 0, Font = OsuFont.Numeric.With(null, 22f), UseFullGlyphHeight = false, Text = mod.Acronym }, modIcon = new SpriteIcon { Origin = Anchor.Centre, Anchor = Anchor.Centre, Colour = OsuColour.Gray(84), Size = new Vector2(45), Icon = FontAwesome.Solid.Question }, }; } protected override void LoadComplete() { base.LoadComplete(); Selected.BindValueChanged(_ => updateColour()); updateMod(mod); } private void updateMod(IMod value) { 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(); } Color4 typeColour = colours.ForModType(value.Type); backgroundColour = typeColour; highlightedColour = ((Colour4)typeColour).Lighten(.2f); if (value.Type == ModType.System) modIcon.Colour = colours.Yellow; updateColour(); } private void updateColour() { background.Colour = Selected.Value ? highlightedColour : backgroundColour; } } }