1
0
mirror of https://github.com/ppy/osu.git synced 2026-06-13 20:54:24 +08:00
Files
osu-lazer/osu.Game/Graphics/UserInterfaceV2/FormButton.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

265 lines
8.2 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.Diagnostics;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterfaceV2
{
public partial class FormButton : CompositeDrawable
{
/// <summary>
/// Caption describing this button, displayed on the left of it.
/// </summary>
public LocalisableString Caption { get; init; }
/// <summary>
/// Sets text inside the button.
/// </summary>
public LocalisableString ButtonText { get; init; }
/// <summary>
/// Sets a custom button icon. Not shown when <see cref="ButtonText"/> is set.
/// </summary>
public IconUsage ButtonIcon { get; init; } = FontAwesome.Solid.ChevronRight;
private readonly Color4? backgroundColour;
/// <summary>
/// Sets a custom background colour for the button.
/// </summary>
public Color4? BackgroundColour
{
get => backgroundColour;
init
{
backgroundColour = value;
if (IsLoaded)
updateState();
}
}
/// <summary>
/// The action to invoke when the button is clicked.
/// </summary>
public Action? Action { get; set; }
/// <summary>
/// Whether the button is enabled.
/// </summary>
public readonly BindableBool Enabled = new BindableBool(true);
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
private FormControlBackground background = null!;
private OsuTextFlowContainer text = null!;
private Button button = null!;
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Masking = true,
CornerRadius = 5,
CornerExponent = 2.5f,
Children = new Drawable[]
{
background = new FormControlBackground(),
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Left = 9,
Right = 5,
Vertical = 5,
},
Children = new Drawable[]
{
text = new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = Caption,
},
button = new Button
{
Action = () => Action?.Invoke(),
Text = ButtonText,
Icon = ButtonIcon,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Enabled = { BindTarget = Enabled },
}
},
},
}
};
if (ButtonText == default)
{
text.Padding = new MarginPadding { Right = 100 };
button.Width = 90;
}
else
{
text.Width = 0.55f;
text.Padding = new MarginPadding { Right = 10 };
button.RelativeSizeAxes = Axes.X;
button.Width = 0.45f;
}
}
protected override void LoadComplete()
{
base.LoadComplete();
Enabled.BindValueChanged(_ => 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)
{
if (Enabled.Value)
{
background.FlashOnCommit();
button.TriggerClick();
}
return true;
}
private void updateState()
{
text.Colour = Enabled.Value ? colourProvider.Content1 : colourProvider.Background1;
if (!Enabled.Value)
background.VisualStyle = VisualStyle.Disabled;
else if (IsHovered)
background.VisualStyle = VisualStyle.Hovered;
else
background.VisualStyle = VisualStyle.Normal;
// TODO: Support BackgroundColour?
}
public partial class Button : OsuButton
{
private TrianglesV2? triangles { get; set; }
protected override float HoverLayerFinalAlpha => 0;
private Color4? triangleGradientSecondColour;
public override Color4 BackgroundColour
{
get => base.BackgroundColour;
set
{
base.BackgroundColour = value;
triangleGradientSecondColour = BackgroundColour.Lighten(0.2f);
updateColours();
}
}
public IconUsage Icon { get; init; }
[BackgroundDependencyLoader]
private void load(OverlayColourProvider overlayColourProvider)
{
DefaultBackgroundColour = overlayColourProvider.Colour3;
triangleGradientSecondColour ??= DefaultBackgroundColour.Lighten(0.2f);
if (Text == default)
{
Add(new SpriteIcon
{
Icon = Icon,
Size = new Vector2(16),
Shadow = true,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
});
}
}
protected override void LoadComplete()
{
base.LoadComplete();
Content.CornerRadius = 4;
Add(triangles = new TrianglesV2
{
Thickness = 0.02f,
SpawnRatio = 0.6f,
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue,
});
updateColours();
}
private void updateColours()
{
if (triangles == null)
return;
Debug.Assert(triangleGradientSecondColour != null);
triangles.Colour = ColourInfo.GradientVertical(triangleGradientSecondColour.Value, BackgroundColour);
}
protected override bool OnHover(HoverEvent e)
{
Debug.Assert(triangleGradientSecondColour != null);
Background.FadeColour(triangleGradientSecondColour.Value, 300, Easing.OutQuint);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
Background.FadeColour(BackgroundColour, 300, Easing.OutQuint);
base.OnHoverLost(e);
}
}
}
}