PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/UnityTestTools/Assertions/Editor/AssertionComponentEditor.cs

https://gitlab.com/zerohun/RaputaJump
C# | 226 lines | 200 code | 26 blank | 0 comment | 32 complexity | 601b111a8ffe1b5b8c13cc3b79f6b270 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEditor.SceneManagement;
  8. namespace UnityTest
  9. {
  10. [CustomEditor(typeof(AssertionComponent))]
  11. public class AssertionComponentEditor : Editor
  12. {
  13. private readonly DropDownControl<Type> m_ComparerDropDown = new DropDownControl<Type>();
  14. private readonly PropertyPathSelector m_ThisPathSelector = new PropertyPathSelector("Compare");
  15. private readonly PropertyPathSelector m_OtherPathSelector = new PropertyPathSelector("Compare to");
  16. #region GUI Contents
  17. private readonly GUIContent m_GUICheckAfterTimeGuiContent = new GUIContent("Check after (seconds)", "After how many seconds the assertion should be checked");
  18. private readonly GUIContent m_GUIRepeatCheckTimeGuiContent = new GUIContent("Repeat check", "Should the check be repeated.");
  19. private readonly GUIContent m_GUIRepeatEveryTimeGuiContent = new GUIContent("Frequency of repetitions", "How often should the check be done");
  20. private readonly GUIContent m_GUICheckAfterFramesGuiContent = new GUIContent("Check after (frames)", "After how many frames the assertion should be checked");
  21. private readonly GUIContent m_GUIRepeatCheckFrameGuiContent = new GUIContent("Repeat check", "Should the check be repeated.");
  22. #endregion
  23. private static List<Type> allComparersList = null;
  24. public AssertionComponentEditor()
  25. {
  26. m_ComparerDropDown.convertForButtonLabel = type => type.Name;
  27. m_ComparerDropDown.convertForGUIContent = type => type.Name;
  28. m_ComparerDropDown.ignoreConvertForGUIContent = types => false;
  29. m_ComparerDropDown.tooltip = "Comparer that will be used to compare values and determine the result of assertion.";
  30. }
  31. public override void OnInspectorGUI()
  32. {
  33. var script = (AssertionComponent)target;
  34. EditorGUILayout.BeginHorizontal();
  35. var obj = DrawComparerSelection(script);
  36. script.checkMethods = (CheckMethod)EditorGUILayout.EnumMaskField(script.checkMethods,
  37. EditorStyles.popup,
  38. GUILayout.ExpandWidth(false));
  39. EditorGUILayout.EndHorizontal();
  40. if (script.IsCheckMethodSelected(CheckMethod.AfterPeriodOfTime))
  41. {
  42. DrawOptionsForAfterPeriodOfTime(script);
  43. }
  44. if (script.IsCheckMethodSelected(CheckMethod.Update))
  45. {
  46. DrawOptionsForOnUpdate(script);
  47. }
  48. if (obj)
  49. {
  50. EditorGUILayout.Space();
  51. m_ThisPathSelector.Draw(script.Action.go, script.Action,
  52. script.Action.thisPropertyPath, script.Action.GetAccepatbleTypesForA(),
  53. go =>
  54. {
  55. script.Action.go = go;
  56. AssertionExplorerWindow.Reload();
  57. },
  58. s =>
  59. {
  60. script.Action.thisPropertyPath = s;
  61. AssertionExplorerWindow.Reload();
  62. });
  63. EditorGUILayout.Space();
  64. DrawCustomFields(script);
  65. EditorGUILayout.Space();
  66. if (script.Action is ComparerBase)
  67. {
  68. DrawCompareToType(script.Action as ComparerBase);
  69. }
  70. }
  71. if(GUI.changed)
  72. EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  73. }
  74. private void DrawOptionsForAfterPeriodOfTime(AssertionComponent script)
  75. {
  76. EditorGUILayout.Space();
  77. script.checkAfterTime = EditorGUILayout.FloatField(m_GUICheckAfterTimeGuiContent,
  78. script.checkAfterTime);
  79. if (script.checkAfterTime < 0)
  80. script.checkAfterTime = 0;
  81. script.repeatCheckTime = EditorGUILayout.Toggle(m_GUIRepeatCheckTimeGuiContent,
  82. script.repeatCheckTime);
  83. if (script.repeatCheckTime)
  84. {
  85. script.repeatEveryTime = EditorGUILayout.FloatField(m_GUIRepeatEveryTimeGuiContent,
  86. script.repeatEveryTime);
  87. if (script.repeatEveryTime < 0)
  88. script.repeatEveryTime = 0;
  89. }
  90. }
  91. private void DrawOptionsForOnUpdate(AssertionComponent script)
  92. {
  93. EditorGUILayout.Space();
  94. script.checkAfterFrames = EditorGUILayout.IntField(m_GUICheckAfterFramesGuiContent,
  95. script.checkAfterFrames);
  96. if (script.checkAfterFrames < 1)
  97. script.checkAfterFrames = 1;
  98. script.repeatCheckFrame = EditorGUILayout.Toggle(m_GUIRepeatCheckFrameGuiContent,
  99. script.repeatCheckFrame);
  100. if (script.repeatCheckFrame)
  101. {
  102. script.repeatEveryFrame = EditorGUILayout.IntField(m_GUIRepeatEveryTimeGuiContent,
  103. script.repeatEveryFrame);
  104. if (script.repeatEveryFrame < 1)
  105. script.repeatEveryFrame = 1;
  106. }
  107. }
  108. private void DrawCompareToType(ComparerBase comparer)
  109. {
  110. comparer.compareToType = (ComparerBase.CompareToType)EditorGUILayout.EnumPopup("Compare to type",
  111. comparer.compareToType,
  112. EditorStyles.popup);
  113. if (comparer.compareToType == ComparerBase.CompareToType.CompareToConstantValue)
  114. {
  115. try
  116. {
  117. DrawConstCompareField(comparer);
  118. }
  119. catch (NotImplementedException)
  120. {
  121. Debug.LogWarning("This comparer can't compare to static value");
  122. comparer.compareToType = ComparerBase.CompareToType.CompareToObject;
  123. }
  124. }
  125. else if (comparer.compareToType == ComparerBase.CompareToType.CompareToObject)
  126. {
  127. DrawObjectCompareField(comparer);
  128. }
  129. }
  130. private void DrawObjectCompareField(ComparerBase comparer)
  131. {
  132. m_OtherPathSelector.Draw(comparer.other, comparer,
  133. comparer.otherPropertyPath, comparer.GetAccepatbleTypesForB(),
  134. go =>
  135. {
  136. comparer.other = go;
  137. AssertionExplorerWindow.Reload();
  138. },
  139. s =>
  140. {
  141. comparer.otherPropertyPath = s;
  142. AssertionExplorerWindow.Reload();
  143. }
  144. );
  145. }
  146. private void DrawConstCompareField(ComparerBase comparer)
  147. {
  148. if (comparer.ConstValue == null)
  149. {
  150. comparer.ConstValue = comparer.GetDefaultConstValue();
  151. }
  152. var so = new SerializedObject(comparer);
  153. var sp = so.FindProperty("constantValueGeneric");
  154. if (sp != null)
  155. {
  156. EditorGUILayout.PropertyField(sp, new GUIContent("Constant"), true);
  157. so.ApplyModifiedProperties();
  158. }
  159. }
  160. private bool DrawComparerSelection(AssertionComponent script)
  161. {
  162. if(allComparersList == null)
  163. {
  164. allComparersList = new List<Type>();
  165. var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  166. foreach (var assembly in allAssemblies)
  167. {
  168. var types = assembly.GetTypes();
  169. allComparersList.AddRange(types.Where(type => type.IsSubclassOf(typeof(ActionBase)) && !type.IsAbstract));
  170. }
  171. }
  172. var allComparers = allComparersList.ToArray();
  173. if (script.Action == null)
  174. script.Action = (ActionBase)CreateInstance(allComparers.First());
  175. m_ComparerDropDown.Draw(script.Action.GetType(), allComparers,
  176. type =>
  177. {
  178. if (script.Action == null || script.Action.GetType().Name != type.Name)
  179. {
  180. script.Action = (ActionBase)CreateInstance(type);
  181. AssertionExplorerWindow.Reload();
  182. }
  183. });
  184. return script.Action != null;
  185. }
  186. private void DrawCustomFields(AssertionComponent script)
  187. {
  188. foreach (var prop in script.Action.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
  189. {
  190. var so = new SerializedObject(script.Action);
  191. var sp = so.FindProperty(prop.Name);
  192. if (sp != null)
  193. {
  194. EditorGUILayout.PropertyField(sp, true);
  195. so.ApplyModifiedProperties();
  196. }
  197. }
  198. }
  199. }
  200. }