From 740826f23d753c85255866f8b1836d39d5502103 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Thu, 10 Aug 2023 10:57:16 +0200 Subject: [PATCH 1/9] work on copy of input actions asset (reset not working fix) --- .../UITKAssetEditor/InputActionsEditorWindow.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index e76064f195..b87ac0420f 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -25,6 +25,7 @@ internal class InputActionsEditorWindow : EditorWindow { private static readonly string k_FileExtension = "." + InputActionAsset.Extension; private int m_AssetId; + private static string m_AssetPath; [OnOpenAsset] public static bool OpenAsset(int instanceId, int line) @@ -74,7 +75,8 @@ private static InputActionsEditorWindow GetOrCreateWindow(int id, out bool isAlr private void SetAsset(InputActionAsset asset) { - var serializedAsset = new SerializedObject(asset); + m_AssetPath = AssetDatabase.GetAssetPath(asset); + var serializedAsset = new SerializedObject(Instantiate(asset)); m_State = new InputActionsEditorState(serializedAsset); bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out m_AssetGUID, out long _); Debug.Assert(isGUIDObtained, $"Failed to get asset {asset.name} GUID"); @@ -95,6 +97,7 @@ private void CreateGUI() if (m_State.serializedObject == null) { var asset = GetAssetFromDatabase(); + m_AssetPath = AssetDatabase.GetAssetPath(asset); var serializedAsset = new SerializedObject(asset); m_State = new InputActionsEditorState(m_State, serializedAsset); } @@ -136,15 +139,14 @@ private InputActionAsset GetAssetFromDatabase() public static void SaveAsset(SerializedObject serializedAsset) { var asset = (InputActionAsset)serializedAsset.targetObject; - var assetPath = AssetDatabase.GetAssetPath(asset); var assetJson = asset.ToJson(); - var existingJson = File.ReadAllText(assetPath); + var existingJson = File.ReadAllText(m_AssetPath); if (assetJson != existingJson) { - EditorHelpers.CheckOut(assetPath); - File.WriteAllText(assetPath, assetJson); - AssetDatabase.ImportAsset(assetPath); + EditorHelpers.CheckOut(m_AssetPath); + File.WriteAllText(m_AssetPath, assetJson); + AssetDatabase.ImportAsset(m_AssetPath); } } } From 0f82eb507a3d126b0bf0d26c74a2fa266f57ef4f Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Thu, 10 Aug 2023 12:09:13 +0200 Subject: [PATCH 2/9] dirty input actions editor window if changes were made --- .../InputActionsEditorWindow.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index b87ac0420f..41e5bfb2b6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -26,6 +26,7 @@ internal class InputActionsEditorWindow : EditorWindow private static readonly string k_FileExtension = "." + InputActionAsset.Extension; private int m_AssetId; private static string m_AssetPath; + private static string m_AssetJson; [OnOpenAsset] public static bool OpenAsset(int instanceId, int line) @@ -76,6 +77,7 @@ private static InputActionsEditorWindow GetOrCreateWindow(int id, out bool isAlr private void SetAsset(InputActionAsset asset) { m_AssetPath = AssetDatabase.GetAssetPath(asset); + m_AssetJson = File.ReadAllText(m_AssetPath); var serializedAsset = new SerializedObject(Instantiate(asset)); m_State = new InputActionsEditorState(serializedAsset); bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out m_AssetGUID, out long _); @@ -98,6 +100,7 @@ private void CreateGUI() { var asset = GetAssetFromDatabase(); m_AssetPath = AssetDatabase.GetAssetPath(asset); + m_AssetJson = File.ReadAllText(m_AssetPath); var serializedAsset = new SerializedObject(asset); m_State = new InputActionsEditorState(m_State, serializedAsset); } @@ -122,10 +125,26 @@ private void BuildUI() private void OnStateChanged(InputActionsEditorState newState) { + DirtyInputActionsEditorWindow(newState); if (InputEditorUserSettings.autoSaveInputActionAssets) SaveAsset(m_State.serializedObject); } + private void DirtyInputActionsEditorWindow(InputActionsEditorState newState) + { + if (!InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject)) + titleContent = new GUIContent("(*) Input Actions Editor"); + else + titleContent = new GUIContent("Input Actions Editor"); + } + + private bool HasAssetChanged(SerializedObject serializedAsset) + { + var asset = (InputActionAsset)serializedAsset.targetObject; + var newAssetJson = asset.ToJson(); + return newAssetJson != m_AssetJson; + } + private InputActionAsset GetAssetFromDatabase() { Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty"); @@ -147,6 +166,7 @@ public static void SaveAsset(SerializedObject serializedAsset) EditorHelpers.CheckOut(m_AssetPath); File.WriteAllText(m_AssetPath, assetJson); AssetDatabase.ImportAsset(m_AssetPath); + m_AssetJson = assetJson; } } } From 679a89e3760c534fe698516b2c572ce9f14873cf Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Thu, 10 Aug 2023 15:48:10 +0200 Subject: [PATCH 3/9] show dialog on close input actions editor (WIP) --- .../InputActionsEditorWindow.cs | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index 41e5bfb2b6..dcb97fe3ec 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -27,6 +27,7 @@ internal class InputActionsEditorWindow : EditorWindow private int m_AssetId; private static string m_AssetPath; private static string m_AssetJson; + private static bool m_IsDirty; [OnOpenAsset] public static bool OpenAsset(int instanceId, int line) @@ -132,10 +133,8 @@ private void OnStateChanged(InputActionsEditorState newState) private void DirtyInputActionsEditorWindow(InputActionsEditorState newState) { - if (!InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject)) - titleContent = new GUIContent("(*) Input Actions Editor"); - else - titleContent = new GUIContent("Input Actions Editor"); + m_IsDirty = !InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject); + titleContent = m_IsDirty ? new GUIContent("(*) Input Actions Editor") : new GUIContent("Input Actions Editor"); } private bool HasAssetChanged(SerializedObject serializedAsset) @@ -145,6 +144,32 @@ private bool HasAssetChanged(SerializedObject serializedAsset) return newAssetJson != m_AssetJson; } + private void OnDestroy() + { + ConfirmSaveChangesIfNeeded(); + } + + private void ConfirmSaveChangesIfNeeded() + { + // Ask for confirmation if we have unsaved changes. + if (!m_IsDirty) + return; + + var result = EditorUtility.DisplayDialogComplex("Input Action Asset has been modified", $"Do you want to save the changes you made in:\n{m_AssetPath}\n\nYour changes will be lost if you don't save them.", "Save", "Cancel", "Don't Save"); + switch (result) + { + case 0: // Save + SaveAsset(m_State.serializedObject); + break; + case 1: + // Cancel editor quit. + Instantiate(this).Show(); //TODO + break; + case 2: // Don't save, don't ask again. + break; + } + } + private InputActionAsset GetAssetFromDatabase() { Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty"); From 66d81e0719c4d6d94f9c7e51b36ab5ff0f383e88 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Fri, 11 Aug 2023 15:00:27 +0200 Subject: [PATCH 4/9] fixed cancel closing input asset editor --- .../InputActionsEditorWindow.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index dcb97fe3ec..994e9a30b3 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -151,7 +151,7 @@ private void OnDestroy() private void ConfirmSaveChangesIfNeeded() { - // Ask for confirmation if we have unsaved changes. + // Do we have unsaved changes? if (!m_IsDirty) return; @@ -161,15 +161,23 @@ private void ConfirmSaveChangesIfNeeded() case 0: // Save SaveAsset(m_State.serializedObject); break; - case 1: - // Cancel editor quit. - Instantiate(this).Show(); //TODO + case 1: // Cancel editor quit. (open new editor window with the edited asset) + ReshowEditorWindowWithUnsavedChanges(); break; - case 2: // Don't save, don't ask again. + case 2: // Don't save, quit break; } } + private void ReshowEditorWindowWithUnsavedChanges() + { + var window = CreateWindow(); + window.m_AssetId = m_AssetId; + window.m_State = m_State; + window.BuildUI(); + window.Show(); + } + private InputActionAsset GetAssetFromDatabase() { Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty"); From aed623039267ad0b07c3f9b215edb55cdb6483c9 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Fri, 11 Aug 2023 16:09:23 +0200 Subject: [PATCH 5/9] avoid changing asset name to (Clone) in the name --- .../Editor/UITKAssetEditor/InputActionsEditorWindow.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index 994e9a30b3..58dc24a7d3 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -54,6 +54,7 @@ public static bool OpenAsset(int instanceId, int line) window.Focus(); return true; } + m_IsDirty = false; window.m_AssetId = instanceId; window.titleContent = new GUIContent("Input Actions Editor"); window.SetAsset(asset); @@ -78,9 +79,11 @@ private static InputActionsEditorWindow GetOrCreateWindow(int id, out bool isAlr private void SetAsset(InputActionAsset asset) { m_AssetPath = AssetDatabase.GetAssetPath(asset); - m_AssetJson = File.ReadAllText(m_AssetPath); - var serializedAsset = new SerializedObject(Instantiate(asset)); + var clone = Instantiate(asset); + clone.name = asset.name; + var serializedAsset = new SerializedObject(clone); m_State = new InputActionsEditorState(serializedAsset); + m_AssetJson = File.ReadAllText(m_AssetPath); bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out m_AssetGUID, out long _); Debug.Assert(isGUIDObtained, $"Failed to get asset {asset.name} GUID"); From fdf460d62055913ea5846d56b3c65737476dabf3 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Fri, 11 Aug 2023 16:39:17 +0200 Subject: [PATCH 6/9] fixed saving logic for multiple open assets --- .../InputActionsEditorWindow.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index 58dc24a7d3..2092852fc8 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -25,9 +25,9 @@ internal class InputActionsEditorWindow : EditorWindow { private static readonly string k_FileExtension = "." + InputActionAsset.Extension; private int m_AssetId; - private static string m_AssetPath; - private static string m_AssetJson; - private static bool m_IsDirty; + private string m_AssetPath; + private string m_AssetJson; + private bool m_IsDirty; [OnOpenAsset] public static bool OpenAsset(int instanceId, int line) @@ -54,7 +54,7 @@ public static bool OpenAsset(int instanceId, int line) window.Focus(); return true; } - m_IsDirty = false; + window.m_IsDirty = false; window.m_AssetId = instanceId; window.titleContent = new GUIContent("Input Actions Editor"); window.SetAsset(asset); @@ -162,7 +162,7 @@ private void ConfirmSaveChangesIfNeeded() switch (result) { case 0: // Save - SaveAsset(m_State.serializedObject); + SaveAsset(m_State.serializedObject, this); break; case 1: // Cancel editor quit. (open new editor window with the edited asset) ReshowEditorWindowWithUnsavedChanges(); @@ -191,18 +191,21 @@ private InputActionAsset GetAssetFromDatabase() [SerializeField] private InputActionsEditorState m_State; [SerializeField] private string m_AssetGUID; - public static void SaveAsset(SerializedObject serializedAsset) + public static void SaveAsset(SerializedObject serializedAsset, InputActionsEditorWindow currentWindow = null) { + if ((focusedWindow == null || focusedWindow is not InputActionsEditorWindow) && currentWindow == null) + return; + currentWindow = currentWindow ? currentWindow : (InputActionsEditorWindow)focusedWindow; var asset = (InputActionAsset)serializedAsset.targetObject; var assetJson = asset.ToJson(); - var existingJson = File.ReadAllText(m_AssetPath); + var existingJson = File.ReadAllText(currentWindow.m_AssetPath); if (assetJson != existingJson) { - EditorHelpers.CheckOut(m_AssetPath); - File.WriteAllText(m_AssetPath, assetJson); - AssetDatabase.ImportAsset(m_AssetPath); - m_AssetJson = assetJson; + EditorHelpers.CheckOut(currentWindow.m_AssetPath); + File.WriteAllText(currentWindow.m_AssetPath, assetJson); + AssetDatabase.ImportAsset(currentWindow.m_AssetPath); + currentWindow.m_AssetJson = assetJson; } } } From b948af4f2c89c32bba392f064148f27ab6cc36b9 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Mon, 14 Aug 2023 15:48:39 +0200 Subject: [PATCH 7/9] only change header if needed --- .../Editor/UITKAssetEditor/InputActionsEditorWindow.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index 2092852fc8..f5f3dcf0bc 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -136,7 +136,10 @@ private void OnStateChanged(InputActionsEditorState newState) private void DirtyInputActionsEditorWindow(InputActionsEditorState newState) { - m_IsDirty = !InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject); + var isWindowDirty = !InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject); + if (m_IsDirty == isWindowDirty) + return; + m_IsDirty = isWindowDirty; titleContent = m_IsDirty ? new GUIContent("(*) Input Actions Editor") : new GUIContent("Input Actions Editor"); } From a97a5d3b72aff02cb18cf21bed714654580b1dfb Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Wed, 16 Aug 2023 15:55:00 +0200 Subject: [PATCH 8/9] PR refactor - reload asset from json instead of cloning it --- .../Editor/UITKAssetEditor/InputActionsEditorWindow.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index f5f3dcf0bc..e8045bcac3 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -79,9 +79,7 @@ private static InputActionsEditorWindow GetOrCreateWindow(int id, out bool isAlr private void SetAsset(InputActionAsset asset) { m_AssetPath = AssetDatabase.GetAssetPath(asset); - var clone = Instantiate(asset); - clone.name = asset.name; - var serializedAsset = new SerializedObject(clone); + var serializedAsset = new SerializedObject(asset); m_State = new InputActionsEditorState(serializedAsset); m_AssetJson = File.ReadAllText(m_AssetPath); bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out m_AssetGUID, out long _); @@ -170,7 +168,8 @@ private void ConfirmSaveChangesIfNeeded() case 1: // Cancel editor quit. (open new editor window with the edited asset) ReshowEditorWindowWithUnsavedChanges(); break; - case 2: // Don't save, quit + case 2: // Don't save, quit - reload the old asset from the json to prevent the asset from being dirtied + AssetDatabase.ImportAsset(m_AssetPath); break; } } From 2327d75daa28346256705a59ce2f186e65490f52 Mon Sep 17 00:00:00 2001 From: Rita Merkl Date: Thu, 17 Aug 2023 14:42:26 +0200 Subject: [PATCH 9/9] fixed reopening window after Cancel --- .../UITKAssetEditor/InputActionsEditorWindow.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index e8045bcac3..ef2f08cfd6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -177,12 +177,20 @@ private void ConfirmSaveChangesIfNeeded() private void ReshowEditorWindowWithUnsavedChanges() { var window = CreateWindow(); - window.m_AssetId = m_AssetId; - window.m_State = m_State; + CopyOldStatsToNewWindow(window); window.BuildUI(); window.Show(); } + private void CopyOldStatsToNewWindow(InputActionsEditorWindow window) + { + window.m_AssetId = m_AssetId; + window.m_State = m_State; + window.m_AssetPath = m_AssetPath; + window.m_AssetJson = m_AssetJson; + window.m_IsDirty = true; + } + private InputActionAsset GetAssetFromDatabase() { Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty");