1
0
mirror of https://github.com/ppy/osu.git synced 2026-06-13 16:44:33 +08:00
Files
osu-lazer/osu.Game/Graphics/UserInterfaceV2/FormDropdown.cs
T
Denis Titovets 9143e4936c Fix flashes on some form controls going beyond the borders (#37903)
changes can be reviewed commit by commit, explanations are attached

| master | 58c677007e |
2f21914ee2 |
|-|-|-|
| <img width="517" height="86" alt="Снимок экрана 2026-05-23 235003"
src="https://github.com/user-attachments/assets/6c11bdac-118e-4f63-883a-ec6a802b94a9"
/> | <img width="514" height="86" alt="Снимок экрана 2026-05-23 235109"
src="https://github.com/user-attachments/assets/d46aac3e-08d1-4462-b4f6-083c1e321c09"
/> | <img width="516" height="82" alt="Снимок экрана 2026-05-24 142215"
src="https://github.com/user-attachments/assets/063317f2-7643-4fe6-bae8-e8e8c4145641"
/> |
| <img width="521" height="90" alt="Снимок экрана 2026-05-24 142702"
src="https://github.com/user-attachments/assets/5530fd71-a2d8-4700-827d-485ca41bc1a6"
/> | <img width="518" height="90" alt="Снимок экрана 2026-05-24 142552"
src="https://github.com/user-attachments/assets/35865f21-9120-4384-ab32-7dd135dd907e"
/> | <img width="526" height="89" alt="Снимок экрана 2026-05-24 142412"
src="https://github.com/user-attachments/assets/27ea3af6-9b1d-400b-9f02-eee453e23b64"
/> |

master


https://github.com/user-attachments/assets/0d37c1b1-ab1d-4f07-ac3a-024fa4af5fc3

58c677007e


https://github.com/user-attachments/assets/a315a5da-330a-4dec-b323-ac11f081939a

2f21914ee2


https://github.com/user-attachments/assets/b8197983-2be7-478b-b5b1-c554b72d56e2

---------

Co-authored-by: Bartłomiej Dach <dach.bartlomiej@gmail.com>
2026-06-10 14:11:30 +02:00

323 lines
11 KiB
C#

// 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.
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Resources.Localisation.Web;
using osuTK;
namespace osu.Game.Graphics.UserInterfaceV2
{
public partial class FormDropdown<T> : OsuDropdown<T>, IFormControl
{
/// <summary>
/// Caption describing this slider bar, displayed on top of the controls.
/// </summary>
public LocalisableString Caption { get; init; }
/// <summary>
/// Hint text containing an extended description of this slider bar, displayed in a tooltip when hovering the caption.
/// </summary>
public LocalisableString HintText
{
get => header.HintText;
set => header.HintText = value;
}
/// <summary>
/// The maximum height of the dropdown's menu.
/// By default, this is set to 200px high. Set to <see cref="float.PositiveInfinity"/> to remove such limit.
/// </summary>
public float MaxHeight { get; set; } = 200;
private FormDropdownHeader header = null!;
private const float header_menu_spacing = 5;
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.X;
header.Caption = Caption;
header.HintText = HintText;
}
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(_ => ValueChanged?.Invoke());
}
public virtual IEnumerable<LocalisableString> FilterTerms
{
get
{
yield return Caption;
foreach (var item in MenuItems)
yield return item.Text.Value;
}
}
public event Action? ValueChanged;
public bool IsDefault => Current.IsDefault;
public void SetDefault() => Current.SetDefault();
public bool IsDisabled => Current.Disabled;
public float MainDrawHeight => header.DrawHeight;
protected override DropdownHeader CreateHeader() => header = new FormDropdownHeader
{
Dropdown = this,
};
protected override DropdownMenu CreateMenu() => new FormDropdownMenu
{
MaxHeight = MaxHeight,
};
private partial class FormDropdownHeader : DropdownHeader
{
public FormDropdown<T> Dropdown { get; set; } = null!;
protected override DropdownSearchBar CreateSearchBar() => SearchBar = new FormDropdownSearchBar();
private LocalisableString captionText;
private LocalisableString hintText;
private LocalisableString labelText;
public LocalisableString Caption
{
get => captionText;
set
{
captionText = value;
if (caption.IsNotNull())
caption.Caption = value;
}
}
public LocalisableString HintText
{
get => hintText;
set
{
hintText = value;
if (caption.IsNotNull())
caption.TooltipText = value;
}
}
protected override LocalisableString Label
{
get => labelText;
set
{
labelText = value;
if (label.IsNotNull())
label.Text = labelText;
}
}
protected new FormDropdownSearchBar SearchBar { get; set; } = null!;
private FormFieldCaption caption = null!;
private OsuSpriteText label = null!;
private SpriteIcon chevron = null!;
private FormControlBackground background = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[BackgroundDependencyLoader]
private void load()
{
// We use our own background for more control.
Background.Alpha = 0;
Foreground.Children = new Drawable[]
{
background = new FormControlBackground(),
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding(9),
Children = new Drawable[]
{
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 4),
Children = new Drawable[]
{
caption = new FormFieldCaption
{
Caption = Caption,
TooltipText = HintText,
},
label = new TruncatingSpriteText
{
RelativeSizeAxes = Axes.X,
Padding = new MarginPadding { Right = 25 },
AlwaysPresent = true,
},
}
},
chevron = new SpriteIcon
{
Icon = FontAwesome.Solid.ChevronDown,
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
Size = new Vector2(16),
Margin = new MarginPadding { Right = 5 },
},
}
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Dropdown.Current.BindDisabledChanged(_ => updateState());
SearchBar.SearchTerm.BindValueChanged(_ => updateState(), true);
Dropdown.Menu.StateChanged += _ =>
{
updateState();
updateChevron();
};
SearchBar.TextBox.OnCommit += (_, _) =>
{
Background.FlashColour(ColourInfo.GradientVertical(colourProvider.Background5, colourProvider.Dark2), 800, Easing.OutQuint);
};
}
protected override bool OnHover(HoverEvent e)
{
updateState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateState();
}
private void updateState()
{
caption.Colour = Dropdown.Current.Disabled ? colourProvider.Background1 : colourProvider.Content2;
label.Colour = Dropdown.Current.Disabled ? colourProvider.Background1 : colourProvider.Content1;
chevron.Colour = Dropdown.Current.Disabled ? colourProvider.Background1 : colourProvider.Content1;
DisabledColour = Colour4.White;
bool dropdownOpen = Dropdown.Menu.State == MenuState.Open;
if (dropdownOpen)
label.Alpha = AlwaysShowSearchBar || !string.IsNullOrEmpty(SearchBar.SearchTerm.Value) ? 0 : 1;
else
label.Alpha = 1;
if (Dropdown.Current.Disabled)
background.VisualStyle = VisualStyle.Disabled;
else if (dropdownOpen)
background.VisualStyle = VisualStyle.Focused;
else if (IsHovered)
background.VisualStyle = VisualStyle.Hovered;
else
background.VisualStyle = VisualStyle.Normal;
}
private void updateChevron()
{
bool open = Dropdown.Menu.State == MenuState.Open;
chevron.ScaleTo(open ? new Vector2(1f, -1f) : Vector2.One, 300, Easing.OutQuint);
chevron.MoveToY(open ? -chevron.DrawHeight : 0, 300, Easing.OutQuint);
}
}
private partial class FormDropdownSearchBar : DropdownSearchBar
{
public FormTextBox.InnerTextBox TextBox { get; private set; } = null!;
protected override void PopIn() => this.FadeIn();
protected override void PopOut() => this.FadeOut();
protected override TextBox CreateTextBox() => TextBox = new FormTextBox.InnerTextBox
{
PlaceholderText = HomeStrings.SearchPlaceholder,
};
[BackgroundDependencyLoader]
private void load()
{
TextBox.Anchor = Anchor.BottomLeft;
TextBox.Origin = Anchor.BottomLeft;
TextBox.RelativeSizeAxes = Axes.X;
Padding = new MarginPadding { Left = 9, Bottom = 9, Right = 34 };
}
}
private partial class FormDropdownMenu : OsuDropdownMenu
{
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
ItemsContainer.Padding = new MarginPadding(9);
MaskingContainer.BorderThickness = FormControlBackground.BORDER_THICKNESS;
MaskingContainer.CornerExponent = FormControlBackground.CORNER_EXPONENT;
MaskingContainer.BorderColour = colourProvider.Highlight1;
}
protected override void AnimateOpen()
{
base.AnimateOpen();
this.TransformTo(nameof(Margin), new MarginPadding
{
Top = header_menu_spacing,
}, 300, Easing.OutQuint);
}
protected override void AnimateClose()
{
base.AnimateClose();
this.TransformTo(nameof(Margin), new MarginPadding(), 300, Easing.OutQuint);
}
}
}
public partial class FormEnumDropdown<T> : FormDropdown<T>
where T : struct, Enum
{
public FormEnumDropdown()
{
Items = Enum.GetValues<T>();
}
}
}