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.

101 lines
3.5 KiB

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