From 07d54d261a0359b7a2df15e685ee0874b2cbb4bf Mon Sep 17 00:00:00 2001 From: Derrick Timmermans Date: Tue, 6 Jul 2021 13:24:18 +0200 Subject: [PATCH] Let selection container handle manual selection changes --- .../SelectionCycleFillFlowContainer.cs | 31 +++++++++++++++++++ osu.Game/Overlays/VolumeOverlay.cs | 12 ------- osu.Game/Screens/Play/GameplayMenuOverlay.cs | 12 ------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs index 5849fbbe30..b48a697903 100644 --- a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs +++ b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.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 System.Collections.Generic; using osu.Framework; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -60,5 +62,34 @@ namespace osu.Game.Graphics.Containers } public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null; + + private readonly Dictionary> handlerMap = new Dictionary>(); + + public override void Add(T drawable) + { + // This event is used to update selection state when modified within the drawable itself. + // It is added to a dictionary so that we can unsubscribe if the drawable is removed from this container + drawable.StateChanged += handlerMap[drawable] = state => selectionChanged(drawable, state); + + base.Add(drawable); + } + + public override bool Remove(T drawable) + { + if (!base.Remove(drawable)) + return false; + + drawable.StateChanged -= handlerMap[drawable]; + handlerMap.Remove(drawable); + return true; + } + + private void selectionChanged(T drawable, SelectionState state) + { + if (state == SelectionState.NotSelected) + Deselect(); + else + Select(drawable); + } } } diff --git a/osu.Game/Overlays/VolumeOverlay.cs b/osu.Game/Overlays/VolumeOverlay.cs index 56aa62d6e4..a96949e96f 100644 --- a/osu.Game/Overlays/VolumeOverlay.cs +++ b/osu.Game/Overlays/VolumeOverlay.cs @@ -13,7 +13,6 @@ 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; @@ -92,10 +91,7 @@ namespace osu.Game.Overlays base.LoadComplete(); foreach (var volumeMeter in volumeMeters) - { volumeMeter.Bindable.ValueChanged += _ => Show(); - volumeMeter.StateChanged += selected => volumeMeterSelectionChanged(volumeMeter, selected); - } muteButton.Current.ValueChanged += _ => Show(); } @@ -141,14 +137,6 @@ namespace osu.Game.Overlays return false; } - private void volumeMeterSelectionChanged(VolumeMeter meter, SelectionState state) - { - if (state == SelectionState.NotSelected) - volumeMeters.Deselect(); - else - volumeMeters.Select(meter); - } - private ScheduledDelegate popOutDelegate; public override void Show() diff --git a/osu.Game/Screens/Play/GameplayMenuOverlay.cs b/osu.Game/Screens/Play/GameplayMenuOverlay.cs index 32f7a93cef..c71dd2ce65 100644 --- a/osu.Game/Screens/Play/GameplayMenuOverlay.cs +++ b/osu.Game/Screens/Play/GameplayMenuOverlay.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Humanizer; using osu.Framework.Allocation; using osu.Framework.Extensions.Color4Extensions; @@ -184,8 +183,6 @@ namespace osu.Game.Screens.Play } }; - button.StateChanged += selected => buttonSelectionChanged(button, selected); - InternalButtons.Add(button); } @@ -216,15 +213,6 @@ namespace osu.Game.Screens.Play public void OnReleased(GlobalAction action) { } - - private void buttonSelectionChanged(DialogButton button, SelectionState state) - { - if (state == SelectionState.NotSelected) - InternalButtons.Deselect(); - else - InternalButtons.Select(button); - } - private void updateRetryCount() { // "You've retried 1,065 times in this session"