Skip to content

Commit

Permalink
Refactor mod select button initialisation to allow shared usage of de…
Browse files Browse the repository at this point in the history
…select button
  • Loading branch information
peppy committed May 14, 2022
1 parent 282c8ae commit 8a01050
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
43 changes: 25 additions & 18 deletions osu.Game/Overlays/Mods/ModSelectOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,32 @@ public Func<Mod, bool> IsValidMod
/// </summary>
protected virtual bool ShowTotalMultiplier => true;

/// <summary>
/// Whether per-mod customisation controls are visible.
/// </summary>
protected virtual bool AllowCustomisation => true;

protected virtual ModColumn CreateModColumn(ModType modType, Key[]? toggleKeys = null) => new ModColumn(modType, false, toggleKeys);

protected virtual IReadOnlyList<Mod> ComputeNewModsFromSelection(IReadOnlyList<Mod> oldSelection, IReadOnlyList<Mod> newSelection) => newSelection;

protected virtual IEnumerable<ShearedButton> CreateFooterButtons() => createDefaultFooterButtons();
protected virtual IEnumerable<ShearedButton> CreateFooterButtons()
{
if (AllowCustomisation)
{
yield return customisationButton = new ShearedToggleButton(BUTTON_WIDTH)
{
Text = ModSelectOverlayStrings.ModCustomisation,
Active = { BindTarget = customisationVisible }
};
}

yield return deselectAllButton = new ShearedButton(BUTTON_WIDTH)
{
Text = CommonStrings.DeselectAll,
Action = DeselectAll
};
}

private readonly Bindable<Dictionary<ModType, IReadOnlyList<Mod>>> availableMods = new Bindable<Dictionary<ModType, IReadOnlyList<Mod>>>();
private readonly Dictionary<ModType, IReadOnlyList<ModState>> localAvailableMods = new Dictionary<ModType, IReadOnlyList<ModState>>();
Expand All @@ -77,6 +98,7 @@ public Func<Mod, bool> IsValidMod
private DifficultyMultiplierDisplay? multiplierDisplay;

private ShearedToggleButton? customisationButton;
private ShearedButton? deselectAllButton;

protected ModSelectOverlay(OverlayColourScheme colourScheme = OverlayColourScheme.Green)
: base(colourScheme)
Expand Down Expand Up @@ -201,7 +223,7 @@ protected override void LoadComplete()

// This is an optimisation to prevent refreshing the available settings controls when it can be
// reasonably assumed that the settings panel is never to be displayed (e.g. FreeModSelectOverlay).
if (customisationButton != null)
if (AllowCustomisation)
((IBindable<IReadOnlyList<Mod>>)modSettingsArea.SelectedMods).BindTo(SelectedMods);

SelectedMods.BindValueChanged(val =>
Expand Down Expand Up @@ -256,21 +278,6 @@ private ColumnDimContainer createModColumnContent(ModType modType, Key[]? toggle
};
}

private ShearedButton[] createDefaultFooterButtons()
=> new[]
{
customisationButton = new ShearedToggleButton(BUTTON_WIDTH)
{
Text = ModSelectOverlayStrings.ModCustomisation,
Active = { BindTarget = customisationVisible }
},
new ShearedButton(BUTTON_WIDTH)
{
Text = CommonStrings.DeselectAll,
Action = DeselectAll
}
};

private void createLocalMods()
{
localAvailableMods.Clear();
Expand Down Expand Up @@ -510,7 +517,7 @@ public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
}

case GlobalAction.DeselectAllMods:
DeselectAll();
deselectAllButton?.TriggerClick();
return true;
}

Expand Down
16 changes: 5 additions & 11 deletions osu.Game/Screens/OnlinePlay/FreeModSelectOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using osu.Game.Overlays;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Mods;
Expand All @@ -17,6 +18,8 @@ public class FreeModSelectOverlay : ModSelectOverlay
{
protected override bool ShowTotalMultiplier => false;

protected override bool AllowCustomisation => false;

public new Func<Mod, bool> IsValidMod
{
get => base.IsValidMod;
Expand All @@ -31,22 +34,13 @@ public FreeModSelectOverlay()

protected override ModColumn CreateModColumn(ModType modType, Key[] toggleKeys = null) => new ModColumn(modType, true, toggleKeys);

protected override IEnumerable<ShearedButton> CreateFooterButtons() => new[]
{
protected override IEnumerable<ShearedButton> CreateFooterButtons() => base.CreateFooterButtons().Prepend(
new ShearedButton(BUTTON_WIDTH)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Text = CommonStrings.SelectAll,
Action = SelectAll
},
new ShearedButton(BUTTON_WIDTH)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Text = CommonStrings.DeselectAll,
Action = DeselectAll
}
};
});
}
}

0 comments on commit 8a01050

Please sign in to comment.