From e86444c4bf433a34adce2a62f3f0eae9fc9a12ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 11 May 2022 18:37:31 +0200 Subject: [PATCH] Hoist `ModState` to column level --- .../Mods/IncompatibilityDisplayingModPanel.cs | 5 +++++ osu.Game/Overlays/Mods/ModColumn.cs | 21 ++++++++++--------- osu.Game/Overlays/Mods/ModPanel.cs | 15 ++++++++----- .../Overlays/Mods/UserModSelectOverlay.cs | 2 +- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/osu.Game/Overlays/Mods/IncompatibilityDisplayingModPanel.cs b/osu.Game/Overlays/Mods/IncompatibilityDisplayingModPanel.cs index aeb983d352..34c4458a21 100644 --- a/osu.Game/Overlays/Mods/IncompatibilityDisplayingModPanel.cs +++ b/osu.Game/Overlays/Mods/IncompatibilityDisplayingModPanel.cs @@ -19,6 +19,11 @@ public class IncompatibilityDisplayingModPanel : ModPanel, IHasCustomTooltip> selectedMods { get; set; } + public IncompatibilityDisplayingModPanel(ModState modState) + : base(modState) + { + } + public IncompatibilityDisplayingModPanel(Mod mod) : base(mod) { diff --git a/osu.Game/Overlays/Mods/ModColumn.cs b/osu.Game/Overlays/Mods/ModColumn.cs index b32ebb4a5c..c5364fb403 100644 --- a/osu.Game/Overlays/Mods/ModColumn.cs +++ b/osu.Game/Overlays/Mods/ModColumn.cs @@ -20,6 +20,7 @@ using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Sprites; using osu.Framework.Input.Events; +using osu.Framework.Lists; using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.UserInterface; @@ -83,20 +84,20 @@ public Func? Filter protected override bool ReceivePositionalInputAtSubTree(Vector2 screenSpacePos) => base.ReceivePositionalInputAtSubTree(screenSpacePos) && Active.Value; - protected virtual ModPanel CreateModPanel(Mod mod) => new ModPanel(mod); + protected virtual ModPanel CreateModPanel(ModState mod) => new ModPanel(mod); private readonly Key[]? toggleKeys; private readonly Bindable>> availableMods = new Bindable>>(); /// - /// All mods that are available for the current ruleset in this particular column. + /// Contains information about state of all mods that are available for the current ruleset in this particular column. /// /// /// Note that the mod instances in this list are owned solely by this column /// (as in, they are locally-managed clones, to ensure proper isolation from any other external instances). /// - private IReadOnlyList localAvailableMods = Array.Empty(); + private IReadOnlyList localAvailableMods = Array.Empty(); private readonly TextFlowContainer headerText; private readonly Box headerBackground; @@ -291,10 +292,10 @@ private void updateToggleAllText() private void updateLocalAvailableMods(bool asyncLoadContent) { var newMods = ModUtils.FlattenMods(availableMods.Value.GetValueOrDefault(ModType) ?? Array.Empty()) - .Select(m => m.DeepClone()) + .Select(m => new ModState(m.DeepClone())) .ToList(); - if (newMods.SequenceEqual(localAvailableMods)) + if (newMods.SequenceEqual(localAvailableMods, new FuncEqualityComparer((x, y) => ReferenceEquals(x.Mod, y.Mod)))) return; localAvailableMods = newMods; @@ -393,18 +394,18 @@ internal void SetSelection(IReadOnlyList mods) var newSelection = new List(); - foreach (var mod in localAvailableMods) + foreach (var modState in localAvailableMods) { - var matchingSelectedMod = mods.SingleOrDefault(selected => selected.GetType() == mod.GetType()); + var matchingSelectedMod = mods.SingleOrDefault(selected => selected.GetType() == modState.GetType()); if (matchingSelectedMod != null) { - mod.CopyFrom(matchingSelectedMod); - newSelection.Add(mod); + modState.Mod.CopyFrom(matchingSelectedMod); + newSelection.Add(modState.Mod); } else { - mod.ResetSettingsToDefaults(); + modState.Mod.ResetSettingsToDefaults(); } } diff --git a/osu.Game/Overlays/Mods/ModPanel.cs b/osu.Game/Overlays/Mods/ModPanel.cs index 02b8c195ec..7010342bd8 100644 --- a/osu.Game/Overlays/Mods/ModPanel.cs +++ b/osu.Game/Overlays/Mods/ModPanel.cs @@ -57,9 +57,9 @@ public class ModPanel : OsuClickableContainer private Sample? sampleOff; private Sample? sampleOn; - public ModPanel(Mod mod) + public ModPanel(ModState modState) { - modState = new ModState(mod); + this.modState = modState; RelativeSizeAxes = Axes.X; Height = 42; @@ -81,7 +81,7 @@ public ModPanel(Mod mod) SwitchContainer = new Container { RelativeSizeAxes = Axes.Y, - Child = new ModSwitchSmall(mod) + Child = new ModSwitchSmall(Mod) { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -117,7 +117,7 @@ public ModPanel(Mod mod) { new OsuSpriteText { - Text = mod.Name, + Text = Mod.Name, Font = OsuFont.TorusAlternate.With(size: 18, weight: FontWeight.SemiBold), Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0), Margin = new MarginPadding @@ -127,7 +127,7 @@ public ModPanel(Mod mod) }, new OsuSpriteText { - Text = mod.Description, + Text = Mod.Description, Font = OsuFont.Default.With(size: 12), RelativeSizeAxes = Axes.X, Truncate = true, @@ -143,6 +143,11 @@ public ModPanel(Mod mod) Action = Active.Toggle; } + public ModPanel(Mod mod) + : this(new ModState(mod)) + { + } + [BackgroundDependencyLoader(true)] private void load(AudioManager audio, OsuColour colours, ISamplePlaybackDisabler? samplePlaybackDisabler) { diff --git a/osu.Game/Overlays/Mods/UserModSelectOverlay.cs b/osu.Game/Overlays/Mods/UserModSelectOverlay.cs index 8ff5e28c8f..7100446730 100644 --- a/osu.Game/Overlays/Mods/UserModSelectOverlay.cs +++ b/osu.Game/Overlays/Mods/UserModSelectOverlay.cs @@ -47,7 +47,7 @@ public UserModColumn(ModType modType, bool allowBulkSelection, [CanBeNull] Key[] { } - protected override ModPanel CreateModPanel(Mod mod) => new IncompatibilityDisplayingModPanel(mod); + protected override ModPanel CreateModPanel(ModState modState) => new IncompatibilityDisplayingModPanel(modState); } } }