GUIWrapper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using UnityEngine;
  2. namespace UnityEditor.U2D.Animation
  3. {
  4. internal struct SliderData
  5. {
  6. public Vector3 position;
  7. public Vector3 forward;
  8. public Vector3 up;
  9. public Vector3 right;
  10. public static readonly SliderData zero = new SliderData() { position = Vector3.zero, forward = Vector3.forward, up = Vector3.up, right = Vector3.right };
  11. }
  12. internal interface IGUIWrapper
  13. {
  14. Vector2 mousePosition { get; }
  15. int mouseButton { get; }
  16. int clickCount { get; }
  17. bool isShiftDown { get; }
  18. bool isAltDown { get; }
  19. bool isActionKeyDown { get; }
  20. EventType eventType { get; }
  21. string commandName { get; }
  22. bool IsMouseDown(int button);
  23. bool IsMouseUp(int button);
  24. bool IsKeyDown(KeyCode keyCode);
  25. int GetControlID(int hint, FocusType focusType);
  26. void LayoutControl(int controlID, float distance);
  27. bool IsControlNearest(int controlID);
  28. bool IsControlHot(int controlID);
  29. bool IsMultiStepControlHot(int controlID);
  30. void SetControlHot(int controlID);
  31. void SetMultiStepControlHot(int controlID);
  32. bool DoSlider(int id, SliderData sliderData, out Vector3 newPosition);
  33. void UseCurrentEvent();
  34. float DistanceToSegment(Vector3 p1, Vector3 p2);
  35. float DistanceToSegmentClamp(Vector3 p1, Vector3 p2);
  36. float DistanceToCircle(Vector3 center, float radius);
  37. Vector3 GUIToWorld(Vector2 guiPosition);
  38. Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePosition);
  39. void Repaint();
  40. bool IsRepainting();
  41. bool IsEventOutsideWindow();
  42. void SetGuiChanged(bool changed);
  43. float GetHandleSize(Vector3 position);
  44. bool IsViewToolActive();
  45. bool HasCurrentCamera();
  46. }
  47. internal class GUIWrapper : IGUIWrapper
  48. {
  49. private Handles.CapFunction nullCap = (int c, Vector3 p , Quaternion r, float s, EventType ev) => {};
  50. private int m_MultiStepHotControl = 0;
  51. public Vector2 mousePosition
  52. {
  53. get { return Event.current.mousePosition; }
  54. }
  55. public int mouseButton
  56. {
  57. get { return Event.current.button; }
  58. }
  59. public int clickCount
  60. {
  61. get { return Event.current.clickCount; }
  62. }
  63. public bool isShiftDown
  64. {
  65. get { return Event.current.shift; }
  66. }
  67. public bool isAltDown
  68. {
  69. get { return Event.current.alt; }
  70. }
  71. public bool isActionKeyDown
  72. {
  73. get { return EditorGUI.actionKey; }
  74. }
  75. public EventType eventType
  76. {
  77. get { return Event.current.type; }
  78. }
  79. public string commandName
  80. {
  81. get { return Event.current.commandName; }
  82. }
  83. public bool IsMouseDown(int button)
  84. {
  85. return Event.current.type == EventType.MouseDown && Event.current.button == button;
  86. }
  87. public bool IsMouseUp(int button)
  88. {
  89. return Event.current.type == EventType.MouseUp && Event.current.button == button;
  90. }
  91. public bool IsKeyDown(KeyCode keyCode)
  92. {
  93. return Event.current.type == EventType.KeyDown && Event.current.keyCode == keyCode;
  94. }
  95. public int GetControlID(int hint, FocusType focusType)
  96. {
  97. return GUIUtility.GetControlID(hint, focusType);
  98. }
  99. public void LayoutControl(int controlID, float distance)
  100. {
  101. if (Event.current.type == EventType.Layout)
  102. HandleUtility.AddControl(controlID, distance);
  103. }
  104. public bool IsControlNearest(int controlID)
  105. {
  106. return HandleUtility.nearestControl == controlID;
  107. }
  108. public bool IsControlHot(int controlID)
  109. {
  110. return GUIUtility.hotControl == controlID;
  111. }
  112. public bool IsMultiStepControlHot(int controlID)
  113. {
  114. return m_MultiStepHotControl == controlID;
  115. }
  116. public void SetControlHot(int controlID)
  117. {
  118. GUIUtility.hotControl = controlID;
  119. }
  120. public void SetMultiStepControlHot(int controlID)
  121. {
  122. m_MultiStepHotControl = controlID;
  123. }
  124. public bool DoSlider(int id, SliderData sliderData, out Vector3 newPosition)
  125. {
  126. EditorGUI.BeginChangeCheck();
  127. if (HasCurrentCamera())
  128. newPosition = Handles.Slider2D(id, sliderData.position, sliderData.forward, sliderData.right, sliderData.up, 1f, nullCap, Vector2.zero);
  129. else
  130. newPosition = Slider2D.Do(id, sliderData.position, null);
  131. return EditorGUI.EndChangeCheck();
  132. }
  133. public void UseCurrentEvent()
  134. {
  135. Event.current.Use();
  136. }
  137. public float DistanceToSegment(Vector3 p1, Vector3 p2)
  138. {
  139. p1 = HandleUtility.WorldToGUIPoint(p1);
  140. p2 = HandleUtility.WorldToGUIPoint(p2);
  141. return HandleUtility.DistancePointToLineSegment(mousePosition, p1, p2);
  142. }
  143. public float DistanceToSegmentClamp(Vector3 p1, Vector3 p2)
  144. {
  145. p1 = HandleUtility.WorldToGUIPoint(p1);
  146. p2 = HandleUtility.WorldToGUIPoint(p2);
  147. return MathUtility.DistanceToSegmentClamp(mousePosition, p1, p2);
  148. }
  149. public float DistanceToCircle(Vector3 center, float radius)
  150. {
  151. return HandleUtility.DistanceToCircle(center, radius);
  152. }
  153. public Vector3 GUIToWorld(Vector2 guiPosition)
  154. {
  155. return ModuleUtility.GUIToWorld(guiPosition);
  156. }
  157. public Vector3 GUIToWorld(Vector2 guiPosition, Vector3 planeNormal, Vector3 planePosition)
  158. {
  159. return ModuleUtility.GUIToWorld(guiPosition, planeNormal, planePosition);
  160. }
  161. public void Repaint()
  162. {
  163. HandleUtility.Repaint();
  164. }
  165. public bool IsRepainting()
  166. {
  167. return eventType == EventType.Repaint;
  168. }
  169. public void SetGuiChanged(bool changed)
  170. {
  171. GUI.changed = true;
  172. }
  173. public bool IsEventOutsideWindow()
  174. {
  175. return Event.current.type == EventType.Ignore;
  176. }
  177. public float GetHandleSize(Vector3 position)
  178. {
  179. return HandleUtility.GetHandleSize(position);
  180. }
  181. public bool IsViewToolActive()
  182. {
  183. return UnityEditor.Tools.current == Tool.View || isAltDown || mouseButton == 1 || mouseButton == 2;
  184. }
  185. public bool HasCurrentCamera()
  186. {
  187. return Camera.current != null;
  188. }
  189. }
  190. }