Files
test/Assets/PerfectWorld/Scripts/Task/UI/TaskTreeViewItem.cs
T

132 lines
3.8 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using BrewMonster.Scripts.UI;
namespace BrewMonster.Scripts.Task.UI
{
public class TaskTreeViewItem : MonoBehaviour, IRefreshLayout
{
[SerializeField] private TextOutlet m_text;
[SerializeField] private Button m_Button;
[SerializeField] private Toggle _expandToggle;
[SerializeField] private Image _expandBG;
[SerializeField] private Sprite ExpandBGActive;
[SerializeField] private Sprite ExpandBGInactive;
[SerializeField] private RectTransform _rectTransform;
public UnityEvent OnClick = new UnityEvent();
public UnityEvent<bool> OnExpand = new UnityEvent<bool>();
#region UNITY_METHODS
private void Awake()
{
m_Button.onClick.AddListener(OnClick.Invoke);
_expandToggle.onValueChanged.AddListener(SetExpand);
_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)
{
gameObject.name = $"Task_{uItemData}";
}
public void SetItemTextColor(Color color)
{
if (m_text != null)
{
m_text.SetColor(color);
}
}
public void SetLastItem(bool isLastItem)
{
if(isLastItem)
{
_expandToggle.onValueChanged.RemoveAllListeners();
}
else
{
_expandToggle.onValueChanged.AddListener(OnExpandToggleValueChanged);
}
}
public void SetItemText(string text)
{
if (m_text != null)
{
m_text.Set(text);
}
}
public void SetExpand(bool expand)
{
_expandBG.sprite = expand ? ExpandBGActive : ExpandBGInactive;
//_expandButton.gameObject.SetActive(!expand);
//_collapseButton.gameObject.SetActive(expand);
//RefreshLayout();
OnExpand.Invoke(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()
{
if (transform.parent &&
transform.parent.TryGetComponent<IRefreshLayout>(out var refreshLayout))
{
refreshLayout.RefreshLayout();
}
}
public void RefreshSelf()
{
var rectTransform = _rectTransform;
if (rectTransform == null)
{
return;
}
rectTransform.ForceUpdateRectTransforms();
LayoutRebuilder.ForceRebuildLayoutImmediate(rectTransform);
}
void OnExpandToggleValueChanged(bool isOn)
{
SetExpand(isOn);
}
}
public struct TaskItemClickEvent
{
public uint Data;
public TaskItemClickEvent(uint data)
{
Data = data;
}
}
}