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

5 years ago
5 years ago
  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)
  26. {
  27. insertIndex = -1;
  28. UpdateDisplay();
  29. }
  30. public void InsertLogicElement(LogicBlock item)
  31. {
  32. if (insertIndex >= 0 && insertIndex <= list.Count)
  33. {
  34. list.Insert(insertIndex, item);
  35. }
  36. UpdateDisplay();
  37. }
  38. public void OnHoverUpdate(BagItem item, RectTransform rect)
  39. {
  40. foreach(Transform child in content)
  41. {
  42. RectTransform childRect = child as RectTransform;
  43. if (child == null)
  44. continue;
  45. if (rect.GlobalRect().Overlaps(childRect.GlobalRect()))
  46. {
  47. if (insertIndex != child.GetSiblingIndex())
  48. {
  49. insertIndex = child.GetSiblingIndex();
  50. UpdateDisplay();
  51. }
  52. break;
  53. }
  54. }
  55. }
  56. [ContextMenu("Update Display")]
  57. public void UpdateDisplay()
  58. {
  59. int destroyedCount = content.childCount;
  60. foreach (Transform child in content)
  61. Destroy(child.gameObject);
  62. foreach(LogicBlock element in list)
  63. {
  64. GameObject elementObject = Instantiate(Prefab, content);
  65. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = element.name;
  66. }
  67. if (insertIndex != -1)
  68. {
  69. GameObject elementObject = Instantiate(Prefab, content);
  70. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
  71. elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  72. elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
  73. }
  74. }
  75. }