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.

181 lines
5.2 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. Vector2 offset = new Vector2(element.rectTransform.rect.width/2, 0);
  77. int index = GetInsertIndex(element.rectTransform,offset);
  78. if (index >= reader.LogicChain.Count)
  79. index = reader.LogicChain.Count - 1;
  80. reader.LogicChain.RemoveAt(index);
  81. }
  82. /// <summary>
  83. /// When a logicElement needs to be added back to this Tray
  84. /// </summary>
  85. /// <param name="element">Element to add</param>
  86. public override void OnAdd(LogicElementUI element)
  87. {
  88. int index = GetInsertIndex(element.rectTransform);
  89. reader.Insert(index, element.logicElement);
  90. Destroy(element.gameObject);
  91. }
  92. /// <summary>
  93. /// When a logic Element is hovering over this holder
  94. /// </summary>
  95. /// <param name="element">Element which is hovering</param>
  96. public override void OnHover(LogicElementUI element)
  97. {
  98. insertIndex = GetInsertIndex(element.rectTransform);
  99. UpdateDisplay();
  100. }
  101. /// <summary>
  102. /// First frame a logic Element is hovering over this holder
  103. /// </summary>
  104. /// <param name="element">Element which is hovering</param>
  105. public override void OnHoverStart(LogicElementUI element)
  106. {
  107. Debug.Log("OnHoverStart");
  108. //Create insert prefab at End
  109. insertIndex = content.childCount;
  110. UpdateDisplay();
  111. }
  112. /// <summary>
  113. /// first frame a logic Element is no longer hovering over this holder
  114. /// </summary>
  115. /// <param name="element">Element which is hovering</param>
  116. public override void OnHoverEnd(LogicElementUI element)
  117. {
  118. //Remove insert prefab
  119. insertIndex = -1;
  120. UpdateDisplay();
  121. }
  122. #endregion LogicElementHolder Implementation
  123. /// <summary>
  124. /// Called when an element in this holder is double clicked
  125. /// </summary>
  126. /// <param name="element">element which was double clicked</param>
  127. public override void OnDoubleClick(LogicElementUI element)
  128. {
  129. throw new System.NotImplementedException();
  130. }
  131. #region Helper Functions
  132. public int GetInsertIndex(RectTransform rt, Vector2 offset)
  133. {
  134. Rect rect = rt.GlobalRect();
  135. rect.center += offset;
  136. foreach (Transform child in content)
  137. {
  138. RectTransform childRect = child as RectTransform;
  139. if (rect.Overlaps(childRect.GlobalRect()))
  140. return child.GetSiblingIndex();
  141. }
  142. return reader.LogicChain.Count;
  143. }
  144. public int GetInsertIndex(RectTransform rt)
  145. {
  146. return GetInsertIndex(rt, Vector2.zero);
  147. }
  148. #endregion Helper Functions
  149. }