From 52063f4a6853efe2a75172a916b25d6d412b1c80 Mon Sep 17 00:00:00 2001 From: Jonathon Manning Date: Tue, 23 Feb 2016 00:04:37 +1100 Subject: [PATCH] Nodes tagged 'rawText' can have their source code accessed via Dialogue.GetTextForNode() [closes #18] --- Tests/Example.json | 2 +- YarnSpinner/Dialogue.cs | 12 ++++++++++++ YarnSpinner/Loader.cs | 15 ++++++++++++--- YarnSpinner/Parser.cs | 2 ++ YarnSpinnerTests/Test.cs | 17 +++++++++++++++-- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Tests/Example.json b/Tests/Example.json index cf19d8fa5..e21b40c3a 100644 --- a/Tests/Example.json +++ b/Tests/Example.json @@ -21,7 +21,7 @@ }, { "title": "LearnMore", - "tags": "", + "tags": "rawText", "body": "A: HAHAHA", "position": { "x": 763, diff --git a/YarnSpinner/Dialogue.cs b/YarnSpinner/Dialogue.cs index f72bf80ce..f88bccf25 100644 --- a/YarnSpinner/Dialogue.cs +++ b/YarnSpinner/Dialogue.cs @@ -286,6 +286,18 @@ public string currentNode { } } + public string GetTextForNode(string nodeName) { + if (loader.nodes.Count == 0) { + LogErrorMessage ("No nodes are loaded!"); + } + if (loader.nodes.ContainsKey(nodeName)) { + return loader.nodes [nodeName].source; + } else { + LogErrorMessage ("No node named " + nodeName); + return null; + } + } + // Unloads ALL nodes. public void UnloadAll(bool clearVisitedNodes = true) { if (clearVisitedNodes) diff --git a/YarnSpinner/Loader.cs b/YarnSpinner/Loader.cs index 5ad9cc358..e614bf223 100644 --- a/YarnSpinner/Loader.cs +++ b/YarnSpinner/Loader.cs @@ -107,6 +107,12 @@ public int Load(string text, Library library, bool showTokens = false, bool show var node = new Parser (tokens, library).Parse(); + // If this node is tagged "rawText", then preserve its source + if (string.IsNullOrEmpty(nodeInfo.tags) == false && + nodeInfo.tags.Contains("rawText")) { + node.source = nodeInfo.text; + } + node.name = nodeInfo.title; if (showParseTree) @@ -136,9 +142,11 @@ public int Load(string text, Library library, bool showTokens = false, bool show struct NodeInfo { public string title; public string text; - public NodeInfo(string title, string text) { + public string tags; + public NodeInfo(string title, string text, string tags) { this.title = title; this.text = text; + this.tags = tags; } } @@ -151,7 +159,7 @@ NodeInfo[] ParseInput(string text) if (text.IndexOf("//") == 0) { // If it starts with a comment, treat it as a single-node file - nodes.Add (new NodeInfo ("Start", text)); + nodes.Add (new NodeInfo ("Start", text, null)); } else { // Blindly assume it's JSON! \:D/ try { @@ -172,7 +180,8 @@ NodeInfo[] ParseInput(string text) nodes.Add( new NodeInfo( nodeJSON["title"] as string, - nodeJSON["body"] as string + nodeJSON["body"] as string, + nodeJSON["tags"] as string ) ); } diff --git a/YarnSpinner/Parser.cs b/YarnSpinner/Parser.cs index b86c7eaea..f096596b1 100644 --- a/YarnSpinner/Parser.cs +++ b/YarnSpinner/Parser.cs @@ -267,6 +267,8 @@ internal class Node : ParseNode { internal string name { get; set;} + internal string source { get; set; } + // Read-only internal accessor for statements internal IEnumerable statements { get { return _statements; }} diff --git a/YarnSpinnerTests/Test.cs b/YarnSpinnerTests/Test.cs index e93282436..fa48535eb 100644 --- a/YarnSpinnerTests/Test.cs +++ b/YarnSpinnerTests/Test.cs @@ -73,7 +73,7 @@ public void TestNodeExists () { - dialogue.LoadFile ("Space.json"); + dialogue.LoadFile ("../Unity/Assets/Yarn Spinner/Examples/Demo Assets/Space.json"); dialogue.Compile (); @@ -125,7 +125,7 @@ public void TestMissingNode() [Test()] public void TestGettingCurrentNodeName() { - dialogue.LoadFile ("Space.json"); + dialogue.LoadFile ("../Unity/Assets/Yarn Spinner/Examples/Demo Assets/Space.json"); // dialogue should not be running yet Assert.IsNull (dialogue.currentNode); @@ -141,6 +141,19 @@ public void TestGettingCurrentNodeName() { Assert.IsNull (dialogue.currentNode); } + [Test()] + public void TestGettingRawSource() { + dialogue.LoadFile ("Example.json"); + + dialogue.Compile (); + + var source = dialogue.GetTextForNode ("LearnMore"); + + Assert.IsNotNull (source); + + Assert.AreEqual (source, "A: HAHAHA"); + } + private void HandleResult(Yarn.Dialogue.RunnerResult result) { Console.WriteLine (result);