1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 12:33:01 +08:00

Make IsValidMod adjustable

This commit is contained in:
smoogipoo 2021-02-02 20:47:50 +09:00
parent 75f81bfa06
commit 10ceddf3ff
4 changed files with 64 additions and 10 deletions

View File

@ -137,6 +137,22 @@ namespace osu.Game.Tests.Visual.UserInterface
AddAssert("ensure all buttons are spread out", () => modSelect.ChildrenOfType<ModButton>().All(m => m.Mods.Length <= 1));
}
[Test]
public void TestChangeIsValidChangesButtonVisibility()
{
changeRuleset(0);
AddAssert("double time visible", () => modSelect.ChildrenOfType<ModButton>().Any(b => b.Mods.Any(m => m is OsuModDoubleTime)));
AddStep("make double time invalid", () => modSelect.IsValidMod = m => !(m is OsuModDoubleTime));
AddAssert("double time not visible", () => modSelect.ChildrenOfType<ModButton>().All(b => !b.Mods.Any(m => m is OsuModDoubleTime)));
AddAssert("nightcore still visible", () => modSelect.ChildrenOfType<ModButton>().Any(b => b.Mods.Any(m => m is OsuModNightcore)));
AddStep("make double time valid again", () => modSelect.IsValidMod = m => true);
AddAssert("double time visible", () => modSelect.ChildrenOfType<ModButton>().Any(b => b.Mods.Any(m => m is OsuModDoubleTime)));
AddAssert("nightcore still visible", () => modSelect.ChildrenOfType<ModButton>().Any(b => b.Mods.Any(m => m is OsuModNightcore)));
}
private void testSingleMod(Mod mod)
{
selectNext(mod);

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
@ -31,7 +32,6 @@ namespace osu.Game.Overlays.Mods
{
public class ModSelectOverlay : WaveOverlayContainer
{
private readonly Func<Mod, bool> isValidMod;
public const float HEIGHT = 510;
protected readonly TriangleButton DeselectAllButton;
@ -49,6 +49,23 @@ namespace osu.Game.Overlays.Mods
/// </summary>
protected virtual bool Stacked => true;
[NotNull]
private Func<Mod, bool> isValidMod = m => true;
/// <summary>
/// A function that checks whether a given mod is selectable.
/// </summary>
[NotNull]
public Func<Mod, bool> IsValidMod
{
get => isValidMod;
set
{
isValidMod = value ?? throw new ArgumentNullException(nameof(value));
updateAvailableMods();
}
}
protected readonly FillFlowContainer<ModSection> ModSectionsContainer;
protected readonly ModSettingsContainer ModSettingsContainer;
@ -67,10 +84,8 @@ namespace osu.Game.Overlays.Mods
private SampleChannel sampleOn, sampleOff;
public ModSelectOverlay(Func<Mod, bool> isValidMod = null)
public ModSelectOverlay()
{
this.isValidMod = isValidMod ?? (m => true);
Waves.FirstWaveColour = Color4Extensions.FromHex(@"19b0e2");
Waves.SecondWaveColour = Color4Extensions.FromHex(@"2280a2");
Waves.ThirdWaveColour = Color4Extensions.FromHex(@"005774");
@ -351,7 +366,7 @@ namespace osu.Game.Overlays.Mods
{
base.LoadComplete();
availableMods.BindValueChanged(availableModsChanged, true);
availableMods.BindValueChanged(_ => updateAvailableMods(), true);
SelectedMods.BindValueChanged(selectedModsChanged, true);
}
@ -406,9 +421,10 @@ namespace osu.Game.Overlays.Mods
public override bool OnPressed(GlobalAction action) => false; // handled by back button
private void availableModsChanged(ValueChangedEvent<Dictionary<ModType, IReadOnlyList<Mod>>> mods)
private void updateAvailableMods()
{
if (mods.NewValue == null) return;
if (availableMods?.Value == null)
return;
foreach (var section in ModSectionsContainer.Children)
{
@ -417,10 +433,32 @@ namespace osu.Game.Overlays.Mods
if (!Stacked)
modEnumeration = ModUtils.FlattenMods(modEnumeration);
section.Mods = modEnumeration.Where(isValidMod);
section.Mods = modEnumeration.Select(getValidModOrNull).Where(m => m != null);
}
}
/// <summary>
/// Returns a valid form of a given <see cref="Mod"/> if possible, or null otherwise.
/// </summary>
/// <remarks>
/// This is a recursive process during which any invalid mods are culled while preserving <see cref="MultiMod"/> structures where possible.
/// </remarks>
/// <param name="mod">The <see cref="Mod"/> to check.</param>
/// <returns>A valid form of <paramref name="mod"/> if exists, or null otherwise.</returns>
[CanBeNull]
private Mod getValidModOrNull([NotNull] Mod mod)
{
if (!(mod is MultiMod multi))
return IsValidMod(mod) ? mod : null;
var validSubset = multi.Mods.Select(getValidModOrNull).Where(m => m != null).ToArray();
if (validSubset.Length == 0)
return null;
return validSubset.Length == 1 ? validSubset[0] : new MultiMod(validSubset);
}
private void selectedModsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{
foreach (var section in ModSectionsContainer.Children)

View File

@ -111,7 +111,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
protected override BeatmapDetailArea CreateBeatmapDetailArea() => new PlayBeatmapDetailArea();
protected override ModSelectOverlay CreateModSelectOverlay() => new ModSelectOverlay(isValidMod);
protected override ModSelectOverlay CreateModSelectOverlay() => new ModSelectOverlay { IsValidMod = isValidMod };
private bool isValidMod(Mod mod) => !(mod is ModAutoplay) && (mod as MultiMod)?.Mods.Any(mm => mm is ModAutoplay) != true;
}

View File

@ -81,7 +81,7 @@ namespace osu.Game.Screens.Select
item.RequiredMods.AddRange(Mods.Value.Select(m => m.CreateCopy()));
}
protected override ModSelectOverlay CreateModSelectOverlay() => new ModSelectOverlay(isValidMod);
protected override ModSelectOverlay CreateModSelectOverlay() => new ModSelectOverlay { IsValidMod = isValidMod };
private bool isValidMod(Mod mod) => !(mod is ModAutoplay) && (mod as MultiMod)?.Mods.Any(mm => mm is ModAutoplay) != true;
}