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.

151 lines
4.5 KiB

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