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.

204 lines
5.9 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. private int maxLength = 6;
  18. [SerializeField]
  19. public BlockReader reader;
  20. #region Unity Functions
  21. public void Start()
  22. {
  23. UpdateDisplay();
  24. }
  25. protected override void OnEnable()
  26. {
  27. base.OnEnable();
  28. if (reader != null)
  29. reader.OnUpdate += UpdateDisplay;
  30. }
  31. protected override void OnDisable()
  32. {
  33. base.OnDisable();
  34. if (reader != null)
  35. reader.OnUpdate -= UpdateDisplay;
  36. }
  37. #endregion Unity Functions
  38. public void SetBlockReader(BlockReader newBlockReader)
  39. {
  40. if (reader != null)
  41. reader.OnUpdate -= UpdateDisplay;
  42. reader = newBlockReader;
  43. reader.OnUpdate += UpdateDisplay;
  44. UpdateDisplay();
  45. }
  46. [ContextMenu("Update Display")]
  47. public void UpdateDisplay()
  48. {
  49. if (reader == null)
  50. return;
  51. //Destroy all children
  52. 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
  53. foreach (Transform child in content)
  54. Destroy(child.gameObject);
  55. //Instatiate logicElement prefab
  56. foreach (LogicBlock element in reader.LogicChain)
  57. {
  58. LogicElementUI elementUI = Instantiate(Prefab.gameObject, content).GetComponent<LogicElementUI>();
  59. elementUI.Initialise(element, this);
  60. }
  61. //Add insertIndex object if needed
  62. if (insertIndex != -1)
  63. {
  64. GameObject elementObject = Instantiate(Prefab.gameObject, content);
  65. if (reader.Length < maxLength)
  66. {
  67. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "insert";
  68. elementObject.GetComponentInChildren<Image>().color = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  69. }
  70. else
  71. {
  72. elementObject.GetComponentInChildren<TextMeshProUGUI>().text = "X";
  73. elementObject.GetComponentInChildren<Image>().color = new Color(1.0f, 0.25f, 0.25f, 0.5f);
  74. }
  75. //Set sibling index to where we want it in list + add destroyedCount since technically they are still here
  76. elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex);
  77. }
  78. }
  79. #region LogicElementHolder Implementation
  80. /// <summary>
  81. /// When a LogicElement has started being dragged from this Tray
  82. /// </summary>
  83. /// <param name="element">Element which is being dragged</param>
  84. public override void OnRemove(LogicElementUI element)
  85. {
  86. Vector2 offset = new Vector2(element.rectTransform.rect.width / 2, 0);
  87. int index = GetInsertIndex(element.rectTransform, offset);
  88. if (index >= reader.LogicChain.Count)
  89. index = reader.LogicChain.Count - 1;
  90. reader.LogicChain.RemoveAt(index);
  91. }
  92. /// <summary>
  93. /// When a logicElement needs to be added back to this Tray
  94. /// </summary>
  95. /// <param name="element">Element to add</param>
  96. public override void OnAdd(LogicElementUI element)
  97. {
  98. int index = GetInsertIndex(element.rectTransform);
  99. reader.Insert(index, element.logicElement);
  100. Destroy(element.gameObject);
  101. }
  102. /// <summary>
  103. /// When a logic Element is hovering over this holder
  104. /// </summary>
  105. /// <param name="element">Element which is hovering</param>
  106. public override void OnHover(LogicElementUI element)
  107. {
  108. insertIndex = GetInsertIndex(element.rectTransform);
  109. UpdateDisplay();
  110. }
  111. /// <summary>
  112. /// First frame a logic Element is hovering over this holder
  113. /// </summary>
  114. /// <param name="element">Element which is hovering</param>
  115. public override void OnHoverStart(LogicElementUI element)
  116. {
  117. Debug.Log("OnHoverStart");
  118. //Create insert prefab at End
  119. insertIndex = content.childCount;
  120. UpdateDisplay();
  121. }
  122. /// <summary>
  123. /// first frame a logic Element is no longer hovering over this holder
  124. /// </summary>
  125. /// <param name="element">Element which is hovering</param>
  126. public override void OnHoverEnd(LogicElementUI element)
  127. {
  128. //Remove insert prefab
  129. insertIndex = -1;
  130. UpdateDisplay();
  131. }
  132. /// <summary>
  133. /// Called when an element in this holder is double clicked
  134. /// </summary>
  135. /// <param name="element">element which was double clicked</param>
  136. public override void OnDoubleClick(LogicElementUI element)
  137. {
  138. throw new System.NotImplementedException();
  139. }
  140. /// <summary>
  141. /// Called to check if this holder can hold the provided element;
  142. /// </summary>
  143. /// <param name="element">element to check if it can hold</param>
  144. /// <returns>returns true if this can hold the element </returns>
  145. public override bool canHold(LogicElementUI element)
  146. {
  147. return reader.Length < maxLength;
  148. }
  149. #endregion LogicElementHolder Implementation
  150. #region Helper Functions
  151. public int GetInsertIndex(RectTransform rt, Vector2 offset)
  152. {
  153. Rect rect = rt.GlobalRect();
  154. rect.center += offset;
  155. foreach (Transform child in content)
  156. {
  157. RectTransform childRect = child as RectTransform;
  158. if (rect.Overlaps(childRect.GlobalRect()))
  159. return child.GetSiblingIndex();
  160. }
  161. return reader.LogicChain.Count;
  162. }
  163. public int GetInsertIndex(RectTransform rt)
  164. {
  165. return GetInsertIndex(rt, Vector2.zero);
  166. }
  167. #endregion Helper Functions
  168. }