You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class LogicTrayUI : MonoBehaviour
{
[SerializeField]
protected List<LogicBlock> list;
[SerializeField]
protected GameObject Prefab;
[SerializeField]
protected int insertIndex = -1;
[SerializeField]
private Transform content;
public void Start()
{
UpdateDisplay();
}
public void OnHoverStart(BagItem item, RectTransform rect)
{
insertIndex = content.childCount;
UpdateDisplay();
}
public void OnHoverEnd(BagItem item)
{
insertIndex = -1;
UpdateDisplay();
}
public void InsertLogicElement(LogicBlock item)
{
if (insertIndex >= 0 && insertIndex <= list.Count)
{
list.Insert(insertIndex, item);
}
UpdateDisplay();
}
public void OnHoverUpdate(BagItem item, RectTransform rect)
{
foreach(Transform child in content)
{
RectTransform childRect = child as RectTransform;
if (child == null)
continue;
if (rect.GlobalRect().Overlaps(childRect.GlobalRect()))
{
if (insertIndex != child.GetSiblingIndex())
{
insertIndex = child.GetSiblingIndex();
UpdateDisplay();
}
break;
}
}
}
[ContextMenu("Update Display")]
public void UpdateDisplay()
{
int destroyedCount = content.childCount;
foreach (Transform child in content)
Destroy(child.gameObject);
foreach(LogicBlock element in list)
{
GameObject elementObject = Instantiate(Prefab, content);
elementObject.GetComponentInChildren<TextMeshProUGUI>().text = element.name;
}
if (insertIndex != -1)
{
GameObject elementObject = Instantiate(Prefab, content);
elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
}
}
}