osu/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs

150 lines
4.4 KiB
C#
Raw Normal View History

2019-08-07 05:42:43 +00:00
// 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.
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Mods;
using osu.Framework.Bindables;
using osu.Game.Rulesets;
using osuTK;
using osu.Game.Rulesets.UI;
using osu.Framework.Input.Events;
using osu.Game.Graphics.UserInterface;
using osuTK.Graphics;
using System;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Overlays.BeatmapSet
{
public class LeaderboardModSelector : CompositeDrawable
2019-08-07 05:42:43 +00:00
{
2019-11-10 20:58:07 +00:00
public readonly BindableList<Mod> SelectedMods = new BindableList<Mod>();
2019-11-15 08:52:49 +00:00
private RulesetInfo ruleset;
public RulesetInfo Ruleset
{
get => ruleset;
set
{
2019-11-15 09:09:31 +00:00
if (ruleset?.Equals(value) ?? false)
2019-11-15 08:52:49 +00:00
{
DeselectAll();
return;
}
ruleset = value;
SelectedMods.Clear();
modsContainer.Clear();
if (ruleset == null)
return;
2019-11-15 08:57:40 +00:00
modsContainer.Add(new ModButton(new ModNoMod()));
2019-11-15 08:52:49 +00:00
modsContainer.AddRange(ruleset.CreateInstance().GetAllMods().Where(m => m.Ranked).Select(m => new ModButton(m)));
modsContainer.ForEach(button => button.OnSelectionChanged = selectionChanged);
}
}
2019-08-07 05:42:43 +00:00
private readonly FillFlowContainer<ModButton> modsContainer;
public LeaderboardModSelector()
{
AutoSizeAxes = Axes.Both;
InternalChild = modsContainer = new FillFlowContainer<ModButton>
2019-08-07 05:42:43 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Full,
Spacing = new Vector2(4),
};
2019-08-12 11:57:16 +00:00
}
2019-08-07 05:42:43 +00:00
private void selectionChanged(Mod mod, bool selected)
{
if (selected)
2019-11-10 20:58:07 +00:00
SelectedMods.Add(mod);
2019-08-07 05:42:43 +00:00
else
2019-11-10 20:58:07 +00:00
SelectedMods.Remove(mod);
2019-09-19 14:03:52 +00:00
2019-11-10 20:58:07 +00:00
if (!SelectedMods.Any() && !IsHovered)
highlightAll();
2019-08-07 05:42:43 +00:00
}
2019-08-12 13:20:36 +00:00
protected override bool OnHover(HoverEvent e)
{
2019-11-10 20:58:07 +00:00
if (!SelectedMods.Any())
2019-11-12 05:45:21 +00:00
modsContainer.Children.Where(button => !button.IsHovered).ForEach(button => button.Highlighted.Value = false);
2019-08-12 13:20:36 +00:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
2019-11-10 20:58:07 +00:00
if (!SelectedMods.Any())
highlightAll();
2019-08-12 13:20:36 +00:00
}
2019-08-07 05:42:43 +00:00
public void DeselectAll() => modsContainer.ForEach(mod => mod.Selected.Value = false);
private void highlightAll() => modsContainer.ForEach(mod => mod.Highlighted.Value = true);
2019-08-07 05:42:43 +00:00
private class ModButton : ModIcon
{
private const float mod_scale = 0.4f;
private const int duration = 200;
public readonly BindableBool Selected = new BindableBool();
public Action<Mod, bool> OnSelectionChanged;
public ModButton(Mod mod)
: base(mod)
{
Scale = new Vector2(mod_scale);
2019-09-19 14:03:52 +00:00
Highlighted.Value = true;
2019-08-07 05:42:43 +00:00
Add(new HoverClickSounds());
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-08-07 05:42:43 +00:00
Selected.BindValueChanged(selected =>
{
updateState();
OnSelectionChanged?.Invoke(Mod, selected.NewValue);
2019-08-12 13:20:36 +00:00
});
2019-08-07 05:42:43 +00:00
}
protected override bool OnClick(ClickEvent e)
{
2019-11-13 13:04:15 +00:00
Selected.Toggle();
return true;
2019-08-07 05:42:43 +00:00
}
protected override bool OnHover(HoverEvent e)
{
updateState();
2019-11-13 13:04:15 +00:00
return false;
2019-08-07 05:42:43 +00:00
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateState();
}
private void updateState() => Highlighted.Value = IsHovered || Selected.Value;
2019-09-19 14:03:52 +00:00
protected override void OnHighlightedChanged(ValueChangedEvent<bool> highlighted) =>
this.FadeColour(highlighted.NewValue ? Color4.White : Color4.Gray, duration, Easing.OutQuint);
2019-08-07 05:42:43 +00:00
}
}
}