1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 23:02:59 +08:00
osu-lazer/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs

163 lines
4.8 KiB
C#
Raw Normal View History

2019-08-07 13:42:43 +08: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.
2022-06-17 15:37:17 +08:00
#nullable disable
2019-08-07 13:42:43 +08:00
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.Allocation;
2019-08-07 13:42:43 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Overlays.BeatmapSet
{
public class LeaderboardModSelector : CompositeDrawable
2019-08-07 13:42:43 +08:00
{
public readonly BindableList<IMod> SelectedMods = new BindableList<IMod>();
public readonly Bindable<IRulesetInfo> Ruleset = new Bindable<IRulesetInfo>();
2019-08-07 13:42:43 +08:00
private readonly FillFlowContainer<ModButton> modsContainer;
public LeaderboardModSelector()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = modsContainer = new FillFlowContainer<ModButton>
2019-08-07 13:42:43 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2019-08-07 13:42:43 +08:00
Direction = FillDirection.Full,
Spacing = new Vector2(4),
};
2019-08-12 19:57:16 +08:00
}
2019-08-07 13:42:43 +08:00
2019-11-21 23:56:48 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
Ruleset.BindValueChanged(onRulesetChanged, true);
}
[Resolved]
private IRulesetStore rulesets { get; set; }
private void onRulesetChanged(ValueChangedEvent<IRulesetInfo> ruleset)
2019-11-21 23:56:48 +08:00
{
SelectedMods.Clear();
modsContainer.Clear();
if (ruleset.NewValue == null)
return;
var rulesetInstance = rulesets.GetRuleset(ruleset.NewValue.OnlineID)?.CreateInstance();
if (rulesetInstance == null)
return;
2019-11-21 23:56:48 +08:00
modsContainer.Add(new ModButton(new ModNoMod()));
modsContainer.AddRange(rulesetInstance.AllMods.Where(m => m.UserPlayable).Select(m => new ModButton(m)));
2019-11-21 23:56:48 +08:00
modsContainer.ForEach(button =>
{
button.Anchor = Anchor.TopCentre;
button.Origin = Anchor.TopCentre;
button.OnSelectionChanged = selectionChanged;
});
2019-11-21 23:56:48 +08:00
}
2019-11-22 09:29:16 +08:00
protected override bool OnHover(HoverEvent e)
{
updateHighlighted();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateHighlighted();
}
private void selectionChanged(IMod mod, bool selected)
2019-08-07 13:42:43 +08:00
{
if (selected)
2019-11-11 04:58:07 +08:00
SelectedMods.Add(mod);
2019-08-07 13:42:43 +08:00
else
2019-11-11 04:58:07 +08:00
SelectedMods.Remove(mod);
2019-09-19 22:03:52 +08:00
2019-11-22 00:02:24 +08:00
updateHighlighted();
2019-08-07 13:42:43 +08:00
}
2019-11-22 00:02:24 +08:00
private void updateHighlighted()
2019-08-12 21:20:36 +08:00
{
2019-11-22 00:02:24 +08:00
if (SelectedMods.Any())
return;
2019-11-22 01:34:19 +08:00
modsContainer.Children.Where(button => !button.IsHovered).ForEach(button => button.Highlighted.Value = !IsHovered);
2019-11-22 00:02:24 +08:00
}
2019-08-12 21:20:36 +08:00
2019-11-22 01:34:19 +08:00
public void DeselectAll() => modsContainer.ForEach(mod => mod.Selected.Value = false);
2019-08-07 13:42:43 +08:00
private class ModButton : ModIcon
{
private const int duration = 200;
2019-11-22 01:34:19 +08:00
public readonly BindableBool Highlighted = new BindableBool();
public Action<IMod, bool> OnSelectionChanged;
2019-08-07 13:42:43 +08:00
public ModButton(IMod mod)
2019-08-07 13:42:43 +08:00
: base(mod)
{
2019-11-21 23:50:29 +08:00
Scale = new Vector2(0.4f);
2019-08-07 13:42:43 +08:00
Add(new HoverClickSounds());
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-08-07 13:42:43 +08:00
2019-11-22 01:34:19 +08:00
Highlighted.BindValueChanged(highlighted =>
2019-08-07 13:42:43 +08:00
{
2019-11-22 01:34:19 +08:00
if (Selected.Value)
return;
2019-11-22 01:34:19 +08:00
this.FadeColour(highlighted.NewValue ? Color4.White : Color4.DimGray, duration, Easing.OutQuint);
}, true);
2019-11-22 01:34:19 +08:00
Selected.BindValueChanged(selected =>
2019-11-22 01:22:15 +08:00
{
2019-11-22 01:34:19 +08:00
OnSelectionChanged?.Invoke(Mod, selected.NewValue);
Highlighted.TriggerChange();
2019-11-22 01:22:15 +08:00
}, true);
2019-08-07 13:42:43 +08:00
}
protected override bool OnClick(ClickEvent e)
{
2019-11-22 01:34:19 +08:00
Selected.Toggle();
2019-11-13 21:04:15 +08:00
return true;
2019-08-07 13:42:43 +08:00
}
protected override bool OnHover(HoverEvent e)
{
2019-11-22 01:34:19 +08:00
Highlighted.Value = true;
2019-11-13 21:04:15 +08:00
return false;
2019-08-07 13:42:43 +08:00
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
2019-11-22 01:34:19 +08:00
Highlighted.Value = false;
2019-08-07 13:42:43 +08:00
}
}
}
}