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.

161 lines
4.5 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. [SerializeField]
  17. public BlockReader reader;
  18. #region Unity Functions
  19. public void Start()
  20. {
  21. UpdateDisplay();
  22. }
  23. protected override void OnEnable()
  24. {
  25. base.OnEnable();
  26. if (reader != null)
  27. reader.OnUpdate += UpdateDisplay;
  28. }
  29. protected override void OnDisable()
  30. {
  31. base.OnDisable();
  32. if (reader != null)
  33. reader.OnUpdate -= UpdateDisplay;
  34. }
  35. #endregion Unity Functions
  36. public void SetBlockReader(BlockReader newBlockReader)
  37. {
  38. if (reader != null)
  39. reader.OnUpdate -= UpdateDisplay;
  40. reader = newBlockReader;
  41. reader.OnUpdate += UpdateDisplay;
  42. UpdateDisplay();
  43. }
  44. [ContextMenu("Update Display")]
  45. public void UpdateDisplay()
  46. {
  47. if (reader == null)
  48. return;
  49. //Destroy all children
  50. 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
  51. foreach (Transform child in content)
  52. Destroy(child.gameObject);
  53. //Instatiate logicElement prefab
  54. foreach (LogicBlock element in reader.LogicChain)
  55. {
  56. LogicElementUI elementUI = Instantiate(Prefab.gameObject, content).GetComponent<LogicElementUI>();
  57. elementUI.Initialise(element, this);
  58. }
  59. //Add insertIndex object if needed
  60. if (insertIndex != -1)
  61. {
  62. GameObject elementObject = Instantiate(Prefab.gameObject, content);
  63. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
  64. elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  65. //Set sibling index to where we want it in list + add destroyedCount since technically they are still here
  66. elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
  67. }
  68. }
  69. #region LogicElementHolder Implementation
  70. /// <summary>
  71. /// When a LogicElement has started being dragged from this Tray
  72. /// </summary>
  73. /// <param name="element">Element which is being dragged</param>
  74. public override void OnRemove(LogicElementUI element)
  75. {
  76. reader.Remove(element.logicElement);
  77. }
  78. /// <summary>
  79. /// When a logicElement needs to be added back to this Tray
  80. /// </summary>
  81. /// <param name="element">Element to add</param>
  82. public override void OnAdd(LogicElementUI element)
  83. {
  84. int index = GetInsertIndex(element.rectTransform);
  85. reader.Insert(index, element.logicElement);
  86. Destroy(element.gameObject);
  87. }
  88. /// <summary>
  89. /// When a logic Element is hovering over this holder
  90. /// </summary>
  91. /// <param name="element">Element which is hovering</param>
  92. public override void OnHover(LogicElementUI element)
  93. {
  94. insertIndex = GetInsertIndex(element.rectTransform);
  95. UpdateDisplay();
  96. }
  97. /// <summary>
  98. /// First frame a logic Element is hovering over this holder
  99. /// </summary>
  100. /// <param name="element">Element which is hovering</param>
  101. public override void OnHoverStart(LogicElementUI element)
  102. {
  103. Debug.Log("OnHoverStart");
  104. //Create insert prefab at End
  105. insertIndex = content.childCount;
  106. UpdateDisplay();
  107. }
  108. /// <summary>
  109. /// first frame a logic Element is no longer hovering over this holder
  110. /// </summary>
  111. /// <param name="element">Element which is hovering</param>
  112. public override void OnHoverEnd(LogicElementUI element)
  113. {
  114. //Remove insert prefab
  115. insertIndex = -1;
  116. UpdateDisplay();
  117. }
  118. #endregion LogicElementHolder Implementation
  119. #region Helper Functions
  120. public int GetInsertIndex(RectTransform rt)
  121. {
  122. Rect rect = rt.GlobalRect();
  123. foreach (Transform child in content)
  124. {
  125. RectTransform childRect = child as RectTransform;
  126. if (rect.Overlaps(childRect.GlobalRect()))
  127. return child.GetSiblingIndex();
  128. }
  129. return reader.LogicChain.Count;
  130. }
  131. #endregion Helper Functions
  132. }