Refactor mod panel selection logic to avoid overwriting

This commit is contained in:
Bartłomiej Dach
2022-08-16 22:41:32 +02:00
parent a494e55d93
commit 6bfdfeb153
3 changed files with 35 additions and 21 deletions

View File

@ -37,8 +37,6 @@ namespace osu.Game.Overlays.Mods
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0), Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Scale = new Vector2(HEIGHT / ModSwitchSmall.DEFAULT_SIZE) Scale = new Vector2(HEIGHT / ModSwitchSmall.DEFAULT_SIZE)
}; };
Action = select;
} }
public ModPanel(Mod mod) public ModPanel(Mod mod)
@ -59,19 +57,17 @@ namespace osu.Game.Overlays.Mods
Filtered.BindValueChanged(_ => updateFilterState(), true); Filtered.BindValueChanged(_ => updateFilterState(), true);
} }
private void select() protected override void Select()
{
if (!Active.Value)
{ {
modState.RequiresConfiguration = Mod.RequiresConfiguration; modState.RequiresConfiguration = Mod.RequiresConfiguration;
Active.Value = true; Active.Value = true;
} }
else
protected override void Deselect()
{ {
modState.RequiresConfiguration = false; modState.RequiresConfiguration = false;
Active.Value = false; Active.Value = false;
} }
}
#region Filtering support #region Filtering support

View File

@ -37,8 +37,6 @@ namespace osu.Game.Overlays.Mods
Title = preset.Value.Name; Title = preset.Value.Name;
Description = preset.Value.Description; Description = preset.Value.Description;
Action = toggleRequestedByUser;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -54,15 +52,19 @@ namespace osu.Game.Overlays.Mods
selectedMods.BindValueChanged(_ => selectedModsChanged(), true); selectedMods.BindValueChanged(_ => selectedModsChanged(), true);
} }
private void toggleRequestedByUser() protected override void Select()
{
// if the preset is not active at the point of the user click, then set the mods using the preset directly, discarding any previous selections,
// which will also have the side effect of activating the preset (see `updateActiveState()`).
selectedMods.Value = Preset.Value.Mods.ToArray();
}
protected override void Deselect()
{ {
// if the preset is not active at the point of the user click, then set the mods using the preset directly, discarding any previous selections.
// if the preset is active when the user has clicked it, then it means that the set of active mods is exactly equal to the set of mods in the preset // if the preset is active when the user has clicked it, then it means that the set of active mods is exactly equal to the set of mods in the preset
// (there are no other active mods than what the preset specifies, and the mod settings match exactly). // (there are no other active mods than what the preset specifies, and the mod settings match exactly).
// therefore it's safe to just clear selected mods, since it will have the effect of toggling the preset off. // therefore it's safe to just clear selected mods, since it will have the effect of toggling the preset off.
selectedMods.Value = !Active.Value selectedMods.Value = Array.Empty<Mod>();
? Preset.Value.Mods.ToArray()
: Array.Empty<Mod>();
} }
private void selectedModsChanged() private void selectedModsChanged()

View File

@ -143,9 +143,25 @@ namespace osu.Game.Overlays.Mods
} }
}; };
Action = () => Active.Toggle(); Action = () =>
{
if (!Active.Value)
Select();
else
Deselect();
};
} }
/// <summary>
/// Performs all actions necessary to select this <see cref="ModSelectPanel"/>.
/// </summary>
protected abstract void Select();
/// <summary>
/// Performs all actions necessary to deselect this <see cref="ModSelectPanel"/>.
/// </summary>
protected abstract void Deselect();
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio, ISamplePlaybackDisabler? samplePlaybackDisabler) private void load(AudioManager audio, ISamplePlaybackDisabler? samplePlaybackDisabler)
{ {