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.

134 lines
3.8 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 : LogicElementHolder
  7. {
  8. [SerializeField]
  9. protected List<LogicBlock> list;
  10. [SerializeField]
  11. protected LogicElementUI Prefab;
  12. [SerializeField]
  13. protected int insertIndex = -1;
  14. [SerializeField]
  15. private Transform content;
  16. #region Unity Functions
  17. public void Start()
  18. {
  19. UpdateDisplay();
  20. }
  21. #endregion Unity Functions
  22. [ContextMenu("Update Display")]
  23. public void UpdateDisplay()
  24. {
  25. //Destroy all children
  26. int destroyedCount = content.childCount; //we need to know how many were destroyed since unity doesn't actually destroy anything until the end of the frame
  27. foreach (Transform child in content)
  28. Destroy(child.gameObject);
  29. //Instatiate logicElement prefab
  30. foreach (LogicBlock element in list)
  31. {
  32. LogicElementUI elementUI = Instantiate(Prefab.gameObject, content).GetComponent<LogicElementUI>();
  33. elementUI.Initialise(element, this);
  34. }
  35. //Add insertIndex object if needed
  36. if (insertIndex != -1)
  37. {
  38. GameObject elementObject = Instantiate(Prefab.gameObject, content);
  39. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
  40. elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  41. //Set sibling index to where we want it in list + add destroyedCount since technically they are still here
  42. elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
  43. }
  44. }
  45. #region LogicElementHolder Implementation
  46. /// <summary>
  47. /// When a LogicElement has started being dragged from this Tray
  48. /// </summary>
  49. /// <param name="element">Element which is being dragged</param>
  50. public override void OnRemove(LogicElementUI element)
  51. {
  52. list.Remove(element.logicElement);
  53. UpdateDisplay();
  54. }
  55. /// <summary>
  56. /// When a logicElement needs to be added back to this Tray
  57. /// </summary>
  58. /// <param name="element">Element to add</param>
  59. public override void OnAdd(LogicElementUI element)
  60. {
  61. int index = GetInsertIndex(element.rectTransform);
  62. list.Insert(index, element.logicElement);
  63. UpdateDisplay();
  64. Destroy(element.gameObject);
  65. }
  66. /// <summary>
  67. /// When a logic Element is hovering over this holder
  68. /// </summary>
  69. /// <param name="element">Element which is hovering</param>
  70. public override void OnHover(LogicElementUI element)
  71. {
  72. insertIndex = GetInsertIndex(element.rectTransform);
  73. UpdateDisplay();
  74. }
  75. /// <summary>
  76. /// First frame a logic Element is hovering over this holder
  77. /// </summary>
  78. /// <param name="element">Element which is hovering</param>
  79. public override void OnHoverStart(LogicElementUI element)
  80. {
  81. //Create insert prefab at End
  82. insertIndex = content.childCount;
  83. UpdateDisplay();
  84. }
  85. /// <summary>
  86. /// first frame a logic Element is no longer hovering over this holder
  87. /// </summary>
  88. /// <param name="element">Element which is hovering</param>
  89. public override void OnHoverEnd(LogicElementUI element)
  90. {
  91. //Remove insert prefab
  92. insertIndex = -1;
  93. UpdateDisplay();
  94. }
  95. #endregion LogicElementHolder Implementation
  96. #region Helper Functions
  97. public int GetInsertIndex(RectTransform rt)
  98. {
  99. Rect rect = rt.GlobalRect();
  100. foreach (Transform child in content)
  101. {
  102. RectTransform childRect = child as RectTransform;
  103. if (rect.Overlaps(childRect.GlobalRect()))
  104. return child.GetSiblingIndex();
  105. }
  106. return list.Count;
  107. }
  108. #endregion Helper Functions
  109. }