osu/osu.Game/Overlays/Options/OptionDropDown.cs

85 lines
2.6 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
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;
2017-02-03 10:13:10 +00:00
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays.Options
{
2017-02-03 10:13:10 +00:00
public class OptionDropDown<T> : FlowContainer
{
private DropDownMenu<T> dropdown;
private SpriteText text;
2016-12-01 22:33:30 +00:00
public string LabelText
{
get { return text.Text; }
set
{
text.Text = value;
}
}
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 14:24:54 +00:00
void Dropdown_ValueChanged(object sender, EventArgs e)
{
bindable.Value = dropdown.SelectedValue;
}
2016-12-05 14:24:54 +00:00
protected override void Dispose(bool isDisposing)
{
bindable.ValueChanged -= Bindable_ValueChanged;
dropdown.ValueChanged -= Dropdown_ValueChanged;
base.Dispose(isDisposing);
}
2017-02-03 10:13:10 +00:00
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,
},
2017-02-03 10:13:10 +00:00
dropdown = new OsuDropDownMenu<T>
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
2017-01-04 06:14:25 +00:00
Items = (T[])Enum.GetValues(typeof(T)),
}
};
2016-12-05 14:24:54 +00:00
dropdown.ValueChanged += Dropdown_ValueChanged;
}
}
2016-12-02 11:35:55 +00:00
}