161 lines
4.6 KiB
C#
161 lines
4.6 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BrewMonster.Scripts.Task.UI
|
|
{
|
|
public class TaskTreeViewItem : MonoBehaviour, IRefreshLayout
|
|
{
|
|
[SerializeField] private TMP_Text m_text;
|
|
[SerializeField] private Button m_Button;
|
|
[SerializeField] private Sprite[] _levelSprites;
|
|
[SerializeField] private Button _expandButton;
|
|
[SerializeField] private Button _collapseButton;
|
|
|
|
[Header("DEBUG")]
|
|
[SerializeField] private uint m_uItemData;
|
|
[SerializeField] private int _treeLevel;
|
|
private RectTransform _rectTransform;
|
|
|
|
public UnityEvent<uint> OnClick = new UnityEvent<uint>();
|
|
|
|
#region UNITY_METHODS
|
|
|
|
private void Awake()
|
|
{
|
|
m_Button.onClick.AddListener(OnBtnClick);
|
|
_expandButton.onClick.AddListener(OnExpandBtnClicked);
|
|
_collapseButton.onClick.AddListener(OnCollapseBtnClicked);
|
|
_expandButton.gameObject.SetActive(false);
|
|
_collapseButton.gameObject.SetActive(false);
|
|
|
|
_rectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
// private void Start()
|
|
// {
|
|
// RectTransform parent = transform.parent.GetComponent<RectTransform>();
|
|
// if (parent != null && parent.GetComponent<ContentSizeFitter>())
|
|
// {
|
|
// // Force Unity to rebuild layout immediately
|
|
// parent.ForceUpdateRectTransforms();
|
|
// LayoutRebuilder.ForceRebuildLayoutImmediate(parent);
|
|
// }
|
|
// }
|
|
|
|
#endregion
|
|
|
|
#region PUBLIC_METHODS
|
|
|
|
public void SetItemData(uint uItemData)
|
|
{
|
|
m_uItemData = uItemData;
|
|
gameObject.name = $"Task_{uItemData}";
|
|
}
|
|
|
|
public uint GetItemData()
|
|
{
|
|
return m_uItemData;
|
|
}
|
|
|
|
public void SetItemTextColor(Color color)
|
|
{
|
|
|
|
if (m_text != null)
|
|
{
|
|
m_text.color = color;
|
|
}
|
|
}
|
|
|
|
public void SetItemText(string text)
|
|
{
|
|
if (m_text != null)
|
|
{
|
|
m_text.text = text;
|
|
}
|
|
}
|
|
|
|
public void SetTreeLevel(int level)
|
|
{
|
|
_treeLevel = level;
|
|
Image btnImage = m_Button.GetComponent<Image>();
|
|
btnImage.sprite = _treeLevel < _levelSprites.Length ? _levelSprites[_treeLevel] : _levelSprites[^1];
|
|
btnImage.SetNativeSize();
|
|
}
|
|
|
|
public int GetTreeLevel() => _treeLevel;
|
|
|
|
public void SetExpand(bool expand)
|
|
{
|
|
var children = GetComponentsInChildren<TaskTreeViewItem>(true);
|
|
|
|
for (int i=1; i<children.Length; i++)
|
|
{
|
|
if(children[i].GetTreeLevel() == _treeLevel + 1)
|
|
children[i].gameObject.SetActive(expand);
|
|
}
|
|
|
|
_expandButton.gameObject.SetActive(!expand);
|
|
_collapseButton.gameObject.SetActive(expand);
|
|
|
|
RefreshLayout();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// public void OnPointerClick(PointerEventData eventData)
|
|
// {
|
|
// OnClick.Invoke(m_uItemData);
|
|
// EventBus.Publish(new TaskItemClickEvent() { Data = m_uItemData });
|
|
// // TaskWindow.Instance.OnEventLButtonDown_Tv_Quest(m_uItemData);
|
|
// }
|
|
|
|
public void RefreshLayout()
|
|
{
|
|
var rectTransform = _rectTransform;
|
|
if (rectTransform == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Force Unity to rebuild layout immediately
|
|
rectTransform.ForceUpdateRectTransforms();
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);
|
|
|
|
if (transform.parent &&
|
|
transform.parent.TryGetComponent<IRefreshLayout>(out var refreshLayout))
|
|
{
|
|
refreshLayout.RefreshLayout();
|
|
}
|
|
}
|
|
|
|
void OnBtnClick()
|
|
{
|
|
OnClick.Invoke(m_uItemData);
|
|
EventBus.Publish(new TaskItemClickEvent() { Data = m_uItemData });
|
|
}
|
|
|
|
void OnExpandBtnClicked()
|
|
{
|
|
SetExpand(expand: true);
|
|
}
|
|
|
|
void OnCollapseBtnClicked()
|
|
{
|
|
SetExpand(false);
|
|
}
|
|
}
|
|
|
|
public struct TaskItemClickEvent
|
|
{
|
|
public uint Data;
|
|
|
|
public TaskItemClickEvent(uint data)
|
|
{
|
|
Data = data;
|
|
}
|
|
}
|
|
} |