Skip to content

Commit

Permalink
Nodes tagged 'rawText' can have their source code accessed via Dialog…
Browse files Browse the repository at this point in the history
…ue.GetTextForNode() [closes #18]
  • Loading branch information
desplesda committed Feb 22, 2016
1 parent 24ca7b9 commit 52063f4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Tests/Example.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
{
"title": "LearnMore",
"tags": "",
"tags": "rawText",
"body": "A: HAHAHA",
"position": {
"x": 763,
Expand Down
12 changes: 12 additions & 0 deletions YarnSpinner/Dialogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions YarnSpinner/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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 {
Expand All @@ -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
)
);
}
Expand Down
2 changes: 2 additions & 0 deletions YarnSpinner/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Statement> statements { get { return _statements; }}

Expand Down
17 changes: 15 additions & 2 deletions YarnSpinnerTests/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void TestNodeExists ()
{


dialogue.LoadFile ("Space.json");
dialogue.LoadFile ("../Unity/Assets/Yarn Spinner/Examples/Demo Assets/Space.json");

dialogue.Compile ();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 52063f4

Please # to comment.