1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 08:02:55 +08:00

Merge pull request #11386 from frenzibyte/fix-mod-buttons-not-copying-settings

This commit is contained in:
Bartłomiej Dach 2021-01-10 16:37:25 +01:00 committed by GitHub
commit b79e8d0192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 18 deletions

View File

@ -131,6 +131,18 @@ namespace osu.Game.Tests.Visual.UserInterface
AddAssert("ensure mods not selected", () => modDisplay.Current.Value.Count == 0); AddAssert("ensure mods not selected", () => modDisplay.Current.Value.Count == 0);
} }
[Test]
public void TestExternallySetCustomizedMod()
{
AddStep("set customized mod externally", () => SelectedMods.Value = new[] { new OsuModDoubleTime { SpeedChange = { Value = 1.01 } } });
AddAssert("ensure button is selected and customized accordingly", () =>
{
var button = modSelect.GetModButton(SelectedMods.Value.Single());
return ((OsuModDoubleTime)button.SelectedMod).SpeedChange.Value == 1.01;
});
}
private void testSingleMod(Mod mod) private void testSingleMod(Mod mod)
{ {
selectNext(mod); selectNext(mod);

View File

@ -127,20 +127,30 @@ namespace osu.Game.Overlays.Mods
} }
/// <summary> /// <summary>
/// Select one or more mods in this section and deselects all other ones. /// Updates all buttons with the given list of selected mods.
/// </summary> /// </summary>
/// <param name="modTypes">The types of <see cref="Mod"/>s which should be selected.</param> /// <param name="newSelectedMods">The new list of selected mods to select.</param>
public void SelectTypes(IEnumerable<Type> modTypes) public void UpdateSelectedMods(IReadOnlyList<Mod> newSelectedMods)
{ {
foreach (var button in buttons) foreach (var button in buttons)
{ updateButtonMods(button, newSelectedMods);
int i = Array.FindIndex(button.Mods, m => modTypes.Any(t => t == m.GetType())); }
if (i >= 0) private void updateButtonMods(ModButton button, IReadOnlyList<Mod> newSelectedMods)
button.SelectAt(i); {
else foreach (var mod in newSelectedMods)
button.Deselect(); {
var index = Array.FindIndex(button.Mods, m1 => mod.GetType() == m1.GetType());
if (index < 0)
continue;
var buttonMod = button.Mods[index];
buttonMod.CopyFrom(mod);
button.SelectAt(index);
return;
} }
button.Deselect();
} }
protected ModSection() protected ModSection()

View File

@ -409,7 +409,7 @@ namespace osu.Game.Overlays.Mods
private void selectedModsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods) private void selectedModsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{ {
foreach (var section in ModSectionsContainer.Children) foreach (var section in ModSectionsContainer.Children)
section.SelectTypes(mods.NewValue.Select(m => m.GetType()).ToList()); section.UpdateSelectedMods(mods.NewValue);
updateMods(); updateMods();
} }

View File

@ -128,20 +128,29 @@ namespace osu.Game.Rulesets.Mods
/// </summary> /// </summary>
public virtual Mod CreateCopy() public virtual Mod CreateCopy()
{ {
var copy = (Mod)Activator.CreateInstance(GetType()); var result = (Mod)Activator.CreateInstance(GetType());
result.CopyFrom(this);
return result;
}
/// <summary>
/// Copies mod setting values from <paramref name="source"/> into this instance.
/// </summary>
/// <param name="source">The mod to copy properties from.</param>
public void CopyFrom(Mod source)
{
if (source.GetType() != GetType())
throw new ArgumentException($"Expected mod of type {GetType()}, got {source.GetType()}.", nameof(source));
// Copy bindable values across
foreach (var (_, prop) in this.GetSettingsSourceProperties()) foreach (var (_, prop) in this.GetSettingsSourceProperties())
{ {
var origBindable = (IBindable)prop.GetValue(this); var targetBindable = (IBindable)prop.GetValue(this);
var copyBindable = (IBindable)prop.GetValue(copy); var sourceBindable = (IBindable)prop.GetValue(source);
// we only care about changes that have been made away from defaults. // we only care about changes that have been made away from defaults.
if (!origBindable.IsDefault) if (!sourceBindable.IsDefault)
copy.CopyAdjustedSetting(copyBindable, origBindable); CopyAdjustedSetting(targetBindable, sourceBindable);
} }
return copy;
} }
/// <summary> /// <summary>