1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 16:27:43 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuDropdown.cs

437 lines
15 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
{
public partial class OsuDropdown<T> : Dropdown<T>, IKeyBindingHandler<GlobalAction>
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();
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat) return false;
if (e.Action == GlobalAction.Back)
return Back();
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2018-04-13 17:19:50 +08:00
#region OsuDropdownMenu
2019-02-28 12:31:40 +08:00
protected partial class OsuDropdownMenu : DropdownMenu
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);
Foreground.AutoSizeAxes = Axes.Y;
Foreground.RelativeSizeAxes = Axes.X;
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()
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 TruncatingSpriteText
2018-04-13 17:19:50 +08:00
{
Padding = new MarginPadding { Left = 15 },
2018-04-13 17:19:50 +08:00
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
RelativeSizeAxes = Axes.X,
2018-04-13 17:19:50 +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);
}
}
}
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
}
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[]
{
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),
},
}
}
2018-04-13 17:19:50 +08:00
};
AddInternal(new HoverClickSounds());
2018-04-13 17:19:50 +08:00
}
[Resolved]
private OverlayColourProvider? colourProvider { get; set; }
[Resolved]
private OsuColour colours { get; set; } = null!;
protected override void LoadComplete()
2018-04-13 17:19:50 +08:00
{
base.LoadComplete();
SearchBar.State.ValueChanged += _ => updateColour();
Enabled.BindValueChanged(_ => updateColour());
updateColour();
}
protected override bool OnHover(HoverEvent e)
{
updateColour();
return false;
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateColour();
}
private void updateColour()
{
bool hovered = Enabled.Value && IsHovered;
var hoveredColour = colourProvider?.Light4 ?? colours.PinkDarker;
var unhoveredColour = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
Colour = Color4.White;
Alpha = Enabled.Value ? 1 : 0.3f;
if (SearchBar.State.Value == Visibility.Visible)
{
Icon.Colour = hovered ? hoveredColour.Lighten(0.5f) : Colour4.White;
Background.Colour = unhoveredColour;
}
else
{
Icon.Colour = Color4.White;
Background.Colour = hovered ? hoveredColour : unhoveredColour;
}
}
protected override DropdownSearchBar CreateSearchBar() => new OsuDropdownSearchBar
{
2023-12-18 21:28:23 +08:00
Padding = new MarginPadding { Right = 26 },
};
private partial class OsuDropdownSearchBar : DropdownSearchBar
{
protected override void PopIn() => this.FadeIn();
protected override void PopOut() => this.FadeOut();
protected override TextBox CreateTextBox() => new DropdownSearchTextBox
{
FontSize = OsuFont.Default.Size,
};
private partial class DropdownSearchTextBox : SearchTextBox
{
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Action == GlobalAction.Back)
// this method is blocking Dropdown from receiving the back action, despite this text box residing in a separate input manager.
// to fix this properly, a local global action container needs to be added as well, but for simplicity, just don't handle the back action here.
return false;
return base.OnPressed(e);
}
}
2018-04-13 17:19:50 +08:00
}
}
}
}