Draggable.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. namespace UnityEditor.U2D.Layout
  4. {
  5. internal interface IDraggable
  6. {
  7. bool IsMovableNow();
  8. void UpdatePresenterPosition();
  9. }
  10. internal class Draggable : MouseManipulator
  11. {
  12. private Vector2 m_Start;
  13. protected bool m_Active;
  14. public Vector2 panSpeed { get; set; }
  15. public bool clampToParentEdges { get; set; }
  16. public Draggable(bool clampToParentEdges = false)
  17. {
  18. activators.Add(new ManipulatorActivationFilter {button = MouseButton.LeftMouse});
  19. panSpeed = Vector2.one;
  20. this.clampToParentEdges = clampToParentEdges;
  21. m_Active = false;
  22. }
  23. protected Rect CalculatePosition(float x, float y, float width, float height)
  24. {
  25. var rect = new Rect(x, y, width, height);
  26. if (clampToParentEdges)
  27. {
  28. if (rect.x < 0f)
  29. rect.x = 0f;
  30. else if (rect.xMax > target.parent.layout.width)
  31. rect.x = target.parent.layout.width - rect.width;
  32. if (rect.y < 0f)
  33. rect.y = 0f;
  34. else if (rect.yMax > target.parent.layout.height)
  35. rect.y = target.parent.layout.height - rect.height;
  36. // Reset size, we never intended to change them in the first place
  37. rect.width = width;
  38. rect.height = height;
  39. }
  40. return rect;
  41. }
  42. protected override void RegisterCallbacksOnTarget()
  43. {
  44. target.RegisterCallback<MouseDownEvent>(OnMouseDown);
  45. target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
  46. target.RegisterCallback<MouseUpEvent>(OnMouseUp);
  47. }
  48. protected override void UnregisterCallbacksFromTarget()
  49. {
  50. target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
  51. target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
  52. target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
  53. }
  54. protected void OnMouseDown(MouseDownEvent e)
  55. {
  56. if (m_Active)
  57. {
  58. e.StopImmediatePropagation();
  59. return;
  60. }
  61. /*
  62. IDraggable ce = e.target as IDraggable;
  63. if (ce == null || !ce.IsMovableNow())
  64. {
  65. return;
  66. }
  67. */
  68. if (CanStartManipulation(e))
  69. {
  70. m_Start = e.localMousePosition;
  71. m_Active = true;
  72. target.CaptureMouse();
  73. e.StopPropagation();
  74. }
  75. }
  76. protected void OnMouseMove(MouseMoveEvent e)
  77. {
  78. /*
  79. IDraggable ce = e.target as IDraggable;
  80. if (ce == null || !ce.IsMovableNow())
  81. {
  82. return;
  83. }
  84. */
  85. if (m_Active)
  86. {
  87. Vector2 diff = e.localMousePosition - m_Start;
  88. Rect rect = CalculatePosition(target.layout.x + diff.x, target.layout.y + diff.y, target.layout.width, target.layout.height);
  89. if (target.style.position == Position.Relative)
  90. {
  91. target.style.left = rect.xMin;
  92. target.style.top = rect.yMin;
  93. target.style.right = rect.xMax;
  94. target.style.bottom = rect.yMax;
  95. }
  96. else if (target.style.position == Position.Absolute)
  97. {
  98. target.style.left = rect.x;
  99. target.style.top = rect.y;
  100. }
  101. e.StopPropagation();
  102. }
  103. }
  104. protected void OnMouseUp(MouseUpEvent e)
  105. {
  106. /*
  107. IDraggable ce = e.target as IDraggable;
  108. if (ce == null || !ce.IsMovableNow())
  109. {
  110. return;
  111. }
  112. */
  113. if (m_Active)
  114. {
  115. if (CanStopManipulation(e))
  116. {
  117. //ce.UpdatePresenterPosition();
  118. m_Active = false;
  119. target.ReleaseMouse();
  120. e.StopPropagation();
  121. }
  122. }
  123. }
  124. }
  125. }