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.

84 lines
2.6 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. #region Unity Functions
  14. private void OnEnable()
  15. {
  16. EnabledHolders.Add(this);
  17. }
  18. private void OnDisable()
  19. {
  20. EnabledHolders.Remove(this);
  21. }
  22. #endregion
  23. #region Class Funtions
  24. /// <summary>
  25. /// When a LogicElement has started being dragged from this holder
  26. /// </summary>
  27. /// <param name="element">Element which is being dragged</param>
  28. public abstract void OnRemove(LogicElementUI element);
  29. /// <summary>
  30. /// When a logicElement needs to be added back to this holder
  31. /// </summary>
  32. /// <param name="element">Element which is being Added</param>
  33. public abstract void OnAdd(LogicElementUI element);
  34. /// <summary>
  35. /// When a logic Element is hovering over this holder
  36. /// </summary>
  37. /// <param name="element">Element which is hovering</param>
  38. public virtual void OnHover(LogicElementUI element) { }
  39. /// <summary>
  40. /// First frame a logic Element is hovering over this holder
  41. /// </summary>
  42. /// <param name="element">Element which is hovering</param>
  43. public virtual void OnHoverStart(LogicElementUI element) { }
  44. /// <summary>
  45. /// first frame a logic Element is no longer hovering over this holder
  46. /// </summary>
  47. /// <param name="element">Element which is hovering</param>
  48. public virtual void OnHoverEnd(LogicElementUI element) { }
  49. #endregion Class Funtions
  50. #region Static Functions
  51. /// <summary>
  52. /// Gets an array of Any element holders a RectTransform is overlapping with
  53. /// </summary>
  54. /// <param name="rt">recttransform to check overlapping</param>
  55. /// <returns>array of Any element holders a rt is overlapping with</returns>
  56. public static LogicElementHolder[] OverlappingElements(RectTransform rt)
  57. {
  58. List<LogicElementHolder> retVal = new List<LogicElementHolder>();
  59. Rect rect = rt.GlobalRect();
  60. Rect holderRect;
  61. foreach (LogicElementHolder holder in EnabledHolders)
  62. {
  63. holderRect = (holder.transform as RectTransform).GlobalRect();
  64. if (rect.Overlaps(holderRect))
  65. retVal.Add(holder);
  66. }
  67. return retVal.ToArray();
  68. }
  69. #endregion
  70. }