Rework OptionDropdowns to be more versatile

The existing OptionDropdown only supported enums and was thus renamed
to OptionEnumDropDown. A new OptionDropdown has been created in its
place to allow binding to arbitrary values, with a set of user-provided
items.
This commit is contained in:
default0
2017-02-06 01:21:26 +01:00
parent b230b5cfb9
commit b4f30dd417
8 changed files with 64 additions and 10 deletions

View File

@ -7,6 +7,7 @@ using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using System.Collections.Generic;
namespace osu.Game.Overlays.Options namespace osu.Game.Overlays.Options
{ {
@ -56,10 +57,25 @@ namespace osu.Game.Overlays.Options
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }
private IEnumerable<KeyValuePair<string, T>> items;
public IEnumerable<KeyValuePair<string, T>> Items
{
get
{
return items;
}
set
{
items = value;
if(dropdown != null)
dropdown.Items = value;
}
}
public OptionDropDown() public OptionDropDown()
{ {
if (!typeof(T).IsEnum) Items = new KeyValuePair<string, T>[0];
throw new InvalidOperationException("OptionsDropdown only supports enums as the generic type argument");
Direction = FlowDirection.VerticalOnly; Direction = FlowDirection.VerticalOnly;
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
@ -72,7 +88,7 @@ namespace osu.Game.Overlays.Options
{ {
Margin = new MarginPadding { Top = 5 }, Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Items = (T[])Enum.GetValues(typeof(T)), Items = this.Items,
} }
}; };
dropdown.ValueChanged += Dropdown_ValueChanged; dropdown.ValueChanged += Dropdown_ValueChanged;

View File

@ -0,0 +1,37 @@
using System;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
namespace osu.Game.Overlays.Options
{
public class OptionEnumDropDown<T> : OptionDropDown<T>
{
public OptionEnumDropDown()
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException("OptionsDropdown only supports enums as the generic type argument");
List<KeyValuePair<string, T>> items = new List<KeyValuePair<string, T>>();
foreach(var val in (T[])Enum.GetValues(typeof(T)))
{
var field = typeof(T).GetField(Enum.GetName(typeof(T), val));
items.Add(
new KeyValuePair<string, T>(
field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? Enum.GetName(typeof(T), val),
val
)
);
}
Items = items;
}
}
}

View File

