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