osu/osu.Game/Graphics/UserInterface/OsuDropdown.cs

175 lines
6.0 KiB
C#
Raw Normal View History

2017-03-21 22:51:26 +00:00
// 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.Linq;
using OpenTK.Graphics;
using osu.Framework.Allocation;
2017-03-22 23:44:52 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2017-03-21 22:51:26 +00:00
using osu.Framework.Graphics.UserInterface;
2017-03-22 23:44:52 +00:00
using osu.Game.Graphics.Sprites;
2017-03-21 22:51:26 +00:00
namespace osu.Game.Graphics.UserInterface
{
2017-03-22 14:32:32 +00:00
public class OsuDropdown<T> : Dropdown<T>
2017-03-21 22:51:26 +00:00
{
2017-03-22 14:32:32 +00:00
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader { AccentColour = AccentColour };
2017-03-21 22:51:26 +00:00
protected override Menu CreateMenu() => new OsuMenu();
private Color4? accentColour;
public virtual Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
set
{
accentColour = value;
if (Header != null)
2017-03-22 14:32:32 +00:00
((OsuDropdownHeader)Header).AccentColour = value;
2017-03-22 23:57:14 +00:00
foreach (var item in MenuItems.OfType<OsuDropdownMenuItem>())
2017-03-21 22:51:26 +00:00
item.AccentColour = value;
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == null)
AccentColour = colours.PinkDarker;
}
2017-03-22 23:57:14 +00:00
protected override DropdownMenuItem<T> CreateMenuItem(string text, T value) => new OsuDropdownMenuItem(text, value) { AccentColour = AccentColour };
2017-03-22 23:44:52 +00:00
public class OsuDropdownMenuItem : DropdownMenuItem<T>
2017-03-22 23:44:52 +00:00
{
public OsuDropdownMenuItem(string text, T current) : base(text, current)
2017-03-22 23:44:52 +00:00
{
Foreground.Padding = new MarginPadding(2);
Masking = true;
CornerRadius = 6;
Children = new[]
{
new FillFlowContainer
{
Direction = FillDirection.Horizontal,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
2017-05-25 00:51:00 +00:00
Chevron = new TextAwesome
2017-03-22 23:44:52 +00:00
{
AlwaysPresent = true,
Icon = FontAwesome.fa_chevron_right,
UseFullGlyphHeight = false,
Colour = Color4.Black,
Alpha = 0.5f,
TextSize = 8,
Margin = new MarginPadding { Left = 3, Right = 3 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
2017-05-31 05:03:13 +00:00
Label = new OsuSpriteText {
2017-03-22 23:44:52 +00:00
Text = text,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
}
}
}
};
}
private Color4? accentColour;
2017-05-25 00:51:00 +00:00
protected readonly TextAwesome Chevron;
2017-05-31 05:03:13 +00:00
protected readonly OsuSpriteText Label;
2017-03-22 23:44:52 +00:00
protected override void FormatForeground(bool hover = false)
{
base.FormatForeground(hover);
2017-05-25 00:51:00 +00:00
Chevron.Alpha = hover ? 1 : 0;
2017-03-22 23:44:52 +00:00
}
public Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
set
{
accentColour = value;
BackgroundColourHover = BackgroundColourSelected = value;
FormatBackground();
FormatForeground();
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Transparent;
BackgroundColourHover = accentColour ?? colours.PinkDarker;
BackgroundColourSelected = Color4.Black.Opacity(0.5f);
}
}
public class OsuDropdownHeader : DropdownHeader
2017-03-22 23:44:52 +00:00
{
protected readonly SpriteText Text;
2017-03-22 23:44:52 +00:00
protected override string Label
{
get { return Text.Text; }
set { Text.Text = value; }
2017-03-22 23:44:52 +00:00
}
2017-03-24 08:59:26 +00:00
protected readonly TextAwesome Icon;
2017-03-22 23:44:52 +00:00
private Color4? accentColour;
public virtual Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
set
{
accentColour = value;
BackgroundColourHover = value;
}
}
public OsuDropdownHeader()
2017-05-18 20:43:39 +00:00
{
2017-03-22 23:44:52 +00:00
Foreground.Padding = new MarginPadding(4);
AutoSizeAxes = Axes.None;
Margin = new MarginPadding { Bottom = 4 };
CornerRadius = 4;
Height = 40;
Foreground.Children = new Drawable[]
{
Text = new OsuSpriteText
2017-03-24 08:59:26 +00:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
Icon = new TextAwesome
{
Icon = FontAwesome.fa_chevron_down,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 4 },
TextSize = 20
}
2017-03-22 23:44:52 +00:00
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = accentColour ?? colours.PinkDarker;
}
}
2017-03-21 22:51:26 +00:00
}
}