mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 16:32:54 +08:00
Fix mods sharing bindable instances
This commit is contained in:
parent
1c771471d5
commit
3cb22fad82
@ -8,6 +8,7 @@ using NUnit.Framework;
|
|||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Framework.Utils;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Graphics.UserInterface;
|
using osu.Game.Graphics.UserInterface;
|
||||||
@ -15,6 +16,7 @@ using osu.Game.Overlays.Mods;
|
|||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Rulesets.Difficulty;
|
using osu.Game.Rulesets.Difficulty;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
|
using osu.Game.Rulesets.Osu.Mods;
|
||||||
using osu.Game.Rulesets.UI;
|
using osu.Game.Rulesets.UI;
|
||||||
|
|
||||||
namespace osu.Game.Tests.Visual.UserInterface
|
namespace osu.Game.Tests.Visual.UserInterface
|
||||||
@ -75,6 +77,24 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
AddAssert("Customisation closed", () => modSelect.ModSettingsContainer.Alpha == 0);
|
AddAssert("Customisation closed", () => modSelect.ModSettingsContainer.Alpha == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestModSettingsUnboundWhenCopied()
|
||||||
|
{
|
||||||
|
OsuModDoubleTime original = null;
|
||||||
|
OsuModDoubleTime copy = null;
|
||||||
|
|
||||||
|
AddStep("create mods", () =>
|
||||||
|
{
|
||||||
|
original = new OsuModDoubleTime();
|
||||||
|
copy = (OsuModDoubleTime)original.CreateCopy();
|
||||||
|
});
|
||||||
|
|
||||||
|
AddStep("change property", () => original.SpeedChange.Value = 2);
|
||||||
|
|
||||||
|
AddAssert("original has new value", () => Precision.AlmostEquals(2.0, original.SpeedChange.Value));
|
||||||
|
AddAssert("copy has original value", () => Precision.AlmostEquals(1.5, copy.SpeedChange.Value));
|
||||||
|
}
|
||||||
|
|
||||||
private void createModSelect()
|
private void createModSelect()
|
||||||
{
|
{
|
||||||
AddStep("create mod select", () =>
|
AddStep("create mod select", () =>
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@ -126,7 +127,25 @@ namespace osu.Game.Rulesets.Mods
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
/// Creates a copy of this <see cref="Mod"/> initialised to a default state.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual Mod CreateCopy() => (Mod)MemberwiseClone();
|
public virtual Mod CreateCopy()
|
||||||
|
{
|
||||||
|
var copy = (Mod)Activator.CreateInstance(GetType());
|
||||||
|
|
||||||
|
// Copy bindable values across
|
||||||
|
foreach (var (_, prop) in this.GetSettingsSourceProperties())
|
||||||
|
{
|
||||||
|
var origBindable = prop.GetValue(this);
|
||||||
|
var copyBindable = prop.GetValue(copy);
|
||||||
|
|
||||||
|
// The bindables themselves are readonly, so the value must be transferred through the Bindable<T>.Value property.
|
||||||
|
var valueProperty = origBindable.GetType().GetProperty(nameof(Bindable<object>.Value), BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
Debug.Assert(valueProperty != null);
|
||||||
|
|
||||||
|
valueProperty.SetValue(copyBindable, valueProperty.GetValue(origBindable));
|
||||||
|
}
|
||||||
|
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
public bool Equals(IMod other) => GetType() == other?.GetType();
|
public bool Equals(IMod other) => GetType() == other?.GetType();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user