GUIState.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. public class GUIState : IGUIState
  6. {
  7. private Handles.CapFunction nullCap = (int c, Vector3 p , Quaternion r, float s, EventType ev) => {};
  8. public Vector2 mousePosition
  9. {
  10. get { return Event.current.mousePosition; }
  11. }
  12. public int mouseButton
  13. {
  14. get { return Event.current.button; }
  15. }
  16. public int clickCount
  17. {
  18. get { return Event.current.clickCount; }
  19. set { Event.current.clickCount = Mathf.Max(0, value); }
  20. }
  21. public bool isShiftDown
  22. {
  23. get { return Event.current.shift; }
  24. }
  25. public bool isAltDown
  26. {
  27. get { return Event.current.alt; }
  28. }
  29. public bool isActionKeyDown
  30. {
  31. get { return EditorGUI.actionKey; }
  32. }
  33. public KeyCode keyCode
  34. {
  35. get { return Event.current.keyCode; }
  36. }
  37. public EventType eventType
  38. {
  39. get { return Event.current.type; }
  40. }
  41. public string commandName
  42. {
  43. get { return Event.current.commandName; }
  44. }
  45. public int nearestControl
  46. {
  47. get { return HandleUtility.nearestControl; }
  48. set { HandleUtility.nearestControl = value; }
  49. }
  50. public int hotControl
  51. {
  52. get { return GUIUtility.hotControl; }
  53. set { GUIUtility.hotControl = value; }
  54. }
  55. public bool changed
  56. {
  57. get { return GUI.changed; }
  58. set { GUI.changed = value; }
  59. }
  60. public int GetControlID(int hint, FocusType focusType)
  61. {
  62. return GUIUtility.GetControlID(hint, focusType);
  63. }
  64. public void AddControl(int controlID, float distance)
  65. {
  66. HandleUtility.AddControl(controlID, distance);
  67. }
  68. public bool Slider(int id, SliderData sliderData, out Vector3 newPosition)
  69. {
  70. if (mouseButton == 0 && eventType == EventType.MouseDown)
  71. {
  72. hotControl = 0;
  73. nearestControl = id;
  74. }
  75. EditorGUI.BeginChangeCheck();
  76. newPosition = Handles.Slider2D(id, sliderData.position, sliderData.forward, sliderData.right, sliderData.up, 1f, nullCap, Vector2.zero);
  77. return EditorGUI.EndChangeCheck();
  78. }
  79. public void UseEvent()
  80. {
  81. Event.current.Use();
  82. }
  83. public void Repaint()
  84. {
  85. HandleUtility.Repaint();
  86. }
  87. public bool HasCurrentCamera()
  88. {
  89. return Camera.current != null;
  90. }
  91. public float GetHandleSize(Vector3 position)
  92. {
  93. var scale = HasCurrentCamera() ? 0.01f : 0.05f;
  94. return HandleUtility.GetHandleSize(position) * scale;
  95. }
  96. public float DistanceToSegment(Vector3 p1, Vector3 p2)
  97. {
  98. p1 = HandleUtility.WorldToGUIPoint(p1);
  99. p2 = HandleUtility.WorldToGUIPoint(p2);
  100. return HandleUtility.DistancePointToLineSegment(Event.current.mousePosition, p1, p2);
  101. }
  102. public float DistanceToCircle(Vector3 center, float radius)
  103. {
  104. return HandleUtility.DistanceToCircle(center, radius);
  105. }
  106. public Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePos)
  107. {
  108. Vector3 worldPos = Handles.inverseMatrix.MultiplyPoint(guiPosition);
  109. if (Camera.current)
  110. {
  111. Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);
  112. planeNormal = Handles.matrix.MultiplyVector(planeNormal);
  113. planePos = Handles.matrix.MultiplyPoint(planePos);
  114. Plane plane = new Plane(planeNormal, planePos);
  115. float distance = 0f;
  116. if (plane.Raycast(ray, out distance))
  117. {
  118. worldPos = Handles.inverseMatrix.MultiplyPoint(ray.GetPoint(distance));
  119. }
  120. }
  121. return worldPos;
  122. }
  123. }
  124. }