Fix checkbox not being updated

This commit is contained in:
smoogipoo
2021-02-22 15:47:47 +09:00
parent ca92ad715a
commit ccb83ef3a3
3 changed files with 31 additions and 11 deletions

View File

@ -23,13 +23,15 @@ namespace osu.Game.Overlays.Mods
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; } public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
protected IReadOnlyList<ModButton> Buttons { get; private set; } = Array.Empty<ModButton>();
public Action<Mod> Action; public Action<Mod> Action;
public Key[] ToggleKeys; public Key[] ToggleKeys;
public readonly ModType ModType; public readonly ModType ModType;
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null); public IEnumerable<Mod> SelectedMods => Buttons.Select(b => b.SelectedMod).Where(m => m != null);
private CancellationTokenSource modsLoadCts; private CancellationTokenSource modsLoadCts;
@ -77,7 +79,7 @@ namespace osu.Game.Overlays.Mods
ButtonsContainer.ChildrenEnumerable = c; ButtonsContainer.ChildrenEnumerable = c;
}, (modsLoadCts = new CancellationTokenSource()).Token); }, (modsLoadCts = new CancellationTokenSource()).Token);
buttons = modContainers.OfType<ModButton>().ToArray(); Buttons = modContainers.OfType<ModButton>().ToArray();
header.FadeIn(200); header.FadeIn(200);
this.FadeIn(200); this.FadeIn(200);
@ -88,8 +90,6 @@ namespace osu.Game.Overlays.Mods
{ {
} }
private ModButton[] buttons = Array.Empty<ModButton>();
protected override bool OnKeyDown(KeyDownEvent e) protected override bool OnKeyDown(KeyDownEvent e)
{ {
if (e.ControlPressed) return false; if (e.ControlPressed) return false;
@ -97,8 +97,8 @@ namespace osu.Game.Overlays.Mods
if (ToggleKeys != null) if (ToggleKeys != null)
{ {
var index = Array.IndexOf(ToggleKeys, e.Key); var index = Array.IndexOf(ToggleKeys, e.Key);
if (index > -1 && index < buttons.Length) if (index > -1 && index < Buttons.Count)
buttons[index].SelectNext(e.ShiftPressed ? -1 : 1); Buttons[index].SelectNext(e.ShiftPressed ? -1 : 1);
} }
return base.OnKeyDown(e); return base.OnKeyDown(e);
@ -141,7 +141,7 @@ namespace osu.Game.Overlays.Mods
{ {
pendingSelectionOperations.Clear(); pendingSelectionOperations.Clear();
foreach (var button in buttons.Where(b => !b.Selected)) foreach (var button in Buttons.Where(b => !b.Selected))
pendingSelectionOperations.Enqueue(() => button.SelectAt(0)); pendingSelectionOperations.Enqueue(() => button.SelectAt(0));
} }
@ -151,7 +151,7 @@ namespace osu.Game.Overlays.Mods
public void DeselectAll() public void DeselectAll()
{ {
pendingSelectionOperations.Clear(); pendingSelectionOperations.Clear();
DeselectTypes(buttons.Select(b => b.SelectedMod?.GetType()).Where(t => t != null)); DeselectTypes(Buttons.Select(b => b.SelectedMod?.GetType()).Where(t => t != null));
} }
/// <summary> /// <summary>
@ -161,7 +161,7 @@ namespace osu.Game.Overlays.Mods
/// <param name="immediate">Whether the deselection should happen immediately. Should only be used when required to ensure correct selection flow.</param> /// <param name="immediate">Whether the deselection should happen immediately. Should only be used when required to ensure correct selection flow.</param>
public void DeselectTypes(IEnumerable<Type> modTypes, bool immediate = false) public void DeselectTypes(IEnumerable<Type> modTypes, bool immediate = false)
{ {
foreach (var button in buttons) foreach (var button in Buttons)
{ {
if (button.SelectedMod == null) continue; if (button.SelectedMod == null) continue;
@ -184,7 +184,7 @@ namespace osu.Game.Overlays.Mods
/// <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 UpdateSelectedButtons(IReadOnlyList<Mod> newSelectedMods) public void UpdateSelectedButtons(IReadOnlyList<Mod> newSelectedMods)
{ {
foreach (var button in buttons) foreach (var button in Buttons)
updateButtonSelection(button, newSelectedMods); updateButtonSelection(button, newSelectedMods);
} }

View File

@ -456,6 +456,7 @@ namespace osu.Game.Overlays.Mods
} }
updateSelectedButtons(); updateSelectedButtons();
OnAvailableModsChanged();
} }
/// <summary> /// <summary>
@ -533,6 +534,13 @@ namespace osu.Game.Overlays.Mods
private void playSelectedSound() => sampleOn?.Play(); private void playSelectedSound() => sampleOn?.Play();
private void playDeselectedSound() => sampleOff?.Play(); private void playDeselectedSound() => sampleOff?.Play();
/// <summary>
/// Invoked after <see cref="availableMods"/> has changed.
/// </summary>
protected virtual void OnAvailableModsChanged()
{
}
/// <summary> /// <summary>
/// Invoked when a new <see cref="Mod"/> has been selected. /// Invoked when a new <see cref="Mod"/> has been selected.
/// </summary> /// </summary>

View File

@ -75,6 +75,14 @@ namespace osu.Game.Screens.OnlinePlay
section.DeselectAll(); section.DeselectAll();
} }
protected override void OnAvailableModsChanged()
{
base.OnAvailableModsChanged();
foreach (var section in ModSectionsContainer.Children)
((FreeModSection)section).UpdateCheckboxState();
}
protected override ModSection CreateModSection(ModType type) => new FreeModSection(type); protected override ModSection CreateModSection(ModType type) => new FreeModSection(type);
private class FreeModSection : ModSection private class FreeModSection : ModSection
@ -108,10 +116,14 @@ namespace osu.Game.Screens.OnlinePlay
protected override void ModButtonStateChanged(Mod mod) protected override void ModButtonStateChanged(Mod mod)
{ {
base.ModButtonStateChanged(mod); base.ModButtonStateChanged(mod);
UpdateCheckboxState();
}
public void UpdateCheckboxState()
{
if (!SelectionAnimationRunning) if (!SelectionAnimationRunning)
{ {
var validButtons = ButtonsContainer.OfType<ModButton>().Where(b => b.Mod.HasImplementation); var validButtons = Buttons.Where(b => b.Mod.HasImplementation);
checkbox.Current.Value = validButtons.All(b => b.Selected); checkbox.Current.Value = validButtons.All(b => b.Selected);
} }
} }