From c973739b084d14fa11767516c1c1987bd3c58c6b Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Fri, 24 Dec 2021 11:50:48 +0900 Subject: [PATCH] Fix OsuButton event binding + inital-enablement colour --- .../UserInterface/TestSceneOsuButton.cs | 46 +++++++++++++++++++ osu.Game/Graphics/UserInterface/OsuButton.cs | 14 ++---- 2 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 osu.Game.Tests/Visual/UserInterface/TestSceneOsuButton.cs diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuButton.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuButton.cs new file mode 100644 index 0000000000..9d086cce5c --- /dev/null +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuButton.cs @@ -0,0 +1,46 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using NUnit.Framework; +using osu.Framework.Graphics; +using osu.Game.Graphics.UserInterface; +using osuTK; + +namespace osu.Game.Tests.Visual.UserInterface +{ + public class TestSceneOsuButton : OsuTestScene + { + [Test] + public void TestToggleEnabled() + { + OsuButton button = null; + + AddStep("add button", () => Child = button = new OsuButton + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(200), + Text = "Button" + }); + + AddToggleStep("toggle enabled", toggle => + { + for (int i = 0; i < 6; i++) + button.Action = toggle ? () => { } : (Action)null; + }); + } + + [Test] + public void TestInitiallyDisabled() + { + AddStep("add button", () => Child = new OsuButton + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(200), + Text = "Button" + }); + } + } +} diff --git a/osu.Game/Graphics/UserInterface/OsuButton.cs b/osu.Game/Graphics/UserInterface/OsuButton.cs index 82a3e73b84..b132f41335 100644 --- a/osu.Game/Graphics/UserInterface/OsuButton.cs +++ b/osu.Game/Graphics/UserInterface/OsuButton.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using osu.Framework.Allocation; -using osu.Framework.Bindables; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -85,8 +84,6 @@ namespace osu.Game.Graphics.UserInterface if (hoverSounds.HasValue) AddInternal(new HoverClickSounds(hoverSounds.Value)); - - Enabled.BindValueChanged(enabledChanged, true); } [BackgroundDependencyLoader] @@ -95,10 +92,12 @@ namespace osu.Game.Graphics.UserInterface if (backgroundColour == null) BackgroundColour = colours.BlueDark; - Enabled.ValueChanged += enabledChanged; - Enabled.TriggerChange(); + Colour = enablementColour; + Enabled.BindValueChanged(_ => this.FadeColour(enablementColour, 200, Easing.OutQuint)); } + private Color4 enablementColour => Enabled.Value ? Color4.White : Color4.Gray; + protected override bool OnClick(ClickEvent e) { if (Enabled.Value) @@ -144,10 +143,5 @@ namespace osu.Game.Graphics.UserInterface Anchor = Anchor.Centre, Font = OsuFont.GetFont(weight: FontWeight.Bold) }; - - private void enabledChanged(ValueChangedEvent e) - { - this.FadeColour(e.NewValue ? Color4.White : Color4.Gray, 200, Easing.OutQuint); - } } }