1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:07:25 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuDropdown.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

367 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;
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;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
2017-03-23 07:44:52 +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
2017-03-22 06:51:26 +08:00
namespace osu.Game.Graphics.UserInterface
{
public partial class OsuDropdown<T> : Dropdown<T>
2017-03-22 06:51:26 +08:00
{
private const float corner_radius = 5;
2020-09-08 18:04:46 +08:00
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader();
2018-04-13 17:19:50 +08:00
protected override DropdownMenu CreateMenu() => new OsuDropdownMenu();
2018-04-13 17:19:50 +08:00
#region OsuDropdownMenu
2019-02-28 12:31:40 +08:00
protected partial class OsuDropdownMenu : DropdownMenu, IKeyBindingHandler<GlobalAction>
2017-03-23 07:44:52 +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
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;
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
[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;
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()
{
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
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
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
{
get => selectionColour;
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
2017-08-25 17:41:12 +08:00
#region DrawableOsuDropdownMenuItem
2019-02-28 12:31:40 +08:00
public partial class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem
2017-03-23 07:44:52 +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
{
get => base.BackgroundColourSelected;
set
{
base.BackgroundColourSelected = value;
updateColours();
}
}
2018-04-13 17:19:50 +08:00
private void updateColours()
{
BackgroundColour = BackgroundColourHover.Opacity(0);
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)
{
Foreground.Padding = new MarginPadding(2);
2018-04-13 17:19:50 +08:00
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()
{
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);
}
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
if (Foreground.Children.FirstOrDefault() is Content content)
content.Hovering = IsHovered;
}
2018-04-13 17:19:50 +08:00
protected override Drawable CreateContent() => new Content();
2018-04-13 17:19:50 +08:00
protected new partial class Content : CompositeDrawable, IHasText
2017-03-23 07:44:52 +08:00
{
public LocalisableString Text
2017-03-23 07:44:52 +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
private const float chevron_offset = -3;
public Content()
2017-08-28 11:33:31 +08:00
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2018-04-13 17:19:50 +08:00
InternalChildren = new Drawable[]
2017-03-23 07:44:52 +08:00
{
2017-08-28 11:33:31 +08:00
Chevron = new SpriteIcon
{
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.ChevronRight,
Size = new Vector2(8),
Alpha = 0,
X = chevron_offset,
Margin = new MarginPadding { Left = 3, Right = 3 },
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
Label = new OsuSpriteText
{
X = 15,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
},
2017-08-28 11:33:31 +08:00
};
}
[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);
}
}
}
}
2017-03-23 07:44:52 +08:00
}
2019-02-28 12:31:40 +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)
{
}
2017-03-23 07:44:52 +08:00
}
2019-02-28 12:31:40 +08:00
#endregion
2018-04-13 17:19:50 +08:00
public partial class OsuDropdownHeader : DropdownHeader
2017-03-23 07:44:52 +08:00
{
protected readonly SpriteText Text;
2019-02-28 12:31:40 +08:00
protected override LocalisableString Label
2017-03-23 07:44:52 +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
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
{
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
Foreground.Child = new GridContainer
2017-03-23 07:44:52 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[]
2017-03-24 16:59:26 +08:00
{
new Dimension(GridSizeMode.AutoSize),
2017-03-24 16:59:26 +08:00
},
ColumnDimensions = new[]
2017-03-24 16:59:26 +08:00
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
},
Content = new[]
{
new Drawable[]
{
2023-06-09 08:11:17 +08:00
Text = new TruncatingSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
},
Icon = new SpriteIcon
{
Icon = FontAwesome.Solid.ChevronDown,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Size = new Vector2(16),
},
}
}
2017-03-23 07:44:52 +08:00
};
2018-04-13 17:19:50 +08:00
AddInternal(new HoverClickSounds());
2017-03-23 07:44:52 +08:00
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider, OsuColour colours)
2017-03-23 07:44:52 +08:00
{
BackgroundColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
BackgroundColourHover = colourProvider?.Light4 ?? colours.PinkDarker;
2017-03-23 07:44:52 +08:00
}
}
2017-03-22 06:51:26 +08:00
}
}