diff --git a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs index 4a1b2ec..1719426 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; @@ -13,7 +10,8 @@ namespace Subtegral.DialogueSystem.Editor { public class StoryGraph : EditorWindow { - private string _fileName = "New Narrative"; + private string _fileName; + private string _filePath; private StoryGraphView _graphView; private DialogueContainer _dialogueContainer; @@ -34,37 +32,61 @@ 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"}); - - toolbar.Add(new Button(() => RequestDataOperation(false)) {text = "Load Data"}); - // toolbar.Add(new Button(() => _graphView.CreateNewDialogueNode("Dialogue Node")) {text = "New Node",}); + var toolbar = new Toolbar(); + 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); } - private void RequestDataOperation(bool save) + private void RequestDataOperation(byte option) { - 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); + 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); + } else saveUtility.SaveGraph(out _filePath); + + Debug.Log($"Saved Narrative at: {_filePath}"); + _fileName = _filePath.Split('/').Last(); + _fileName = _fileName[..^6]; + RegenerateToolbar(); + break; + } + case 2: + { + saveUtility.LoadNarrative(out _filePath, out _fileName); + RegenerateToolbar(); + break; + } } } @@ -88,11 +110,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)) @@ -111,9 +133,6 @@ private void GenerateBlackBoard() _graphView.Blackboard = blackboard; } - private void OnDisable() - { - rootVisualElement.Remove(_graphView); - } + private void OnDisable() => rootVisualElement.Remove(_graphView); } -} \ No newline at end of file +} diff --git a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs index fe42d22..7f35e78 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,45 @@ public static GraphSaveUtility GetInstance(StoryGraphView graphView) }; } - public void SaveGraph(string fileName) + public void SaveGraph() => SaveGraph(out _); + + public void SaveGraph(out string filePath) + { + filePath = EditorUtility.SaveFilePanelInProject("Save Narrative", "New Narrative", "asset", "Pick a save location"); + if (string.IsNullOrEmpty(filePath)) + return; + + SaveGraph(filePath); + EditorUtility.RevealInFinder($"{filePath}"); + } + + public void SaveGraph(string filePath) { var dialogueContainerObject = ScriptableObject.CreateInstance(); - if (!SaveNodes(fileName, dialogueContainerObject)) return; + if (!SaveNodes(dialogueContainerObject)) + return; SaveExposedProperties(dialogueContainerObject); SaveCommentBlocks(dialogueContainerObject); - 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(); } - 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,15 +120,22 @@ private void SaveCommentBlocks(DialogueContainer dialogueContainer) }); } } - - public void LoadNarrative(string fileName) + + public void LoadNarrative(out string filePath, out string fileName) { - _dialogueContainer = Resources.Load(fileName); - if (_dialogueContainer == null) - { - EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK"); + fileName = String.Empty; + // open file explorer to get file path + filePath = EditorUtility.OpenFilePanel("Load Narrative", Application.dataPath + "/Resources", "asset"); + if (filePath.Length == 0) 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(); @@ -214,4 +230,4 @@ private void GenerateCommentBlocks() } } } -} \ No newline at end of file +}