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 System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using TMPro;
  8. /// <summary>
  9. /// Component which controls how a LogicElement is displayed in the UI;
  10. /// </summary>
  11. public class LogicElementUI : Dragable
  12. {
  13. #region Inspector Fields
  14. [SerializeField]
  15. [Tooltip("Logic Block this will display")]
  16. protected LogicBlock LogicElement;
  17. #endregion Inspector Fields
  18. #region ReadOnly Variables
  19. /// <summary>
  20. /// RectTransform of LogicElementUI
  21. /// </summary>
  22. public RectTransform rectTransform { get { return transform as RectTransform; } }
  23. public LogicBlock logicElement { get { return LogicElement; } }
  24. #endregion ReadOnly Variables
  25. #region Private Variables
  26. //Text Mesh pro to display Name
  27. protected TextMeshProUGUI nameText;
  28. //background image to color;
  29. public GameObject background;
  30. //sprite imager;
  31. public GameObject icon;
  32. protected LogicElementHolder currentHolder;
  33. private List<LogicElementHolder> lastHover = new List<LogicElementHolder>();
  34. #endregion Private Variables
  35. #region Unity Functions
  36. private void Start()
  37. {
  38. UpdateUI();
  39. }
  40. #endregion Unity Functions
  41. #region Class Implementation
  42. /// <summary>
  43. /// Updates the UI to match the LogicElemnts name and color
  44. /// </summary>
  45. public virtual void UpdateUI()
  46. {
  47. //Make sure we have the UI components
  48. if (nameText == null)
  49. nameText = GetComponentInChildren<TextMeshProUGUI>();
  50. //Set UI
  51. if (LogicElement != null)
  52. {
  53. //If name null use file name
  54. nameText.text = LogicElement.DisplayName;
  55. nameText.outlineColor = new Color32(0, 0, 0, 255);
  56. nameText.outlineWidth = 0.1f;
  57. icon.GetComponent<Image>().color = LogicElement.Color;
  58. if(logicElement.icon != null)
  59. {
  60. background.GetComponent<Image>().sprite = LogicElement.icon;
  61. }
  62. else
  63. {
  64. background.GetComponent<Image>().enabled = false;
  65. }
  66. }
  67. }
  68. public virtual void Initialise(LogicBlock LogicElement, LogicElementHolder Holder,bool copy)
  69. {
  70. if (copy)
  71. this.LogicElement = LogicElement.Clone();
  72. else
  73. this.LogicElement = LogicElement;
  74. currentHolder = Holder;
  75. }
  76. #endregion Class Implementation
  77. #region Drag Implementation
  78. public override void OnBeginDrag(PointerEventData eventData)
  79. {
  80. base.OnBeginDrag(eventData);
  81. if (currentHolder != null)
  82. currentHolder.OnRemove(this);
  83. }
  84. public override void OnDrag(PointerEventData data)
  85. {
  86. base.OnDrag(data);
  87. //Get all overlapping holders
  88. List<LogicElementHolder> currentHover = LogicElementHolder.OverlappingElements(transform as RectTransform).ToList();
  89. DebugExtensions.DrawRect(rectTransform, Color.blue);
  90. LogicElementHolder.DebugDrawAll(Color.red);
  91. //Call OnHoverStart() for each holder which wasn't in the last update
  92. currentHover.Except(lastHover).ForEach(p => p.OnHoverStart(this));
  93. //Call OnHover() for each overlapping holder
  94. currentHover.ForEach(p => p.OnHover(this));
  95. //Call OnHoverEnd() for each element not overlapping but was last update
  96. lastHover.Except(currentHover).ForEach(p => p.OnHoverEnd(this));
  97. //Save all overlapping elements this update
  98. lastHover = currentHover;
  99. }
  100. public override void OnEndDrag(PointerEventData eventData)
  101. {
  102. base.OnEndDrag(eventData);
  103. //Call OnHoverStop to each element we are hovering over
  104. lastHover.ForEach(p => p.OnHoverEnd(this));
  105. //order the UIHolders by priority and get the first which will accept this
  106. lastHover = lastHover.OrderByDescending(p => p.Priority).ToList();
  107. lastHover.ForEach(p => Debug.Log(p.gameObject.name));
  108. LogicElementHolder holderToAdd = lastHover.FirstOrDefault(p => p.canHold(LogicElement));
  109. //If we are still hovering over anything add this to it
  110. //Else add it back to where it came from
  111. if (holderToAdd != default)
  112. {
  113. holderToAdd.OnAdd(this);
  114. currentHolder = holderToAdd;
  115. }
  116. else
  117. currentHolder.OnAdd(this);
  118. }
  119. protected override void OnDoubleClick()
  120. {
  121. base.OnDoubleClick();
  122. currentHolder.OnDoubleClick(this);
  123. }
  124. #endregion Drag Implementaion
  125. }