mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 19:02:57 +08:00
Merge pull request #11664 from smoogipoo/modselect-dynamic-isvalid
Make it possible to change IsValidMod in mod selection
This commit is contained in:
commit
17072a5dda
@ -137,6 +137,34 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
AddAssert("ensure all buttons are spread out", () => modSelect.ChildrenOfType<ModButton>().All(m => m.Mods.Length <= 1));
|
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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestChangeIsValidPreservesSelection()
|
||||||
|
{
|
||||||
|
changeRuleset(0);
|
||||||
|
|
||||||
|
AddStep("select DT + HD", () => SelectedMods.Value = new Mod[] { new OsuModDoubleTime(), new OsuModHidden() });
|
||||||
|
AddAssert("DT + HD selected", () => modSelect.ChildrenOfType<ModButton>().Count(b => b.Selected) == 2);
|
||||||
|
|
||||||
|
AddStep("make NF invalid", () => modSelect.IsValidMod = m => !(m is ModNoFail));
|
||||||
|
AddAssert("DT + HD still selected", () => modSelect.ChildrenOfType<ModButton>().Count(b => b.Selected) == 2);
|
||||||
|
}
|
||||||
|
|
||||||
private void testSingleMod(Mod mod)
|
private void testSingleMod(Mod mod)
|
||||||
{
|
{
|
||||||
selectNext(mod);
|
selectNext(mod);
|
||||||
|
@ -127,13 +127,13 @@ namespace osu.Game.Overlays.Mods
|
|||||||
/// Updates all buttons with the given list of selected mods.
|
/// Updates all buttons with the given list of selected mods.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="newSelectedMods">The new list of selected mods to select.</param>
|
/// <param name="newSelectedMods">The new list of selected mods to select.</param>
|
||||||
public void UpdateSelectedMods(IReadOnlyList<Mod> newSelectedMods)
|
public void UpdateSelectedButtons(IReadOnlyList<Mod> newSelectedMods)
|
||||||
{
|
{
|
||||||
foreach (var button in buttons)
|
foreach (var button in buttons)
|
||||||
updateButtonMods(button, newSelectedMods);
|
updateButtonSelection(button, newSelectedMods);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateButtonMods(ModButton button, IReadOnlyList<Mod> newSelectedMods)
|
private void updateButtonSelection(ModButton button, IReadOnlyList<Mod> newSelectedMods)
|
||||||
{
|
{
|
||||||
foreach (var mod in newSelectedMods)
|
foreach (var mod in newSelectedMods)
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Audio;
|
using osu.Framework.Audio;
|
||||||
using osu.Framework.Audio.Sample;
|
using osu.Framework.Audio.Sample;
|
||||||
@ -30,7 +31,6 @@ namespace osu.Game.Overlays.Mods
|
|||||||
{
|
{
|
||||||
public class ModSelectOverlay : WaveOverlayContainer
|
public class ModSelectOverlay : WaveOverlayContainer
|
||||||
{
|
{
|
||||||
private readonly Func<Mod, bool> isValidMod;
|
|
||||||
public const float HEIGHT = 510;
|
public const float HEIGHT = 510;
|
||||||
|
|
||||||
protected readonly TriangleButton DeselectAllButton;
|
protected readonly TriangleButton DeselectAllButton;
|
||||||
@ -48,6 +48,23 @@ namespace osu.Game.Overlays.Mods
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual bool Stacked => true;
|
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 FillFlowContainer<ModSection> ModSectionsContainer;
|
||||||
|
|
||||||
protected readonly ModSettingsContainer ModSettingsContainer;
|
protected readonly ModSettingsContainer ModSettingsContainer;
|
||||||
@ -66,10 +83,8 @@ namespace osu.Game.Overlays.Mods
|
|||||||
|
|
||||||
private SampleChannel sampleOn, sampleOff;
|
private SampleChannel sampleOn, sampleOff;
|
||||||
|
|
||||||
public ModSelectOverlay(Func<Mod, bool> isValidMod = null)
|
public ModSelectOverlay()
|
||||||
{
|
{
|
||||||
this.isValidMod = isValidMod ?? (m => true);
|
|
||||||
|
|
||||||
Waves.FirstWaveColour = Color4Extensions.FromHex(@"19b0e2");
|
Waves.FirstWaveColour = Color4Extensions.FromHex(@"19b0e2");
|
||||||
Waves.SecondWaveColour = Color4Extensions.FromHex(@"2280a2");
|
Waves.SecondWaveColour = Color4Extensions.FromHex(@"2280a2");
|
||||||
Waves.ThirdWaveColour = Color4Extensions.FromHex(@"005774");
|
Waves.ThirdWaveColour = Color4Extensions.FromHex(@"005774");
|
||||||
@ -368,8 +383,8 @@ namespace osu.Game.Overlays.Mods
|
|||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
|
|
||||||
availableMods.BindValueChanged(availableModsChanged, true);
|
availableMods.BindValueChanged(_ => updateAvailableMods(), true);
|
||||||
SelectedMods.BindValueChanged(selectedModsChanged, true);
|
SelectedMods.BindValueChanged(_ => updateSelectedButtons(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void PopOut()
|
protected override void PopOut()
|
||||||
@ -423,9 +438,10 @@ namespace osu.Game.Overlays.Mods
|
|||||||
|
|
||||||
public override bool OnPressed(GlobalAction action) => false; // handled by back button
|
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)
|
foreach (var section in ModSectionsContainer.Children)
|
||||||
{
|
{
|
||||||
@ -434,14 +450,41 @@ namespace osu.Game.Overlays.Mods
|
|||||||
if (!Stacked)
|
if (!Stacked)
|
||||||
modEnumeration = ModUtils.FlattenMods(modEnumeration);
|
modEnumeration = ModUtils.FlattenMods(modEnumeration);
|
||||||
|
|
||||||
section.Mods = modEnumeration.Where(isValidMod);
|
section.Mods = modEnumeration.Select(getValidModOrNull).Where(m => m != null);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void selectedModsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
|
updateSelectedButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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 updateSelectedButtons()
|
||||||
|
{
|
||||||
|
// Enumeration below may update the bindable list.
|
||||||
|
var selectedMods = SelectedMods.Value.ToList();
|
||||||
|
|
||||||
foreach (var section in ModSectionsContainer.Children)
|
foreach (var section in ModSectionsContainer.Children)
|
||||||
section.UpdateSelectedMods(mods.NewValue);
|
section.UpdateSelectedButtons(selectedMods);
|
||||||
|
|
||||||
updateMods();
|
updateMods();
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
|||||||
|
|
||||||
protected override BeatmapDetailArea CreateBeatmapDetailArea() => new PlayBeatmapDetailArea();
|
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;
|
private bool isValidMod(Mod mod) => !(mod is ModAutoplay) && (mod as MultiMod)?.Mods.Any(mm => mm is ModAutoplay) != true;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ namespace osu.Game.Screens.Select
|
|||||||
item.RequiredMods.AddRange(Mods.Value.Select(m => m.CreateCopy()));
|
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;
|
private bool isValidMod(Mod mod) => !(mod is ModAutoplay) && (mod as MultiMod)?.Mods.Any(mm => mm is ModAutoplay) != true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user