osu/osu.Game/Graphics/Containers/SelectionCycleFillFlowConta...

88 lines
2.7 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using osu.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// A FillFlowContainer that provides functionality to cycle selection between children
/// The selection wraps around when overflowing past the first or last child.
/// </summary>
public class SelectionCycleFillFlowContainer<T> : FillFlowContainer<T> where T : Drawable, IStateful<SelectionState>
{
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
private int? selectedIndex;
public void SelectNext()
{
2021-07-06 09:30:56 +00:00
if (!selectedIndex.HasValue || selectedIndex == Count - 1)
setSelected(0);
else
2021-07-06 09:30:56 +00:00
setSelected(selectedIndex.Value + 1);
}
public void SelectPrevious()
{
2021-07-06 09:30:56 +00:00
if (!selectedIndex.HasValue || selectedIndex == 0)
setSelected(Count - 1);
else
2021-07-06 09:30:56 +00:00
setSelected(selectedIndex.Value - 1);
}
2021-07-06 09:30:56 +00:00
public void Deselect() => setSelected(null);
2021-07-06 09:30:56 +00:00
public void Select(T item)
{
var newIndex = IndexOf(item);
if (newIndex < 0)
setSelected(null);
else
setSelected(IndexOf(item));
}
public override void Add(T drawable)
{
base.Add(drawable);
2021-07-06 12:11:46 +00:00
Debug.Assert(drawable != null);
drawable.StateChanged += state => selectionChanged(drawable, state);
}
public override bool Remove(T drawable)
=> throw new NotSupportedException($"Cannot remove drawables from {nameof(SelectionCycleFillFlowContainer<T>)}");
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)
Deselect();
else
Select(drawable);
}
}
}