Files
Fishing2/Assets/Scripts/UI/Common/List/ClassifyList.cs

155 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using Fantasy;
using Log = NBC.Log;
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;
public event Action<object> OnDoubleClickItem;
private int _columnsCount;
public ListSelector _selector;
private void OnInited()
{
InputManager.OnUICanceled += OnUICanceled;
List.onClickItem.Add(OnClickListItem);
_selector = new ListSelector(List, typeof(ListTitleItem));
}
public override void Dispose()
{
InputManager.OnUICanceled -= OnUICanceled;
base.Dispose();
}
private void OnUICanceled(string action)
{
if (action == InputDef.UI.Right)
{
_selector.Right();
}
else if (action == InputDef.UI.Left)
{
_selector.Left();
}
else if (action == InputDef.UI.Up)
{
_selector.Up();
}
else if (action == InputDef.UI.Down)
{
_selector.Down();
}
else if (action == InputDef.UI.Enter)
{
var selectedItem = _selector.SelectedItem;
if (selectedItem != null)
{
OnClickItem?.Invoke(selectedItem);
}
}
}
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.defaultItem = ListTitleItem.URL;
List.itemProvider = GetListItemResource;
List.itemRenderer = OnRenderItem;
List.selectionMode = selectionMode;
try
{
List.numItems = _listData.Count;
List.ScrollToView(0);
}
catch (Exception e)
{
Log.Error(e);
}
_columnsCount = 6;
_selector.Refresh();
}
void OnClickListItem(EventContext context)
{
OnClickItem?.Invoke(context.data);
if (context.inputEvent.isDoubleClick)
{
OnDoubleClickItem?.Invoke(context.data);
}
}
void OnRenderItem(int index, GObject obj)
{
var itemData = _listData[index];
if (obj is ListItemBase item)
{
item.SetData(itemData);
}
else if (obj is BagItem bagItem)
{
bagItem.SetData(itemData as ItemInfo);
}
else if (obj is ListTitleItem titleItem)
{
titleItem.SetData(itemData);
titleItem.SetSize(List.width, 32);
}
else if (obj is ShopGearItem shopGearItem)
{
shopGearItem.SetData(itemData as GoodsConfig);
}
}
//根据索引的不同返回不同的资源URL
string GetListItemResource(int index)
{
var itemData = _listData[index];
if (itemData is ItemInfo itemInfo)
{
return BagItem.URL;
}
if (itemData is GoodsConfig goodsConfig)
{
return ShopGearItem.URL;
}
if (itemData is ClassifyListTitleData titleData)
{
return ListTitleItem.URL;
}
return List.defaultItem;
}
}
}