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

303 lines
10 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System.Linq;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2021-06-18 17:57:40 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class OsuDropdown<T> : Dropdown<T>, IHasAccentColour
{
2020-09-08 18:04:46 +08:00
private const float corner_radius = 4;
2018-04-13 17:19:50 +08:00
private Color4 accentColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour;
2018-04-13 17:19:50 +08:00
set
{
accentColour = value;
updateAccentColour();
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2018-10-16 10:40:51 +08:00
if (accentColour == default)
2018-04-13 17:19:50 +08:00
accentColour = colours.PinkDarker;
updateAccentColour();
}
private void updateAccentColour()
{
2019-02-28 13:35:00 +08:00
if (Header is IHasAccentColour header) header.AccentColour = accentColour;
2018-04-13 17:19:50 +08:00
2019-02-28 13:35:00 +08:00
if (Menu is IHasAccentColour menu) menu.AccentColour = accentColour;
2018-04-13 17:19:50 +08:00
}
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader();
protected override DropdownMenu CreateMenu() => new OsuDropdownMenu();
#region OsuDropdownMenu
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
protected class OsuDropdownMenu : DropdownMenu, IHasAccentColour
{
2018-10-06 20:12:29 +08:00
public override bool HandleNonPositionalInput => State == MenuState.Open;
2021-06-18 17:57:40 +08:00
private Sample sampleOpen;
private Sample sampleClose;
2018-04-13 17:19:50 +08:00
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
public OsuDropdownMenu()
{
2020-09-08 18:04:46 +08:00
CornerRadius = corner_radius;
2018-04-13 17:19:50 +08:00
BackgroundColour = Color4.Black.Opacity(0.5f);
2020-09-08 18:04:46 +08:00
MaskingContainer.CornerRadius = corner_radius;
2018-04-13 17:19:50 +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);
}
2021-06-18 17:57:40 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleOpen = audio.Samples.Get(@"UI/dropdown-open");
sampleClose = audio.Samples.Get(@"UI/dropdown-close");
}
// todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
private bool wasOpened;
2018-04-13 17:19:50 +08:00
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
2021-06-18 17:57:40 +08:00
protected override void AnimateOpen()
{
wasOpened = true;
2021-06-18 17:57:40 +08:00
this.FadeIn(300, Easing.OutQuint);
sampleOpen?.Play();
}
protected override void AnimateClose()
{
this.FadeOut(300, Easing.OutQuint);
if (wasOpened)
sampleClose?.Play();
2021-06-18 17:57:40 +08:00
}
2018-04-13 17:19:50 +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 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);
}
}
private Color4 accentColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour;
2018-04-13 17:19:50 +08:00
set
{
accentColour = value;
foreach (var c in Children.OfType<IHasAccentColour>())
c.AccentColour = value;
}
}
protected override Menu CreateSubMenu() => new OsuMenu(Direction.Vertical);
protected override DrawableDropdownMenuItem CreateDrawableDropdownMenuItem(MenuItem item) => new DrawableOsuDropdownMenuItem(item) { AccentColour = accentColour };
2018-04-13 17:19:50 +08:00
2019-06-21 11:33:49 +08:00
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
2018-04-13 17:19:50 +08:00
#region DrawableOsuDropdownMenuItem
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem, IHasAccentColour
{
// IsHovered is used
public override bool HandlePositionalInput => true;
2018-04-13 17:19:50 +08:00
private Color4? accentColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour ?? nonAccentSelectedColour;
2018-04-13 17:19:50 +08:00
set
{
accentColour = value;
updateColours();
}
}
private void updateColours()
{
BackgroundColourHover = accentColour ?? nonAccentHoverColour;
BackgroundColourSelected = accentColour ?? nonAccentSelectedColour;
UpdateBackgroundColour();
UpdateForegroundColour();
}
private Color4 nonAccentHoverColour;
private Color4 nonAccentSelectedColour;
public DrawableOsuDropdownMenuItem(MenuItem item)
: base(item)
{
Foreground.Padding = new MarginPadding(2);
Masking = true;
2020-09-08 18:04:46 +08:00
CornerRadius = corner_radius;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Transparent;
nonAccentHoverColour = colours.PinkDarker;
nonAccentSelectedColour = Color4.Black.Opacity(0.5f);
updateColours();
2021-06-18 17:57:40 +08:00
AddInternal(new HoverSounds());
2018-04-13 17:19:50 +08:00
}
protected override void UpdateForegroundColour()
{
base.UpdateForegroundColour();
2019-02-28 13:35:00 +08:00
if (Foreground.Children.FirstOrDefault() is Content content) content.Chevron.Alpha = IsHovered ? 1 : 0;
2018-04-13 17:19:50 +08:00
}
protected override Drawable CreateContent() => new Content();
protected new class Content : FillFlowContainer, IHasText
{
public LocalisableString Text
2018-04-13 17:19:50 +08:00
{
get => Label.Text;
set => Label.Text = value;
2018-04-13 17:19:50 +08:00
}
public readonly OsuSpriteText Label;
public readonly SpriteIcon Chevron;
public Content()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Direction = FillDirection.Horizontal;
Children = new Drawable[]
{
Chevron = new SpriteIcon
{
AlwaysPresent = true,
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronRight,
2018-04-13 17:19:50 +08:00
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,
},
};
}
}
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
#endregion
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
#endregion
public class OsuDropdownHeader : DropdownHeader, IHasAccentColour
{
protected readonly SpriteText Text;
2019-02-28 12:31:40 +08:00
protected override LocalisableString Label
2018-04-13 17:19:50 +08:00
{
get => Text.Text;
set => Text.Text = value;
2018-04-13 17:19:50 +08:00
}
protected readonly SpriteIcon Icon;
private Color4 accentColour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public virtual Color4 AccentColour
{
get => accentColour;
2018-04-13 17:19:50 +08:00
set
{
accentColour = value;
BackgroundColourHover = accentColour;
}
}
public OsuDropdownHeader()
{
Foreground.Padding = new MarginPadding(4);
AutoSizeAxes = Axes.None;
Margin = new MarginPadding { Bottom = 4 };
2020-09-08 18:04:46 +08:00
CornerRadius = corner_radius;
2018-04-13 17:19:50 +08:00
Height = 40;
Foreground.Children = new Drawable[]
{
Text = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
Icon = new SpriteIcon
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronDown,
2018-04-13 17:19:50 +08:00
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2019-06-25 18:32:00 +08:00
Margin = new MarginPadding { Right = 5 },
Size = new Vector2(12),
2018-04-13 17:19:50 +08:00
},
};
2021-06-18 17:57:40 +08:00
AddInternal(new HoverSounds());
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = colours.PinkDarker;
}
}
}
}