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.

111 lines
3.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Class which component which an LogicElementUI can be dragged into should derive from
  6. /// </summary>
  7. public abstract class LogicElementHolder : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// List of all Holders in scene
  11. /// </summary>
  12. private static List<LogicElementHolder> EnabledHolders = new List<LogicElementHolder>();
  13. [Tooltip("An Element with a higher priority will be used if two object overlap")]
  14. public int Priority = 0;
  15. #region Unity Functions
  16. protected virtual void OnEnable()
  17. {
  18. EnabledHolders.Add(this);
  19. }
  20. protected virtual void OnDisable()
  21. {
  22. EnabledHolders.Remove(this);
  23. }
  24. #endregion
  25. #region Class Funtions
  26. /// <summary>
  27. /// When a LogicElement has started being dragged from this holder
  28. /// </summary>
  29. /// <param name="element">Element which is being dragged</param>
  30. public abstract void OnRemove(LogicElementUI element);
  31. /// <summary>
  32. /// When a logicElement needs to be added back to this holder
  33. /// </summary>
  34. /// <param name="element">Element which is being Added</param>
  35. public abstract void OnAdd(LogicElementUI element);
  36. /// <summary>
  37. /// When a logic Element is hovering over this holder
  38. /// </summary>
  39. /// <param name="element">Element which is hovering</param>
  40. public virtual void OnHover(LogicElementUI element) { }
  41. /// <summary>
  42. /// First frame a logic Element is hovering over this holder
  43. /// </summary>
  44. /// <param name="element">Element which is hovering</param>
  45. public virtual void OnHoverStart(LogicElementUI element) { }
  46. /// <summary>
  47. /// first frame a logic Element is no longer hovering over this holder
  48. /// </summary>
  49. /// <param name="element">Element which is hovering</param>
  50. public virtual void OnHoverEnd(LogicElementUI element) { }
  51. /// <summary>
  52. /// Called when an element in this holder is double clicked
  53. /// </summary>
  54. /// <param name="element">element which was double clicked</param>
  55. public abstract void OnDoubleClick(LogicElementUI element);
  56. /// <summary>
  57. /// Called to check if this holder can hold the provided element;
  58. /// </summary>
  59. /// <param name="element">element to check if it can hold</param>
  60. /// <returns>returns true if this can hold the element </returns>
  61. public virtual bool canHold(LogicBlock element) { return true; }
  62. #endregion Class Funtions
  63. #region Static Functions
  64. /// <summary>
  65. /// Gets an array of Any element holders a RectTransform is overlapping with
  66. /// </summary>
  67. /// <param name="rt">recttransform to check overlapping</param>
  68. /// <returns>array of Any element holders a rt is overlapping with</returns>
  69. public static LogicElementHolder[] OverlappingElements(RectTransform rt)
  70. {
  71. List<LogicElementHolder> retVal = new List<LogicElementHolder>();
  72. Rect rect = rt.GlobalRect();
  73. Rect holderRect;
  74. foreach (LogicElementHolder holder in EnabledHolders)
  75. {
  76. holderRect = (holder.transform as RectTransform).GlobalRect();
  77. if (rect.Overlaps(holderRect))
  78. retVal.Add(holder);
  79. }
  80. return retVal.ToArray();
  81. }
  82. public static void DebugDrawAll(Color color, float duration = 0.0f, bool depthTest = false)
  83. {
  84. EnabledHolders.ForEach(p => DebugExtensions.DrawRect(p.transform as RectTransform, color,duration, depthTest));
  85. }
  86. #endregion
  87. }