Files
test/Assets/PerfectWorld/Scripts/UI/Dialogs/AUIListBox.cs
T
2025-11-17 18:52:07 +07:00

116 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace BrewMonster.UI
{
public class AUIListBox : MonoBehaviour
{
[SerializeField] private RectTransform content;
[SerializeField] private ScrollView scrollView;
[SerializeField] private ItemUIListBox prefabItemUIListBox;
[SerializeField] private float heightItem = 100f;
[SerializeField] private float paddingY = 10f;
List<ItemUIListBox> m_Item = new List<ItemUIListBox>();
int m_nCurSel = 0;
const uint AUILISTBOX_ERROR = 0xFFFFFFFF; // same as -1 for uint
public void ResetContent()
{
foreach(var item in m_Item)
{
Destroy(item.gameObject);
}
m_Item.Clear();
content.anchoredPosition = Vector2.zero;
content.sizeDelta = Vector2.zero;
}
public void AddString(string pszString)
{
ItemUIListBox item = Instantiate(prefabItemUIListBox, content);
item.SetText(pszString);
item.gameObject.SetActive(true);
m_Item.Add(item);
Vector2 size = content.sizeDelta;
size.y = heightItem * m_Item.Count + m_Item.Count * paddingY;
content.sizeDelta = size;
}
public int SetItemData(int nIndex, uint dwItemData, int nSubIndex = 0, string strName = "")
{
if (nIndex < 0 || nIndex >= (m_Item.Count))
return -1;
if (nSubIndex < 0 || nSubIndex >= 20) // #define AUILISTBOX_DATA_NUM AUILISTBOX_MAX_COLUMN 20
return -1;
m_Item[nIndex].strDataName[nSubIndex] = strName;
m_Item[nIndex].dwData[nSubIndex] = dwItemData;
return nIndex;
}
public int SetItemDataPtr(int nIndex, object pData, int nSubIndex = 0, string strName = "")
{
if (nIndex < 0 || nIndex >= (m_Item.Count))
return -1;
if (nSubIndex < 0 || nSubIndex >= 20) // #define AUILISTBOX_DATA_NUM AUILISTBOX_MAX_COLUMN 20
return -1;
m_Item[nIndex].strDataPtrName[nSubIndex] = strName;
m_Item[nIndex].pvData[nSubIndex] = pData;
return nIndex;
}
public int GetCount()
{
return m_Item.Count;
}
public object GetItemDataPtr(int nIndex, int nSubIndex, string strName)
{
if (nIndex < 0 || nIndex >= m_Item.Count)
return null;
if (nSubIndex < 0 || nSubIndex >= 20) // #define AUILISTBOX_DATA_NUM AUILISTBOX_MAX_COLUMN 20
return null;
//if (0 != m_Item[nIndex].pvData[nSubIndex] && strName != m_Item[nIndex].strDataPtrName[nSubIndex])
// AUI_ReportError(__LINE__, 1, "AUIListBox::GetItemDataPtr(), data name not match");
return m_Item[nIndex].pvData[nSubIndex];
}
public uint GetItemData(int nIndex, int nSubIndex, string strName)
{
if (nIndex < 0 || nIndex >= m_Item.Count)
return AUILISTBOX_ERROR;
if (nSubIndex < 0 || nSubIndex >= 20)
return AUILISTBOX_ERROR;
//if (0 != m_Item[nIndex].dwData[nSubIndex] && strName != m_Item[nIndex].strDataName[nSubIndex])
// AUI_ReportError(__LINE__, 1, "AUIListBox::GetItemData(), data name not match");
return m_Item[nIndex].dwData[nSubIndex];
}
public void SetCurSel(int nIndex)
{
//if (nIndex != m_nCurSel)
// UpdateRenderTarget();
m_nCurSel = nIndex;
}
public int GetCurSel()
{
return m_nCurSel;
}
public string GetText(int nIndex)
{
if (nIndex < 0 || nIndex >= (m_Item.Count))
return "";
else
return m_Item[nIndex].strText;
}
}
}