mirror of
https://github.com/ppy/osu.git
synced 2025-02-06 22:33:13 +08:00
Implement selection in FilteringSearchList
This commit is contained in:
parent
8e03ceb3e5
commit
e0f7bf4bb8
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using osu.Framework.GameModes.Testing;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
@ -7,6 +8,7 @@ using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Graphics.UserInterface;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
|
||||
namespace osu.Desktop.Tests
|
||||
{
|
||||
@ -22,25 +24,15 @@ namespace osu.Desktop.Tests
|
||||
public override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
var items = new List<SpriteText>
|
||||
{
|
||||
new SpriteText
|
||||
{
|
||||
Text = "A search textbox"
|
||||
},
|
||||
new SpriteText
|
||||
{
|
||||
Text = "A filtering search list"
|
||||
},
|
||||
new SpriteText
|
||||
{
|
||||
Text = "A dropdown menu"
|
||||
},
|
||||
new SpriteText
|
||||
{
|
||||
Text = "Possibly more I've missed"
|
||||
}
|
||||
};
|
||||
|
||||
var items = new DirectoryInfo(Environment.CurrentDirectory)
|
||||
.GetFiles()
|
||||
.Select(
|
||||
f =>
|
||||
new SpriteText
|
||||
{
|
||||
Text = f.Name
|
||||
}).ToList();
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new FlowContainer
|
||||
@ -54,7 +46,7 @@ namespace osu.Desktop.Tests
|
||||
{
|
||||
Size = new Vector2(300, 30)
|
||||
},
|
||||
filteringSearchList = new FilteringSearchList<SpriteText>(items)
|
||||
filteringSearchList = new FilteringSearchList<SpriteText>(items, si => si.Colour = Color4.Orange, nsi => nsi.Colour = Color4.White)
|
||||
{
|
||||
Size = new Vector2(300, 400)
|
||||
}
|
||||
|
@ -1,21 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Input;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
public class FilteringSearchList<T> : Container
|
||||
where T : Drawable
|
||||
{
|
||||
public List<T> Items { get; }
|
||||
public List<FilteringSearchListItem<T>> Items { get; }
|
||||
public FilteringSearchListItem<T> SelectedItem { get; private set; }
|
||||
private Action<T> selectedItemAction, notSelectedItemAction;
|
||||
|
||||
private FlowContainer contentFlowContainer;
|
||||
|
||||
public FilteringSearchList(List<T> items)
|
||||
public FilteringSearchList(List<T> items, Action<T> selectedItemAction, Action<T> notSelectedItemAction, T selectedItem = null)
|
||||
{
|
||||
Items = items;
|
||||
this.selectedItemAction = selectedItemAction;
|
||||
this.notSelectedItemAction = notSelectedItemAction;
|
||||
Items = new List<FilteringSearchListItem<T>>();
|
||||
foreach (var item in items)
|
||||
{
|
||||
var filteringSearchListItem = new FilteringSearchListItem<T>(item);
|
||||
filteringSearchListItem.OnSelected += selectionChanged;
|
||||
Items.Add(filteringSearchListItem);
|
||||
}
|
||||
SelectedItem = selectedItem != null ? Items.First(i => i.Item == selectedItem) : null;
|
||||
}
|
||||
|
||||
public override void Load(BaseGame game)
|
||||
@ -42,12 +55,77 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
foreach (var item in Items)
|
||||
{
|
||||
if (predicate(item))
|
||||
if (predicate(item.Item))
|
||||
item.Show();
|
||||
else
|
||||
item.Hide();
|
||||
}
|
||||
contentFlowContainer.Invalidate();
|
||||
}
|
||||
|
||||
private void selectionChanged(object sender)
|
||||
{
|
||||
SelectedItem = (FilteringSearchListItem<T>)sender;
|
||||
selectedItemAction(SelectedItem.Item);
|
||||
foreach (var item in Items)
|
||||
{
|
||||
if (item != SelectedItem)
|
||||
{
|
||||
item.State = SelectionState.NotSelected;
|
||||
notSelectedItemAction(item.Item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class FilteringSearchListItem<T> : AutoSizeContainer, IStateful<SelectionState>
|
||||
where T : Drawable
|
||||
{
|
||||
public T Item { get; set; }
|
||||
|
||||
public SelectionState State
|
||||
{
|
||||
get { return state; }
|
||||
set
|
||||
{
|
||||
state = value;
|
||||
if (value == SelectionState.Selected)
|
||||
OnSelected?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
private SelectionState state;
|
||||
|
||||
public FilteringSearchListItem(T item, SelectionState state = SelectionState.NotSelected)
|
||||
{
|
||||
Item = item;
|
||||
State = state;
|
||||
}
|
||||
|
||||
public override void Load(BaseGame game)
|
||||
{
|
||||
base.Load(game);
|
||||
|
||||
Children = new Drawable[]
|
||||
{
|
||||
Item
|
||||
};
|
||||
}
|
||||
|
||||
protected override bool OnClick(InputState state)
|
||||
{
|
||||
State = SelectionState.Selected;
|
||||
return true;
|
||||
}
|
||||
|
||||
public delegate void OnSelectedHandler(object sender);
|
||||
|
||||
public event OnSelectedHandler OnSelected;
|
||||
}
|
||||
|
||||
public enum SelectionState
|
||||
{
|
||||
Selected,
|
||||
NotSelected
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user