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/FormTextBox.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

246 lines
7.8 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.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osuTK;
namespace osu.Game.Graphics.UserInterfaceV2
{
public partial class FormTextBox : CompositeDrawable, IHasCurrentValue<string>, IFormControl
{
public Bindable<string> Current
{
get => current.Current;
set => current.Current = value;
}
private bool readOnly;
public bool ReadOnly
{
get => readOnly;
set
{
readOnly = value;
if (textBox.IsNotNull())
updateState();
}
}
private CompositeDrawable? tabbableContentContainer;
public CompositeDrawable? TabbableContentContainer
{
set
{
tabbableContentContainer = value;
if (textBox.IsNotNull())
textBox.TabbableContentContainer = tabbableContentContainer;
}
}
public event TextBox.OnCommitHandler? OnCommit;
private readonly BindableWithCurrent<string> current = new BindableWithCurrent<string>();
/// <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; init; }
/// <summary>
/// Text displayed in the text box when its contents are empty.
/// </summary>
public LocalisableString PlaceholderText { get; init; }
/// <summary>
/// Maximum allowed length of text.
/// </summary>
public int? LengthLimit { get; init; }
private FormControlBackground background = null!;
private InnerTextBox textBox = null!;
private FormFieldCaption caption = null!;
private IFocusManager focusManager = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
background = new FormControlBackground(),
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding(9),
Spacing = new Vector2(0, 4),
Children = new Drawable[]
{
caption = new FormFieldCaption
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Caption = Caption,
TooltipText = HintText,
},
textBox = CreateTextBox().With(t =>
{
t.RelativeSizeAxes = Axes.X;
t.Width = 1;
t.PlaceholderText = PlaceholderText;
t.LengthLimit = LengthLimit;
t.Current = Current;
t.CommitOnFocusLost = true;
t.OnCommit += (textBox, newText) =>
{
OnCommit?.Invoke(textBox, newText);
if (!current.Disabled && !ReadOnly)
background.FlashOnCommit();
};
t.OnInputError = () => background.FlashOnInputError();
t.TabbableContentContainer = tabbableContentContainer;
}),
},
},
};
}
internal virtual InnerTextBox CreateTextBox() => new InnerTextBox();
protected override void LoadComplete()
{
base.LoadComplete();
focusManager = GetContainingFocusManager()!;
textBox.Focused.BindValueChanged(_ => updateState());
current.BindValueChanged(_ => ValueChanged?.Invoke());
current.BindDisabledChanged(_ => updateState(), true);
}
protected override bool OnHover(HoverEvent e)
{
updateState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateState();
}
protected override bool OnClick(ClickEvent e)
{
focusManager.ChangeFocus(textBox);
return true;
}
private void updateState()
{
bool disabled = Current.Disabled || ReadOnly;
textBox.ReadOnly = disabled;
textBox.Alpha = 1;
caption.Colour = disabled ? colourProvider.Background1 : colourProvider.Content2;
textBox.Colour = disabled ? colourProvider.Foreground1 : colourProvider.Content1;
if (Current.Disabled)
background.VisualStyle = VisualStyle.Disabled;
else if (textBox.Focused.Value)
background.VisualStyle = VisualStyle.Focused;
else if (IsHovered)
background.VisualStyle = VisualStyle.Hovered;
else
background.VisualStyle = VisualStyle.Normal;
}
internal partial class InnerTextBox : OsuTextBox
{
public BindableBool Focused { get; } = new BindableBool();
public Action? OnInputError { get; set; }
protected override float LeftRightPadding => 0;
public InnerTextBox()
{
DrawBorder = false;
}
[BackgroundDependencyLoader]
private void load()
{
Height = 16;
TextContainer.Height = 1;
BackgroundUnfocused = BackgroundFocused = BackgroundCommit = Colour4.Transparent;
}
protected override SpriteText CreatePlaceholder() => base.CreatePlaceholder().With(t => t.Margin = default);
protected override void OnFocus(FocusEvent e)
{
base.OnFocus(e);
Focused.Value = true;
}
protected override void OnFocusLost(FocusLostEvent e)
{
base.OnFocusLost(e);
Focused.Value = false;
}
protected override void NotifyInputError()
{
PlayFeedbackSample(FeedbackSampleType.TextInvalid);
// base call intentionally suppressed
OnInputError?.Invoke();
}
}
public event Action? ValueChanged;
public bool IsDefault => current.IsDefault;
public void SetDefault() => current.SetDefault();
public bool IsDisabled => current.Disabled;
public IEnumerable<LocalisableString> FilterTerms => Caption.Yield();
public float MainDrawHeight => DrawHeight;
}
}