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.

139 lines
3.8 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. protected Image background;
  30. protected LogicElementHolder currentHolder;
  31. private List<LogicElementHolder> lastHover = new List<LogicElementHolder>();
  32. #endregion Private Variables
  33. #region Unity Functions
  34. private void Start()
  35. {
  36. UpdateUI();
  37. }
  38. #endregion Unity Functions
  39. #region Class Implementation
  40. /// <summary>
  41. /// Updates the UI to match the LogicElemnts name and color
  42. /// </summary>
  43. public virtual void UpdateUI()
  44. {
  45. //Make sure we have the UI components
  46. if (nameText == null)
  47. nameText = GetComponentInChildren<TextMeshProUGUI>();
  48. if (background == null)
  49. background = GetComponentInChildren<Image>();
  50. //Set UI
  51. if (LogicElement != null)
  52. {
  53. //If name null use file name
  54. nameText.text = LogicElement.DisplayName;
  55. background.color = LogicElement.Color;
  56. }
  57. }
  58. public virtual void Initialise(LogicBlock LogicElement, LogicElementHolder Holder)
  59. {
  60. this.LogicElement = LogicElement;
  61. currentHolder = Holder;
  62. }
  63. #endregion Class Implementation
  64. #region Drag Implementation
  65. public override void OnBeginDrag(PointerEventData eventData)
  66. {
  67. base.OnBeginDrag(eventData);
  68. if (currentHolder != null)
  69. currentHolder.OnRemove(this);
  70. }
  71. public override void OnDrag(PointerEventData data)
  72. {
  73. base.OnDrag(data);
  74. //Get all overlapping holders
  75. List<LogicElementHolder> currentHover = LogicElementHolder.OverlappingElements(transform as RectTransform).ToList();
  76. DebugExtensions.DrawRect(rectTransform, Color.blue);
  77. LogicElementHolder.DebugDrawAll(Color.red);
  78. //Call OnHoverStart() for each holder which wasn't in the last update
  79. currentHover.Except(lastHover).ForEach(p => p.OnHoverStart(this));
  80. //Call OnHover() for each overlapping holder
  81. currentHover.ForEach(p => p.OnHover(this));
  82. //Call OnHoverEnd() for each element not overlapping but was last update
  83. lastHover.Except(currentHover).ForEach(p => p.OnHoverEnd(this));
  84. //Save all overlapping elements this update
  85. lastHover = currentHover;
  86. }
  87. public override void OnEndDrag(PointerEventData eventData)
  88. {
  89. base.OnEndDrag(eventData);
  90. //Call OnHoverStop to each element we are hovering over
  91. lastHover.ForEach(p => p.OnHoverEnd(this));
  92. //If we are still hovering over anything add this to it
  93. //Else add it back to where it came from
  94. if (lastHover.Count > 0)
  95. {
  96. lastHover[0].OnAdd(this);
  97. currentHolder = lastHover[0];
  98. }
  99. else
  100. currentHolder.OnAdd(this);
  101. }
  102. protected override void OnDoubleClick()
  103. {
  104. base.OnDoubleClick();
  105. currentHolder.OnDoubleClick(this);
  106. }
  107. #endregion Drag Implementaion
  108. }