139 lines
3.7 KiB
C#
139 lines
3.7 KiB
C#
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
||
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using FairyGUI;
|
||
using NBC;
|
||
|
||
namespace NBF
|
||
{
|
||
public class ClassifyListTitleData
|
||
{
|
||
public string Title;
|
||
}
|
||
|
||
public partial class ClassifyList : GComponent
|
||
{
|
||
private readonly List<object> _listData = new List<object>();
|
||
|
||
public event Action<object> OnClickItem;
|
||
|
||
private int _columnsCount;
|
||
|
||
private void OnInited()
|
||
{
|
||
Game.Input.OnUICanceled += OnUICanceled;
|
||
List.defaultItem = ListTitleItem.URL;
|
||
List.itemProvider = GetListItemResource;
|
||
List.itemRenderer = OnRenderItem;
|
||
List.onClickItem.Add(OnClickListItem);
|
||
}
|
||
|
||
public override void Dispose()
|
||
{
|
||
Game.Input.OnUICanceled -= OnUICanceled;
|
||
base.Dispose();
|
||
}
|
||
|
||
private void OnUICanceled(string action)
|
||
{
|
||
if (action == InputDef.UI.Right)
|
||
{
|
||
SetListSelected(List.selectedIndex + 1);
|
||
}
|
||
else if (action == InputDef.UI.Left)
|
||
{
|
||
SetListSelected(List.selectedIndex - 1);
|
||
}
|
||
else if (action == InputDef.UI.Up)
|
||
{
|
||
SetListSelected(List.selectedIndex - _columnsCount);
|
||
}
|
||
else if (action == InputDef.UI.Down)
|
||
{
|
||
if (List.selectedIndex < 0)
|
||
{
|
||
SetListSelected(0);
|
||
}
|
||
else
|
||
{
|
||
SetListSelected(List.selectedIndex + _columnsCount);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void SetListSelected(int selectedIndex)
|
||
{
|
||
if (selectedIndex >= 0 && selectedIndex < _listData.Count)
|
||
{
|
||
List.selectedIndex = selectedIndex;
|
||
List.ScrollToView(List.selectedIndex);
|
||
}
|
||
}
|
||
|
||
public void SetListData(List<object> listData,
|
||
ListSelectionMode selectionMode = ListSelectionMode.Single)
|
||
{
|
||
List.selectedIndex = -1;
|
||
_listData.Clear();
|
||
|
||
foreach (var obj in listData)
|
||
{
|
||
_listData.Add(obj);
|
||
}
|
||
List.RemoveChildrenToPool();
|
||
List.selectionMode = selectionMode;
|
||
List.numItems = _listData.Count;
|
||
List.ScrollToView(0);
|
||
_columnsCount = 5;
|
||
}
|
||
|
||
void OnClickListItem(EventContext context)
|
||
{
|
||
OnClickItem?.Invoke(context.data);
|
||
}
|
||
|
||
void OnRenderItem(int index, GObject obj)
|
||
{
|
||
var itemData = _listData[index];
|
||
if (obj is ListItemBase item)
|
||
{
|
||
item.SetData(itemData);
|
||
// obj.SetSize(350, 300);
|
||
}
|
||
else if (obj is BagItem bagItem)
|
||
{
|
||
// obj.SetSize(350, 300);
|
||
}
|
||
else if (obj is ListTitleItem titleItem)
|
||
{
|
||
titleItem.SetData(itemData);
|
||
titleItem.SetSize(List.width, 32);
|
||
}
|
||
}
|
||
|
||
|
||
//根据索引的不同,返回不同的资源URL
|
||
string GetListItemResource(int index)
|
||
{
|
||
var itemData = _listData[index];
|
||
|
||
if (itemData is ShopGearData)
|
||
{
|
||
return ShopGearItem.URL;
|
||
}
|
||
|
||
if (itemData is ItemInfo itemInfo)
|
||
{
|
||
return BagItem.URL;
|
||
}
|
||
if (itemData is ClassifyListTitleData titleData)
|
||
{
|
||
return ListTitleItem.URL;
|
||
}
|
||
|
||
return List.defaultItem;
|
||
}
|
||
}
|
||
} |