From ddb1da5a6611b11769675f7558343c2b441b0e8d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 7 Jul 2021 13:48:35 +0900 Subject: [PATCH] Tidy up class (although it's not in a good state logically) --- .../SelectionCycleFillFlowContainer.cs | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs index 4f20c0039b..656f489772 100644 --- a/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs +++ b/osu.Game/Graphics/Containers/SelectionCycleFillFlowContainer.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using osu.Framework; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -16,25 +17,11 @@ namespace osu.Game.Graphics.Containers /// public class SelectionCycleFillFlowContainer : FillFlowContainer where T : Drawable, IStateful { - private int? selectedIndex; - public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null; - private void setSelected(int? value) - { - if (selectedIndex == value) - return; + private int? selectedIndex; - // Deselect the previously-selected button - if (selectedIndex.HasValue) - this[selectedIndex.Value].State = SelectionState.NotSelected; - - selectedIndex = value; - - // Select the newly-selected button - if (selectedIndex.HasValue) - this[selectedIndex.Value].State = SelectionState.Selected; - } + private readonly Dictionary> handlerMap = new Dictionary>(); public void SelectNext() { @@ -64,20 +51,17 @@ namespace osu.Game.Graphics.Containers setSelected(IndexOf(item)); } - private readonly Dictionary> handlerMap = new Dictionary>(); - public override void Add(T drawable) { base.Add(drawable); - if (drawable != null) - { - // 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 - handlerMap[drawable] = state => selectionChanged(drawable, state); + Debug.Assert(drawable != null); - drawable.StateChanged += handlerMap[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 + handlerMap[drawable] = state => selectionChanged(drawable, state); + + drawable.StateChanged += handlerMap[drawable]; } public override bool Remove(T drawable) @@ -85,7 +69,9 @@ namespace osu.Game.Graphics.Containers if (!base.Remove(drawable)) return false; - if (drawable != null && handlerMap.TryGetValue(drawable, out var action)) + Debug.Assert(drawable != null); + + if (handlerMap.TryGetValue(drawable, out var action)) { drawable.StateChanged -= action; handlerMap.Remove(drawable); @@ -94,6 +80,22 @@ namespace osu.Game.Graphics.Containers return true; } + private void setSelected(int? value) + { + if (selectedIndex == value) + return; + + // Deselect the previously-selected button + if (selectedIndex.HasValue) + this[selectedIndex.Value].State = SelectionState.NotSelected; + + selectedIndex = value; + + // Select the newly-selected button + if (selectedIndex.HasValue) + this[selectedIndex.Value].State = SelectionState.Selected; + } + private void selectionChanged(T drawable, SelectionState state) { if (state == SelectionState.NotSelected)