PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/FunnyFoods2/Assets/FullInspector2/Modules/SerializerMigrationUtility/Editor/fiSerializerMigrationUtilityEditorWindow.cs

https://gitlab.com/saneknovco/refregerator
C# | 272 lines | 205 code | 62 blank | 5 comment | 37 complexity | 520c13abb5cb7635783b7585a620eaae MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FullInspector.Internal;
  5. using FullSerializer;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityObject = UnityEngine.Object;
  9. namespace FullInspector.Modules.SerializerMigrationUtility {
  10. public class fiSerializerMigrationUtilityEditorWindow : EditorWindow {
  11. /// <summary>
  12. /// This is a utility class that wraps the UX that lets the user pick which UnityObjects they
  13. /// want to process.
  14. /// </summary>
  15. private class UnityObjectSelectionGroup {
  16. private class EnabledObject {
  17. public UnityObject Object;
  18. public bool Enabled;
  19. }
  20. private Vector2 _scroll;
  21. private List<EnabledObject> _objects;
  22. public IEnumerable<UnityObject> Selected {
  23. get {
  24. return from obj in _objects
  25. where obj.Enabled
  26. select obj.Object;
  27. }
  28. }
  29. public UnityObjectSelectionGroup(IEnumerable<UnityObject> objects) {
  30. _objects = (from obj in objects
  31. select new EnabledObject {
  32. Object = obj,
  33. Enabled = true
  34. }).ToList();
  35. }
  36. public void OnGUI() {
  37. _scroll = GUILayout.BeginScrollView(_scroll);
  38. fiEditorGUILayout.WithIndent(25, () => {
  39. foreach (EnabledObject obj in _objects) {
  40. GUILayout.BeginHorizontal();
  41. obj.Enabled = EditorGUILayout.ToggleLeft("Process?", obj.Enabled, GUILayout.Width(75));
  42. EditorGUILayout.ObjectField(obj.Object, obj.Object.GetType(), /*allowSceneObjects:*/ true);
  43. GUILayout.EndHorizontal();
  44. }
  45. });
  46. GUILayout.EndScrollView();
  47. }
  48. }
  49. [MenuItem("Window/Full Inspector/Serializer Migration Utility")]
  50. public static void ShowWindow() {
  51. fiEditorWindowUtility.ShowUtility<fiSerializerMigrationUtilityEditorWindow>("Serializer Migration Utility");
  52. }
  53. private TypeSpecifier<BaseSerializer> _currentSerializer = new TypeSpecifier<BaseSerializer>();
  54. private TypeSpecifier<BaseSerializer> _newSerializer = new TypeSpecifier<BaseSerializer>();
  55. private fiGraphMetadata _metadata = new fiGraphMetadata();
  56. [NonSerialized]
  57. private UnityObjectSelectionGroup PersistentObjectSelections;
  58. [NonSerialized]
  59. private UnityObjectSelectionGroup SceneObjectSelections;
  60. private int _selectedMode = 1;
  61. private bool _disablePopups = false;
  62. private static GUIStyle RichLabel;
  63. private static void EnsureResources() {
  64. if (RichLabel == null) {
  65. RichLabel = new GUIStyle(EditorStyles.label);
  66. RichLabel.richText = true;
  67. }
  68. }
  69. private bool CheckAnnotationsPopup() {
  70. if (_disablePopups) return true;
  71. return EditorUtility.DisplayDialog("Annotations", "Have you also added annotations to your models so that they can be *deserialized* using " +
  72. _currentSerializer.Type.CSharpName() + " and then *serialized* using " + _newSerializer.Type.CSharpName() + "?", "Yes", "No, not yet");
  73. }
  74. private void DisplayPostSerializeMessage() {
  75. if (_disablePopups) return;
  76. EditorUtility.DisplayDialog("Important!", "Please go and change the serializers the behaviors use in code now. Do *NOT* inspect any of the migrated objects before this -- you will lose data if you do!", "Ok");
  77. }
  78. private void BeforeMigrate() {
  79. if (fiSerializationManager.DisableAutomaticSerialization == false) {
  80. ShowNotification(new GUIContent("Automatic serialization disabled until next serialization reload -- do NOT use the inspector"));
  81. fiSerializationManager.DisableAutomaticSerialization = true;
  82. }
  83. }
  84. public void OnEnable() {
  85. // UX: Set the current serializer to the default serializer if we have a default serializer
  86. if (fiInstalledSerializerManager.HasDefault) {
  87. _currentSerializer.Type = fiInstalledSerializerManager.DefaultMetadata.SerializerType;
  88. }
  89. }
  90. public void OnSelectionChange() {
  91. Repaint();
  92. }
  93. public void OnGUI() {
  94. EnsureResources();
  95. GUILayout.BeginHorizontal();
  96. GUILayout.FlexibleSpace();
  97. _disablePopups = GUILayout.Toggle(_disablePopups, "Disable Dialog Boxes");
  98. GUILayout.EndHorizontal();
  99. GUILayout.Label("<b><size=25>About</size></b> If you've decided to change serializers (for example, from Json.NET to Full Serializer), then this utility will assist your migration.", RichLabel);
  100. fiEditorGUILayout.Splitter(3);
  101. IPropertyEditor editor = PropertyEditor.Get(typeof(TypeSpecifier<BaseSerializer>), null).FirstEditor;
  102. GUILayout.Label("Select the <i>current</i> serializer and then the <i>new</i> serializer", RichLabel);
  103. fiEditorGUILayout.WithIndent(50, () => {
  104. WithTemporaryLabelWidth(120, () => {
  105. editor.EditWithGUILayout(new GUIContent("Current Serializer"), _currentSerializer, _metadata.Enter(0));
  106. editor.EditWithGUILayout(new GUIContent("New Serializer"), _newSerializer, _metadata.Enter(1));
  107. });
  108. });
  109. fiEditorGUILayout.Splitter(3);
  110. if (_currentSerializer.Type == null || _newSerializer.Type == null) return;
  111. if (_currentSerializer.Type == _newSerializer.Type) {
  112. EditorGUILayout.HelpBox("You cannot migrate to the same serializer", MessageType.Error);
  113. return;
  114. }
  115. _selectedMode = GUILayout.SelectionGrid(_selectedMode, new string[] { "Migrate Active Selection", "Migrate Scene Objects", "Migrate Persistent Objects" }, 3);
  116. if (_selectedMode == 0) {
  117. GameObject[] toMigrate = DisplaySelection();
  118. if (GUILayout.Button("Run Migration") && CheckAnnotationsPopup()) {
  119. BeforeMigrate();
  120. foreach (var obj in toMigrate) {
  121. fiSerializerMigrationUtility.MigrateUnityObject(obj, _currentSerializer.Type, _newSerializer.Type);
  122. }
  123. DisplayPostSerializeMessage();
  124. }
  125. }
  126. else if (_selectedMode == 1) {
  127. DisplayScenesGUI();
  128. if (SceneObjectSelections == null) {
  129. SceneObjectSelections = new UnityObjectSelectionGroup(fiSerializerMigrationUtility.GetSceneObjects());
  130. }
  131. GUILayout.Label("Scene Objects to Process", EditorStyles.boldLabel);
  132. SceneObjectSelections.OnGUI();
  133. GUILayout.FlexibleSpace();
  134. if (GUILayout.Button("Run Migration", GUILayout.ExpandWidth(true)) && CheckAnnotationsPopup()) {
  135. BeforeMigrate();
  136. foreach (var obj in SceneObjectSelections.Selected) {
  137. fiSerializerMigrationUtility.MigrateUnityObject(obj, _currentSerializer.Type, _newSerializer.Type);
  138. }
  139. DisplayPostSerializeMessage();
  140. }
  141. }
  142. else if (_selectedMode == 2) {
  143. if (PersistentObjectSelections == null) {
  144. PersistentObjectSelections = new UnityObjectSelectionGroup(fiSerializerMigrationUtility.GetPersistentObjects());
  145. }
  146. GUILayout.Label("Persistent GameObjects to Process", EditorStyles.boldLabel);
  147. PersistentObjectSelections.OnGUI();
  148. if (GUILayout.Button("Run Migration", GUILayout.ExpandWidth(true)) && CheckAnnotationsPopup()) {
  149. BeforeMigrate();
  150. foreach (var obj in PersistentObjectSelections.Selected) {
  151. fiSerializerMigrationUtility.MigrateUnityObject(obj, _currentSerializer.Type, _newSerializer.Type);
  152. }
  153. DisplayPostSerializeMessage();
  154. }
  155. }
  156. }
  157. private Vector2 _selectionScroll;
  158. private GameObject[] DisplaySelection() {
  159. GUILayout.Label("GameObjects to Process", EditorStyles.boldLabel);
  160. _selectionScroll = GUILayout.BeginScrollView(_selectionScroll);
  161. fiEditorGUILayout.WithIndent(25, () => {
  162. foreach (var go in Selection.gameObjects) {
  163. EditorGUILayout.ObjectField(go, go.GetType(), /*allowSceneObjects:*/ true);
  164. }
  165. });
  166. GUILayout.EndScrollView();
  167. return Selection.gameObjects;
  168. }
  169. private Vector2 _sceneListScroll;
  170. [NonSerialized]
  171. private List<string> _scenes;
  172. private void DisplayScenesGUI() {
  173. if (_scenes == null) _scenes = fiEditorUtility.GetAllScenes();
  174. GUILayout.Label("Quick Scene Loader", EditorStyles.boldLabel);
  175. _sceneListScroll = GUILayout.BeginScrollView(_sceneListScroll);
  176. fiEditorGUILayout.WithIndent(25, () => {
  177. foreach (var scene in _scenes) {
  178. float buttonWidth = 50;
  179. var rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
  180. Rect buttonRect, labelRect;
  181. SplitRect(rect, buttonWidth, 5, out buttonRect, out labelRect);
  182. if (GUI.Button(buttonRect, "Load")) {
  183. EditorApplication.OpenScene(scene);
  184. SceneObjectSelections = null;
  185. }
  186. if (EditorApplication.currentScene == scene) {
  187. EditorGUI.LabelField(labelRect, "<b>" + scene + "</b>", RichLabel);
  188. }
  189. else {
  190. EditorGUI.LabelField(labelRect, scene);
  191. }
  192. }
  193. });
  194. GUILayout.EndScrollView();
  195. }
  196. private static void SplitRect(Rect rect, float leftSize, float margin, out Rect left, out Rect right) {
  197. left = new Rect(rect);
  198. left.width = leftSize;
  199. right = new Rect(rect);
  200. right.x += left.width + margin;
  201. right.width -= left.width + margin;
  202. }
  203. private static void WithTemporaryLabelWidth(float width, Action code) {
  204. float saved = fiSettings.LabelWidthMax;
  205. fiSettings.LabelWidthMax = width;
  206. code();
  207. fiSettings.LabelWidthMax = saved;
  208. }
  209. }
  210. }