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.UserInterface;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using System.Collections.Generic;
namespace osu.Game.Overlays.Options
{
@ -56,10 +57,25 @@ protected override void Dispose(bool 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()
{
if (!typeof(T).IsEnum)
throw new InvalidOperationException("OptionsDropdown only supports enums as the generic type argument");
Items = new KeyValuePair<string, T>[0];
Direction = FlowDirection.VerticalOnly;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
@ -72,7 +88,7 @@ public OptionDropDown()
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
Items = (T[])Enum.GetValues(typeof(T)),
Items = this.Items,
}
};
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 @@ private void load(OsuConfigManager config)
LabelText = "Background dim",
Bindable = (BindableInt)config.GetBindable<int>(OsuConfig.DimLevel)
},
new OptionDropDown<ProgressBarType>
new OptionEnumDropDown<ProgressBarType>
{
LabelText = "Progress display",
Bindable = config.GetBindable<ProgressBarType>(OsuConfig.ProgressBarType)
},
new OptionDropDown<ScoreMeterType>
new OptionEnumDropDown<ScoreMeterType>
{
LabelText = "Score meter type",
Bindable = config.GetBindable<ScoreMeterType>(OsuConfig.ScoreMeter)

View File

@ -18,7 +18,7 @@ private void load(BasicStorage storage, OsuConfigManager config)
{
Children = new Drawable[]
{
new OptionDropDown<ReleaseStream>
new OptionEnumDropDown<ReleaseStream>
{
LabelText = "Release stream",
Bindable = config.GetBindable<ReleaseStream>(OsuConfig.ReleaseStream),

View File

@ -57,7 +57,7 @@ private void load(OsuConfigManager config)
LabelText = "Softening filter",
Bindable = config.GetBindable<bool>(OsuConfig.BloomSoftening)
},
new OptionDropDown<ScreenshotFormat>
new OptionEnumDropDown<ScreenshotFormat>
{
LabelText = "Screenshot",
Bindable = config.GetBindable<ScreenshotFormat>(OsuConfig.ScreenshotFormat)

View File

@ -20,7 +20,7 @@ private void load(OsuConfigManager osuConfig, FrameworkConfigManager config)
Children = new Drawable[]
{
// TODO: this needs to be a custom dropdown at some point
new OptionDropDown<FrameSync>
new OptionEnumDropDown<FrameSync>
{
LabelText = "Frame limiter",
Bindable = config.GetBindable<FrameSync>(FrameworkConfig.FrameSync)

View File

@ -33,7 +33,7 @@ private void load(OsuConfigManager config)
LabelText = "Map absolute raw input to the osu! window",
Bindable = config.GetBindable<bool>(OsuConfig.AbsoluteToOsuWindow)
},
new OptionDropDown<ConfineMouseMode>
new OptionEnumDropDown<ConfineMouseMode>
{
LabelText = "Confine mouse cursor",
Bindable = config.GetBindable<ConfineMouseMode>(OsuConfig.ConfineMouse),

View File

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