From 7b8bd7f21c6b1b7f2b36cde8ce57f46b0e9d9f55 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 7 Aug 2018 14:49:44 +0900 Subject: [PATCH] Fix mod selection not restoring when re-entering song select --- osu.Game.Tests/Visual/TestCaseMods.cs | 3 + osu.Game/OsuGame.cs | 5 +- osu.Game/Overlays/Mods/ModSelectOverlay.cs | 70 ++++++++++++---------- osu.Game/Screens/Select/PlaySongSelect.cs | 15 +++-- 4 files changed, 54 insertions(+), 39 deletions(-) diff --git a/osu.Game.Tests/Visual/TestCaseMods.cs b/osu.Game.Tests/Visual/TestCaseMods.cs index cc396a63e3..6330afe860 100644 --- a/osu.Game.Tests/Visual/TestCaseMods.cs +++ b/osu.Game.Tests/Visual/TestCaseMods.cs @@ -13,6 +13,7 @@ using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Osu.Mods; using System.Linq; using System.Collections.Generic; +using osu.Framework.Configuration; using osu.Game.Rulesets.Osu; using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.Sprites; @@ -237,6 +238,8 @@ namespace osu.Game.Tests.Visual private class TestModSelectOverlay : ModSelectOverlay { + public new Bindable> SelectedMods => base.SelectedMods; + public ModButton GetModButton(Mod mod) { var section = ModSectionsContainer.Children.Single(s => s.ModType == mod.Type); diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 025d5f50e3..69d1236126 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -99,7 +99,7 @@ namespace osu.Game private readonly List overlays = new List(); // todo: move this to SongSelect once Screen has the ability to unsuspend. - public readonly Bindable> SelectedMods = new Bindable>(new List()); + private readonly Bindable> selectedMods = new Bindable>(new List()); public OsuGame(string[] args = null) { @@ -153,6 +153,9 @@ namespace osu.Game dependencies.CacheAs(ruleset); dependencies.CacheAs>(ruleset); + dependencies.CacheAs(selectedMods); + dependencies.CacheAs>>(selectedMods); + // bind config int to database RulesetInfo configRuleset = LocalConfig.GetBindable(OsuSetting.Ruleset); ruleset.Value = RulesetStore.GetRuleset(configRuleset.Value) ?? RulesetStore.AvailableRulesets.First(); diff --git a/osu.Game/Overlays/Mods/ModSelectOverlay.cs b/osu.Game/Overlays/Mods/ModSelectOverlay.cs index 4745eba68d..3f28b7ccfd 100644 --- a/osu.Game/Overlays/Mods/ModSelectOverlay.cs +++ b/osu.Game/Overlays/Mods/ModSelectOverlay.cs @@ -39,9 +39,39 @@ namespace osu.Game.Overlays.Mods protected readonly FillFlowContainer ModSectionsContainer; - public readonly Bindable> SelectedMods = new Bindable>(); + protected readonly Bindable> SelectedMods = new Bindable>(); - public readonly IBindable Ruleset = new Bindable(); + protected readonly IBindable Ruleset = new Bindable(); + + [BackgroundDependencyLoader] + private void load(OsuColour colours, IBindable ruleset, AudioManager audio, Bindable> selectedMods) + { + LowMultiplierColour = colours.Red; + HighMultiplierColour = colours.Green; + UnrankedLabel.Colour = colours.Blue; + + Ruleset.BindTo(ruleset); + SelectedMods.BindTo(selectedMods); + + sampleOn = audio.Sample.Get(@"UI/check-on"); + sampleOff = audio.Sample.Get(@"UI/check-off"); + } + + protected override void LoadComplete() + { + base.LoadComplete(); + + Ruleset.BindValueChanged(rulesetChanged, true); + SelectedMods.BindValueChanged(selectedModsChanged, true); + } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + + Ruleset.UnbindAll(); + SelectedMods.UnbindAll(); + } private void rulesetChanged(RulesetInfo newRuleset) { @@ -51,33 +81,16 @@ namespace osu.Game.Overlays.Mods foreach (ModSection section in ModSectionsContainer.Children) section.Mods = instance.GetModsFor(section.ModType); + + // attempt to re-select any already selected mods. + // this may be the first time we are receiving the ruleset, in which case they will still match. + selectedModsChanged(SelectedMods.Value); + + // write the mods back to the SelectedMods bindable in the case a change was not applicable. + // this generally isn't required as the previous line will perform deselection; just here for safety. refreshSelectedMods(); } - [BackgroundDependencyLoader] - private void load(OsuColour colours, IBindable ruleset, AudioManager audio) - { - SelectedMods.ValueChanged += selectedModsChanged; - - LowMultiplierColour = colours.Red; - HighMultiplierColour = colours.Green; - UnrankedLabel.Colour = colours.Blue; - - Ruleset.BindTo(ruleset); - Ruleset.BindValueChanged(rulesetChanged, true); - - sampleOn = audio.Sample.Get(@"UI/check-on"); - sampleOff = audio.Sample.Get(@"UI/check-off"); - } - - protected override void Dispose(bool isDisposing) - { - base.Dispose(isDisposing); - - Ruleset.UnbindAll(); - SelectedMods.UnbindAll(); - } - private void selectedModsChanged(IEnumerable obj) { foreach (ModSection section in ModSectionsContainer.Children) @@ -176,10 +189,7 @@ namespace osu.Game.Overlays.Mods refreshSelectedMods(); } - private void refreshSelectedMods() - { - SelectedMods.Value = ModSectionsContainer.Children.SelectMany(s => s.SelectedMods).ToArray(); - } + private void refreshSelectedMods() => SelectedMods.Value = ModSectionsContainer.Children.SelectMany(s => s.SelectedMods).ToArray(); public ModSelectOverlay() { diff --git a/osu.Game/Screens/Select/PlaySongSelect.cs b/osu.Game/Screens/Select/PlaySongSelect.cs index a346911ca2..7a832caf41 100644 --- a/osu.Game/Screens/Select/PlaySongSelect.cs +++ b/osu.Game/Screens/Select/PlaySongSelect.cs @@ -50,13 +50,12 @@ namespace osu.Game.Screens.Select private SampleChannel sampleConfirm; - public readonly Bindable> SelectedMods = new Bindable>(new List()); + private readonly Bindable> selectedMods = new Bindable>(new List()); [BackgroundDependencyLoader(true)] - private void load(OsuColour colours, AudioManager audio, BeatmapManager beatmaps, DialogOverlay dialogOverlay, OsuGame osu) + private void load(OsuColour colours, AudioManager audio, BeatmapManager beatmaps, DialogOverlay dialogOverlay, Bindable> selectedMods) { - if (osu != null) SelectedMods.BindTo(osu.SelectedMods); - modSelect.SelectedMods.BindTo(SelectedMods); + if (selectedMods != null) this.selectedMods.BindTo(selectedMods); sampleConfirm = audio.Sample.Get(@"SongSelect/confirm-selection"); @@ -84,7 +83,7 @@ namespace osu.Game.Screens.Select protected override void UpdateBeatmap(WorkingBeatmap beatmap) { - beatmap.Mods.BindTo(SelectedMods); + beatmap.Mods.BindTo(selectedMods); base.UpdateBeatmap(beatmap); @@ -131,7 +130,7 @@ namespace osu.Game.Screens.Select if (Beatmap.Value.Track != null) Beatmap.Value.Track.Looping = false; - SelectedMods.UnbindAll(); + selectedMods.UnbindAll(); Beatmap.Value.Mods.Value = new Mod[] { }; return false; @@ -147,10 +146,10 @@ namespace osu.Game.Screens.Select var auto = Ruleset.Value.CreateInstance().GetAutoplayMod(); var autoType = auto.GetType(); - var mods = modSelect.SelectedMods.Value; + var mods = selectedMods.Value; if (mods.All(m => m.GetType() != autoType)) { - modSelect.SelectedMods.Value = mods.Append(auto); + selectedMods.Value = mods.Append(auto); removeAutoModOnResume = true; } }