Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to the Loading and Saving System #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -34,37 +32,61 @@ private void ConstructGraphView()
_graphView.StretchToParentSize();
rootVisualElement.Add(_graphView);
}


private void RegenerateToolbar()
{
// remove the old toolbar
rootVisualElement.Remove(rootVisualElement.Q<Toolbar>());
// 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;
}
}
}

Expand All @@ -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))
Expand All @@ -111,9 +133,6 @@ private void GenerateBlackBoard()
_graphView.Blackboard = blackboard;
}

private void OnDisable()
{
rootVisualElement.Remove(_graphView);
}
private void OnDisable() => rootVisualElement.Remove(_graphView);
}
}
}
74 changes: 45 additions & 29 deletions com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -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<DialogueContainer>();
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();
Expand Down Expand Up @@ -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<DialogueContainer>(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<DialogueContainer>(filePath);

ClearGraph();
GenerateDialogueNodes();
Expand Down Expand Up @@ -214,4 +230,4 @@ private void GenerateCommentBlocks()
}
}
}
}
}