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.

98 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class BagItem : Dragable
  6. {
  7. [SerializeField]
  8. protected LogicBlock logicElement;
  9. protected LogicTrayUI tray;
  10. protected RectTransform trayRect;
  11. private bool isHovered;
  12. public override void OnBeginDrag(PointerEventData eventData)
  13. {
  14. base.OnBeginDrag(eventData);
  15. tray = FindObjectOfType<LogicTrayUI>();
  16. if (tray != null)
  17. trayRect = (tray.transform as RectTransform);
  18. }
  19. public override void OnDrag(PointerEventData data)
  20. {
  21. base.OnDrag(data);
  22. DoTrayLogic(data);
  23. string output = "";
  24. foreach (GameObject hover in data.hovered)
  25. output += hover.name + " ";
  26. Debug.Log(output);
  27. }
  28. protected virtual void DoTrayLogic(PointerEventData data)
  29. {
  30. if (trayRect == null)
  31. return;
  32. var rt = m_DraggingIcon.GetComponent<RectTransform>();
  33. if (GetWorldSpaceRect(trayRect).Overlaps(GetWorldSpaceRect(rt)))
  34. {
  35. if (!isHovered)
  36. {
  37. tray.OnHoverStart(this, rt);
  38. isHovered = true;
  39. }
  40. tray.OnHoverUpdate(this, rt);
  41. }else if (isHovered)
  42. {
  43. isHovered = false;
  44. tray.OnHoverEnd(this);
  45. }
  46. }
  47. public override void OnEndDrag(PointerEventData eventData)
  48. {
  49. base.OnEndDrag(eventData);
  50. if (isHovered)
  51. {
  52. isHovered = false;
  53. tray.InsertLogicElement(logicElement);
  54. tray.OnHoverEnd(this);
  55. }
  56. }
  57. private void drawRect(RectTransform transform,Color color,bool global = true)
  58. {
  59. Rect rect = transform.rect;
  60. if (global)
  61. rect = GetWorldSpaceRect(transform);
  62. Debug.DrawLine(new Vector3(rect.min.x,rect.max.y,0), new Vector3(rect.max.x, rect.max.y, 0), color);
  63. Debug.DrawLine(new Vector3(rect.max.x, rect.max.y, 0), new Vector3(rect.max.x, rect.min.y, 0), color);
  64. Debug.DrawLine(new Vector3(rect.max.x, rect.min.y, 0), new Vector3(rect.min.x, rect.min.y, 0), color);
  65. Debug.DrawLine(new Vector3(rect.min.x, rect.min.y, 0), new Vector3(rect.min.x, rect.max.y, 0), color);
  66. }
  67. Rect GetWorldSpaceRect(RectTransform rt)
  68. {
  69. var r = rt.rect;
  70. Vector2 centre = rt.TransformPoint(r.center);
  71. r.size = rt.TransformVector(r.size);
  72. r.center = centre;
  73. return r;
  74. }
  75. }