1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 06:02:56 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuDropdown.cs

368 lines
13 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;
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.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using osu.Game.Overlays;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
2022-11-24 13:32:20 +08:00
public partial class OsuDropdown<T> : Dropdown<T>
2018-04-13 17:19:50 +08:00
{
private const float corner_radius = 5;
2020-09-08 18:04:46 +08:00
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
2022-11-24 13:32:20 +08:00
protected partial class OsuDropdownMenu : DropdownMenu, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
2018-10-06 20:12:29 +08:00
public override bool HandleNonPositionalInput => State == MenuState.Open;
private Sample? sampleOpen;
private Sample? sampleClose;
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
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;
Alpha = 0;
2020-09-08 18:04:46 +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
ItemsContainer.Padding = new MarginPadding(5);
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider, OsuColour colours, AudioManager audio)
2021-06-18 17:57:40 +08:00
{
BackgroundColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
HoverColour = colourProvider?.Light4 ?? colours.PinkDarker;
SelectionColour = colourProvider?.Background3 ?? colours.PinkDarker.Opacity(0.5f);
2021-06-18 17:57:40 +08:00
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()
{
if (wasOpened)
{
this.FadeOut(300, Easing.OutQuint);
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 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
public Color4 SelectionColour
2018-04-13 17:19:50 +08:00
{
get => selectionColour;
2018-04-13 17:19:50 +08:00
set
{
selectionColour = value;
foreach (var c in Children.OfType<DrawableOsuDropdownMenuItem>())
c.BackgroundColourSelected = value;
2018-04-13 17:19:50 +08:00
}
}
protected override Menu CreateSubMenu() => new OsuMenu(Direction.Vertical);
protected override DrawableDropdownMenuItem CreateDrawableDropdownMenuItem(MenuItem item) => new DrawableOsuDropdownMenuItem(item)
{
BackgroundColourHover = HoverColour,
BackgroundColourSelected = SelectionColour
};
2018-04-13 17:19:50 +08:00
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
2019-06-21 11:33:49 +08:00
2018-04-13 17:19:50 +08:00
#region DrawableOsuDropdownMenuItem
2019-02-28 12:31:40 +08:00
2022-11-24 13:32:20 +08:00
public partial class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem
2018-04-13 17:19:50 +08:00
{
// IsHovered is used
public override bool HandlePositionalInput => true;
public new Color4 BackgroundColourHover
{
get => base.BackgroundColourHover;
set
{
base.BackgroundColourHover = value;
updateColours();
}
}
2019-02-28 12:31:40 +08:00
public new Color4 BackgroundColourSelected
2018-04-13 17:19:50 +08:00
{
get => base.BackgroundColourSelected;
2018-04-13 17:19:50 +08:00
set
{
base.BackgroundColourSelected = value;
2018-04-13 17:19:50 +08:00
updateColours();
}
}
private void updateColours()
{
BackgroundColour = BackgroundColourHover.Opacity(0);
2018-04-13 17:19:50 +08:00
UpdateBackgroundColour();
UpdateForegroundColour();
}
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()
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
}
protected override void UpdateBackgroundColour()
{
Background.FadeColour(IsPreSelected ? BackgroundColourHover : BackgroundColourSelected, 100, Easing.OutQuint);
if (IsPreSelected || IsSelected)
Background.FadeIn(100, Easing.OutQuint);
else
Background.FadeOut(600, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
protected override void UpdateForegroundColour()
{
base.UpdateForegroundColour();
if (Foreground.Children.FirstOrDefault() is Content content)
content.Hovering = IsHovered;
2018-04-13 17:19:50 +08:00
}
protected override Drawable CreateContent() => new Content();
2022-11-24 13:32:20 +08:00
protected new partial class Content : CompositeDrawable, IHasText
2018-04-13 17:19:50 +08:00
{
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;
private const float chevron_offset = -3;
2018-04-13 17:19:50 +08:00
public Content()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
2018-04-13 17:19:50 +08:00
{
Chevron = new SpriteIcon
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronRight,
2018-04-13 17:19:50 +08:00
Size = new Vector2(8),
Alpha = 0,
X = chevron_offset,
2018-04-13 17:19:50 +08:00
Margin = new MarginPadding { Left = 3, Right = 3 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
Label = new OsuSpriteText
{
X = 15,
2018-04-13 17:19:50 +08:00
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
};
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider)
{
Chevron.Colour = colourProvider?.Background5 ?? Color4.Black;
}
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 17:19:50 +08:00
}
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
#endregion
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat) return false;
if (e.Action == GlobalAction.Back)
{
State = MenuState.Closed;
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2018-04-13 17:19:50 +08:00
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
#endregion
2022-11-24 13:32:20 +08:00
public partial class OsuDropdownHeader : DropdownHeader
2018-04-13 17:19:50 +08:00
{
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;
public OsuDropdownHeader()
{
Foreground.Padding = new MarginPadding(10);
2018-04-13 17:19:50 +08:00
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.Child = new GridContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[]
2018-04-13 17:19:50 +08:00
{
new Dimension(GridSizeMode.AutoSize),
2018-04-13 17:19:50 +08:00
},
ColumnDimensions = new[]
2018-04-13 17:19:50 +08:00
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
2018-04-13 17:19:50 +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,
Size = new Vector2(16),
},
}
}
2018-04-13 17:19:50 +08:00
};
AddInternal(new HoverClickSounds());
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider, OsuColour colours)
2018-04-13 17:19:50 +08:00
{
BackgroundColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
BackgroundColourHover = colourProvider?.Light4 ?? colours.PinkDarker;
2018-04-13 17:19:50 +08:00
}
}
}
}