1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 16:57:48 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuDropdown.cs

256 lines
9.3 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;
2017-03-22 06:51:26 +08:00
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;
using OpenTK;
2017-03-22 06:51:26 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class OsuDropdown<T> : Dropdown<T>, IHasAccentColour
2017-03-22 06:51:26 +08:00
{
private Color4 accentColour;
public Color4 AccentColour
{
get { return accentColour; }
set
{
accentColour = value;
updateAccentColour();
}
}
2017-03-22 06:51:26 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
2017-03-22 06:51:26 +08:00
{
if (accentColour == default(Color4))
accentColour = colours.PinkDarker;
updateAccentColour();
2017-03-22 06:51:26 +08:00
}
private void updateAccentColour()
2017-03-22 06:51:26 +08:00
{
var header = Header as IHasAccentColour;
if (header != null) header.AccentColour = accentColour;
var menu = Menu as IHasAccentColour;
if (menu != null) menu.AccentColour = accentColour;
2017-03-22 06:51:26 +08:00
}
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader();
protected override DropdownMenu CreateMenu() => new OsuDropdownMenu();
2017-03-23 07:44:52 +08:00
#region OsuDropdownMenu
protected class OsuDropdownMenu : DropdownMenu, IHasAccentColour
2017-03-23 07:44:52 +08:00
{
2017-08-25 17:41:12 +08:00
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
public OsuDropdownMenu()
{
CornerRadius = 4;
BackgroundColour = Color4.Black.Opacity(0.5f);
2017-09-04 08:10:04 +08:00
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
ItemsContainer.Padding = new MarginPadding(5);
2017-08-25 17:41:12 +08:00
}
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
protected override void AnimateOpen() => this.FadeIn(300, Easing.OutQuint);
protected override void AnimateClose() => this.FadeOut(300, Easing.OutQuint);
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
2017-09-04 08:32:44 +08:00
protected override void UpdateSize(Vector2 newSize)
{
if (Direction == Direction.Vertical)
{
Width = newSize.X;
this.ResizeHeightTo(newSize.Y, 300, Easing.OutQuint);
}
else
{
Height = newSize.Y;
this.ResizeWidthTo(newSize.X, 300, Easing.OutQuint);
}
}
2017-08-25 17:41:12 +08:00
private Color4 accentColour;
public Color4 AccentColour
{
get { return accentColour; }
set
{
accentColour = value;
foreach (var c in Children.OfType<IHasAccentColour>())
c.AccentColour = value;
}
}
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuDropdownMenuItem(item) { AccentColour = accentColour };
2017-08-25 17:41:12 +08:00
#region DrawableOsuDropdownMenuItem
2017-09-06 22:34:21 +08:00
public class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem, IHasAccentColour
2017-03-23 07:44:52 +08:00
{
private Color4? accentColour;
public Color4 AccentColour
{
get { return accentColour ?? nonAccentSelectedColour; }
set
{
accentColour = value;
updateColours();
}
}
private void updateColours()
{
BackgroundColourHover = accentColour ?? nonAccentHoverColour;
BackgroundColourSelected = accentColour ?? nonAccentSelectedColour;
UpdateBackgroundColour();
UpdateForegroundColour();
}
private Color4 nonAccentHoverColour;
private Color4 nonAccentSelectedColour;
2017-08-28 13:42:52 +08:00
public DrawableOsuDropdownMenuItem(MenuItem item)
2017-08-25 17:41:12 +08:00
: base(item)
{
Foreground.Padding = new MarginPadding(2);
Masking = true;
CornerRadius = 6;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Transparent;
nonAccentHoverColour = colours.PinkDarker;
nonAccentSelectedColour = Color4.Black.Opacity(0.5f);
updateColours();
AddInternal(new HoverClickSounds(HoverSampleSet.Soft));
}
2017-03-23 07:44:52 +08:00
2017-08-25 17:41:12 +08:00
protected override void UpdateForegroundColour()
2017-03-23 07:44:52 +08:00
{
2017-08-25 17:41:12 +08:00
base.UpdateForegroundColour();
2017-08-28 11:33:31 +08:00
2017-08-29 17:31:51 +08:00
var content = Foreground.Children.FirstOrDefault() as Content;
if (content != null) content.Chevron.Alpha = IsHovered ? 1 : 0;
}
protected override Drawable CreateContent() => new Content();
2017-08-28 11:33:31 +08:00
2017-09-04 08:10:04 +08:00
protected new class Content : FillFlowContainer, IHasText
2017-03-23 07:44:52 +08:00
{
2017-08-28 11:33:31 +08:00
public string Text
2017-03-23 07:44:52 +08:00
{
2017-08-28 11:33:31 +08:00
get { return Label.Text; }
set { Label.Text = value; }
}
public readonly OsuSpriteText Label;
public readonly SpriteIcon Chevron;
public Content()
2017-08-28 11:33:31 +08:00
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Direction = FillDirection.Horizontal;
Children = new Drawable[]
2017-03-23 07:44:52 +08:00
{
2017-08-28 11:33:31 +08:00
Chevron = new SpriteIcon
{
AlwaysPresent = true,
Icon = FontAwesome.fa_chevron_right,
Colour = Color4.Black,
Alpha = 0.5f,
Size = new Vector2(8),
Margin = new MarginPadding { Left = 3, Right = 3 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
Label = new OsuSpriteText
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
2017-08-28 11:33:31 +08:00
};
}
}
2017-03-23 07:44:52 +08:00
}
#endregion
2017-03-23 07:44:52 +08:00
}
#endregion
2017-03-23 07:44:52 +08:00
public class OsuDropdownHeader : DropdownHeader, IHasAccentColour
2017-03-23 07:44:52 +08:00
{
protected readonly SpriteText Text;
2017-03-23 07:44:52 +08:00
protected override string Label
{
get { return Text.Text; }
set { Text.Text = value; }
2017-03-23 07:44:52 +08:00
}
protected readonly SpriteIcon Icon;
2017-03-24 16:59:26 +08:00
private Color4 accentColour;
public virtual Color4 AccentColour
{
get { return accentColour; }
set
{
accentColour = value;
BackgroundColourHover = accentColour;
}
}
2017-03-23 07:44:52 +08:00
public OsuDropdownHeader()
2017-05-19 04:43:39 +08:00
{
2017-03-23 07:44:52 +08: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 16:59:26 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
Icon = new SpriteIcon
2017-03-24 16:59:26 +08:00
{
Icon = FontAwesome.fa_chevron_down,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 4 },
Size = new Vector2(20),
},
2017-03-23 07:44:52 +08:00
};
AddInternal(new HoverClickSounds());
2017-03-23 07:44:52 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = colours.PinkDarker;
2017-03-23 07:44:52 +08:00
}
}
2017-03-22 06:51:26 +08:00
}
}