1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Big simplifying

This commit is contained in:
Terochi 2023-03-11 23:29:36 +01:00
parent b51c41a804
commit 8e8dda3ac0
3 changed files with 19 additions and 167 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Localisation;
@ -42,16 +41,20 @@ namespace osu.Game.Tests.Mods
{
const double setting_change = 50.4;
var modDouble = new TestNonMatchingSettingTypeModDouble();
var modBool = new TestNonMatchingSettingTypeModBool();
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { Value = setting_change } };
var modBool = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = false, Value = true } };
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { Value = (int)setting_change } };
modDouble.TestSetting.Value = setting_change;
modBool.TestSetting.Value = !modBool.TestSetting.Default;
modDouble.CopySharedSettings(modBool);
modDouble.CopySharedSettings(modInt);
modBool.CopySharedSettings(modDouble);
modBool.CopySharedSettings(modInt);
modInt.CopySharedSettings(modDouble);
modInt.CopySharedSettings(modBool);
Assert.That(modDouble.TestSetting.Value, Is.EqualTo(setting_change));
Assert.That(modBool.TestSetting.Value, Is.EqualTo(!modBool.TestSetting.Default));
Assert.That(modInt.TestSetting.Value, Is.EqualTo((int)setting_change));
}
[Test]
@ -70,75 +73,11 @@ namespace osu.Game.Tests.Mods
public void TestValueResetsToDefaultWhenCopied()
{
var modDouble = new TestNonMatchingSettingTypeModDouble();
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { Value = 1 } };
var modBool = new TestNonMatchingSettingTypeModBool { TestSetting = { Default = false, Value = true } };
modInt.CopySharedSettings(modDouble);
modBool.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(modInt.TestSetting.Default));
}
[Test]
public void TestRelativelyScaleWithClampedRangeWhenCopied()
{
const double setting_change = 50.4;
var modDouble100 = new TestNonMatchingSettingTypeModDouble { TestSetting = { MaxValue = 100, MinValue = 0, Value = setting_change } };
var modDouble200 = new TestNonMatchingSettingTypeModDouble { TestSetting = { MaxValue = 200, MinValue = 0 } };
modDouble200.CopySharedSettings(modDouble100);
Assert.That(modDouble200.TestSetting.Value, Is.EqualTo(setting_change * 2));
}
[Test]
public void TestCopyDoubleToIntWithDefaultRange()
{
const double setting_change = 50.4;
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { Value = setting_change } };
var modInt = new TestNonMatchingSettingTypeModInt();
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(Convert.ToInt32(setting_change)));
}
[Test]
public void TestCopyDoubleToIntWithOutOfBoundsRange()
{
const double setting_change = 50.4;
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { MinValue = int.MinValue - 1d, Value = setting_change } };
// make RangeConstrainedBindable.HasDefinedRange return true
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { MinValue = int.MinValue + 1 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(Convert.ToInt32(setting_change)));
}
[Test]
public void TestCopyDoubleToIntWithOutOfBoundsValue()
{
var modDouble = new TestNonMatchingSettingTypeModDouble { TestSetting = { MinValue = int.MinValue + 1, Value = int.MaxValue + 1d } };
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { MinValue = int.MinValue + 1 } };
modInt.CopySharedSettings(modDouble);
Assert.That(modInt.TestSetting.Value, Is.EqualTo(int.MaxValue));
}
[Test]
public void TestCopyIntToDoubleWithDefaultRange()
{
const int setting_change = 50;
var modInt = new TestNonMatchingSettingTypeModInt { TestSetting = { Value = setting_change } };
var modDouble = new TestNonMatchingSettingTypeModDouble();
modDouble.CopySharedSettings(modInt);
Assert.That(modDouble.TestSetting.Value, Is.EqualTo(setting_change));
Assert.That(modBool.TestSetting.Value, Is.EqualTo(modBool.TestSetting.Default));
}
private class TestNonMatchingSettingTypeModDouble : TestNonMatchingSettingTypeMod

View File

