VisibilityToolResizer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. namespace UnityEditor.U2D.Layout
  4. {
  5. internal class VisibilityToolResizer : MouseManipulator
  6. {
  7. private Vector2 m_Start;
  8. private VisualElement m_Root;
  9. protected bool m_Active;
  10. private Rect m_StartPos;
  11. public VisibilityToolResizer()
  12. {
  13. activators.Add(new ManipulatorActivationFilter {button = MouseButton.LeftMouse});
  14. m_Active = false;
  15. }
  16. protected override void RegisterCallbacksOnTarget()
  17. {
  18. target.RegisterCallback<MouseDownEvent>(OnMouseDown);
  19. target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
  20. target.RegisterCallback<MouseUpEvent>(OnMouseUp);
  21. }
  22. protected override void UnregisterCallbacksFromTarget()
  23. {
  24. target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
  25. target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
  26. target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
  27. }
  28. protected void OnMouseDown(MouseDownEvent e)
  29. {
  30. if (m_Active)
  31. {
  32. e.StopImmediatePropagation();
  33. return;
  34. }
  35. if (CanStartManipulation(e))
  36. {
  37. m_Root = target;
  38. while (m_Root.parent != null)
  39. m_Root = m_Root.parent;
  40. m_Start = target.ChangeCoordinatesTo(m_Root, e.localMousePosition);
  41. m_StartPos = target.parent.layout;
  42. m_Active = true;
  43. target.CaptureMouse();
  44. e.StopPropagation();
  45. }
  46. }
  47. protected void OnMouseMove(MouseMoveEvent e)
  48. {
  49. if (m_Active)
  50. {
  51. var ce = target.parent;
  52. Vector2 diff = target.ChangeCoordinatesTo(m_Root, e.localMousePosition) - m_Start;
  53. var newSize = new Vector2(m_StartPos.width - diff.x, m_StartPos.height - diff.y);
  54. float minWidth = ce.resolvedStyle.minWidth == StyleKeyword.Auto ? 0 : ce.resolvedStyle.minWidth.value;
  55. float minHeight = ce.resolvedStyle.minHeight == StyleKeyword.Auto ? 0 : ce.resolvedStyle.minHeight.value;
  56. float maxWidth = ce.resolvedStyle.maxWidth == StyleKeyword.None ? float.MaxValue : ce.resolvedStyle.maxWidth.value;
  57. float maxHeight = ce.resolvedStyle.maxHeight == StyleKeyword.None ? float.MaxValue : ce.resolvedStyle.maxHeight.value;
  58. newSize.x = (newSize.x < minWidth) ? minWidth : ((newSize.x > maxWidth) ? maxWidth : newSize.x);
  59. newSize.y = (newSize.y < minHeight) ? minHeight : ((newSize.y > maxHeight) ? maxHeight : newSize.y);
  60. ce.style.width = newSize.x;
  61. e.StopPropagation();
  62. }
  63. }
  64. protected void OnMouseUp(MouseUpEvent e)
  65. {
  66. if (m_Active)
  67. {
  68. if (CanStopManipulation(e))
  69. {
  70. m_Active = false;
  71. target.ReleaseMouse();
  72. e.StopPropagation();
  73. }
  74. }
  75. }
  76. }
  77. }