暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. /// <summary>
  6. /// Represents a UI control in a custom editor.
  7. /// </summary>
  8. public abstract class Control
  9. {
  10. private string m_Name;
  11. private int m_NameHashCode;
  12. private int m_ID;
  13. private LayoutData m_LayoutData;
  14. private int m_ActionID = -1;
  15. private LayoutData m_HotLayoutData;
  16. /// <summary>
  17. /// The name of the control.
  18. /// </summary>
  19. public string name
  20. {
  21. get { return m_Name; }
  22. }
  23. /// <summary>
  24. /// The control ID. The GUI uses this to identify the control.
  25. /// </summary>
  26. public int ID
  27. {
  28. get { return m_ID; }
  29. }
  30. /// <summary>
  31. /// The action ID.
  32. /// </summary>
  33. public int actionID
  34. {
  35. get { return m_ActionID; }
  36. }
  37. /// <summary>
  38. /// The control's layout data. This contains information about the control's position and orientation.
  39. /// </summary>
  40. public LayoutData layoutData
  41. {
  42. get { return m_LayoutData; }
  43. set { m_LayoutData = value; }
  44. }
  45. /// <summary>
  46. /// The control's hot layout data
  47. /// </summary>
  48. public LayoutData hotLayoutData
  49. {
  50. get { return m_HotLayoutData; }
  51. }
  52. /// <summary>
  53. /// Initializes and returns an instance of Control
  54. /// </summary>
  55. /// <param name="name">The name of the control</param>
  56. public Control(string name)
  57. {
  58. m_Name = name;
  59. m_NameHashCode = name.GetHashCode();
  60. }
  61. /// <summary>
  62. /// Gets the control from the guiState.
  63. /// </summary>
  64. /// <param name="guiState">The current state of the custom editor.</param>
  65. public void GetControl(IGUIState guiState)
  66. {
  67. m_ID = guiState.GetControlID(m_NameHashCode, FocusType.Passive);
  68. }
  69. internal void SetActionID(int actionID)
  70. {
  71. m_ActionID = actionID;
  72. m_HotLayoutData = m_LayoutData;
  73. }
  74. /// <summary>
  75. /// Begins the layout for this control. A call to EndLayout must always follow a call to this function.
  76. /// </summary>
  77. /// <param name="guiState">The current state of the custom editor.</param>
  78. public void BeginLayout(IGUIState guiState)
  79. {
  80. Debug.Assert(guiState.eventType == EventType.Layout);
  81. m_LayoutData = OnBeginLayout(LayoutData.zero, guiState);
  82. }
  83. /// <summary>
  84. /// Gets the control's layout data from the guiState.
  85. /// </summary>
  86. /// <param name="guiState">The current state of the custom editor.</param>
  87. public void Layout(IGUIState guiState)
  88. {
  89. Debug.Assert(guiState.eventType == EventType.Layout);
  90. for (var i = 0; i < GetCount(); ++i)
  91. {
  92. if (guiState.hotControl == actionID && hotLayoutData.index == i)
  93. continue;
  94. var layoutData = new LayoutData()
  95. {
  96. index = i,
  97. position = GetPosition(guiState, i),
  98. distance = GetDistance(guiState, i),
  99. forward = GetForward(guiState, i),
  100. up = GetUp(guiState, i),
  101. right = GetRight(guiState, i),
  102. userData = GetUserData(guiState, i)
  103. };
  104. m_LayoutData = LayoutData.Nearest(m_LayoutData, layoutData);
  105. }
  106. }
  107. /// <summary>
  108. /// Ends the layout for this control. This function must always follow a call to BeginLayout().
  109. /// </summary>
  110. /// <param name="guiState">The current state of the custom editor.</param>
  111. public void EndLayout(IGUIState guiState)
  112. {
  113. Debug.Assert(guiState.eventType == EventType.Layout);
  114. OnEndLayout(guiState);
  115. }
  116. /// <summary>
  117. /// Repaints the control.
  118. /// </summary>
  119. /// <param name="guiState">The current state of the custom editor.</param>
  120. public void Repaint(IGUIState guiState)
  121. {
  122. for (var i = 0; i < GetCount(); ++i)
  123. OnRepaint(guiState, i);
  124. }
  125. /// <summary>
  126. /// Called when the control begins its layout.
  127. /// </summary>
  128. /// <param name="data">The layout data.</param>
  129. /// <param name="guiState">The current state of the custom editor.</param>
  130. /// <returns>Returns the layout data to use.</returns>
  131. protected virtual LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
  132. {
  133. return data;
  134. }
  135. /// <summary>
  136. /// Called when the control ends its layout.
  137. /// /// </summary>
  138. /// <param name="guiState">The current state of the custom editor.</param>
  139. protected virtual void OnEndLayout(IGUIState guiState)
  140. {
  141. }
  142. /// <summary>
  143. /// Called when the control repaints its contents.
  144. /// </summary>
  145. /// <param name="guiState">The current state of the custom editor.</param>
  146. /// <param name="index">The index.</param>
  147. protected virtual void OnRepaint(IGUIState guiState, int index)
  148. {
  149. }
  150. /// <summary>
  151. /// Gets the number of sub-controllers.
  152. /// </summary>
  153. /// <remarks>
  154. /// By default, this is `1`. If you implement your own controller and want to use multiple sub-controllers within it, you can override this function to declare how to count the sub-controllers.
  155. /// </remarks>
  156. /// <returns>Returns the number of sub-controllers. If you do not override this function, this returns 1.</returns>
  157. protected virtual int GetCount()
  158. {
  159. return 1;
  160. }
  161. /// <summary>
  162. /// Gets the position of the control.
  163. /// </summary>
  164. /// <param name="guiState">The current state of the custom editor.</param>
  165. /// <param name="index">The index.</param>
  166. /// <returns>Returns Vector3.zero.</returns>
  167. protected virtual Vector3 GetPosition(IGUIState guiState, int index)
  168. {
  169. return Vector3.zero;
  170. }
  171. /// <summary>
  172. /// Gets the forward vector of the control.
  173. /// </summary>
  174. /// <param name="guiState">The current state of the custom editor.</param>
  175. /// <param name="index">The index.</param>
  176. /// <returns>Returns Vector3.forward.</returns>
  177. protected virtual Vector3 GetForward(IGUIState guiState, int index)
  178. {
  179. return Vector3.forward;
  180. }
  181. /// <summary>
  182. /// Gets the up vector of the control.
  183. /// </summary>
  184. /// <param name="guiState">The current state of the custom editor.</param>
  185. /// <param name="index">The index.</param>
  186. /// <returns>Returns Vector3.up,</returns>
  187. protected virtual Vector3 GetUp(IGUIState guiState, int index)
  188. {
  189. return Vector3.up;
  190. }
  191. /// <summary>
  192. /// Gets the right vector of the control.
  193. /// </summary>
  194. /// <param name="guiState">The current state of the custom editor.</param>
  195. /// <param name="index">The index.</param>
  196. /// <returns>Returns Vector3.right.</returns>
  197. protected virtual Vector3 GetRight(IGUIState guiState, int index)
  198. {
  199. return Vector3.right;
  200. }
  201. /// <summary>
  202. /// Gets the distance from the Scene view camera to the control.
  203. /// </summary>
  204. /// <param name="guiState">The current state of the custom editor.</param>
  205. /// <param name="index">The index.</param>
  206. /// <returns>Returns layoutData.distance.</returns>
  207. protected virtual float GetDistance(IGUIState guiState, int index)
  208. {
  209. return layoutData.distance;
  210. }
  211. /// <summary>
  212. /// Gets the control's user data.
  213. /// </summary>
  214. /// <param name="guiState">The current state of the custom editor.</param>
  215. /// <param name="index">The index.</param>
  216. /// <returns>Returns `null`.</returns>
  217. protected virtual object GetUserData(IGUIState guiState, int index)
  218. {
  219. return null;
  220. }
  221. }
  222. }