From 984ec11a78ca071e70d04cd5899ee7b07cfad140 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Thu, 21 Nov 2019 18:56:48 +0300 Subject: [PATCH] Make Ruleset a bindable --- .../Online/TestSceneLeaderboardModSelector.cs | 14 ++++-- .../BeatmapSet/LeaderboardModSelector.cs | 49 ++++++++----------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneLeaderboardModSelector.cs b/osu.Game.Tests/Visual/Online/TestSceneLeaderboardModSelector.cs index c0c18c2583..ebe233a5f4 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneLeaderboardModSelector.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneLeaderboardModSelector.cs @@ -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 selectedMods; + var ruleset = new Bindable(); Add(selectedMods = new FillFlowContainer { @@ -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); } } } diff --git a/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs b/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs index ac8952bebb..ba42e8c310 100644 --- a/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs +++ b/osu.Game/Overlays/BeatmapSet/LeaderboardModSelector.cs @@ -20,34 +20,7 @@ namespace osu.Game.Overlays.BeatmapSet public class LeaderboardModSelector : CompositeDrawable { public readonly BindableList SelectedMods = new BindableList(); - - 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 Ruleset = new Bindable(); private readonly FillFlowContainer modsContainer; @@ -64,6 +37,26 @@ namespace osu.Game.Overlays.BeatmapSet }; } + protected override void LoadComplete() + { + base.LoadComplete(); + Ruleset.BindValueChanged(onRulesetChanged, true); + } + + private void onRulesetChanged(ValueChangedEvent 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)