Skip to content

Commit

Permalink
Hoist ModState to column level
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed May 11, 2022
1 parent 74599c9 commit e86444c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
5 changes: 5 additions & 0 deletions osu.Game/Overlays/Mods/IncompatibilityDisplayingModPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class IncompatibilityDisplayingModPanel : ModPanel, IHasCustomTooltip<Mod
[Resolved]
private Bindable<IReadOnlyList<Mod>> selectedMods { get; set; }

public IncompatibilityDisplayingModPanel(ModState modState)
: base(modState)
{
}

public IncompatibilityDisplayingModPanel(Mod mod)
: base(mod)
{
Expand Down
21 changes: 11 additions & 10 deletions osu.Game/Overlays/Mods/ModColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,20 +84,20 @@ public Func<Mod, bool>? 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<Dictionary<ModType, IReadOnlyList<Mod>>> availableMods = new Bindable<Dictionary<ModType, IReadOnlyList<Mod>>>();

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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).
/// </remarks>
private IReadOnlyList<Mod> localAvailableMods = Array.Empty<Mod>();
private IReadOnlyList<ModState> localAvailableMods = Array.Empty<ModState>();

private readonly TextFlowContainer headerText;
private readonly Box headerBackground;
Expand Down Expand Up @@ -291,10 +292,10 @@ private void updateToggleAllText()
private void updateLocalAvailableMods(bool asyncLoadContent)
{
var newMods = ModUtils.FlattenMods(availableMods.Value.GetValueOrDefault(ModType) ?? Array.Empty<Mod>())
.Select(m => m.DeepClone())
.Select(m => new ModState(m.DeepClone()))
.ToList();

if (newMods.SequenceEqual(localAvailableMods))
if (newMods.SequenceEqual(localAvailableMods, new FuncEqualityComparer<ModState>((x, y) => ReferenceEquals(x.Mod, y.Mod))))
return;

localAvailableMods = newMods;
Expand Down Expand Up @@ -393,18 +394,18 @@ internal void SetSelection(IReadOnlyList<Mod> mods)

var newSelection = new List<Mod>();

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();
}
}

Expand Down
15 changes: 10 additions & 5 deletions osu.Game/Overlays/Mods/ModPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Overlays/Mods/UserModSelectOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit e86444c

Please sign in to comment.