1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 04:40:07 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuDropdown.cs

173 lines
6.0 KiB
C#
Raw Normal View History

2017-03-22 06:51:26 +08: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-23 07:44:52 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2017-03-22 06:51:26 +08:00
using osu.Framework.Graphics.UserInterface;
2017-03-23 07:44:52 +08:00
using osu.Game.Graphics.Sprites;
2017-03-22 06:51:26 +08:00
namespace osu.Game.Graphics.UserInterface
{
2017-03-22 22:32:32 +08:00
public class OsuDropdown<T> : Dropdown<T>
2017-03-22 06:51:26 +08:00
{
2017-03-22 22:32:32 +08:00
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader { AccentColour = AccentColour };
2017-03-22 06:51:26 +08: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 22:32:32 +08:00
((OsuDropdownHeader)Header).AccentColour = value;
2017-03-23 07:57:14 +08:00
foreach (var item in MenuItems.OfType<OsuDropdownMenuItem>())
2017-03-22 06:51:26 +08:00
item.AccentColour = value;
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == null)
AccentColour = colours.PinkDarker;
}
2017-03-23 07:57:14 +08:00
protected override DropdownMenuItem<T> CreateMenuItem(string text, T value) => new OsuDropdownMenuItem(text, value) { AccentColour = AccentColour };
2017-03-23 07:44:52 +08:00
2017-03-23 07:57:14 +08:00
private class OsuDropdownMenuItem : DropdownMenuItem<T>
2017-03-23 07:44:52 +08:00
{
public OsuDropdownMenuItem(string text, T current) : base(text, current)
2017-03-23 07:44:52 +08: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[]
{
chevron = new TextAwesome
{
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,
},
new OsuSpriteText {
Text = text,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
}
}
}
};
}
private Color4? accentColour;
private readonly TextAwesome chevron;
2017-03-23 07:44:52 +08:00
protected override void FormatForeground(bool hover = false)
{
base.FormatForeground(hover);
chevron.Alpha = hover ? 1 : 0;
}
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);
}
}
protected class OsuDropdownHeader : DropdownHeader
{
private readonly SpriteText label;
2017-03-23 07:44:52 +08:00
protected override string Label
{
get { return label.Text; }
set { label.Text = value; }
}
2017-03-24 16:59:26 +08:00
protected readonly TextAwesome Icon;
2017-03-23 07:44:52 +08:00
private Color4? accentColour;
public virtual Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
set
{
accentColour = value;
BackgroundColourHover = value;
}
}
public OsuDropdownHeader()
{
Foreground.Padding = new MarginPadding(4);
AutoSizeAxes = Axes.None;
Margin = new MarginPadding { Bottom = 4 };
CornerRadius = 4;
Height = 40;
Foreground.Children = new Drawable[]
{
2017-03-24 16:59:26 +08:00
label = new OsuSpriteText
{
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-23 07:44:52 +08:00
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = accentColour ?? colours.PinkDarker;
}
}
2017-03-22 06:51:26 +08:00
}
}