Use BindableList for selected mods

This commit is contained in:
Andrei Zavatski 2019-11-10 23:58:07 +03:00
parent e3d8e29cdd
commit 93954c8da0
2 changed files with 29 additions and 22 deletions

View File

@ -13,6 +13,7 @@
using osu.Framework.Graphics.Sprites;
using osu.Framework.Bindables;
using osu.Game.Rulesets;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Tests.Visual.Online
{
@ -26,10 +27,10 @@ public class TestSceneLeaderboardModSelector : OsuTestScene
public TestSceneLeaderboardModSelector()
{
LeaderboardModSelector modSelector;
FillFlowContainer selectedMods;
FillFlowContainer<SpriteText> selectedMods;
Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
Add(selectedMods = new FillFlowContainer
Add(selectedMods = new FillFlowContainer<SpriteText>
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
@ -42,16 +43,28 @@ public TestSceneLeaderboardModSelector()
Ruleset = { BindTarget = ruleset }
});
modSelector.SelectedMods.BindValueChanged(mods =>
modSelector.SelectedMods.ItemsAdded += mods =>
{
selectedMods.Clear();
mods.ForEach(mod => selectedMods.Add(new SpriteText
{
Text = mod.Acronym,
}));
};
foreach (var mod in mods.NewValue)
selectedMods.Add(new SpriteText
modSelector.SelectedMods.ItemsRemoved += mods =>
{
mods.ForEach(mod =>
{
foreach (var selected in selectedMods)
{
Text = mod.Acronym,
});
});
if (selected.Text == mod.Acronym)
{
selectedMods.Remove(selected);
break;
}
}
});
};
AddStep("osu ruleset", () => ruleset.Value = new OsuRuleset().RulesetInfo);
AddStep("mania ruleset", () => ruleset.Value = new ManiaRuleset().RulesetInfo);

View File

@ -5,7 +5,6 @@
using osu.Framework.Graphics;
using osu.Game.Rulesets.Mods;
using osu.Framework.Bindables;
using System.Collections.Generic;
using osu.Game.Rulesets;
using osuTK;
using osu.Game.Rulesets.UI;
@ -21,7 +20,7 @@ namespace osu.Game.Overlays.BeatmapSet
{
public class LeaderboardModSelector : CompositeDrawable
{
public readonly Bindable<IEnumerable<Mod>> SelectedMods = new Bindable<IEnumerable<Mod>>();
public readonly BindableList<Mod> SelectedMods = new BindableList<Mod>();
public readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
private readonly FillFlowContainer<ModButton> modsContainer;
@ -47,8 +46,7 @@ protected override void LoadComplete()
private void onRulesetChanged(ValueChangedEvent<RulesetInfo> ruleset)
{
SelectedMods.Value = new List<Mod>();
SelectedMods.Clear();
modsContainer.Clear();
if (ruleset.NewValue == null)
@ -67,22 +65,18 @@ private void onRulesetChanged(ValueChangedEvent<RulesetInfo> ruleset)
private void selectionChanged(Mod mod, bool selected)
{
var mods = SelectedMods.Value.ToList();
if (selected)
mods.Add(mod);
SelectedMods.Add(mod);
else
mods.Remove(mod);
SelectedMods.Remove(mod);
SelectedMods.Value = mods;
if (!mods.Any() && !IsHovered)
if (!SelectedMods.Any() && !IsHovered)
highlightAll();
}
protected override bool OnHover(HoverEvent e)
{
if (!SelectedMods.Value.Any())
if (!SelectedMods.Any())
modsContainer.ForEach(button =>
{
if (!button.IsHovered)
@ -96,7 +90,7 @@ protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
if (!SelectedMods.Value.Any())
if (!SelectedMods.Any())
highlightAll();
}