PanelSplitter.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. #if UNITY_2019_1_OR_NEWER
  4. using UnityEditor.UIElements;
  5. using UnityEngine.UIElements;
  6. #else
  7. using UnityEditor.Experimental.UIElements;
  8. using UnityEngine.Experimental.UIElements;
  9. #endif
  10. namespace UnityEditor.Recorder
  11. {
  12. class PanelSplitter : VisualElement
  13. {
  14. readonly VisualElement m_AffectedElement;
  15. bool m_Grabbed;
  16. Vector2 m_GrabbedMousePosition;
  17. float m_ElementOriginalWidth;
  18. const float k_SplitterWidth = 5.0f;
  19. void SetWidth(float value)
  20. {
  21. m_AffectedElement.style.width = value;
  22. RecorderOptions.recorderPanelWith = value;
  23. }
  24. public PanelSplitter(VisualElement affectedElement)
  25. {
  26. m_AffectedElement = affectedElement;
  27. style.width = k_SplitterWidth;
  28. style.minWidth = k_SplitterWidth;
  29. style.maxWidth = k_SplitterWidth;
  30. UIElementHelper.RegisterTrickleDownCallback<MouseDownEvent>(this, OnMouseDown);
  31. UIElementHelper.RegisterTrickleDownCallback<MouseMoveEvent>(this, OnMouseMove);
  32. UIElementHelper.RegisterTrickleDownCallback<MouseUpEvent>(this, OnMouseUp);
  33. var w = RecorderOptions.recorderPanelWith;
  34. if (w > 0.0f)
  35. SetWidth(w);
  36. }
  37. void OnMouseDown(MouseDownEvent evt)
  38. {
  39. if (evt.button != (int) MouseButton.LeftMouse)
  40. return;
  41. if (m_Grabbed)
  42. return;
  43. #if UNITY_2018_3_OR_NEWER
  44. this.CaptureMouse();
  45. #else
  46. this.TakeMouseCapture();
  47. #endif
  48. m_Grabbed = true;
  49. m_GrabbedMousePosition = evt.mousePosition;
  50. #if UNITY_2019_1_OR_NEWER
  51. m_ElementOriginalWidth = m_AffectedElement.resolvedStyle.width;
  52. #else
  53. m_ElementOriginalWidth = m_AffectedElement.style.width;
  54. #endif
  55. evt.StopImmediatePropagation();
  56. }
  57. void OnMouseMove(MouseMoveEvent evt)
  58. {
  59. if (!m_Grabbed)
  60. return;
  61. var delta = evt.mousePosition.x - m_GrabbedMousePosition.x;
  62. #if UNITY_2019_1_OR_NEWER
  63. var minWidth = m_AffectedElement.resolvedStyle.minWidth.value;
  64. var maxWidth = m_AffectedElement.resolvedStyle.maxWidth.value;
  65. #else
  66. var minWidth = m_AffectedElement.style.minWidth;
  67. var maxWidth = m_AffectedElement.style.maxWidth;
  68. #endif
  69. var newWidth = Mathf.Max(m_ElementOriginalWidth + delta, minWidth);
  70. if (maxWidth > 0.0f)
  71. newWidth = Mathf.Min(newWidth, maxWidth);
  72. SetWidth(newWidth);
  73. }
  74. void OnMouseUp(MouseUpEvent evt)
  75. {
  76. if (evt.button != (int) MouseButton.LeftMouse)
  77. return;
  78. if (!m_Grabbed)
  79. return;
  80. m_Grabbed = false;
  81. #if UNITY_2018_3_OR_NEWER
  82. this.ReleaseMouse();
  83. #else
  84. this.ReleaseMouseCapture();
  85. #endif
  86. evt.StopImmediatePropagation();
  87. }
  88. }
  89. }