1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 07:27:24 +08:00
osu-lazer/osu.Game/Overlays/Options/OptionDropdown.cs

222 lines
7.3 KiB
C#
Raw Normal View History

using System;
2017-01-04 14:14:25 +08:00
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
2016-12-02 05:45:43 +08:00
using System.Reflection;
2016-12-02 20:24:48 +08:00
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Allocation;
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;
2017-01-04 12:00:28 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Overlays.Options
{
public class OptionDropdown<T> : FlowContainer
{
private DropDownMenu<T> dropdown;
private SpriteText text;
2016-12-02 06:33:30 +08:00
public string LabelText
{
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-05 22:24:54 +08:00
void Dropdown_ValueChanged(object sender, EventArgs e)
{
bindable.Value = dropdown.SelectedValue;
}
2016-12-05 22:24:54 +08:00
protected override void Dispose(bool isDisposing)
{
bindable.ValueChanged -= Bindable_ValueChanged;
dropdown.ValueChanged -= Dropdown_ValueChanged;
base.Dispose(isDisposing);
}
public OptionDropdown()
{
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;
Children = new Drawable[]
{
text = new OsuSpriteText {
Alpha = 0,
},
2016-12-02 20:24:48 +08:00
dropdown = new StyledDropDownMenu<T>
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
2017-01-04 14:14:25 +08:00
Items = (T[])Enum.GetValues(typeof(T)),
}
};
2016-12-05 22:24:54 +08:00
dropdown.ValueChanged += Dropdown_ValueChanged;
}
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();
}
2017-01-04 14:14:25 +08:00
protected override IEnumerable<DropDownMenuItem<U>> GetDropDownItems(IEnumerable<U> values)
{
return values.Select(v =>
{
var field = typeof(U).GetField(Enum.GetName(typeof(U), v));
return new StyledDropDownMenuItem<U>(
field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name, v);
});
2017-01-04 14:14:25 +08:00
}
2016-12-02 20:24:48 +08:00
public StyledDropDownMenu()
{
ComboBox.CornerRadius = 4;
DropDown.CornerRadius = 4;
}
protected override void AnimateOpen()
{
2017-01-05 15:10:05 +08:00
foreach (StyledDropDownMenuItem<U> child in DropDownItemsContainer.Children)
2016-12-02 20:24:48 +08:00
{
child.FadeIn(200);
child.ResizeTo(new Vector2(1, 24), 200);
}
DropDown.Show();
}
protected override void AnimateClose()
{
2017-01-05 15:10:05 +08:00
foreach (StyledDropDownMenuItem<U> child in DropDownItemsContainer.Children)
2016-12-02 20:24:48 +08:00
{
child.ResizeTo(new Vector2(1, 0), 200);
child.FadeOut(200);
}
}
}
private class StyledDropDownComboBox : DropDownComboBox
{
2017-01-04 12:00:28 +08:00
private SpriteText label;
protected override string Label
{
get { return label.Text; }
set { label.Text = value; }
}
2016-12-02 20:24:48 +08:00
public StyledDropDownComboBox()
{
Foreground.Padding = new MarginPadding(4);
2017-01-04 12:00:28 +08:00
AutoSizeAxes = Axes.None;
Height = 40;
2017-01-04 12:00:28 +08:00
Children = new[]
{
label = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
2017-01-04 12:00:28 +08:00
new TextAwesome
{
Icon = FontAwesome.fa_chevron_down,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 4 },
}
};
2016-12-02 20:24:48 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = colours.PinkDarker;
}
2016-12-02 20:24:48 +08:00
}
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);
2017-01-04 12:00:28 +08:00
Children = new[]
{
new FlowContainer
{
Direction = FlowDirection.HorizontalOnly,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
2017-01-05 13:57:19 +08:00
new TextAwesome
2017-01-04 12:00:28 +08:00
{
2017-01-05 13:57:19 +08:00
Icon = FontAwesome.fa_chevron_right,
Colour = Color4.Black,
TextSize = 12,
2017-01-05 13:57:19 +08:00
Margin = new MarginPadding { Right = 3 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
2017-01-04 12:00:28 +08:00
},
new OsuSpriteText {
Text = text,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
}
2017-01-04 12:00:28 +08:00
}
}
};
2016-12-02 20:24:48 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = colours.PinkDarker;
BackgroundColourSelected = Color4.Black.Opacity(0.5f);
}
2016-12-02 20:24:48 +08:00
}
}
2016-12-02 19:35:55 +08:00
}