1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 13:47:38 +08:00
osu-lazer/osu.Game/Overlays/Options/OptionDropDown.cs

111 lines
3.3 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 18:13:10 +08:00
using osu.Game.Graphics.UserInterface;
using System.Collections.Generic;
namespace osu.Game.Overlays.Options
{
2017-02-03 18:13:10 +08:00
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;
}
}
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);
if (bindable?.Disabled ?? true)
Alpha = 0.3f;
}
}
private Bindable<T> bindable;
void bindable_ValueChanged(object sender, EventArgs e)
{
dropdown.SelectedValue = bindable.Value;
}
void dropdown_ValueChanged(object sender, EventArgs e)
2016-12-05 22:24:54 +08:00
{
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;
2016-12-05 22:24:54 +08:00
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;
// We need to refresh the dropdown because our items changed,
// thus its selected value may be outdated.
if (bindable != null)
dropdown.SelectedValue = bindable.Value;
}
}
}
2017-02-03 18:13:10 +08:00
public OptionDropDown()
{
Items = new KeyValuePair<string, T>[0];
2017-02-13 15:02:14 +08:00
Direction = FlowDirections.Vertical;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Children = new Drawable[]
{
text = new OsuSpriteText {
Alpha = 0,
},
2017-02-03 18:13:10 +08:00
dropdown = new OsuDropDownMenu<T>
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
Items = this.Items,
}
};
dropdown.ValueChanged += dropdown_ValueChanged;
}
}
2016-12-02 19:35:55 +08:00
}