1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-04 06:03:22 +08:00

Tidy up class (although it's not in a good state logically)

This commit is contained in:
Dean Herbert 2021-07-07 13:48:35 +09:00
parent 4451598bcf
commit ddb1da5a66

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using osu.Framework; using osu.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -16,25 +17,11 @@ namespace osu.Game.Graphics.Containers
/// </summary> /// </summary>
public class SelectionCycleFillFlowContainer<T> : FillFlowContainer<T> where T : Drawable, IStateful<SelectionState> public class SelectionCycleFillFlowContainer<T> : FillFlowContainer<T> where T : Drawable, IStateful<SelectionState>
{ {
private int? selectedIndex;
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null; public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
private void setSelected(int? value) private int? selectedIndex;
{
if (selectedIndex == value)
return;
// Deselect the previously-selected button private readonly Dictionary<T, Action<SelectionState>> handlerMap = new Dictionary<T, Action<SelectionState>>();
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;
}
public void SelectNext() public void SelectNext()
{ {
@ -64,28 +51,27 @@ namespace osu.Game.Graphics.Containers
setSelected(IndexOf(item)); setSelected(IndexOf(item));
} }
private readonly Dictionary<T, Action<SelectionState>> handlerMap = new Dictionary<T, Action<SelectionState>>();
public override void Add(T drawable) public override void Add(T drawable)
{ {
base.Add(drawable); base.Add(drawable);
if (drawable != null) Debug.Assert(drawable != null);
{
// This event is used to update selection state when modified within the drawable itself. // 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 // 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); handlerMap[drawable] = state => selectionChanged(drawable, state);
drawable.StateChanged += handlerMap[drawable]; drawable.StateChanged += handlerMap[drawable];
} }
}
public override bool Remove(T drawable) public override bool Remove(T drawable)
{ {
if (!base.Remove(drawable)) if (!base.Remove(drawable))
return false; 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; drawable.StateChanged -= action;
handlerMap.Remove(drawable); handlerMap.Remove(drawable);
@ -94,6 +80,22 @@ namespace osu.Game.Graphics.Containers
return true; 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) private void selectionChanged(T drawable, SelectionState state)
{ {
if (state == SelectionState.NotSelected) if (state == SelectionState.NotSelected)