GitMergeWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.SceneManagement;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. using GitMerge.Utilities;
  7. namespace GitMerge
  8. {
  9. /// <summary>
  10. /// The window that lets you perform merges on scenes and prefabs.
  11. /// </summary>
  12. public class GitMergeWindow : EditorWindow
  13. {
  14. private VCS vcs = new VCSGit();
  15. private const string EDITOR_PREFS_AUTOMERGE = "GitMerge_automerge";
  16. private const string EDITOR_PREFS_AUTOFOCUS = "GitMerge_autofocus";
  17. public static bool automerge { private set; get; }
  18. public static bool autofocus { private set; get; }
  19. private MergeManagerBase mergeManager;
  20. private MergeFilter filter = new MergeFilter();
  21. private MergeFilterBar filterBar = new MergeFilterBar();
  22. public bool mergeInProgress => mergeManager != null;
  23. private PageView pageView = new PageView();
  24. private Vector2 scrollPosition = Vector2.zero;
  25. private int tab = 0;
  26. private List<GameObjectMergeActions> mergeActionsFiltered;
  27. [MenuItem("Window/GitMerge")]
  28. static void OpenEditor()
  29. {
  30. var window = EditorWindow.GetWindow(typeof(GitMergeWindow), false, "GitMerge");
  31. // In case we're merging and the scene becomes edited,
  32. // the shown SerializedProperties should be repainted
  33. window.autoRepaintOnSceneChange = true;
  34. window.minSize = new Vector2(500, 100);
  35. }
  36. private void OnEnable()
  37. {
  38. pageView.NumElementsPerPage = 200;
  39. filterBar.filter = filter;
  40. filter.OnChanged += CacheMergeActions;
  41. LoadSettings();
  42. }
  43. private static void LoadSettings()
  44. {
  45. automerge = EditorPrefs.GetBool(EDITOR_PREFS_AUTOMERGE, true);
  46. autofocus = EditorPrefs.GetBool(EDITOR_PREFS_AUTOFOCUS, true);
  47. }
  48. void OnHierarchyChange()
  49. {
  50. // Repaint if we changed the scene
  51. this.Repaint();
  52. }
  53. // Always check for editor state changes, and abort the active merge process if needed
  54. private void Update()
  55. {
  56. if (MergeAction.inMergePhase &&
  57. (EditorApplication.isCompiling ||
  58. EditorApplication.isPlayingOrWillChangePlaymode))
  59. {
  60. ShowNotification(new GUIContent("Aborting merge due to editor state change."));
  61. AbortMerge(false);
  62. }
  63. }
  64. private void AbortMerge(bool showNotification = true)
  65. {
  66. mergeManager.AbortMerge(showNotification);
  67. mergeManager = null;
  68. }
  69. private void OnGUI()
  70. {
  71. Resources.DrawLogo();
  72. DrawTabButtons();
  73. switch (tab)
  74. {
  75. case 0:
  76. OnGUIStartMergeTab();
  77. break;
  78. default:
  79. OnGUISettingsTab();
  80. break;
  81. }
  82. }
  83. /// <summary>
  84. /// Tab that offers scene merging.
  85. /// </summary>
  86. private void OnGUIStartMergeTab()
  87. {
  88. if (!mergeInProgress)
  89. {
  90. DisplayPrefabMergeField();
  91. GUILayout.Space(20);
  92. DisplaySceneMergeButton();
  93. }
  94. else
  95. {
  96. DisplayMergeProcess();
  97. }
  98. }
  99. private void DisplaySceneMergeButton()
  100. {
  101. var activeScene = SceneManager.GetActiveScene();
  102. GUILayout.Label("Open Scene: " + activeScene.path);
  103. if (activeScene.path != "" &&
  104. !mergeInProgress &&
  105. GUILayout.Button("Start merging the open scene", GUILayout.Height(30)))
  106. {
  107. var manager = new MergeManagerScene(this, vcs);
  108. if (manager.TryInitializeMerge())
  109. {
  110. this.mergeManager = manager;
  111. CacheMergeActions();
  112. }
  113. }
  114. }
  115. private void DisplayPrefabMergeField()
  116. {
  117. if (!mergeInProgress)
  118. {
  119. var path = PathDetectingDragAndDropField("Drag a scene or prefab here to start merging", 80);
  120. if (path != null)
  121. {
  122. var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
  123. if (IsPrefabAsset(asset))
  124. {
  125. var manager = new MergeManagerPrefab(this, vcs);
  126. if (manager.TryInitializeMerge(path))
  127. {
  128. this.mergeManager = manager;
  129. CacheMergeActions();
  130. }
  131. }
  132. else if (IsSceneAsset(asset))
  133. {
  134. var manager = new MergeManagerScene(this, vcs);
  135. if (manager.TryInitializeMerge(path))
  136. {
  137. this.mergeManager = manager;
  138. CacheMergeActions();
  139. }
  140. }
  141. }
  142. }
  143. }
  144. private static bool IsPrefabAsset(Object asset)
  145. {
  146. var assetType = asset.GetType();
  147. return assetType == typeof(GameObject) || assetType == typeof(BrokenPrefabAsset) ||
  148. assetType == typeof(DefaultAsset);
  149. }
  150. private static bool IsSceneAsset(Object asset)
  151. {
  152. var assetType = asset.GetType();
  153. return assetType == typeof(SceneAsset);
  154. }
  155. private static string PathDetectingDragAndDropField(string text, float height)
  156. {
  157. var currentEvent = Event.current;
  158. using (new GUIBackgroundColor(Color.black))
  159. {
  160. // Caching these sounds good on paper, but Unity tends to forget them randomly
  161. var content = EditorGUIUtility.IconContent("RectMask2D Icon", string.Empty);
  162. content.text = text;
  163. var buttonStyle = GUI.skin.GetStyle("Button");
  164. var style = new GUIStyle(GUI.skin.GetStyle("Box"));
  165. style.stretchWidth = true;
  166. style.normal.background = buttonStyle.normal.background;
  167. style.normal.textColor = buttonStyle.normal.textColor;
  168. style.alignment = TextAnchor.MiddleCenter;
  169. style.imagePosition = ImagePosition.ImageAbove;
  170. GUILayout.Box(content, style, GUILayout.Height(height));
  171. }
  172. var rect = GUILayoutUtility.GetLastRect();
  173. if (rect.Contains(currentEvent.mousePosition))
  174. {
  175. if (DragAndDrop.objectReferences.Length == 1)
  176. {
  177. switch (currentEvent.type)
  178. {
  179. case EventType.DragUpdated:
  180. var asset = DragAndDrop.objectReferences[0];
  181. if (IsPrefabAsset(asset) || IsSceneAsset(asset))
  182. {
  183. DragAndDrop.visualMode = DragAndDropVisualMode.Move;
  184. }
  185. break;
  186. case EventType.DragPerform:
  187. var path = AssetDatabase.GetAssetPath(DragAndDrop.objectReferences[0]);
  188. DragAndDrop.AcceptDrag();
  189. return path;
  190. }
  191. }
  192. }
  193. return null;
  194. }
  195. /// <summary>
  196. /// Tab that offers various settings for the tool.
  197. /// </summary>
  198. private void OnGUISettingsTab()
  199. {
  200. var vcsPath = vcs.GetExePath();
  201. var vcsPathNew = EditorGUILayout.TextField("Path to git.exe", vcsPath);
  202. if (vcsPath != vcsPathNew)
  203. {
  204. vcs.SetPath(vcsPathNew);
  205. }
  206. automerge = DisplaySettingsToggle(automerge,
  207. EDITOR_PREFS_AUTOMERGE,
  208. "Automerge",
  209. "(Automerge new/deleted GameObjects/Components upon merge start)");
  210. autofocus = DisplaySettingsToggle(autofocus,
  211. EDITOR_PREFS_AUTOFOCUS,
  212. "Auto Highlight",
  213. "(Highlight GameObjects when applying a MergeAction to it)");
  214. }
  215. private static bool DisplaySettingsToggle(bool value, string editorPrefsKey, string title, string description)
  216. {
  217. var newValue = EditorGUILayout.Toggle(title, value);
  218. if (value != newValue)
  219. {
  220. EditorPrefs.SetBool(editorPrefsKey, value);
  221. }
  222. GUILayout.Label(description);
  223. return newValue;
  224. }
  225. /// <summary>
  226. /// If no merge is in progress, draws the buttons to switch between tabs.
  227. /// Otherwise, draws the "abort merge" button.
  228. /// </summary>
  229. private void DrawTabButtons()
  230. {
  231. if (!mergeInProgress)
  232. {
  233. string[] tabs = { "Merge", "Settings" };
  234. tab = GUI.SelectionGrid(new Rect(72, 36, 300, 22), tab, tabs, 3);
  235. }
  236. else
  237. {
  238. GUI.backgroundColor = new Color(1, 0.4f, 0.4f, 1);
  239. if (GUI.Button(new Rect(72, 36, 300, 22), "Abort merge"))
  240. {
  241. mergeManager.AbortMerge();
  242. mergeManager = null;
  243. }
  244. GUI.backgroundColor = Color.white;
  245. }
  246. }
  247. /// <summary>
  248. /// Displays all MergeActions and the "apply merge" button if a merge is in progress.
  249. /// </summary>
  250. private void DisplayMergeProcess()
  251. {
  252. DrawCommandBar();
  253. var done = DisplayMergeActions();
  254. GUILayout.BeginHorizontal();
  255. if (done && GUILayout.Button("Apply merge", GUILayout.Height(40)))
  256. {
  257. mergeManager.CompleteMerge();
  258. mergeManager = null;
  259. }
  260. GUILayout.EndHorizontal();
  261. }
  262. /// <summary>
  263. /// Display extra commands to simplify merge process
  264. /// </summary>
  265. private void DrawCommandBar()
  266. {
  267. DrawQuickMergeSideSelectionCommands();
  268. filterBar.Draw();
  269. }
  270. /// <summary>
  271. /// Allow to select easily 'use ours' or 'use theirs' for all actions
  272. /// </summary>
  273. private void DrawQuickMergeSideSelectionCommands()
  274. {
  275. GUILayout.BeginHorizontal();
  276. {
  277. if (GUILayout.Button(new GUIContent("Use ours", "Use theirs for all. Do not apply merge automatically.")))
  278. {
  279. mergeManager.allMergeActions.ForEach((action) => action.UseOurs());
  280. }
  281. if (GUILayout.Button(new GUIContent("Use theirs", "Use theirs for all. Do not apply merge automatically.")))
  282. {
  283. mergeManager.allMergeActions.ForEach((action) => action.UseTheirs());
  284. }
  285. GUILayout.FlexibleSpace();
  286. }
  287. GUILayout.EndHorizontal();
  288. }
  289. /// <summary>
  290. /// Displays all GameObjectMergeActions.
  291. /// </summary>
  292. /// <returns>True, if all MergeActions are flagged as "merged".</returns>
  293. private bool DisplayMergeActions()
  294. {
  295. var textColor = GUI.skin.label.normal.textColor;
  296. GUI.skin.label.normal.textColor = Color.black;
  297. bool done = true;
  298. pageView.Draw(mergeActionsFiltered.Count, (index) =>
  299. {
  300. var actions = mergeActionsFiltered[index];
  301. actions.OnGUI();
  302. done = done && actions.merged;
  303. });
  304. GUI.skin.label.normal.textColor = textColor;
  305. return done;
  306. }
  307. private void CacheMergeActions()
  308. {
  309. if (filter.useFilter)
  310. {
  311. mergeActionsFiltered = mergeManager.allMergeActions.Where((actions) => filter.IsPassingFilter(actions)).ToList();
  312. }
  313. else
  314. {
  315. mergeActionsFiltered = mergeManager.allMergeActions;
  316. }
  317. }
  318. }
  319. }