No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GUIState.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. /// <summary>
  6. /// An implementation of an IGUIState that represents a generic GUI state.
  7. /// </summary>
  8. public class GUIState : IGUIState
  9. {
  10. private Handles.CapFunction nullCap = (int c, Vector3 p , Quaternion r, float s, EventType ev) => {};
  11. /// <summary>
  12. /// The current mouse position.
  13. /// </summary>
  14. public Vector2 mousePosition
  15. {
  16. get { return Event.current.mousePosition; }
  17. }
  18. /// <summary>
  19. /// The currently pressed button.
  20. /// </summary>
  21. public int mouseButton
  22. {
  23. get { return Event.current.button; }
  24. }
  25. /// <summary>
  26. /// The current number of mouse clicks.
  27. /// </summary>
  28. public int clickCount
  29. {
  30. get { return Event.current.clickCount; }
  31. set { Event.current.clickCount = Mathf.Max(0, value); }
  32. }
  33. /// <summary>
  34. /// Indicates whether the shift key is pressed.
  35. /// </summary>
  36. public bool isShiftDown
  37. {
  38. get { return Event.current.shift; }
  39. }
  40. /// <summary>
  41. /// Indicates whether the alt key is pressed.
  42. /// </summary>
  43. public bool isAltDown
  44. {
  45. get { return Event.current.alt; }
  46. }
  47. /// <summary>
  48. /// Indicates whether the action key is pressed.
  49. /// </summary>
  50. public bool isActionKeyDown
  51. {
  52. get { return EditorGUI.actionKey; }
  53. }
  54. /// <summary>
  55. /// The KeyCode of the currently pressed key.
  56. /// </summary>
  57. public KeyCode keyCode
  58. {
  59. get { return Event.current.keyCode; }
  60. }
  61. /// <summary>
  62. /// The type of the current event.
  63. /// </summary>
  64. public EventType eventType
  65. {
  66. get { return Event.current.type; }
  67. }
  68. /// <summary>
  69. /// The name of the current event's command.
  70. /// </summary>
  71. public string commandName
  72. {
  73. get { return Event.current.commandName; }
  74. }
  75. /// <summary>
  76. /// The closest control to the event.
  77. /// </summary>
  78. public int nearestControl
  79. {
  80. get { return HandleUtility.nearestControl; }
  81. set { HandleUtility.nearestControl = value; }
  82. }
  83. /// <summary>
  84. /// Hot Control
  85. /// </summary>
  86. public int hotControl
  87. {
  88. get { return GUIUtility.hotControl; }
  89. set { GUIUtility.hotControl = value; }
  90. }
  91. /// <summary>
  92. /// Indicates whether the GUI has changed.
  93. /// </summary>
  94. public bool changed
  95. {
  96. get { return GUI.changed; }
  97. set { GUI.changed = value; }
  98. }
  99. /// <summary>
  100. /// Gets the ID of a nested control by a hint and focus type.
  101. /// </summary>
  102. /// <param name="hint">The hint this function uses to identify the control ID.</param>
  103. /// <param name="focusType">The focus Type</param>
  104. /// <returns>Returns the ID of the control that matches the hint and focus type.</returns>
  105. public int GetControlID(int hint, FocusType focusType)
  106. {
  107. return GUIUtility.GetControlID(hint, focusType);
  108. }
  109. /// <summary>
  110. /// Adds a control to the GUIState.
  111. /// </summary>
  112. /// <param name="controlID">The ID of the control to add.</param>
  113. /// <param name="distance">The distance from the camera to the control.</param>
  114. public void AddControl(int controlID, float distance)
  115. {
  116. HandleUtility.AddControl(controlID, distance);
  117. }
  118. /// <summary>
  119. /// Checks whether a slider value has changed.
  120. /// </summary>
  121. /// <param name="id">The ID of the slider to check.</param>
  122. /// <param name="sliderData">The slider's data.</param>
  123. /// <param name="newPosition">The new position of the slider.</param>
  124. /// <returns>Returns `true` if the slider has changed. Otherwise, returns `false`.</returns>
  125. public bool Slider(int id, SliderData sliderData, out Vector3 newPosition)
  126. {
  127. if (mouseButton == 0 && eventType == EventType.MouseDown)
  128. {
  129. hotControl = 0;
  130. nearestControl = id;
  131. }
  132. EditorGUI.BeginChangeCheck();
  133. newPosition = Handles.Slider2D(id, sliderData.position, sliderData.forward, sliderData.right, sliderData.up, 1f, nullCap, Vector2.zero);
  134. return EditorGUI.EndChangeCheck();
  135. }
  136. /// <summary>
  137. /// Uses the current event.
  138. /// </summary>
  139. public void UseEvent()
  140. {
  141. Event.current.Use();
  142. }
  143. /// <summary>
  144. /// Repaints the GUI.
  145. /// </summary>
  146. public void Repaint()
  147. {
  148. HandleUtility.Repaint();
  149. }
  150. /// <summary>
  151. /// Checks if the current camera is valid.
  152. /// </summary>
  153. /// <returns>Returns `true` if the current camera is not null. Otherwise, returns `false`.</returns>
  154. public bool HasCurrentCamera()
  155. {
  156. return Camera.current != null;
  157. }
  158. /// <summary>
  159. /// Gets the size of the handle.
  160. /// </summary>
  161. /// <param name="position">The position of the handle.</param>
  162. /// <returns>Returns the size of the handle.</returns>
  163. public float GetHandleSize(Vector3 position)
  164. {
  165. var scale = HasCurrentCamera() ? 0.01f : 0.05f;
  166. return HandleUtility.GetHandleSize(position) * scale;
  167. }
  168. /// <summary>
  169. /// Measures the GUI-space distance between two points of a segment.
  170. /// </summary>
  171. /// <param name="p1">The first point.</param>
  172. /// <param name="p2">The seconde point.</param>
  173. /// <returns>Returns the GUI-space distance between p1 and p2.</returns>
  174. public float DistanceToSegment(Vector3 p1, Vector3 p2)
  175. {
  176. p1 = HandleUtility.WorldToGUIPoint(p1);
  177. p2 = HandleUtility.WorldToGUIPoint(p2);
  178. return HandleUtility.DistancePointToLineSegment(Event.current.mousePosition, p1, p2);
  179. }
  180. /// <summary>
  181. /// Measures the distance to a circle.
  182. /// </summary>
  183. /// <param name="center">The center of the circle.</param>
  184. /// <param name="radius">The radius of the circle.</param>
  185. /// <returns>Returns the distance to a circle with the specified center and radius.</returns>
  186. public float DistanceToCircle(Vector3 center, float radius)
  187. {
  188. return HandleUtility.DistanceToCircle(center, radius);
  189. }
  190. /// <summary>
  191. /// Transforms a GUI-space position into world space.
  192. /// </summary>
  193. /// <param name="guiPosition">The GUI position</param>
  194. /// <param name="planeNormal">The plane normal.</param>
  195. /// <param name="planePos">The plane position.</param>
  196. /// <returns>Returns the world-space position of `guiPosition`.</returns>
  197. public Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePos)
  198. {
  199. Vector3 worldPos = Handles.inverseMatrix.MultiplyPoint(guiPosition);
  200. if (Camera.current)
  201. {
  202. Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);
  203. planeNormal = Handles.matrix.MultiplyVector(planeNormal);
  204. planePos = Handles.matrix.MultiplyPoint(planePos);
  205. Plane plane = new Plane(planeNormal, planePos);
  206. float distance = 0f;
  207. if (plane.Raycast(ray, out distance))
  208. {
  209. worldPos = Handles.inverseMatrix.MultiplyPoint(ray.GetPoint(distance));
  210. }
  211. }
  212. return worldPos;
  213. }
  214. }
  215. }