2019-01-24 16:43:03 +08:00
|
|
|
// 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
|
|
|
|
2021-10-14 04:03:08 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
using System.Linq;
|
2017-03-22 06:51:26 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-06-18 17:57:40 +08:00
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Audio.Sample;
|
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;
|
2021-02-22 16:14:00 +08:00
|
|
|
using osu.Framework.Localisation;
|
2019-06-25 05:17:07 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2017-03-23 07:44:52 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-10-14 04:03:08 +08:00
|
|
|
using osu.Game.Overlays;
|
2018-11-20 15:51:59 +08:00
|
|
|
using osuTK;
|
2021-10-14 12:34:08 +08:00
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-03-22 06:51:26 +08:00
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
2021-10-17 00:33:17 +08:00
|
|
|
public class OsuDropdown<T> : Dropdown<T>
|
2017-03-22 06:51:26 +08:00
|
|
|
{
|
2021-10-04 00:11:22 +08:00
|
|
|
private const float corner_radius = 5;
|
2020-09-08 18:04:46 +08:00
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
protected override DropdownMenu CreateMenu() => new OsuDropdownMenu();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-25 14:58:21 +08:00
|
|
|
#region OsuDropdownMenu
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
protected class OsuDropdownMenu : DropdownMenu
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2018-10-06 20:12:29 +08:00
|
|
|
public override bool HandleNonPositionalInput => State == MenuState.Open;
|
2018-10-06 20:06:26 +08:00
|
|
|
|
2021-10-14 04:03:08 +08:00
|
|
|
private Sample? sampleOpen;
|
|
|
|
private Sample? sampleClose;
|
2021-06-18 17:57:40 +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()
|
|
|
|
{
|
2020-09-08 18:04:46 +08:00
|
|
|
CornerRadius = corner_radius;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-09-08 18:04:46 +08:00
|
|
|
MaskingContainer.CornerRadius = corner_radius;
|
2021-08-16 17:56:02 +08:00
|
|
|
Alpha = 0;
|
2020-09-08 18:04:46 +08:00
|
|
|
|
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
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 04:03:08 +08:00
|
|
|
[BackgroundDependencyLoader(true)]
|
2021-10-17 00:33:17 +08:00
|
|
|
private void load(OverlayColourProvider? colourProvider, OsuColour colours, AudioManager audio)
|
2021-06-18 17:57:40 +08:00
|
|
|
{
|
2021-10-14 04:03:08 +08:00
|
|
|
BackgroundColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
|
2021-10-20 04:55:49 +08:00
|
|
|
HoverColour = colourProvider?.Light4 ?? colours.PinkDarker;
|
|
|
|
SelectionColour = colourProvider?.Background3 ?? colours.PinkDarker.Opacity(0.5f);
|
2021-10-14 04:03:08 +08:00
|
|
|
|
2021-06-18 17:57:40 +08:00
|
|
|
sampleOpen = audio.Samples.Get(@"UI/dropdown-open");
|
|
|
|
sampleClose = audio.Samples.Get(@"UI/dropdown-close");
|
|
|
|
}
|
|
|
|
|
2021-06-18 21:08:59 +08:00
|
|
|
// todo: this shouldn't be required after https://github.com/ppy/osu-framework/issues/4519 is fixed.
|
|
|
|
private bool wasOpened;
|
|
|
|
|
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
|
2021-06-18 17:57:40 +08:00
|
|
|
protected override void AnimateOpen()
|
|
|
|
{
|
2021-06-18 21:08:59 +08:00
|
|
|
wasOpened = true;
|
2021-06-18 17:57:40 +08:00
|
|
|
this.FadeIn(300, Easing.OutQuint);
|
|
|
|
sampleOpen?.Play();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void AnimateClose()
|
|
|
|
{
|
2021-06-18 21:08:59 +08:00
|
|
|
if (wasOpened)
|
2021-08-16 17:56:02 +08:00
|
|
|
{
|
|
|
|
this.FadeOut(300, Easing.OutQuint);
|
2021-06-18 21:08:59 +08:00
|
|
|
sampleClose?.Play();
|
2021-08-16 17:56:02 +08:00
|
|
|
}
|
2021-06-18 17:57:40 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +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
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
private Color4 hoverColour;
|
|
|
|
|
|
|
|
public Color4 HoverColour
|
|
|
|
{
|
|
|
|
get => hoverColour;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
hoverColour = value;
|
|
|
|
foreach (var c in Children.OfType<DrawableOsuDropdownMenuItem>())
|
|
|
|
c.BackgroundColourHover = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Color4 selectionColour;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
public Color4 SelectionColour
|
2017-08-25 14:58:21 +08:00
|
|
|
{
|
2021-10-17 00:33:17 +08:00
|
|
|
get => selectionColour;
|
2017-08-29 17:17:01 +08:00
|
|
|
set
|
|
|
|
{
|
2021-10-17 00:33:17 +08:00
|
|
|
selectionColour = value;
|
|
|
|
foreach (var c in Children.OfType<DrawableOsuDropdownMenuItem>())
|
|
|
|
c.BackgroundColourSelected = value;
|
2017-08-29 17:17:01 +08:00
|
|
|
}
|
2017-08-25 14:58:21 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-06-25 05:17:07 +08:00
|
|
|
protected override Menu CreateSubMenu() => new OsuMenu(Direction.Vertical);
|
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
protected override DrawableDropdownMenuItem CreateDrawableDropdownMenuItem(MenuItem item) => new DrawableOsuDropdownMenuItem(item)
|
|
|
|
{
|
|
|
|
BackgroundColourHover = HoverColour,
|
|
|
|
BackgroundColourSelected = SelectionColour
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2022-04-28 17:11:06 +08:00
|
|
|
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
|
2019-06-21 11:33:49 +08:00
|
|
|
|
2017-08-25 17:41:12 +08:00
|
|
|
#region DrawableOsuDropdownMenuItem
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
public class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2018-10-02 13:41:18 +08:00
|
|
|
// IsHovered is used
|
|
|
|
public override bool HandlePositionalInput => true;
|
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
public new Color4 BackgroundColourHover
|
|
|
|
{
|
|
|
|
get => base.BackgroundColourHover;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
base.BackgroundColourHover = value;
|
|
|
|
updateColours();
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
public new Color4 BackgroundColourSelected
|
2017-08-29 17:17:01 +08:00
|
|
|
{
|
2021-10-17 00:33:17 +08:00
|
|
|
get => base.BackgroundColourSelected;
|
2017-08-29 17:17:01 +08:00
|
|
|
set
|
|
|
|
{
|
2021-10-17 00:33:17 +08:00
|
|
|
base.BackgroundColourSelected = value;
|
2017-08-29 17:17:01 +08:00
|
|
|
updateColours();
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
private void updateColours()
|
|
|
|
{
|
2021-10-14 12:34:08 +08:00
|
|
|
BackgroundColour = BackgroundColourHover.Opacity(0);
|
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
UpdateBackgroundColour();
|
|
|
|
UpdateForegroundColour();
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-28 13:42:52 +08:00
|
|
|
public DrawableOsuDropdownMenuItem(MenuItem item)
|
2017-08-25 17:41:12 +08:00
|
|
|
: base(item)
|
2017-08-25 14:58:21 +08:00
|
|
|
{
|
|
|
|
Foreground.Padding = new MarginPadding(2);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-25 14:58:21 +08:00
|
|
|
Masking = true;
|
2020-09-08 18:04:46 +08:00
|
|
|
CornerRadius = corner_radius;
|
2017-08-25 14:58:21 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-25 14:58:21 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2021-10-17 00:33:17 +08:00
|
|
|
private void load()
|
2017-08-25 14:58:21 +08:00
|
|
|
{
|
2021-06-18 17:57:40 +08:00
|
|
|
AddInternal(new HoverSounds());
|
2017-08-25 14:58:21 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 12:34:08 +08:00
|
|
|
protected override void UpdateBackgroundColour()
|
|
|
|
{
|
|
|
|
if (!IsPreSelected && !IsSelected)
|
|
|
|
{
|
|
|
|
Background.FadeOut(600, Easing.OutQuint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Background.FadeIn(100, Easing.OutQuint);
|
|
|
|
Background.FadeColour(IsPreSelected ? BackgroundColourHover : BackgroundColourSelected, 100, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
|
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();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 12:34:08 +08:00
|
|
|
if (Foreground.Children.FirstOrDefault() is Content content)
|
|
|
|
content.Hovering = IsHovered;
|
2017-08-25 14:58:21 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
protected override Drawable CreateContent() => new Content();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 12:34:08 +08:00
|
|
|
protected new class Content : CompositeDrawable, IHasText
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2021-02-22 16:14:00 +08:00
|
|
|
public LocalisableString Text
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => Label.Text;
|
|
|
|
set => Label.Text = value;
|
2017-08-28 11:33:31 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-28 11:33:31 +08:00
|
|
|
public readonly OsuSpriteText Label;
|
|
|
|
public readonly SpriteIcon Chevron;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 12:34:08 +08:00
|
|
|
private const float chevron_offset = -3;
|
|
|
|
|
2017-08-29 17:17:01 +08:00
|
|
|
public Content()
|
2017-08-28 11:33:31 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 12:34:08 +08:00
|
|
|
InternalChildren = new Drawable[]
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2017-08-28 11:33:31 +08:00
|
|
|
Chevron = new SpriteIcon
|
2017-08-28 10:01:53 +08:00
|
|
|
{
|
2019-04-02 18:55:24 +08:00
|
|
|
Icon = FontAwesome.Solid.ChevronRight,
|
2017-08-28 10:01:53 +08:00
|
|
|
Size = new Vector2(8),
|
2021-10-14 12:34:08 +08:00
|
|
|
Alpha = 0,
|
|
|
|
X = chevron_offset,
|
2017-08-28 10:01:53 +08:00
|
|
|
Margin = new MarginPadding { Left = 3, Right = 3 },
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
},
|
|
|
|
Label = new OsuSpriteText
|
|
|
|
{
|
2021-10-14 12:34:08 +08:00
|
|
|
X = 15,
|
2017-08-28 10:01:53 +08:00
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
Anchor = Anchor.CentreLeft,
|
2017-11-26 01:41:18 +08:00
|
|
|
},
|
2017-08-28 11:33:31 +08:00
|
|
|
};
|
|
|
|
}
|
2021-10-14 04:08:23 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(OverlayColourProvider? colourProvider)
|
|
|
|
{
|
|
|
|
Chevron.Colour = colourProvider?.Background5 ?? Color4.Black;
|
|
|
|
}
|
2021-10-14 12:34:08 +08:00
|
|
|
|
|
|
|
private bool hovering;
|
|
|
|
|
|
|
|
public bool Hovering
|
|
|
|
{
|
|
|
|
get => hovering;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == hovering)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hovering = value;
|
|
|
|
|
|
|
|
if (hovering)
|
|
|
|
{
|
|
|
|
Chevron.FadeIn(400, Easing.OutQuint);
|
|
|
|
Chevron.MoveToX(0, 400, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Chevron.FadeOut(200);
|
|
|
|
Chevron.MoveToX(chevron_offset, 200, Easing.In);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 10:01:53 +08:00
|
|
|
}
|
2017-03-23 07:44:52 +08:00
|
|
|
}
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2017-08-25 14:58:21 +08:00
|
|
|
#endregion
|
2017-03-23 07:44:52 +08:00
|
|
|
}
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2017-08-25 14:58:21 +08:00
|
|
|
#endregion
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-17 00:33:17 +08:00
|
|
|
public class OsuDropdownHeader : DropdownHeader
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2017-05-25 06:44:48 +08:00
|
|
|
protected readonly SpriteText Text;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2021-02-22 16:14:00 +08:00
|
|
|
protected override LocalisableString Label
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => Text.Text;
|
|
|
|
set => Text.Text = value;
|
2017-03-23 07:44:52 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-03 13:36:21 +08:00
|
|
|
protected readonly SpriteIcon Icon;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-03-23 07:44:52 +08:00
|
|
|
public OsuDropdownHeader()
|
2017-05-19 04:43:39 +08:00
|
|
|
{
|
2021-10-04 00:11:22 +08:00
|
|
|
Foreground.Padding = new MarginPadding(10);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-03-23 07:44:52 +08:00
|
|
|
AutoSizeAxes = Axes.None;
|
|
|
|
Margin = new MarginPadding { Bottom = 4 };
|
2020-09-08 18:04:46 +08:00
|
|
|
CornerRadius = corner_radius;
|
2017-03-23 07:44:52 +08:00
|
|
|
Height = 40;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-09-25 12:57:52 +08:00
|
|
|
Foreground.Child = new GridContainer
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2021-09-25 12:57:52 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
RowDimensions = new[]
|
2017-03-24 16:59:26 +08:00
|
|
|
{
|
2021-09-25 12:57:52 +08:00
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
2017-03-24 16:59:26 +08:00
|
|
|
},
|
2021-09-25 12:57:52 +08:00
|
|
|
ColumnDimensions = new[]
|
2017-03-24 16:59:26 +08:00
|
|
|
{
|
2021-09-25 12:57:52 +08:00
|
|
|
new Dimension(),
|
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
2017-11-26 01:41:18 +08:00
|
|
|
},
|
2021-09-25 12:57:52 +08:00
|
|
|
Content = new[]
|
|
|
|
{
|
|
|
|
new Drawable[]
|
|
|
|
{
|
|
|
|
Text = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Truncate = true,
|
|
|
|
},
|
|
|
|
Icon = new SpriteIcon
|
|
|
|
{
|
|
|
|
Icon = FontAwesome.Solid.ChevronDown,
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
Origin = Anchor.CentreRight,
|
2021-10-04 00:11:22 +08:00
|
|
|
Size = new Vector2(16),
|
2021-09-25 12:57:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 07:44:52 +08:00
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-08-06 19:56:49 +08:00
|
|
|
AddInternal(new HoverClickSounds());
|
2017-03-23 07:44:52 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-14 04:03:08 +08:00
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(OverlayColourProvider? colourProvider, OsuColour colours)
|
2017-03-23 07:44:52 +08:00
|
|
|
{
|
2021-10-14 04:03:08 +08:00
|
|
|
BackgroundColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
|
2021-10-20 04:55:49 +08:00
|
|
|
BackgroundColourHover = colourProvider?.Light4 ?? colours.PinkDarker;
|
2017-03-23 07:44:52 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 06:51:26 +08:00
|
|
|
}
|
2017-05-31 12:57:32 +08:00
|
|
|
}
|