From dd88cc6595be1c89f00a67dc1c0179ba695f791b Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Sat, 7 Dec 2019 15:23:50 +0100 Subject: [PATCH] implicit selection of dialogue graph --- TMP.meta => Samples/ScriptableObject.meta | 2 +- Samples/ScriptableObject/DemoDialogue.asset | 34 ++++++++ .../ScriptableObject/DemoDialogue.asset.meta | 8 ++ .../Editor/Graph/StoryGraph.cs | 78 ++++++++++++++----- .../Editor/GraphSaveUtility.cs | 14 ++-- 5 files changed, 105 insertions(+), 31 deletions(-) rename TMP.meta => Samples/ScriptableObject.meta (77%) create mode 100644 Samples/ScriptableObject/DemoDialogue.asset create mode 100644 Samples/ScriptableObject/DemoDialogue.asset.meta diff --git a/TMP.meta b/Samples/ScriptableObject.meta similarity index 77% rename from TMP.meta rename to Samples/ScriptableObject.meta index f9da8b5..e7c48e5 100644 --- a/TMP.meta +++ b/Samples/ScriptableObject.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f54d1bd14bd3ca042bd867b519fee8cc +guid: 68ea37d8c0dac4844b31b76ec4c53b06 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Samples/ScriptableObject/DemoDialogue.asset b/Samples/ScriptableObject/DemoDialogue.asset new file mode 100644 index 0000000..7732bdb --- /dev/null +++ b/Samples/ScriptableObject/DemoDialogue.asset @@ -0,0 +1,34 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 78b3fd9154634ebdb8c8a48960b9a16a, type: 3} + m_Name: DemoDialogue + m_EditorClassIdentifier: + NodeLinks: + - BaseNodeGUID: 9486b70f-454f-4f89-87ff-2c40ab5693a2 + PortName: Next + TargetNodeGUID: 950d1f90-0012-49cd-aeab-334f6d38f063 + - BaseNodeGUID: 950d1f90-0012-49cd-aeab-334f6d38f063 + PortName: Option 1 + TargetNodeGUID: 65023dd8-950a-47ac-b9c3-49f2501a74be + - BaseNodeGUID: 950d1f90-0012-49cd-aeab-334f6d38f063 + PortName: Option 2 + TargetNodeGUID: 53eaef3e-61d3-484a-8a67-c59991fd6a8e + DialogueNodeData: + - NodeGUID: 950d1f90-0012-49cd-aeab-334f6d38f063 + DialogueText: Test + Position: {x: 247, y: 191} + - NodeGUID: 65023dd8-950a-47ac-b9c3-49f2501a74be + DialogueText: 1 + Position: {x: 493, y: 145} + - NodeGUID: 53eaef3e-61d3-484a-8a67-c59991fd6a8e + DialogueText: 2 + Position: {x: 481, y: 266} diff --git a/Samples/ScriptableObject/DemoDialogue.asset.meta b/Samples/ScriptableObject/DemoDialogue.asset.meta new file mode 100644 index 0000000..a867bdb --- /dev/null +++ b/Samples/ScriptableObject/DemoDialogue.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 674dc93a24e86f445b19fe5ddc5c74ea +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs index 2c7d6f4..ded5b63 100644 --- a/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs +++ b/com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs @@ -8,28 +8,46 @@ using UnityEngine; using UnityEngine.UIElements; using Subtegral.DialogueSystem.DataContainers; +using System.IO; namespace Subtegral.DialogueSystem.Editor { public class StoryGraph : EditorWindow { - private string _fileName = "New Narrative"; + private string _filePath = null; private StoryGraphView _graphView; private DialogueContainer _dialogueContainer; + private void Update() + { + if (Selection.activeObject == null || Selection.activeObject.GetType() != typeof(DialogueContainer)) + { + return; + } + var selectedGraph = Selection.activeObject as DialogueContainer; + var path = AssetDatabase.GetAssetPath(selectedGraph.GetInstanceID()); + if (path != null && path != _filePath) + { + var saveUtility = GraphSaveUtility.GetInstance(_graphView); + saveUtility.LoadNarrative(path); + _filePath = path; + UpdateTitle(); + } + } + [MenuItem("Graph/Narrative Graph")] public static void CreateGraphViewWindow() { var window = GetWindow(); - window.titleContent = new GUIContent("Narrative Graph"); + window.UpdateTitle(); } private void ConstructGraphView() { _graphView = new StoryGraphView { - name = "Narrative Graph" + name = _filePath != null ? Path.GetFileName(_filePath) : "Unsaved New Narrative" }; _graphView.StretchToParentSize(); rootVisualElement.Add(_graphView); @@ -39,32 +57,50 @@ 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); + UpdateTitle(); - toolbar.Add(new Button(() => RequestDataOperation(true)) {text = "Save Data"}); + toolbar.Add(new Button(() => Save()) { text = "Save Data" }); - toolbar.Add(new Button(() => RequestDataOperation(false)) {text = "Load Data"}); - toolbar.Add(new Button(() => _graphView.CreateNewDialogueNode("Dialogue Node")) {text = "New Node",}); + toolbar.Add(new Button(() => CreateNew()) { text = "New narrative" }); + toolbar.Add(new Button(() => _graphView.CreateNewDialogueNode("Dialogue Node")) { text = "New Node", }); rootVisualElement.Add(toolbar); } - private void RequestDataOperation(bool save) + private void UpdateTitle() + { + this.titleContent = new GUIContent(_filePath != null ? Path.GetFileName(_filePath) : "Unsaved New Narrative"); + } + + private void CreateNew() { - if (!string.IsNullOrEmpty(_fileName)) + var loadFilePath = EditorUtility.SaveFilePanelInProject("Create Narrative", "New narrative", "asset", ""); + + var saveUtility = GraphSaveUtility.GetInstance(_graphView); + _graphView = new StoryGraphView { - var saveUtility = GraphSaveUtility.GetInstance(_graphView); - if (save) - saveUtility.SaveNodes(_fileName); - else - saveUtility.LoadNarrative(_fileName); + name = "Narrative Graph" + }; + _filePath = loadFilePath; + UpdateTitle(); + } + private void SaveAs() + { + var saveUtility = GraphSaveUtility.GetInstance(_graphView); + // TODO: change file path only if save succeeded. + _filePath = EditorUtility.SaveFilePanelInProject("New narrative", "New narrative", "asset", "Save As..."); + saveUtility.SaveNodes(_filePath); + UpdateTitle(); + } + private void Save() + { + if (_filePath == null) + { + SaveAs(); } else { - EditorUtility.DisplayDialog("Invalid File name", "Please Enter a valid filename", "OK"); + var saveUtility = GraphSaveUtility.GetInstance(_graphView); + saveUtility.SaveNodes(_filePath); } } @@ -77,7 +113,7 @@ private void OnEnable() private void GenerateMiniMap() { - var miniMap = new MiniMap {anchored = true}; + var miniMap = new MiniMap { anchored = true }; miniMap.SetPosition(new Rect(10, 30, 200, 140)); _graphView.Add(miniMap); } @@ -87,6 +123,6 @@ 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 506b98c..0f925c5 100644 --- a/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs +++ b/com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs @@ -28,7 +28,7 @@ public static GraphSaveUtility GetInstance(StoryGraphView graphView) }; } - public void SaveNodes(string fileName) + public void SaveNodes(string filePath) { if (!Edges.Any()) return; var dialogueContainerObject = ScriptableObject.CreateInstance(); @@ -55,17 +55,13 @@ public void SaveNodes(string fileName) Position = node.GetPosition().position }); } - - if (!AssetDatabase.IsValidFolder("Assets/Resources")) - AssetDatabase.CreateFolder("Assets", "Resources"); - - AssetDatabase.CreateAsset(dialogueContainerObject, $"Assets/Resources/{fileName}.asset"); + AssetDatabase.CreateAsset(dialogueContainerObject, filePath); AssetDatabase.SaveAssets(); } - public void LoadNarrative(string fileName) + public void LoadNarrative(string filePath) { - _dialogueContainer = Resources.Load(fileName); + _dialogueContainer = AssetDatabase.LoadAssetAtPath(filePath); if (_dialogueContainer == null) { EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK"); @@ -118,7 +114,7 @@ private void ConnectDialogueNodes() { var targetNodeGUID = connections[j].TargetNodeGUID; var targetNode = Nodes.First(x => x.GUID == targetNodeGUID); - LinkNodesTogether(Nodes[i].outputContainer[j].Q(), (Port) targetNode.inputContainer[0]); + LinkNodesTogether(Nodes[i].outputContainer[j].Q(), (Port)targetNode.inputContainer[0]); targetNode.SetPosition(new Rect( _dialogueContainer.DialogueNodeData.First(x => x.NodeGUID == targetNodeGUID).Position,