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

implicit selection of dialogue graph #1

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion TMP.meta → Samples/ScriptableObject.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Samples/ScriptableObject/DemoDialogue.asset
Original file line number Diff line number Diff line change
@@ -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}
8 changes: 8 additions & 0 deletions Samples/ScriptableObject/DemoDialogue.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 57 additions & 21 deletions com.subtegral.dialoguesystem/Editor/Graph/StoryGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoryGraph>();
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);
Expand All @@ -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);
}
}

Expand All @@ -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);
}
Expand All @@ -87,6 +123,6 @@ private void OnDisable()
{
rootVisualElement.Remove(_graphView);
}

}
}
14 changes: 5 additions & 9 deletions com.subtegral.dialoguesystem/Editor/GraphSaveUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DialogueContainer>();
Expand All @@ -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<DialogueContainer>(fileName);
_dialogueContainer = AssetDatabase.LoadAssetAtPath<DialogueContainer>(filePath);
if (_dialogueContainer == null)
{
EditorUtility.DisplayDialog("File Not Found", "Target Narrative Data does not exist!", "OK");
Expand Down Expand Up @@ -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>(), (Port) targetNode.inputContainer[0]);
LinkNodesTogether(Nodes[i].outputContainer[j].Q<Port>(), (Port)targetNode.inputContainer[0]);

targetNode.SetPosition(new Rect(
_dialogueContainer.DialogueNodeData.First(x => x.NodeGUID == targetNodeGUID).Position,
Expand Down