using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; 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 m_Item = new List(); int m_nCurSel = 0; Action m_OnClickBtn = null; 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 SetActionOnClickBtnItem(Action onClick) { m_OnClickBtn = onClick; } public void AddString(string pszString) { ItemUIListBox item = Instantiate(prefabItemUIListBox, content); item.SetText(pszString); item.SetIcon(null); 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; LayoutRebuilder.ForceRebuildLayoutImmediate(content); } public void SetItemIcon(int nIndex, Sprite sprite) { if (nIndex < 0 || nIndex >= m_Item.Count) return; m_Item[nIndex].SetIcon(sprite); } 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; m_Item[nIndex].SetActOnClickBtn(m_OnClickBtn, nIndex); 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; m_Item[nIndex].SetActOnClickBtn(m_OnClickBtn, nIndex); return nIndex; } public int SetItemData64(int nIndex, ulong 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].strData64Name[nSubIndex] = strName; m_Item[nIndex].uiData64[nSubIndex] = pData; m_Item[nIndex].SetActOnClickBtn(m_OnClickBtn, nIndex); return nIndex; } public ulong GetItemData64(int nIndex, int nSubIndex, string strName) { if (nIndex < 0 || nIndex >= (m_Item.Count)) return AUILISTBOX_ERROR; if (nSubIndex < 0 || nSubIndex >= 20) // AUILISTBOX_DATA_NUM 20 return AUILISTBOX_ERROR; //if (0 != m_Item[nIndex].uiData64[nSubIndex] && strName != m_Item[nIndex].strData64Name[nSubIndex]) // AUI_ReportError(__LINE__, 1, "AUIListBox::GetItemData64(), data name not match"); return m_Item[nIndex].uiData64[nSubIndex]; } 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; } //public int SetItemTextColor(int nIndex, A3DCOLOR color, int nItem) //{ // if (nIndex < 0 || nIndex >= int(m_Item.size())) // return AUILISTBOX_ERROR; // UpdateRenderTarget(); // if (nItem >= 0) // m_Item[nIndex].color[nItem] = color; // else // for (int i = 0; i < max(1, m_nNumColumns); i++) // m_Item[nIndex].color[i] = color; // return nIndex; //} } }