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.

GUIAction.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. /// <summary>
  6. /// Represents an action that is tied to a GUI element.
  7. /// </summary>
  8. public abstract class GUIAction
  9. {
  10. private int m_ID = -1;
  11. /// <summary>
  12. /// Func for GetEnable
  13. /// </summary>
  14. public Func<IGUIState, GUIAction, bool> enable;
  15. /// <summary>
  16. /// Func for EnabledRepaint
  17. /// </summary>
  18. public Func<IGUIState, GUIAction, bool> enableRepaint;
  19. /// <summary>
  20. /// Func for repaintOnMouseMove
  21. /// </summary>
  22. public Func<IGUIState, GUIAction, bool> repaintOnMouseMove;
  23. /// <summary>
  24. /// Action for OnPreRepaint
  25. /// </summary>
  26. public Action<IGUIState, GUIAction> onPreRepaint;
  27. /// <summary>
  28. /// Func for OnRepaint
  29. /// </summary>
  30. public Action<IGUIState, GUIAction> onRepaint;
  31. /// <summary>
  32. /// The action ID.
  33. /// </summary>
  34. public int ID
  35. {
  36. get { return m_ID; }
  37. }
  38. /// <summary>
  39. /// Calls the methods in its invocation list when Unity draws this GUIAction's GUI.
  40. /// </summary>
  41. /// <param name="guiState">The current state of the custom editor.</param>
  42. public void OnGUI(IGUIState guiState)
  43. {
  44. m_ID = guiState.GetControlID(GetType().GetHashCode(), FocusType.Passive);
  45. if (guiState.hotControl == 0 && IsEnabled(guiState) && CanTrigger(guiState) && GetTriggerContidtion(guiState))
  46. {
  47. guiState.hotControl = ID;
  48. OnTrigger(guiState);
  49. }
  50. if (guiState.hotControl == ID)
  51. {
  52. if (GetFinishContidtion(guiState))
  53. {
  54. OnFinish(guiState);
  55. guiState.hotControl = 0;
  56. }
  57. else
  58. {
  59. OnPerform(guiState);
  60. }
  61. }
  62. if (guiState.eventType == EventType.Repaint && IsRepaintEnabled(guiState))
  63. Repaint(guiState);
  64. }
  65. /// <summary>
  66. /// Checks whether the GUIAction is enabled.
  67. /// </summary>
  68. /// <param name="guiState">The current state of the custom editor.</param>
  69. /// <returns>Returns `true` if the GUIAction is enabled in the custom editor. Otherwise, returns `false`.</returns>
  70. public bool IsEnabled(IGUIState guiState)
  71. {
  72. if (enable != null)
  73. return enable(guiState, this);
  74. return true;
  75. }
  76. /// <summary>
  77. /// Checks whether the GUIAction should repaint.
  78. /// </summary>
  79. /// <param name="guiState">The current state of the custom editor.</param>
  80. /// <returns>Returns `true` if the GUIAction should repaint. Otherwise, returns `false`.</returns>
  81. public bool IsRepaintEnabled(IGUIState guiState)
  82. {
  83. if (!IsEnabled(guiState))
  84. return false;
  85. if (enableRepaint != null)
  86. return enableRepaint(guiState, this);
  87. return true;
  88. }
  89. /// <summary>
  90. /// Preprocessing that occurs before the GUI repaints.
  91. /// </summary>
  92. /// <param name="guiState">The current state of the custom editor.</param>
  93. public void PreRepaint(IGUIState guiState)
  94. {
  95. Debug.Assert(guiState.eventType == EventType.Repaint);
  96. if (IsEnabled(guiState) && onPreRepaint != null)
  97. onPreRepaint(guiState, this);
  98. }
  99. /// <summary>
  100. /// Calls the methods in its invocation list when repainting the GUI.
  101. /// </summary>
  102. /// <param name="guiState">The current state of the custom editor.</param>
  103. private void Repaint(IGUIState guiState)
  104. {
  105. Debug.Assert(guiState.eventType == EventType.Repaint);
  106. if (onRepaint != null)
  107. onRepaint(guiState, this);
  108. }
  109. /// <summary>
  110. /// Checks whether the GUI should repaint if the mouse moves over it.
  111. /// </summary>
  112. /// <param name="guiState">The current state of the custom editor.</param>
  113. /// <returns>Returns `true` if the GUI should repaint if the moves moves over it. Otherwise, returns `false`.</returns>
  114. internal bool IsRepaintOnMouseMoveEnabled(IGUIState guiState)
  115. {
  116. if (!IsEnabled(guiState) || !IsRepaintEnabled(guiState))
  117. return false;
  118. if (repaintOnMouseMove != null)
  119. return repaintOnMouseMove(guiState, this);
  120. return false;
  121. }
  122. /// <summary>
  123. /// Determines whether the finish condition has been met.
  124. /// </summary>
  125. /// <param name="guiState">The current state of the custom editor.</param>
  126. /// <returns>Returns `true` if finish condition has been met. Otherwise, returns `false`.</returns>
  127. protected abstract bool GetFinishContidtion(IGUIState guiState);
  128. /// <summary>
  129. /// Determines whether the trigger condition has been met.
  130. /// </summary>
  131. /// <param name="guiState">The current state of the custom editor.</param>
  132. /// <returns>Returns `true` if finish condition has been met. Otherwise, returns `false`.</returns>
  133. protected abstract bool GetTriggerContidtion(IGUIState guiState);
  134. /// <summary>
  135. /// Determines whether the GUIAction can trigger.
  136. /// </summary>
  137. /// <param name="guiState">The current state of the custom editor.</param>
  138. /// <returns>Always returns `true`.</returns>
  139. protected virtual bool CanTrigger(IGUIState guiState) { return true; }
  140. /// <summary>
  141. /// Calls the methods in its invocation list when triggered.
  142. /// </summary>
  143. /// <param name="guiState">The current state of the custom editor.</param>
  144. protected virtual void OnTrigger(IGUIState guiState)
  145. {
  146. }
  147. /// <summary>
  148. /// Calls the methods in its invocation list when performed.
  149. /// </summary>
  150. /// <param name="guiState">The current state of the custom editor.</param>
  151. protected virtual void OnPerform(IGUIState guiState)
  152. {
  153. }
  154. /// <summary>
  155. /// Calls the methods in its invocation list when finished.
  156. /// </summary>
  157. /// <param name="guiState">The current state of the custom editor.</param>
  158. protected virtual void OnFinish(IGUIState guiState)
  159. {
  160. }
  161. }
  162. }