1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:57:52 +08:00

Make Ruleset a bindable

This commit is contained in:
Andrei Zavatski 2019-11-21 18:56:48 +03:00
parent 737c2bd1c8
commit 984ec11a78
2 changed files with 30 additions and 33 deletions

View File

@ -12,6 +12,8 @@ using osu.Game.Rulesets.Catch;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Bindables;
using osu.Game.Rulesets;
namespace osu.Game.Tests.Visual.Online
{
@ -26,6 +28,7 @@ namespace osu.Game.Tests.Visual.Online
{
LeaderboardModSelector modSelector;
FillFlowContainer<SpriteText> selectedMods;
var ruleset = new Bindable<RulesetInfo>();
Add(selectedMods = new FillFlowContainer<SpriteText>
{
@ -37,6 +40,7 @@ namespace osu.Game.Tests.Visual.Online
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Ruleset = { BindTarget = ruleset }
});
modSelector.SelectedMods.ItemsAdded += mods =>
@ -62,12 +66,12 @@ namespace osu.Game.Tests.Visual.Online
});
};
AddStep("osu ruleset", () => modSelector.Ruleset = new OsuRuleset().RulesetInfo);
AddStep("mania ruleset", () => modSelector.Ruleset = new ManiaRuleset().RulesetInfo);
AddStep("taiko ruleset", () => modSelector.Ruleset = new TaikoRuleset().RulesetInfo);
AddStep("catch ruleset", () => modSelector.Ruleset = new CatchRuleset().RulesetInfo);
AddStep("osu ruleset", () => ruleset.Value = new OsuRuleset().RulesetInfo);
AddStep("mania ruleset", () => ruleset.Value = new ManiaRuleset().RulesetInfo);
AddStep("taiko ruleset", () => ruleset.Value = new TaikoRuleset().RulesetInfo);
AddStep("catch ruleset", () => ruleset.Value = new CatchRuleset().RulesetInfo);
AddStep("Deselect all", () => modSelector.DeselectAll());
AddStep("null ruleset", () => modSelector.Ruleset = null);
AddStep("null ruleset", () => ruleset.Value = null);
}
}
}

View File

@ -20,34 +20,7 @@ namespace osu.Game.Overlays.BeatmapSet
public class LeaderboardModSelector : CompositeDrawable
{
public readonly BindableList<Mod> SelectedMods = new BindableList<Mod>();
private RulesetInfo ruleset;
public RulesetInfo Ruleset
{
get => ruleset;
set
{
if (ruleset?.Equals(value) ?? false)
{
DeselectAll();
return;
}
ruleset = value;
SelectedMods.Clear();
modsContainer.Clear();
if (ruleset == null)
return;
modsContainer.Add(new ModButton(new ModNoMod()));
modsContainer.AddRange(ruleset.CreateInstance().GetAllMods().Where(m => m.Ranked).Select(m => new ModButton(m)));
modsContainer.ForEach(button => button.OnSelectionChanged = selectionChanged);
}
}
public readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
private readonly FillFlowContainer<ModButton> modsContainer;
@ -64,6 +37,26 @@ namespace osu.Game.Overlays.BeatmapSet
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Ruleset.BindValueChanged(onRulesetChanged, true);
}
private void onRulesetChanged(ValueChangedEvent<RulesetInfo> ruleset)
{
SelectedMods.Clear();
modsContainer.Clear();
if (ruleset.NewValue == null)
return;
modsContainer.Add(new ModButton(new ModNoMod()));
modsContainer.AddRange(ruleset.NewValue.CreateInstance().GetAllMods().Where(m => m.Ranked).Select(m => new ModButton(m)));
modsContainer.ForEach(button => button.OnSelectionChanged = selectionChanged);
}
private void selectionChanged(Mod mod, bool selected)
{
if (selected)