From c8ab6172414d273827a76c8238ca8f1d6a68da1e Mon Sep 17 00:00:00 2001 From: Spebby <47935791+Spebby@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:22:24 -0700 Subject: [PATCH 1/5] Update StoryGraph.cs Updated to allow for file explorer usage, instead of the current string based approach --- .../Editor/Graph/StoryGraph.cs | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs index 4a1b2ec..cce09a0 100644 --- a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs +++ b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs @@ -13,7 +13,7 @@ namespace Subtegral.DialogueSystem.Editor { public class StoryGraph : EditorWindow { - private string _fileName = "New Narrative"; + private string _fileName; private StoryGraphView _graphView; private DialogueContainer _dialogueContainer; @@ -34,37 +34,33 @@ private void ConstructGraphView() _graphView.StretchToParentSize(); rootVisualElement.Add(_graphView); } - + + private void RegenerateToolbar() + { + // remove the old toolbar + rootVisualElement.Remove(rootVisualElement.Q()); + // generate a new toolbar + GenerateToolbar(); + } + private void GenerateToolbar() { - var toolbar = new Toolbar(); - - var fileNameTextField = new TextField("File Name:"); - fileNameTextField.SetValueWithoutNotify(_fileName); - fileNameTextField.MarkDirtyRepaint(); - fileNameTextField.RegisterValueChangedCallback(evt => _fileName = evt.newValue); - toolbar.Add(fileNameTextField); - - toolbar.Add(new Button(() => RequestDataOperation(true)) {text = "Save Data"}); - + var toolbar = new Toolbar(); + toolbar.Add(new Button(() => RequestDataOperation(true)) {text = "Save Data"}); toolbar.Add(new Button(() => RequestDataOperation(false)) {text = "Load Data"}); - // toolbar.Add(new Button(() => _graphView.CreateNewDialogueNode("Dialogue Node")) {text = "New Node",}); + var fileNameTextField = new Label($"File Name: {_fileName}"); + toolbar.Add(fileNameTextField); rootVisualElement.Add(toolbar); } private void RequestDataOperation(bool save) { - if (!string.IsNullOrEmpty(_fileName)) - { - var saveUtility = GraphSaveUtility.GetInstance(_graphView); - if (save) - saveUtility.SaveGraph(_fileName); - else - saveUtility.LoadNarrative(_fileName); - } - else - { - EditorUtility.DisplayDialog("Invalid File name", "Please Enter a valid filename", "OK"); + var saveUtility = GraphSaveUtility.GetInstance(_graphView); + if (save) { + saveUtility.SaveGraph(); + } else { + saveUtility.LoadNarrative(out _fileName); + RegenerateToolbar(); } } @@ -116,4 +112,4 @@ private void OnDisable() rootVisualElement.Remove(_graphView); } } -} \ No newline at end of file +} From 37d695e1b5f6331e7eca978e0c468292f79e57e1 Mon Sep 17 00:00:00 2001 From: Spebby <47935791+Spebby@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:26:15 -0700 Subject: [PATCH 2/5] Update GraphSaveUtility.cs supports file browser --- .../Editor/GraphSaveUtility.cs | 63 +++++++++++-------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs index fe42d22..4762858 100644 --- a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs +++ b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.IO; @@ -32,36 +32,39 @@ public static GraphSaveUtility GetInstance(StoryGraphView graphView) }; } - public void SaveGraph(string fileName) + public void SaveGraph() { var dialogueContainerObject = ScriptableObject.CreateInstance(); - if (!SaveNodes(fileName, dialogueContainerObject)) return; + if (!SaveNodes(dialogueContainerObject)) + return; SaveExposedProperties(dialogueContainerObject); SaveCommentBlocks(dialogueContainerObject); + + var filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); + if (string.IsNullOrEmpty(filePath)) + return; - if (!AssetDatabase.IsValidFolder("Assets/Resources")) - AssetDatabase.CreateFolder("Assets", "Resources"); - - UnityEngine.Object loadedAsset = AssetDatabase.LoadAssetAtPath($"Assets/Resources/{fileName}.asset", typeof(DialogueContainer)); - - if (loadedAsset == null || !AssetDatabase.Contains(loadedAsset)) - { - AssetDatabase.CreateAsset(dialogueContainerObject, $"Assets/Resources/{fileName}.asset"); - } - else - { - DialogueContainer container = loadedAsset as DialogueContainer; - container.NodeLinks = dialogueContainerObject.NodeLinks; - container.DialogueNodeData = dialogueContainerObject.DialogueNodeData; - container.ExposedProperties = dialogueContainerObject.ExposedProperties; - container.CommentBlockData = dialogueContainerObject.CommentBlockData; - EditorUtility.SetDirty(container); + var loadedAsset = AssetDatabase.LoadAssetAtPath($"{filePath}", typeof(DialogueContainer)); + + if (loadedAsset == null || !AssetDatabase.Contains(loadedAsset)) { + AssetDatabase.CreateAsset(dialogueContainerObject, $"{filePath}"); + } else { + var container = loadedAsset as DialogueContainer; + if (container != null) { + container.nodeLinks = dialogueContainerObject.nodeLinks; + container.dialogueNodeData = dialogueContainerObject.dialogueNodeData; + container.exposedProperties = dialogueContainerObject.exposedProperties; + container.commentBlockData = dialogueContainerObject.commentBlockData; + EditorUtility.SetDirty(container); + } } AssetDatabase.SaveAssets(); + // open file explorer to the folder where the file is saved + EditorUtility.RevealInFinder($"{filePath}"); } - private bool SaveNodes(string fileName, DialogueContainer dialogueContainerObject) + private bool SaveNodes(DialogueContainer dialogueContainerObject) { if (!Edges.Any()) return false; var connectedSockets = Edges.Where(x => x.input.node != null).ToArray(); @@ -111,12 +114,20 @@ private void SaveCommentBlocks(DialogueContainer dialogueContainer) }); } } - - public void LoadNarrative(string fileName) + + public void LoadNarrative(out string fileName) { + fileName = String.Empty; + // open file explorer to get file path + var path = EditorUtility.OpenFilePanel("Load Narrative", Application.dataPath + "/Resources", "asset"); + if (path.Length == 0) + return; + var startIndex = path.IndexOf("Resources/", StringComparison.Ordinal) + 10; + var endIndex = path.LastIndexOf(".asset", StringComparison.Ordinal); + fileName = path.Substring(startIndex, endIndex - startIndex); + _dialogueContainer = Resources.Load(fileName); - if (_dialogueContainer == null) - { + if (_dialogueContainer == null) { EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK"); return; } @@ -214,4 +225,4 @@ private void GenerateCommentBlocks() } } } -} \ No newline at end of file +} From 313ca4bc67bb0eff29e5c78527b7b225fb65336d Mon Sep 17 00:00:00 2001 From: Spebby Date: Wed, 26 Apr 2023 18:10:13 -0700 Subject: [PATCH 3/5] Fixes :) --- .../Editor/Graph/StoryGraph.cs | 55 +++++++++++++------ .../Editor/GraphSaveUtility.cs | 44 ++++++++------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs index cce09a0..e87fa53 100644 --- a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs +++ b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.UIElements; @@ -14,6 +11,7 @@ namespace Subtegral.DialogueSystem.Editor public class StoryGraph : EditorWindow { private string _fileName; + private string _filePath; private StoryGraphView _graphView; private DialogueContainer _dialogueContainer; @@ -46,21 +44,45 @@ private void RegenerateToolbar() private void GenerateToolbar() { var toolbar = new Toolbar(); - toolbar.Add(new Button(() => RequestDataOperation(true)) {text = "Save Data"}); - toolbar.Add(new Button(() => RequestDataOperation(false)) {text = "Load Data"}); + toolbar.Add(new Button(() => RequestDataOperation(0)) {text = "New Data"}); + toolbar.Add(new Button(() => RequestDataOperation(1)) {text = "Save Data"}); + toolbar.Add(new Button(() => RequestDataOperation(2)) {text = "Load Data"}); var fileNameTextField = new Label($"File Name: {_fileName}"); toolbar.Add(fileNameTextField); rootVisualElement.Add(toolbar); } - private void RequestDataOperation(bool save) + private void RequestDataOperation(byte option) { var saveUtility = GraphSaveUtility.GetInstance(_graphView); - if (save) { - saveUtility.SaveGraph(); - } else { - saveUtility.LoadNarrative(out _fileName); - RegenerateToolbar(); + switch (option) { + case 0: + { + _fileName = string.Empty; + _filePath = string.Empty; + rootVisualElement.Remove(_graphView); + ConstructGraphView(); + RegenerateToolbar(); + GenerateMiniMap(); + GenerateBlackBoard(); + break; + } + case 1: + { + if (_filePath != string.Empty) { + saveUtility.SaveGraph(_filePath); + Debug.Log($"Saved Narrative at: {_filePath}"); + break; + } + saveUtility.SaveGraph(); + break; + } + case 2: + { + saveUtility.LoadNarrative(out _filePath, out _fileName); + RegenerateToolbar(); + break; + } } } @@ -84,11 +106,11 @@ private void GenerateBlackBoard() { var blackboard = new Blackboard(_graphView); blackboard.Add(new BlackboardSection {title = "Exposed Variables"}); - blackboard.addItemRequested = _blackboard => + blackboard.addItemRequested = _ => { _graphView.AddPropertyToBlackBoard(ExposedProperty.CreateInstance(), false); }; - blackboard.editTextRequested = (_blackboard, element, newValue) => + blackboard.editTextRequested = (_, element, newValue) => { var oldPropertyName = ((BlackboardField) element).text; if (_graphView.ExposedProperties.Any(x => x.PropertyName == newValue)) @@ -107,9 +129,6 @@ private void GenerateBlackBoard() _graphView.Blackboard = blackboard; } - private void OnDisable() - { - rootVisualElement.Remove(_graphView); - } + private void OnDisable() => rootVisualElement.Remove(_graphView); } } diff --git a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs index 4762858..bc4fc86 100644 --- a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs +++ b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs @@ -33,16 +33,21 @@ public static GraphSaveUtility GetInstance(StoryGraphView graphView) } public void SaveGraph() + { + var filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); + if (string.IsNullOrEmpty(filePath)) + return; + + SaveGraph(filePath); + } + + public void SaveGraph(string filePath) { var dialogueContainerObject = ScriptableObject.CreateInstance(); if (!SaveNodes(dialogueContainerObject)) return; SaveExposedProperties(dialogueContainerObject); SaveCommentBlocks(dialogueContainerObject); - - var filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); - if (string.IsNullOrEmpty(filePath)) - return; var loadedAsset = AssetDatabase.LoadAssetAtPath($"{filePath}", typeof(DialogueContainer)); @@ -51,10 +56,10 @@ public void SaveGraph() } else { var container = loadedAsset as DialogueContainer; if (container != null) { - container.nodeLinks = dialogueContainerObject.nodeLinks; - container.dialogueNodeData = dialogueContainerObject.dialogueNodeData; - container.exposedProperties = dialogueContainerObject.exposedProperties; - container.commentBlockData = dialogueContainerObject.commentBlockData; + container.NodeLinks = dialogueContainerObject.NodeLinks; + container.DialogueNodeData = dialogueContainerObject.DialogueNodeData; + container.ExposedProperties = dialogueContainerObject.ExposedProperties; + container.CommentBlockData = dialogueContainerObject.CommentBlockData; EditorUtility.SetDirty(container); } } @@ -115,22 +120,21 @@ private void SaveCommentBlocks(DialogueContainer dialogueContainer) } } - public void LoadNarrative(out string fileName) + public void LoadNarrative(out string filePath, out string fileName) { fileName = String.Empty; // open file explorer to get file path - var path = EditorUtility.OpenFilePanel("Load Narrative", Application.dataPath + "/Resources", "asset"); - if (path.Length == 0) + filePath = EditorUtility.OpenFilePanel("Load Narrative", Application.dataPath + "/Resources", "asset"); + if (filePath.Length == 0) return; - var startIndex = path.IndexOf("Resources/", StringComparison.Ordinal) + 10; - var endIndex = path.LastIndexOf(".asset", StringComparison.Ordinal); - fileName = path.Substring(startIndex, endIndex - startIndex); - - _dialogueContainer = Resources.Load(fileName); - if (_dialogueContainer == null) { - EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK"); - return; - } + // reduce the file path to only include the path to the file from the Application.dataPath folder + filePath = filePath.Replace(Application.dataPath, "Assets"); + // find the last / in the file path and get the file name + var startIndex = filePath.LastIndexOf("/", StringComparison.Ordinal) + 1; + var endIndex = filePath.LastIndexOf(".asset", StringComparison.Ordinal); + fileName = filePath.Substring(startIndex, endIndex - startIndex); + // shorten the file path to only include the path to the file from the Assets folder + _dialogueContainer = AssetDatabase.LoadAssetAtPath(filePath); ClearGraph(); GenerateDialogueNodes(); From 9e58632d2e47a8d6217e0c302bb761a4c6660f45 Mon Sep 17 00:00:00 2001 From: Spebby <47935791+Spebby@users.noreply.github.com> Date: Thu, 27 Apr 2023 09:43:06 -0700 Subject: [PATCH 4/5] Reordered some code Moved the Reveal in Explorer call to be inside the pathless save function, since the only time you'd actually care where the file saved is when you first save it. --- com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs index bc4fc86..3894c74 100644 --- a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs +++ b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs @@ -39,6 +39,7 @@ public void SaveGraph() return; SaveGraph(filePath); + EditorUtility.RevealInFinder($"{filePath}"); } public void SaveGraph(string filePath) @@ -65,8 +66,6 @@ public void SaveGraph(string filePath) } AssetDatabase.SaveAssets(); - // open file explorer to the folder where the file is saved - EditorUtility.RevealInFinder($"{filePath}"); } private bool SaveNodes(DialogueContainer dialogueContainerObject) From c6972f185f6900d48d6fa8a6d174383fed7406f5 Mon Sep 17 00:00:00 2001 From: Spebby Date: Thu, 27 Apr 2023 23:29:30 -0700 Subject: [PATCH 5/5] addressed some suggestions --- .../Editor/Graph/StoryGraph.cs | 22 +++++++++++-------- .../Editor/GraphSaveUtility.cs | 10 +++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs index e87fa53..1719426 100644 --- a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs +++ b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs @@ -44,11 +44,13 @@ private void RegenerateToolbar() private void GenerateToolbar() { var toolbar = new Toolbar(); - toolbar.Add(new Button(() => RequestDataOperation(0)) {text = "New Data"}); - toolbar.Add(new Button(() => RequestDataOperation(1)) {text = "Save Data"}); - toolbar.Add(new Button(() => RequestDataOperation(2)) {text = "Load Data"}); - var fileNameTextField = new Label($"File Name: {_fileName}"); - toolbar.Add(fileNameTextField); + toolbar.Add(new Button(() => RequestDataOperation(0)) {text = "New"}); + toolbar.Add(new Button(() => RequestDataOperation(1)) {text = "Save"}); + toolbar.Add(new Button(() => RequestDataOperation(2)) {text = "Load"}); + if (_fileName != string.Empty) { + var fileNameTextField = new Label($"File Name: {_fileName}"); + toolbar.Add(fileNameTextField); + } rootVisualElement.Add(toolbar); } @@ -71,10 +73,12 @@ private void RequestDataOperation(byte option) { if (_filePath != string.Empty) { saveUtility.SaveGraph(_filePath); - Debug.Log($"Saved Narrative at: {_filePath}"); - break; - } - saveUtility.SaveGraph(); + } else saveUtility.SaveGraph(out _filePath); + + Debug.Log($"Saved Narrative at: {_filePath}"); + _fileName = _filePath.Split('/').Last(); + _fileName = _fileName[..^6]; + RegenerateToolbar(); break; } case 2: diff --git a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs index 3894c74..7f35e78 100644 --- a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs +++ b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs @@ -32,14 +32,16 @@ public static GraphSaveUtility GetInstance(StoryGraphView graphView) }; } - public void SaveGraph() + public void SaveGraph() => SaveGraph(out _); + + public void SaveGraph(out string filePath) { - var filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); + filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); if (string.IsNullOrEmpty(filePath)) return; - + SaveGraph(filePath); - EditorUtility.RevealInFinder($"{filePath}"); + EditorUtility.RevealInFinder($"{filePath}"); } public void SaveGraph(string filePath)