@ -23,12 +23,12 @@ namespace osu.Game.Overlays.Options.Sections.Gameplay
LabelText = "Background dim", LabelText = "Background dim",
Bindable = (BindableInt)config.GetBindable<int>(OsuConfig.DimLevel) Bindable = (BindableInt)config.GetBindable<int>(OsuConfig.DimLevel)
}, },
new OptionDropDown<ProgressBarType> new OptionEnumDropDown<ProgressBarType>
{ {
LabelText = "Progress display", LabelText = "Progress display",
Bindable = config.GetBindable<ProgressBarType>(OsuConfig.ProgressBarType) Bindable = config.GetBindable<ProgressBarType>(OsuConfig.ProgressBarType)
}, },
new OptionDropDown<ScoreMeterType> new OptionEnumDropDown<ScoreMeterType>
{ {
LabelText = "Score meter type", LabelText = "Score meter type",
Bindable = config.GetBindable<ScoreMeterType>(OsuConfig.ScoreMeter) Bindable = config.GetBindable<ScoreMeterType>(OsuConfig.ScoreMeter)

View File

@ -18,7 +18,7 @@ namespace osu.Game.Overlays.Options.Sections.General
{ {
Children = new Drawable[] Children = new Drawable[]
{ {
new OptionDropDown<ReleaseStream> new OptionEnumDropDown<ReleaseStream>
{ {
LabelText = "Release stream", LabelText = "Release stream",
Bindable = config.GetBindable<ReleaseStream>(OsuConfig.ReleaseStream), Bindable = config.GetBindable<ReleaseStream>(OsuConfig.ReleaseStream),

View File

@ -57,7 +57,7 @@ namespace osu.Game.Overlays.Options.Sections.Graphics
LabelText = "Softening filter", LabelText = "Softening filter",
Bindable = config.GetBindable<bool>(OsuConfig.BloomSoftening) Bindable = config.GetBindable<bool>(OsuConfig.BloomSoftening)
}, },
new OptionDropDown<ScreenshotFormat> new OptionEnumDropDown<ScreenshotFormat>
{ {
LabelText = "Screenshot", LabelText = "Screenshot",
Bindable = config.GetBindable<ScreenshotFormat>(OsuConfig.ScreenshotFormat) Bindable = config.GetBindable<ScreenshotFormat>(OsuConfig.ScreenshotFormat)

View File

@ -20,7 +20,7 @@ namespace osu.Game.Overlays.Options.Sections.Graphics
Children = new Drawable[] Children = new Drawable[]
{ {
// TODO: this needs to be a custom dropdown at some point // TODO: this needs to be a custom dropdown at some point
new OptionDropDown<FrameSync> new OptionEnumDropDown<FrameSync>
{ {
LabelText = "Frame limiter", LabelText = "Frame limiter",
Bindable = config.GetBindable<FrameSync>(FrameworkConfig.FrameSync) Bindable = config.GetBindable<FrameSync>(FrameworkConfig.FrameSync)

View File

@ -33,7 +33,7 @@ namespace osu.Game.Overlays.Options.Sections.Input
LabelText = "Map absolute raw input to the osu! window", LabelText = "Map absolute raw input to the osu! window",
Bindable = config.GetBindable<bool>(OsuConfig.AbsoluteToOsuWindow) Bindable = config.GetBindable<bool>(OsuConfig.AbsoluteToOsuWindow)
}, },
new OptionDropDown<ConfineMouseMode> new OptionEnumDropDown<ConfineMouseMode>
{ {
LabelText = "Confine mouse cursor", LabelText = "Confine mouse cursor",
Bindable = config.GetBindable<ConfineMouseMode>(OsuConfig.ConfineMouse), Bindable = config.GetBindable<ConfineMouseMode>(OsuConfig.ConfineMouse),

View File

@ -99,6 +99,7 @@
<Compile Include="Beatmaps\Timing\SampleChange.cs" /> <Compile Include="Beatmaps\Timing\SampleChange.cs" />
<Compile Include="Beatmaps\Timing\TimingChange.cs" /> <Compile Include="Beatmaps\Timing\TimingChange.cs" />
<Compile Include="Configuration\OsuConfigManager.cs" /> <Compile Include="Configuration\OsuConfigManager.cs" />
<Compile Include="Overlays\Options\OptionDropDown.cs" />
<Compile Include="Overlays\Options\OptionLabel.cs" /> <Compile Include="Overlays\Options\OptionLabel.cs" />
<Compile Include="Graphics\UserInterface\OsuDropDownHeader.cs" /> <Compile Include="Graphics\UserInterface\OsuDropDownHeader.cs" />
<Compile Include="Graphics\UserInterface\OsuDropDownMenu.cs" /> <Compile Include="Graphics\UserInterface\OsuDropDownMenu.cs" />
@ -239,7 +240,7 @@
<Compile Include="Overlays\Options\OptionTextBox.cs" /> <Compile Include="Overlays\Options\OptionTextBox.cs" />
<Compile Include="Overlays\Options\OptionSlider.cs" /> <Compile Include="Overlays\Options\OptionSlider.cs" />
<Compile Include="Configuration\ProgressBarType.cs" /> <Compile Include="Configuration\ProgressBarType.cs" />
<Compile Include="Overlays\Options\OptionDropDown.cs" /> <Compile Include="Overlays\Options\OptionEnumDropDown.cs" />
<Compile Include="Configuration\RankingType.cs" /> <Compile Include="Configuration\RankingType.cs" />
<Compile Include="Configuration\ScoreMeterType.cs" /> <Compile Include="Configuration\ScoreMeterType.cs" />
<Compile Include="Configuration\ReleaseStream.cs" /> <Compile Include="Configuration\ReleaseStream.cs" />