using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.EventSystems; using UnityEngine.Events; using System; namespace XED.Hierarchy { public class ScrollItemUI : UnityEngine.MonoBehaviour, IPointerClickHandler//, IBeginDragHandler, IEndDragHandler, { public TMP_Text nameText; public Image iconImage; public Image loadProgress; public Button expandButton; public RectTransform paddingRT; public Sprite spriteExpanded; public Sprite spriteCollapsed; public UnityEvent onClickSingle; public UnityEvent onClickAddSingle; public UnityEvent onClickMultiple; public UnityEvent onToggleExpand; public UnityEvent onDragStart; public UnityEvent onDragEnd; public UnityEvent onScroll; public Color selectedColor; public HierarchyItem currentItem; public Image upperLine; public Image lowerLine; public Color lineColor; public GameObject highLight; public List itemSprites; private Image backgroundImage; private void Awake() { backgroundImage = GetComponent(); } public int SetItemData(HierarchyItem item) { currentItem = item; nameText.text = item.name; currentItem.isExpanded = item.isExpanded; item.onLoadProgress.RemoveAllListeners(); item.onLoadProgress.AddListener(OnLoadProgress); loadProgress.gameObject.SetActive(false); HierarchyItemSprite itemSprite = itemSprites.Find((x) => x.type == item.type); if (itemSprite != null) iconImage.sprite = itemSprite.sprite; expandButton.image.sprite = currentItem.isExpanded == true ? spriteExpanded : spriteCollapsed; float width = nameText.GetComponent().sizeDelta.x + iconImage.GetComponent().sizeDelta.x; float paddingSize = item.layerNum * expandButton.GetComponent().sizeDelta.x; if (item.children.Count > 0) { expandButton.gameObject.SetActive(true); width += expandButton.GetComponent().sizeDelta.x; } else { expandButton.gameObject.SetActive(false); paddingSize += expandButton.GetComponent().sizeDelta.x; } width += paddingSize; paddingRT.sizeDelta = new Vector2(paddingSize, paddingRT.sizeDelta.y); upperLine.GetComponent().offsetMin = new Vector2(paddingSize, 0); lowerLine.GetComponent().offsetMin = new Vector2(paddingSize, 0); return Mathf.RoundToInt(width); } public void SetSelected(bool selected) { if (selected) { backgroundImage.color = selectedColor; } else { backgroundImage.color = Color.clear; } } public void SetSelected(List selectedItems) { if (selectedItems.Contains(currentItem)) { backgroundImage.color = selectedColor; } else { backgroundImage.color = Color.clear; } } public void ToggleExpandCollapse() { currentItem.isExpanded = !currentItem.isExpanded; expandButton.image.sprite = currentItem.isExpanded == true ? spriteExpanded : spriteCollapsed; onToggleExpand?.Invoke(); } public void OnPointerClick(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Left) { if (Input.GetKey(KeyCode.LeftControl)) { onClickAddSingle?.Invoke(currentItem); } else if (Input.GetKey(KeyCode.LeftShift)) { onClickMultiple?.Invoke(currentItem); } else { onClickSingle?.Invoke(currentItem); } } } public void OnBeginDrag(BaseEventData data) { Debug.Log("DragStart " + currentItem.name); onDragStart?.Invoke(currentItem); } public void OnEndDrag() { Debug.Log("DragEnd " + currentItem.name); onDragEnd?.Invoke(currentItem); } public void OnEndDrag(BaseEventData data) { ScrollItemUI itemUI = GetDropTarget(data); if (itemUI != null) itemUI.OnEndDrag(); } private ScrollItemUI GetDropTarget(BaseEventData data) { PointerEventData eventData = (PointerEventData)data; PointerEventData pointerData = new PointerEventData(EventSystem.current) { position = eventData.position }; var results = new System.Collections.Generic.List(); EventSystem.current.RaycastAll(pointerData, results); foreach (var result in results) { ScrollItemUI itemUI = result.gameObject.GetComponent(); if (itemUI != null) { return itemUI; } } return null; } public void OnScroll(BaseEventData data) { PointerEventData eventData = (PointerEventData)data; onScroll?.Invoke(eventData); } public void ActivateUpperHighlight() { upperLine.color = lineColor; } public void ActivateLowerHighlight() { lowerLine.color = lineColor; } public void ActivateMidHighlight() { highLight.SetActive(true); } public void DeactivateAllHighlights() { upperLine.color = Color.clear; lowerLine.color = Color.clear; highLight.SetActive(false); } public void OnLoadProgress(float ratio) { if (ratio == 1.0f) { loadProgress.gameObject.SetActive(false); return; } if (loadProgress.gameObject.activeSelf == false) { loadProgress.gameObject.SetActive(true); } loadProgress.fillAmount = ratio; } } [Serializable] public class HierarchyItemSprite { public HierarchyItemType type; public Sprite sprite; } }