From c5a0672277514f27cd2fb84b99669cf9c80f91c4 Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Tue, 6 Jul 2021 12:07:25 +0200 Subject: [PATCH] Use IStateful instead of ISelected --- .../SelectionCycleFillFlowContainer.cs | 7 ++-- .../Graphics/UserInterface/DialogButton.cs | 35 ++++++++++++------- .../Graphics/UserInterface/ISelectable.cs | 10 ------ osu.Game/Overlays/Volume/VolumeMeter.cs | 32 +++++++++++------ osu.Game/Overlays/VolumeOverlay.cs | 7 ++-- osu.Game/Screens/Play/GameplayMenuOverlay.cs | 8 ++--- 6 files changed, 56 insertions(+), 43 deletions(-) delete mode 100644 osu.Game/Graphics/UserInterface/ISelectable.cs diff --git a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs index 19a423c25b..5849fbbe30 100644 --- a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs +++ b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics.UserInterface; @@ -11,7 +12,7 @@ namespace osu.Game.Graphics.Containers /// A FillFlowContainer that provides functionality to cycle selection between children /// The selection wraps around when overflowing past the first or last child. /// - public class SelectionCycleFillFlowContainer : FillFlowContainer where T : Drawable, ISelectable + public class SelectionCycleFillFlowContainer : FillFlowContainer where T : Drawable, IStateful { private int? selectedIndex; @@ -22,13 +23,13 @@ namespace osu.Game.Graphics.Containers // Deselect the previously-selected button if (selectedIndex.HasValue) - this[selectedIndex.Value].Selected = false; + this[selectedIndex.Value].State = SelectionState.NotSelected; selectedIndex = value; // Select the newly-selected button if (selectedIndex.HasValue) - this[selectedIndex.Value].Selected = true; + this[selectedIndex.Value].State = SelectionState.Selected; } public void SelectNext() diff --git a/osu.Game/Graphics/UserInterface/DialogButton.cs b/osu.Game/Graphics/UserInterface/DialogButton.cs index 718a5171c2..f9fbec2b48 100644 --- a/osu.Game/Graphics/UserInterface/DialogButton.cs +++ b/osu.Game/Graphics/UserInterface/DialogButton.cs @@ -1,6 +1,8 @@ // 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 osu.Framework; using osu.Framework.Bindables; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; @@ -19,7 +21,7 @@ using osuTK.Graphics; namespace osu.Game.Graphics.UserInterface { - public class DialogButton : OsuClickableContainer, ISelectable + public class DialogButton : OsuClickableContainer, IStateful { private const float idle_width = 0.8f; private const float hover_width = 0.9f; @@ -27,12 +29,21 @@ namespace osu.Game.Graphics.UserInterface private const float hover_duration = 500; private const float click_duration = 200; - public readonly BindableBool SelectedBindable = new BindableBool(); + public event Action StateChanged; - public bool Selected + private SelectionState state; + + public SelectionState State { - get => SelectedBindable.Value; - set => SelectedBindable.Value = value; + get => state; + set + { + if (state == value) + return; + + state = value; + StateChanged?.Invoke(value); + } } private readonly Container backgroundContainer; @@ -159,7 +170,7 @@ namespace osu.Game.Graphics.UserInterface updateGlow(); - SelectedBindable.ValueChanged += selectionChanged; + StateChanged += selectionChanged; } private Color4 buttonColour; @@ -227,7 +238,7 @@ namespace osu.Game.Graphics.UserInterface .OnComplete(_ => { clickAnimating = false; - SelectedBindable.TriggerChange(); + StateChanged?.Invoke(State); }); return base.OnClick(e); @@ -241,7 +252,7 @@ namespace osu.Game.Graphics.UserInterface protected override void OnMouseUp(MouseUpEvent e) { - if (Selected) + if (State == SelectionState.Selected) colourContainer.ResizeWidthTo(hover_width, click_duration, Easing.In); base.OnMouseUp(e); } @@ -249,7 +260,7 @@ namespace osu.Game.Graphics.UserInterface protected override bool OnHover(HoverEvent e) { base.OnHover(e); - Selected = true; + State = SelectionState.Selected; return true; } @@ -257,15 +268,15 @@ namespace osu.Game.Graphics.UserInterface protected override void OnHoverLost(HoverLostEvent e) { base.OnHoverLost(e); - Selected = false; + State = SelectionState.NotSelected; } - private void selectionChanged(ValueChangedEvent args) + private void selectionChanged(SelectionState newState) { if (clickAnimating) return; - if (args.NewValue) + if (newState == SelectionState.Selected) { spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic); colourContainer.ResizeWidthTo(hover_width, hover_duration, Easing.OutElastic); diff --git a/osu.Game/Graphics/UserInterface/ISelectable.cs b/osu.Game/Graphics/UserInterface/ISelectable.cs deleted file mode 100644 index 49c3d725c8..0000000000 --- a/osu.Game/Graphics/UserInterface/ISelectable.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -namespace osu.Game.Graphics.UserInterface -{ - public interface ISelectable - { - bool Selected { get; set; } - } -} diff --git a/osu.Game/Overlays/Volume/VolumeMeter.cs b/osu.Game/Overlays/Volume/VolumeMeter.cs index 3e9849a077..a7c4fb6e7d 100644 --- a/osu.Game/Overlays/Volume/VolumeMeter.cs +++ b/osu.Game/Overlays/Volume/VolumeMeter.cs @@ -3,6 +3,7 @@ using System; using System.Globalization; +using osu.Framework; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Sample; @@ -26,7 +27,7 @@ using osuTK.Graphics; namespace osu.Game.Overlays.Volume { - public class VolumeMeter : Container, IKeyBindingHandler, ISelectable + public class VolumeMeter : Container, IKeyBindingHandler, IStateful { private CircularProgress volumeCircle; private CircularProgress volumeCircleGlow; @@ -44,12 +45,21 @@ namespace osu.Game.Overlays.Volume private Sample sample; private double sampleLastPlaybackTime; - public BindableBool SelectedBindable = new BindableBool(); + public event Action StateChanged; - public bool Selected + private SelectionState state; + + public SelectionState State { - get => SelectedBindable.Value; - set => SelectedBindable.Value = value; + get => state; + set + { + if (state == value) + return; + + state = value; + StateChanged?.Invoke(value); + } } public VolumeMeter(string name, float circleSize, Color4 meterColour) @@ -220,7 +230,7 @@ namespace osu.Game.Overlays.Volume bgProgress.Current.Value = 0.75f; - SelectedBindable.ValueChanged += selectionChanged; + StateChanged += stateChanged; } private int? displayVolumeInt; @@ -341,7 +351,7 @@ namespace osu.Game.Overlays.Volume protected override bool OnHover(HoverEvent e) { - Selected = true; + State = SelectionState.Selected; return false; } @@ -349,9 +359,9 @@ namespace osu.Game.Overlays.Volume { } - private void selectionChanged(ValueChangedEvent selected) + private void stateChanged(SelectionState newState) { - if (selected.NewValue) + if (newState == SelectionState.Selected) { this.ScaleTo(1.04f, transition_length, Easing.OutExpo); focusGlowContainer.FadeIn(transition_length, Easing.OutExpo); @@ -371,12 +381,12 @@ namespace osu.Game.Overlays.Volume switch (action) { case GlobalAction.SelectPrevious: - Selected = true; + State = SelectionState.Selected; adjust(1, false); return true; case GlobalAction.SelectNext: - Selected = true; + State = SelectionState.Selected; adjust(-1, false); return true; } diff --git a/osu.Game/Overlays/VolumeOverlay.cs b/osu.Game/Overlays/VolumeOverlay.cs index 0c56e48cd4..56aa62d6e4 100644 --- a/osu.Game/Overlays/VolumeOverlay.cs +++ b/osu.Game/Overlays/VolumeOverlay.cs @@ -13,6 +13,7 @@ using osu.Framework.Input.Events; using osu.Framework.Threading; using osu.Game.Graphics; using osu.Game.Graphics.Containers; +using osu.Game.Graphics.UserInterface; using osu.Game.Input.Bindings; using osu.Game.Overlays.Volume; using osuTK; @@ -93,7 +94,7 @@ namespace osu.Game.Overlays foreach (var volumeMeter in volumeMeters) { volumeMeter.Bindable.ValueChanged += _ => Show(); - volumeMeter.SelectedBindable.ValueChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected.NewValue); + volumeMeter.StateChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected); } muteButton.Current.ValueChanged += _ => Show(); @@ -140,9 +141,9 @@ namespace osu.Game.Overlays return false; } - private void volumeMeterSelectionChanged(VolumeMeter meter, bool isSelected) + private void volumeMeterSelectionChanged(VolumeMeter meter, SelectionState state) { - if (!isSelected) + if (state == SelectionState.NotSelected) volumeMeters.Deselect(); else volumeMeters.Select(meter); diff --git a/osu.Game/Screens/Play/GameplayMenuOverlay.cs b/osu.Game/Screens/Play/GameplayMenuOverlay.cs index 876f33d814..32f7a93cef 100644 --- a/osu.Game/Screens/Play/GameplayMenuOverlay.cs +++ b/osu.Game/Screens/Play/GameplayMenuOverlay.cs @@ -184,7 +184,7 @@ namespace osu.Game.Screens.Play } }; - button.SelectedBindable.ValueChanged += selected => buttonSelectionChanged(button, selected.NewValue); + button.StateChanged += selected => buttonSelectionChanged(button, selected); InternalButtons.Add(button); } @@ -217,9 +217,9 @@ namespace osu.Game.Screens.Play { } - private void buttonSelectionChanged(DialogButton button, bool isSelected) + private void buttonSelectionChanged(DialogButton button, SelectionState state) { - if (!isSelected) + if (state == SelectionState.NotSelected) InternalButtons.Deselect(); else InternalButtons.Select(button); @@ -263,7 +263,7 @@ namespace osu.Game.Screens.Play protected override bool OnMouseMove(MouseMoveEvent e) { - Selected = true; + State = SelectionState.Selected; return base.OnMouseMove(e); } }