@ -18,7 +18,6 @@ using osu.Game.Overlays.Mods;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Catch.Mods;
using osu.Game.Rulesets.Mania.Mods;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Mods;
@ -417,47 +416,6 @@ namespace osu.Game.Tests.Visual.UserInterface
});
}
[Test]
public void TestKeepSharedSettingsRatio()
{
const float setting_change = 1.8f;
createScreen();
changeRuleset(0);
AddStep("select flashlight mod", () => SelectedMods.Value = new[] { Ruleset.Value.CreateInstance().CreateMod<ModFlashlight>()! });
changeRuleset(0);
AddAssert("ensure mod still selected", () => SelectedMods.Value.SingleOrDefault() is OsuModFlashlight);
AddStep("change mod settings", () =>
{
var osuMod = getMod<OsuModFlashlight>();
// range <0.5 - 2>
osuMod.SizeMultiplier.Value = setting_change;
});
changeRuleset(3);
AddAssert("mania variant selected", () => SelectedMods.Value.SingleOrDefault() is ManiaModFlashlight);
AddAssert("shared settings changed to closest ratio", () =>
{
var maniaMod = getMod<ManiaModFlashlight>();
// range <0.5 - 3>
// converted value based on ratio = (setting_change - 0.5) / (2 - 0.5) * (3 - 0.5) + 0.5 = 2.66
return maniaMod.SizeMultiplier.Value == 2.7f; // taking precision into account
});
AddAssert("other settings unchanged", () =>
{
var maniaMod = getMod<ManiaModFlashlight>();
return maniaMod.ComboBasedSize.IsDefault;
});
}
[Test]
public void TestExternallySetCustomizedMod()
{

View File

@ -165,8 +165,6 @@ namespace osu.Game.Rulesets.Mods
/// <param name="source">The mod to copy properties from.</param>
internal void CopySharedSettings(Mod source)
{
const string min_value = nameof(BindableNumber<int>.MinValue);
const string max_value = nameof(BindableNumber<int>.MaxValue);
const string value = nameof(Bindable<int>.Value);
Dictionary<string, object> sourceSettings = new Dictionary<string, object>();
@ -190,65 +188,22 @@ namespace osu.Game.Rulesets.Mods
continue;
}
bool hasSameGenericArgument = getGenericBaseType(targetSetting, typeof(Bindable<>))!.GenericTypeArguments.Single() ==
getGenericBaseType(sourceSetting, typeof(Bindable<>))!.GenericTypeArguments.Single();
if (!hasSameGenericArgument)
continue;
Type? targetBindableNumberType = getGenericBaseType(targetSetting, typeof(BindableNumber<>));
Type? sourceBindableNumberType = getGenericBaseType(sourceSetting, typeof(BindableNumber<>));
if (targetBindableNumberType == null || sourceBindableNumberType == null)
{
if (getGenericBaseType(targetSetting, typeof(Bindable<>))!.GenericTypeArguments.Single() ==
getGenericBaseType(sourceSetting, typeof(Bindable<>))!.GenericTypeArguments.Single())
{
// change settings only if the type is the same
setValue(targetSetting, value, getValue(sourceSetting, value));
}
setValue(targetSetting, value, getValue(sourceSetting, value));
continue;
}
bool rangeOutOfBounds = false;
Type targetGenericType = targetBindableNumberType.GenericTypeArguments.Single();
Type sourceGenericType = sourceBindableNumberType.GenericTypeArguments.Single();
if (!Convert.ToBoolean(getValue(targetSetting, nameof(RangeConstrainedBindable<int>.HasDefinedRange))) ||
!Convert.ToBoolean(getValue(sourceSetting, nameof(RangeConstrainedBindable<int>.HasDefinedRange))))
// check if we have a range to rescale from and a range to rescale to
// if not, copy the raw value
rangeOutOfBounds = true;
double allowedMin = Math.Max(
Convert.ToDouble(targetGenericType.GetField("MinValue")!.GetValue(null)),
Convert.ToDouble(sourceGenericType.GetField("MinValue")!.GetValue(null))
);
double allowedMax = Math.Min(
Convert.ToDouble(targetGenericType.GetField("MaxValue")!.GetValue(null)),
Convert.ToDouble(sourceGenericType.GetField("MaxValue")!.GetValue(null))
);
double targetMin = getValueDouble(targetSetting, min_value);
double targetMax = getValueDouble(targetSetting, max_value);
double sourceMin = getValueDouble(sourceSetting, min_value);
double sourceMax = getValueDouble(sourceSetting, max_value);
double sourceValue = Math.Clamp(getValueDouble(sourceSetting, value), allowedMin, allowedMax);
double targetValue = rangeOutOfBounds
// keep raw value
? sourceValue
// convert value to same ratio
: (sourceValue - sourceMin) / (sourceMax - sourceMin) * (targetMax - targetMin) + targetMin;
setValue(targetSetting, value, Convert.ChangeType(targetValue, targetBindableNumberType.GenericTypeArguments.Single()));
double getValueDouble(object setting, string name)
{
double settingValue = Convert.ToDouble(getValue(setting, name)!);
if (settingValue < allowedMin || settingValue > allowedMax)
rangeOutOfBounds = true;
return settingValue;
}
setValue(targetSetting, value, Convert.ChangeType(getValue(sourceSetting, value), targetBindableNumberType.GenericTypeArguments.Single()));
}
object? getValue(object setting, string name) =>