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.

92 lines
2.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. public class LogicTrayUI : MonoBehaviour
  7. {
  8. [SerializeField]
  9. protected List<LogicBlock> list;
  10. [SerializeField]
  11. protected GameObject Prefab;
  12. [SerializeField]
  13. protected int insertIndex = -1;
  14. [SerializeField]
  15. private Transform content;
  16. public void Start()
  17. {
  18. UpdateDisplay();
  19. }
  20. public void OnHoverStart(BagItem item, RectTransform rect)
  21. {
  22. insertIndex = content.childCount;
  23. UpdateDisplay();
  24. }
  25. public void OnHoverEnd(BagItem item, RectTransform rect)
  26. {
  27. insertIndex = -1;
  28. UpdateDisplay();
  29. }
  30. public void OnHoverUpdate(BagItem item, RectTransform rect)
  31. {
  32. foreach(Transform child in content)
  33. {
  34. RectTransform childRect = child as RectTransform;
  35. if (child == null)
  36. continue;
  37. if (rect.GlobalRect().Overlaps(childRect.GlobalRect()))
  38. {
  39. if (insertIndex != child.GetSiblingIndex())
  40. {
  41. insertIndex = child.GetSiblingIndex();
  42. UpdateDisplay();
  43. }
  44. break;
  45. }
  46. }
  47. }
  48. [ContextMenu("Update Display")]
  49. public void UpdateDisplay()
  50. {
  51. int destroyedCount = content.childCount;
  52. foreach (Transform child in content)
  53. Destroy(child.gameObject);
  54. foreach(LogicBlock element in list)
  55. {
  56. GameObject elementObject = Instantiate(Prefab, content);
  57. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = element.name;
  58. }
  59. if (insertIndex != -1)
  60. {
  61. GameObject elementObject = Instantiate(Prefab, content);
  62. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
  63. elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  64. elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
  65. }
  66. }
  67. }