2021-07-06 01:22:55 +08:00
|
|
|
// 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 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, ISelectable
|
|
|
|
{
|
2021-07-06 17:30:56 +08:00
|
|
|
private int? selectedIndex;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
2021-07-06 17:30:56 +08:00
|
|
|
private void setSelected(int? value)
|
2021-07-06 01:22:55 +08:00
|
|
|
{
|
|
|
|
if (selectedIndex == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Deselect the previously-selected button
|
2021-07-06 17:30:56 +08:00
|
|
|
if (selectedIndex.HasValue)
|
|
|
|
this[selectedIndex.Value].Selected = false;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
|
|
|
selectedIndex = value;
|
|
|
|
|
|
|
|
// Select the newly-selected button
|
2021-07-06 17:30:56 +08:00
|
|
|
if (selectedIndex.HasValue)
|
|
|
|
this[selectedIndex.Value].Selected = true;
|
2021-07-06 01:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SelectNext()
|
|
|
|
{
|
2021-07-06 17:30:56 +08:00
|
|
|
if (!selectedIndex.HasValue || selectedIndex == Count - 1)
|
2021-07-06 01:22:55 +08:00
|
|
|
setSelected(0);
|
|
|
|
else
|
2021-07-06 17:30:56 +08:00
|
|
|
setSelected(selectedIndex.Value + 1);
|
2021-07-06 01:22:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SelectPrevious()
|
|
|
|
{
|
2021-07-06 17:30:56 +08:00
|
|
|
if (!selectedIndex.HasValue || selectedIndex == 0)
|
2021-07-06 01:22:55 +08:00
|
|
|
setSelected(Count - 1);
|
|
|
|
else
|
2021-07-06 17:30:56 +08:00
|
|
|
setSelected(selectedIndex.Value - 1);
|
2021-07-06 01:22:55 +08:00
|
|
|
}
|
|
|
|
|
2021-07-06 17:30:56 +08:00
|
|
|
public void Deselect() => setSelected(null);
|
|
|
|
public void Select(T item)
|
|
|
|
{
|
|
|
|
var newIndex = IndexOf(item);
|
|
|
|
|
|
|
|
if (newIndex < 0)
|
|
|
|
setSelected(null);
|
|
|
|
else
|
|
|
|
setSelected(IndexOf(item));
|
|
|
|
}
|
2021-07-06 01:22:55 +08:00
|
|
|
|
2021-07-06 17:30:56 +08:00
|
|
|
public T Selected => (selectedIndex >= 0 && selectedIndex < Count) ? this[selectedIndex.Value] : null;
|
2021-07-06 01:22:55 +08:00
|
|
|
}
|
|
|
|
}
|