Merge pull request #18191 from frenzibyte/button-use-overlay-colour

Fix buttons no longer coloured using `OverlayColourProvider`
This commit is contained in:
Dan Balasescu 2022-05-10 12:14:10 +09:00 committed by GitHub
commit 8a559ff58a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 39 deletions

View File

@ -1,44 +1,40 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System; using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Testing;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterfaceV2; using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
namespace osu.Game.Tests.Visual.UserInterface namespace osu.Game.Tests.Visual.UserInterface
{ {
public class TestSceneRoundedButton : OsuTestScene public class TestSceneRoundedButton : ThemeComparisonTestScene
{ {
[Test] private readonly BindableBool enabled = new BindableBool(true);
public void TestBasic()
protected override Drawable CreateContent() => new RoundedButton
{ {
RoundedButton button = null; Width = 400,
Text = "Test button",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Enabled = { BindTarget = enabled },
};
AddStep("create button", () => Child = new Container [Test]
{ public void TestDisabled()
RelativeSizeAxes = Axes.Both, {
Children = new Drawable[] AddToggleStep("toggle disabled", disabled => enabled.Value = !disabled);
{ }
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.DarkGray
},
button = new RoundedButton
{
Width = 400,
Text = "Test button",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = () => { }
}
}
});
AddToggleStep("toggle disabled", disabled => button.Action = disabled ? (Action)null : () => { }); [Test]
public void TestBackgroundColour()
{
AddStep("set red scheme", () => CreateThemedContent(OverlayColourScheme.Red));
AddAssert("first button has correct colour", () => Cell(0, 1).ChildrenOfType<RoundedButton>().First().BackgroundColour == new OverlayColourProvider(OverlayColourScheme.Red).Highlight1);
} }
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Diagnostics;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -33,9 +32,12 @@ namespace osu.Game.Graphics.UserInterface
private Color4? backgroundColour; private Color4? backgroundColour;
/// <summary>
/// Sets a custom background colour to this button, replacing the provided default.
/// </summary>
public Color4 BackgroundColour public Color4 BackgroundColour
{ {
get => backgroundColour ?? Color4.White; get => backgroundColour ?? defaultBackgroundColour;
set set
{ {
backgroundColour = value; backgroundColour = value;
@ -43,6 +45,23 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
private Color4 defaultBackgroundColour;
/// <summary>
/// Sets a default background colour to this button.
/// </summary>
protected Color4 DefaultBackgroundColour
{
get => defaultBackgroundColour;
set
{
defaultBackgroundColour = value;
if (backgroundColour == null)
Background.FadeColour(value);
}
}
protected override Container<Drawable> Content { get; } protected override Container<Drawable> Content { get; }
protected Box Hover; protected Box Hover;
@ -89,8 +108,7 @@ namespace osu.Game.Graphics.UserInterface
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)
{ {
if (backgroundColour == null) DefaultBackgroundColour = colours.BlueDark;
BackgroundColour = colours.BlueDark;
} }
protected override void LoadComplete() protected override void LoadComplete()
@ -106,10 +124,7 @@ namespace osu.Game.Graphics.UserInterface
protected override bool OnClick(ClickEvent e) protected override bool OnClick(ClickEvent e)
{ {
if (Enabled.Value) if (Enabled.Value)
{ Background.FlashColour(BackgroundColour.Lighten(0.4f), 200);
Debug.Assert(backgroundColour != null);
Background.FlashColour(backgroundColour.Value.Lighten(0.4f), 200);
}
return base.OnClick(e); return base.OnClick(e);
} }

View File

@ -8,7 +8,6 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays; using osu.Game.Overlays;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterfaceV2 namespace osu.Game.Graphics.UserInterfaceV2
{ {
@ -29,8 +28,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader(true)]
private void load([CanBeNull] OverlayColourProvider overlayColourProvider, OsuColour colours) private void load([CanBeNull] OverlayColourProvider overlayColourProvider, OsuColour colours)
{ {
if (BackgroundColour == Color4.White) DefaultBackgroundColour = overlayColourProvider?.Highlight1 ?? colours.Blue3;
BackgroundColour = overlayColourProvider?.Highlight1 ?? colours.Blue3;
} }
protected override void LoadComplete() protected override void LoadComplete()