From 9164f006aa17ac62a17fda40e4260237fc353cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sat, 23 Oct 2021 18:41:31 +0200 Subject: [PATCH] Implement basic behaviour of play button --- .../Beatmaps/TestSceneBeatmapCardThumbnail.cs | 42 ++++++++++++++++++- .../Drawables/Cards/BeatmapCardThumbnail.cs | 15 +++++-- .../Drawables/Cards/Buttons/PlayButton.cs | 38 ++++++++++++++--- 3 files changed, 83 insertions(+), 12 deletions(-) diff --git a/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardThumbnail.cs b/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardThumbnail.cs index b7c87219ca..73424241cb 100644 --- a/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardThumbnail.cs +++ b/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardThumbnail.cs @@ -1,17 +1,24 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Linq; using NUnit.Framework; using osu.Framework.Allocation; using osu.Framework.Graphics; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Testing; using osu.Game.Beatmaps.Drawables.Cards; +using osu.Game.Beatmaps.Drawables.Cards.Buttons; using osu.Game.Overlays; using osuTK; +using osuTK.Input; namespace osu.Game.Tests.Visual.Beatmaps { - public class TestSceneBeatmapCardThumbnail : OsuTestScene + public class TestSceneBeatmapCardThumbnail : OsuManualInputManagerTestScene { + private PlayButton playButton => this.ChildrenOfType().Single(); + [Cached] private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue); @@ -26,7 +33,38 @@ namespace osu.Game.Tests.Visual.Beatmaps Origin = Anchor.Centre, Size = new Vector2(200) }); - AddToggleStep("toggle dim", dimmed => thumbnail.Dimmed.Value = dimmed); + AddStep("enable dim", () => thumbnail.Dimmed.Value = true); + AddUntilStep("button visible", () => playButton.IsPresent); + + AddStep("click button", () => + { + InputManager.MoveMouseTo(playButton); + InputManager.Click(MouseButton.Left); + }); + iconIs(FontAwesome.Solid.Stop); + + AddStep("click again", () => + { + InputManager.MoveMouseTo(playButton); + InputManager.Click(MouseButton.Left); + }); + iconIs(FontAwesome.Solid.Play); + + AddStep("click again", () => + { + InputManager.MoveMouseTo(playButton); + InputManager.Click(MouseButton.Left); + }); + iconIs(FontAwesome.Solid.Stop); + + AddStep("disable dim", () => thumbnail.Dimmed.Value = false); + AddWaitStep("wait some", 3); + AddAssert("button still visible", () => playButton.IsPresent); + + AddStep("end track playback", () => playButton.Playing.Value = false); + AddUntilStep("button hidden", () => !playButton.IsPresent); } + + private void iconIs(IconUsage usage) => AddAssert("icon is correct", () => playButton.ChildrenOfType().Single().Icon.Equals(usage)); } } diff --git a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardThumbnail.cs b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardThumbnail.cs index 75ed5bdcfe..ce1f856b84 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardThumbnail.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardThumbnail.cs @@ -4,6 +4,7 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Game.Beatmaps.Drawables.Cards.Buttons; using osu.Game.Graphics; using osu.Game.Online.API.Requests.Responses; using osuTK.Graphics; @@ -33,7 +34,10 @@ namespace osu.Game.Beatmaps.Drawables.Cards RelativeSizeAxes = Axes.Both, OnlineInfo = beatmapSetInfo }, - playButton = new PlayButton(), + playButton = new PlayButton(beatmapSetInfo) + { + RelativeSizeAxes = Axes.Both + }, content = new Container { RelativeSizeAxes = Axes.Both @@ -44,14 +48,17 @@ namespace osu.Game.Beatmaps.Drawables.Cards protected override void LoadComplete() { base.LoadComplete(); - Dimmed.BindValueChanged(_ => updateState(), true); + Dimmed.BindValueChanged(_ => updateState()); + playButton.Playing.BindValueChanged(_ => updateState(), true); FinishTransforms(true); } private void updateState() { - playButton.FadeTo(Dimmed.Value ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); - cover.FadeColour(Dimmed.Value ? OsuColour.Gray(0.2f) : Color4.White, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); + bool shouldDim = Dimmed.Value || playButton.Playing.Value; + + playButton.FadeTo(shouldDim ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); + cover.FadeColour(shouldDim ? OsuColour.Gray(0.2f) : Color4.White, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); } } } diff --git a/osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs b/osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs index c1cbac1c59..c48999ed1a 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Sprites; using osu.Game.Graphics; @@ -12,17 +13,30 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons { public class PlayButton : OsuHoverContainer { - public PlayButton() + public BindableBool Playing { get; } = new BindableBool(); + + private readonly BeatmapSetInfo beatmapSetInfo; + + private readonly SpriteIcon icon; + + public PlayButton(BeatmapSetInfo beatmapSetInfo) { + this.beatmapSetInfo = beatmapSetInfo; + Anchor = Origin = Anchor.Centre; - Child = new SpriteIcon + Children = new Drawable[] { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - Icon = FontAwesome.Solid.Play, - Size = new Vector2(14) + icon = new SpriteIcon + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Icon = FontAwesome.Solid.Play, + Size = new Vector2(14) + }, }; + + Action = () => Playing.Toggle(); } [BackgroundDependencyLoader] @@ -30,5 +44,17 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons { HoverColour = colours.Yellow; } + + protected override void LoadComplete() + { + base.LoadComplete(); + + Playing.BindValueChanged(_ => updateState(), true); + } + + private void updateState() + { + icon.Icon = Playing.Value ? FontAwesome.Solid.Stop : FontAwesome.Solid.Play; + } } }