Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buttons no longer coloured using OverlayColourProvider #18191

Merged
merged 5 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions osu.Game.Tests/Visual/UserInterface/TestSceneRoundedButton.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;

namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneRoundedButton : OsuTestScene
public class TestSceneRoundedButton : ThemeComparisonTestScene
{
[Test]
public void TestBasic()
private readonly BindableBool enabled = new BindableBool(true);

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
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.DarkGray
},
button = new RoundedButton
{
Width = 400,
Text = "Test button",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = () => { }
}
}
});
[Test]
public void TestDisabled()
{
AddToggleStep("toggle disabled", disabled => enabled.Value = !disabled);
}

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);
}
}
}
31 changes: 23 additions & 8 deletions osu.Game/Graphics/UserInterface/OsuButton.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Diagnostics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
Expand Down Expand Up @@ -33,16 +32,36 @@ public LocalisableString Text

private Color4? backgroundColour;

/// <summary>
/// Sets a custom background colour to this button, replacing the provided default.
/// </summary>
public Color4 BackgroundColour
{
get => backgroundColour ?? Color4.White;
get => backgroundColour ?? defaultBackgroundColour;
set
{
backgroundColour = value;
Background.FadeColour(value);
}
}

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 Box Hover;
Expand Down Expand Up @@ -89,8 +108,7 @@ public OsuButton(HoverSampleSet? hoverSounds = HoverSampleSet.Button)
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (backgroundColour == null)
BackgroundColour = colours.BlueDark;
DefaultBackgroundColour = colours.BlueDark;
}

protected override void LoadComplete()
Expand All @@ -106,10 +124,7 @@ protected override void LoadComplete()
protected override bool OnClick(ClickEvent e)
{
if (Enabled.Value)
{
Debug.Assert(backgroundColour != null);
Background.FlashColour(backgroundColour.Value.Lighten(0.4f), 200);
}
Background.FlashColour(BackgroundColour.Lighten(0.4f), 200);

return base.OnClick(e);
}
Expand Down
4 changes: 1 addition & 3 deletions osu.Game/Graphics/UserInterfaceV2/RoundedButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osuTK.Graphics;

namespace osu.Game.Graphics.UserInterfaceV2
{
Expand All @@ -29,8 +28,7 @@ public override float Height
[BackgroundDependencyLoader(true)]
private void load([CanBeNull] OverlayColourProvider overlayColourProvider, OsuColour colours)
{
if (BackgroundColour == Color4.White)
BackgroundColour = overlayColourProvider?.Highlight1 ?? colours.Blue3;
DefaultBackgroundColour = overlayColourProvider?.Highlight1 ?? colours.Blue3;
}

protected override void LoadComplete()
Expand Down