2016-12-02 05:36:04 +08:00
|
|
|
|
using System;
|
2016-12-02 06:45:31 +08:00
|
|
|
|
using System.ComponentModel;
|
2016-12-02 05:36:04 +08:00
|
|
|
|
using System.Linq;
|
2016-12-02 05:45:43 +08:00
|
|
|
|
using System.Reflection;
|
2016-12-02 20:24:48 +08:00
|
|
|
|
using OpenTK;
|
2016-12-02 05:36:04 +08:00
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
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;
|
2016-12-02 05:45:43 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2016-12-02 05:36:04 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Options
|
|
|
|
|
{
|
2016-12-02 06:33:30 +08:00
|
|
|
|
public class DropdownOption<T> : FlowContainer
|
2016-12-02 05:36:04 +08:00
|
|
|
|
{
|
|
|
|
|
private DropDownMenu<T> dropdown;
|
|
|
|
|
private SpriteText text;
|
|
|
|
|
|
2016-12-02 06:33:30 +08:00
|
|
|
|
public string LabelText
|
2016-12-02 05:36:04 +08:00
|
|
|
|
{
|
|
|
|
|
get { return text.Text; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text.Text = value;
|
|
|
|
|
text.Alpha = string.IsNullOrEmpty(value) ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bindable<T> Bindable
|
|
|
|
|
{
|
|
|
|
|
get { return bindable; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (bindable != null)
|
|
|
|
|
bindable.ValueChanged -= Bindable_ValueChanged;
|
|
|
|
|
bindable = value;
|
|
|
|
|
bindable.ValueChanged += Bindable_ValueChanged;
|
|
|
|
|
Bindable_ValueChanged(null, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Bindable<T> bindable;
|
|
|
|
|
|
|
|
|
|
void Bindable_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
dropdown.SelectedValue = bindable.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 06:33:30 +08:00
|
|
|
|
public DropdownOption()
|
2016-12-02 05:36:04 +08:00
|
|
|
|
{
|
|
|
|
|
if (!typeof(T).IsEnum)
|
|
|
|
|
throw new InvalidOperationException("OptionsDropdown only supports enums as the generic type argument");
|
|
|
|
|
Direction = FlowDirection.VerticalOnly;
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2016-12-02 05:45:43 +08:00
|
|
|
|
var items = typeof(T).GetFields().Where(f => !f.IsSpecialName).Zip(
|
|
|
|
|
(T[])Enum.GetValues(typeof(T)), (a, b) => new Tuple<string, T>(
|
2016-12-02 06:45:31 +08:00
|
|
|
|
a.GetCustomAttribute<DescriptionAttribute>()?.Description ?? a.Name, b));
|
2016-12-02 05:36:04 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
text = new SpriteText { Alpha = 0 },
|
2016-12-02 20:24:48 +08:00
|
|
|
|
dropdown = new StyledDropDownMenu<T>
|
2016-12-02 05:36:04 +08:00
|
|
|
|
{
|
|
|
|
|
Margin = new MarginPadding { Top = 5 },
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2016-12-02 20:24:48 +08:00
|
|
|
|
Items = items.Select(item => new StyledDropDownMenuItem<T>(item.Item1, item.Item2))
|
2016-12-02 05:36:04 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-12-02 20:24:48 +08:00
|
|
|
|
|
|
|
|
|
private class StyledDropDownMenu<U> : DropDownMenu<U>
|
|
|
|
|
{
|
|
|
|
|
protected override float DropDownListSpacing => 4;
|
|
|
|
|
|
|
|
|
|
protected override DropDownComboBox CreateComboBox()
|
|
|
|
|
{
|
|
|
|
|
return new StyledDropDownComboBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StyledDropDownMenu()
|
|
|
|
|
{
|
|
|
|
|
ComboBox.CornerRadius = 4;
|
|
|
|
|
DropDown.CornerRadius = 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void AnimateOpen()
|
|
|
|
|
{
|
|
|
|
|
foreach (StyledDropDownMenuItem<U> child in DropDownList.Children)
|
|
|
|
|
{
|
|
|
|
|
child.FadeIn(200);
|
|
|
|
|
child.ResizeTo(new Vector2(1, 24), 200);
|
|
|
|
|
}
|
|
|
|
|
DropDown.Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void AnimateClose()
|
|
|
|
|
{
|
|
|
|
|
foreach (StyledDropDownMenuItem<U> child in DropDownList.Children)
|
|
|
|
|
{
|
|
|
|
|
child.ResizeTo(new Vector2(1, 0), 200);
|
|
|
|
|
child.FadeOut(200);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class StyledDropDownComboBox : DropDownComboBox
|
|
|
|
|
{
|
|
|
|
|
protected override Color4 BackgroundColour => new Color4(255, 255, 255, 100);
|
|
|
|
|
protected override Color4 BackgroundColourHover => Color4.HotPink;
|
|
|
|
|
|
|
|
|
|
public StyledDropDownComboBox()
|
|
|
|
|
{
|
|
|
|
|
Foreground.Padding = new MarginPadding(4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class StyledDropDownMenuItem<U> : DropDownMenuItem<U>
|
|
|
|
|
{
|
|
|
|
|
public StyledDropDownMenuItem(string text, U value) : base(text, value)
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.None;
|
|
|
|
|
Height = 0;
|
|
|
|
|
Foreground.Padding = new MarginPadding(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnSelectChange()
|
|
|
|
|
{
|
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FormatBackground();
|
|
|
|
|
FormatCaret();
|
|
|
|
|
FormatLabel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void FormatCaret()
|
|
|
|
|
{
|
|
|
|
|
(Caret as SpriteText).Text = IsSelected ? @"+" : @"-";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void FormatLabel()
|
|
|
|
|
{
|
|
|
|
|
if (IsSelected)
|
|
|
|
|
(Label as SpriteText).Text = @"*" + Value + @"*";
|
|
|
|
|
else
|
|
|
|
|
(Label as SpriteText).Text = Value.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-02 05:36:04 +08:00
|
|
|
|
}
|
2016-12-02 19:35:55 +08:00
|
|
|
|
}
|