From e0b9d6cd91108aa6516fc80278bbc914d3a095e5 Mon Sep 17 00:00:00 2001 From: "Bob.Song" Date: Fri, 31 Oct 2025 09:48:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=97=E8=A1=A8=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/UI/Common/List/ClassifyList.cs | 18 +++---- Assets/Scripts/UI/Common/List/ListSelector.cs | 52 +++++++++++++++++-- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/UI/Common/List/ClassifyList.cs b/Assets/Scripts/UI/Common/List/ClassifyList.cs index 96347af3c..8ee536a1f 100644 --- a/Assets/Scripts/UI/Common/List/ClassifyList.cs +++ b/Assets/Scripts/UI/Common/List/ClassifyList.cs @@ -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 listData, ListSelectionMode selectionMode = ListSelectionMode.Single) { diff --git a/Assets/Scripts/UI/Common/List/ListSelector.cs b/Assets/Scripts/UI/Common/List/ListSelector.cs index 9dd3ac5d1..af2804b61 100644 --- a/Assets/Scripts/UI/Common/List/ListSelector.cs +++ b/Assets/Scripts/UI/Common/List/ListSelector.cs @@ -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