1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-24 11:07:20 +08:00

Use SettingsSource for mod cusomisation

This commit is contained in:
Dean Herbert 2019-12-06 17:09:48 +09:00
parent 23e47530c5
commit a5d5099868
4 changed files with 25 additions and 50 deletions

View File

@ -6,9 +6,9 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Mods;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Tests.Visual.UserInterface
@ -47,9 +47,6 @@ namespace osu.Game.Tests.Visual.UserInterface
ModSectionsContainer.Children.Single(s => s.ModType == mod.Type)
.ButtonsContainer.OfType<ModButton>().Single(b => b.Mods.Any(m => m.GetType() == mod.GetType())).SelectNext(1);
public ModControlSection GetControlSection(Mod mod) =>
ModSettingsContent.Children.FirstOrDefault(s => s.Mod == mod);
protected override void LoadComplete()
{
base.LoadComplete();
@ -84,34 +81,23 @@ namespace osu.Game.Tests.Visual.UserInterface
public override string Acronym => "CM2";
}
private abstract class TestModCustomizable : Mod, IModHasSettings
private abstract class TestModCustomizable : Mod, IApplicableMod
{
public override double ScoreMultiplier => 1.0;
public override ModType Type => ModType.Conversion;
public readonly BindableFloat SliderBindable = new BindableFloat
[SettingSource("Sample float", "Change something for a mod")]
public BindableFloat SliderBindable { get; } = new BindableFloat
{
MinValue = 0,
MaxValue = 10,
Default = 5,
Value = 7
};
public readonly BindableBool TickBindable = new BindableBool();
public Drawable[] CreateControls() =>
new Drawable[]
{
new SettingsSlider<float>
{
LabelText = "Slider",
Bindable = SliderBindable
},
new SettingsCheckbox
{
LabelText = "Checkbox",
Bindable = TickBindable
}
};
[SettingSource("Sample bool", "Clicking this changes a setting")]
public BindableBool TickBindable { get; } = new BindableBool();
}
}
}

View File

@ -4,6 +4,7 @@
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Mods;
@ -34,8 +35,7 @@ namespace osu.Game.Overlays.Mods
RelativeSizeAxes = Axes.X,
};
if (Mod is IModHasSettings modHasSettings)
AddRange(modHasSettings.CreateControls());
AddRange(Mod.CreateSettingsControls());
}
[BackgroundDependencyLoader]
@ -53,4 +53,4 @@ namespace osu.Game.Overlays.Mods
});
}
}
}
}

View File

@ -13,6 +13,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Containers;
@ -236,6 +237,7 @@ namespace osu.Game.Overlays.Mods
Width = 180,
Text = "Customization",
Action = () => ModSettingsContainer.Alpha = ModSettingsContainer.Alpha == 1 ? 0 : 1,
Enabled = { Value = false },
Margin = new MarginPadding
{
Right = 20
@ -331,7 +333,6 @@ namespace osu.Game.Overlays.Mods
SelectedMods.ValueChanged += updateModSettings;
Ruleset.ValueChanged += _ => ModSettingsContent.Clear();
CustomizeButton.Enabled.Value = false;
sampleOn = audio.Samples.Get(@"UI/check-on");
sampleOff = audio.Samples.Get(@"UI/check-off");
@ -339,18 +340,21 @@ namespace osu.Game.Overlays.Mods
private void updateModSettings(ValueChangedEvent<IReadOnlyList<Mod>> selectedMods)
{
var added = selectedMods.NewValue.Except(selectedMods.OldValue).FirstOrDefault();
var removed = selectedMods.OldValue.Except(selectedMods.NewValue).FirstOrDefault();
foreach (var added in selectedMods.NewValue.Except(selectedMods.OldValue))
{
var controls = added.CreateSettingsControls().ToList();
if (controls.Count > 0)
ModSettingsContent.Add(new ModControlSection(added) { Children = controls });
}
if (added is IModHasSettings)
ModSettingsContent.Add(new ModControlSection(added));
else if (removed is IModHasSettings)
ModSettingsContent.Remove(ModSettingsContent.Children.Single(section => section.Mod == removed));
foreach (var removed in selectedMods.OldValue.Except(selectedMods.NewValue))
ModSettingsContent.RemoveAll(section => section.Mod == removed);
CustomizeButton.Enabled.Value = ModSettingsContent.Children.Count > 0;
bool hasSettings = ModSettingsContent.Children.Count > 0;
CustomizeButton.Enabled.Value = hasSettings;
if (ModSettingsContainer.Alpha == 1 && ModSettingsContent.Children.Count == 0)
ModSettingsContainer.Alpha = 0;
if (!hasSettings)
ModSettingsContainer.Hide();
}
public void DeselectAll()

View File

@ -1,15 +0,0 @@
// 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 osu.Framework.Graphics;
namespace osu.Game.Rulesets.Mods
{
/// <summary>
/// An interface for mods that allows user control over it's properties.
/// </summary>
public interface IModHasSettings : IApplicableMod
{
Drawable[] CreateControls();
}
}