完成列表选择器

This commit is contained in:
Bob.Song
2025-10-31 09:48:12 +08:00
parent 33b03719a1
commit e0b9d6cd91
2 changed files with 56 additions and 14 deletions

View File

@@ -54,18 +54,16 @@ namespace NBF
{
_selector.Down();
}
else if (action == InputDef.UI.Enter)
{
var selectedItem = _selector.SelectedItem;
if (selectedItem != null)
{
OnClickItem?.Invoke(selectedItem);
}
}
}
// private void SetListSelected(int selectedIndex)
// {
// if (selectedIndex >= 0 && selectedIndex < _listData.Count)
// {
// var count = List.numChildren;
// List.selectedIndex = selectedIndex;
// List.ScrollToView(selectedIndex);
// }
// }
public void SetListData(List<object> listData,
ListSelectionMode selectionMode = ListSelectionMode.Single)
{

View File

@@ -14,12 +14,14 @@ namespace NBF
private float itemWidth = 200f;
private float itemHeight = 180f;
public object SelectedItem => _list.selectedIndex > 0 ? _list.GetChildAt(_list.selectedIndex) : null;
public ListSelector(GList list, params Type[] ignores)
{
_list = list;
if (ignores != null && _ignores.Count > 0)
if (ignores != null && ignores.Length > 0)
{
_ignores.AddRange(_ignores);
_ignores.AddRange(ignores);
}
}
@@ -73,6 +75,21 @@ namespace NBF
var currentPos = GetItemPosition(_list.selectedIndex);
var nearestIndex = FindNearestSelectableItem(currentPos, direction);
// 如果是左右移动且未找到目标,则尝试寻找前一个或后一个可选项
if (nearestIndex == -1 && (direction == Vector2.left || direction == Vector2.right))
{
if (direction == Vector2.left)
{
// 寻找前一个可选项
nearestIndex = FindPreviousSelectableItem(_list.selectedIndex);
}
else if (direction == Vector2.right)
{
// 寻找后一个可选项
nearestIndex = FindNextSelectableItem(_list.selectedIndex);
}
}
if (nearestIndex != -1)
{
SetSelection(nearestIndex);
@@ -116,6 +133,28 @@ namespace NBF
return bestIndex;
}
private int FindPreviousSelectableItem(int currentIndex)
{
for (int i = currentIndex - 1; i >= 0; i--)
{
if (IsSelectableItem(i))
return i;
}
return -1;
}
private int FindNextSelectableItem(int currentIndex)
{
for (int i = currentIndex + 1; i < _list.numItems; i++)
{
if (IsSelectableItem(i))
return i;
}
return -1;
}
private bool IsDirectionMatch(Vector2 direction, Vector2 diff)
{
if (direction == Vector2.up)
@@ -132,7 +171,12 @@ namespace NBF
private bool IsSelectableItem(int index)
{
var item = _list.GetChildAt(index);
return !_ignores.Contains(item.GetType());
var ret = !_ignores.Contains(item.GetType());
if (item.GetType() == typeof(ListTitleItem))
{
}
return ret;
}
private void SelectFirstSelectable()
@@ -150,7 +194,7 @@ namespace NBF
private void SetSelection(int index)
{
_list.selectedIndex = index;
// selection.position = items[index].position;
_list.ScrollToView(index, true);
}
#endregion