1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 03:02:54 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/ModDisplay.cs

129 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
using System;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play.HUD
{
2021-07-21 14:55:51 +08:00
/// <summary>
/// Displays a single-line horizontal auto-sized flow of mods. For cases where wrapping is required, use <see cref="ModFlowDisplay"/> instead.
2021-07-21 14:55:51 +08:00
/// </summary>
2022-11-24 13:32:20 +08:00
public partial class ModDisplay : CompositeDrawable, IHasCurrentValue<IReadOnlyList<Mod>>
2018-04-13 17:19:50 +08:00
{
private const int fade_duration = 1000;
public ExpansionMode ExpansionMode = ExpansionMode.ExpandOnHover;
private readonly BindableWithCurrent<IReadOnlyList<Mod>> current = new BindableWithCurrent<IReadOnlyList<Mod>>(Array.Empty<Mod>());
public Bindable<IReadOnlyList<Mod>> Current
{
2021-07-21 14:55:51 +08:00
get => current.Current;
set
{
ArgumentNullException.ThrowIfNull(value);
2018-04-13 17:19:50 +08:00
2021-07-21 14:55:51 +08:00
current.Current = value;
}
}
2018-04-13 17:19:50 +08:00
2024-05-10 11:47:48 +08:00
private readonly bool showExtendedInformation;
2020-04-06 05:53:49 +08:00
private readonly FillFlowContainer<ModIcon> iconsContainer;
2018-04-13 17:19:50 +08:00
public ModDisplay(bool showExtendedInformation = true)
2018-04-13 17:19:50 +08:00
{
this.showExtendedInformation = showExtendedInformation;
2018-12-12 13:38:03 +08:00
AutoSizeAxes = Axes.Both;
2021-07-21 14:55:51 +08:00
InternalChild = iconsContainer = new ReverseChildIDFillFlowContainer<ModIcon>
2018-04-13 17:19:50 +08:00
{
AutoSizeAxes = Axes.Both,
2021-07-21 14:55:51 +08:00
Direction = FillDirection.Horizontal,
2018-04-13 17:19:50 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-07-21 14:55:51 +08:00
Current.BindValueChanged(updateDisplay, true);
2021-07-21 14:55:51 +08:00
iconsContainer.FadeInFromZero(fade_duration, Easing.OutQuint);
if (ExpansionMode == ExpansionMode.AlwaysExpanded || ExpansionMode == ExpansionMode.AlwaysContracted)
FinishTransforms(true);
2021-07-21 14:55:51 +08:00
}
2021-07-21 14:55:51 +08:00
private void updateDisplay(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{
iconsContainer.Clear();
foreach (Mod mod in mods.NewValue.AsOrdered())
iconsContainer.Add(new ModIcon(mod, showExtendedInformation: showExtendedInformation) { Scale = new Vector2(0.6f) });
2021-07-21 14:55:51 +08:00
appearTransform();
2018-04-13 17:19:50 +08:00
}
private void appearTransform()
{
expand();
2020-04-06 05:53:49 +08:00
using (iconsContainer.BeginDelayedSequence(1200))
2018-04-13 17:19:50 +08:00
contract();
}
private void expand()
{
if (ExpansionMode != ExpansionMode.AlwaysContracted)
2020-04-06 05:53:49 +08:00
iconsContainer.TransformSpacingTo(new Vector2(5, 0), 500, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
private void contract()
{
if (ExpansionMode != ExpansionMode.AlwaysExpanded)
2020-04-06 05:53:49 +08:00
iconsContainer.TransformSpacingTo(new Vector2(-25, 0), 500, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
expand();
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
contract();
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
}
public enum ExpansionMode
{
/// <summary>
/// The <see cref="ModDisplay"/> will expand only when hovered.
/// </summary>
ExpandOnHover,
/// <summary>
/// The <see cref="ModDisplay"/> will always be expanded.
/// </summary>
AlwaysExpanded,
/// <summary>
/// The <see cref="ModDisplay"/> will always be contracted.
/// </summary>
AlwaysContracted
}
2018-04-13 17:19:50 +08:00
}