Slider2D.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. namespace UnityEditor.U2D.Animation
  3. {
  4. internal class Slider2D
  5. {
  6. private static Vector2 s_CurrentMousePosition;
  7. private static Vector2 s_DragStartScreenPosition;
  8. private static Vector2 s_DragScreenOffset;
  9. private static double s_Time;
  10. public static Vector2 Do(int controlID, Vector2 position, Handles.CapFunction drawCapFunction = null)
  11. {
  12. EventType type = Event.current.GetTypeForControl(controlID);
  13. switch (type)
  14. {
  15. case EventType.MouseDown:
  16. if (Event.current.button == 0 && HandleUtility.nearestControl == controlID && !Event.current.alt)
  17. {
  18. s_Time = EditorApplication.timeSinceStartup;
  19. GUIUtility.keyboardControl = controlID;
  20. GUIUtility.hotControl = controlID;
  21. s_CurrentMousePosition = Event.current.mousePosition;
  22. s_DragStartScreenPosition = Event.current.mousePosition;
  23. Vector2 b = HandleUtility.WorldToGUIPoint(position);
  24. s_DragScreenOffset = s_CurrentMousePosition - b;
  25. Event.current.Use();
  26. }
  27. break;
  28. case EventType.MouseUp:
  29. if (GUIUtility.hotControl == controlID && (Event.current.button == 0 || Event.current.button == 2))
  30. {
  31. GUIUtility.hotControl = 0;
  32. Event.current.Use();
  33. }
  34. break;
  35. case EventType.MouseDrag:
  36. if (GUIUtility.hotControl == controlID)
  37. {
  38. s_CurrentMousePosition = Event.current.mousePosition;
  39. float screenDisplacement = (s_CurrentMousePosition - s_DragStartScreenPosition).magnitude;
  40. Vector2 center = position;
  41. Vector2 screenPosition = s_CurrentMousePosition - s_DragScreenOffset;
  42. position = Handles.inverseMatrix.MultiplyPoint(screenPosition);
  43. float displacement = (center - position).magnitude;
  44. if (!Mathf.Approximately(displacement, 0f) && (EditorApplication.timeSinceStartup - s_Time > 0.15 || screenDisplacement >= 10f))
  45. GUI.changed = true;
  46. Event.current.Use();
  47. }
  48. break;
  49. case EventType.KeyDown:
  50. if (GUIUtility.hotControl == controlID && Event.current.keyCode == KeyCode.Escape)
  51. {
  52. position = Handles.inverseMatrix.MultiplyPoint(s_DragStartScreenPosition - s_DragScreenOffset);
  53. GUIUtility.hotControl = 0;
  54. GUI.changed = true;
  55. Event.current.Use();
  56. }
  57. break;
  58. case EventType.Layout:
  59. if (drawCapFunction != null)
  60. drawCapFunction(controlID, position, Quaternion.identity, 1f, EventType.Layout);
  61. break;
  62. case EventType.Repaint:
  63. if (drawCapFunction != null)
  64. drawCapFunction(controlID, position, Quaternion.identity, 1f, EventType.Repaint);
  65. break;
  66. }
  67. return position;
  68. }
  69. }
  70. }