mirror of
https://github.com/ppy/osu.git
synced 2025-03-04 00:42:55 +08:00
Tidy up class (although it's not in a good state logically)
This commit is contained in:
parent
4451598bcf
commit
ddb1da5a66
@ -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,20 +51,17 @@ 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.
|
|
||||||
// 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];
|
// 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)
|
public override bool Remove(T drawable)
|
||||||
@ -85,7 +69,9 @@ namespace osu.Game.Graphics.Containers
|
|||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user