Files
Studio/Assets/TMPFolder/UIAccordion.cs
2025-05-21 17:54:01 +09:00

137 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Studio.UVC.UI
{
public class UIAccordion<T> : MonoBehaviour where T : MonoBehaviour
{
public static string PrefabsPath = "Prefabs/Dashboard/UIAccordion";
protected RectTransform barList;
public RectTransform BarList { get { return barList; } }
protected RectTransform HeadLine;
protected Button scrollBtn;
protected string titletKey;
protected RectTransform rect;
public RectTransform Rect { get { return rect; } }
private Dictionary<string, UISubAccordion<T>> subaccordionTable = new();
private UISubAccordion<T> subAccordion;
private UVCTabButton button;
protected float yMinSize;
protected float yFullSize;
public Action onChangeMainAccodionSize;
private static GameObject prefab;
public static UIAccordion<T> Create(RectTransform parent = null)
{
if (prefab == null) prefab = Resources.Load("Prefabs/Common/UITabButton", typeof(GameObject)) as GameObject;
var instance = Instantiate(prefab, parent);
var btn = instance.GetComponent<UIAccordion<T>>();
return btn;
}
public virtual void Init()
{
HeadLine = transform.Find(nameof(HeadLine)) as RectTransform;
barList = transform.Find(nameof(BarList)) as RectTransform;
button = GetComponentInChildren<UVCTabButton>();
button.Init();
rect = GetComponent<RectTransform>();
scrollBtn = GetComponentInChildren<Button>();
scrollBtn.onClick.AddListener(OnClickScroll);
yMinSize = 28f;
}
public void SetItem(string mainTitle, List<T> subModels)
{
this.titletKey = mainTitle;
if (typeof(T) == typeof(UISubAccordion<T>))
{
//Three Depth
}
else
{
//Two Depth
foreach (var subModel in subModels)
{
//CreateButton(subModel.)
}
}
}
//public void SetItem(string titleKey, string subtitleKey, ShortTermResultValues values)
//{
// this.titletKey = titleKey;
// if (!subaccordionTable.ContainsKey(subtitleKey))
// {
// CreateSubAccordion(subtitleKey);
// }
// subaccordionTable[subtitleKey].SetSubItem(subtitleKey, values.pivotValues);
// RefreshText();
//}
private bool isScrollDown = true;
public void OnClickScroll()
{
isScrollDown = !isScrollDown;
var height = isScrollDown ? yFullSize : yMinSize; //headLine.sizeDelta.y+2f;
scrollBtn.transform.localEulerAngles = isScrollDown ? Vector3.zero : new Vector3(0, 0, 180f);
rect.sizeDelta = new Vector2(rect.sizeDelta.x, height);
//onMainAccodionSize
onChangeMainAccodionSize?.Invoke();
}
private UVCTabButton CreateButton(string title)
{
var asset = Resources.Load<UVCTabButton>("");
var item = Instantiate<UVCTabButton>(asset, BarList);
item.Init();
//item.onClickButton.AddListener();
return item;
}
public UISubAccordion<T> CreateSubAccordion(string key)
{
if (subAccordion == null)
{
subAccordion = Resources.Load<UISubAccordion<T>>(UISubAccordion<T>.subPrefabsPath);
}
var item = Instantiate<UISubAccordion<T>>(subAccordion, transform);
item.transform.localScale = Vector3.one;
item.Init();
item.onExpandYSize += OnExpandSize;
item.onChangeMainAccodionSize += OnExpandSize;
subaccordionTable.Add(key, item);
return item;
}
private void OnExpandSize()
{
var size = 0f;
foreach (var item in subaccordionTable.Values)
{
size += item.rect.sizeDelta.y;
}
rect.sizeDelta = new Vector2(rect.sizeDelta.x, (subaccordionTable.Count * 8f) + size + 28f);
yFullSize = rect.sizeDelta.y;
onChangeMainAccodionSize?.Invoke();
}
}
}