From 2cb88cc634bb65e8a6f820f2fba895aceacc3888 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 9 Apr 2015 11:09:23 +0100 Subject: [PATCH] Import standard text file format #8 --- .../Fungus/Flowchart/Editor/LanguageEditor.cs | 30 ++++++++--- Assets/Fungus/Flowchart/Scripts/Language.cs | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs b/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs index c9060f80..8afc0f41 100644 --- a/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs @@ -24,19 +24,24 @@ namespace Fungus { serializedObject.Update(); - Language t = target as Language; + Language language = target as Language; EditorGUILayout.PropertyField(activeLanguageProp); EditorGUILayout.PropertyField(localizationFileProp); if (GUILayout.Button(new GUIContent("Export Localization File"))) { - ExportLocalizationFile(t); + ExportLocalizationFile(language); } if (GUILayout.Button(new GUIContent("Export Standard Text"))) { - ExportStandardText(t); + ExportStandardText(language); + } + + if (GUILayout.Button(new GUIContent("Import Standard Text"))) + { + ImportStandardText(language); } serializedObject.ApplyModifiedProperties(); @@ -44,7 +49,7 @@ namespace Fungus public virtual void ExportLocalizationFile(Language language) { - string path = EditorUtility.SaveFilePanel("Export Localization File", "", + string path = EditorUtility.SaveFilePanel("Export Localization File", "Assets/", "localization.csv", ""); if (path.Length == 0) { @@ -53,12 +58,12 @@ namespace Fungus string csvData = language.GetCSVData(); File.WriteAllText(path, csvData); + AssetDatabase.Refresh(); } public virtual void ExportStandardText(Language language) { - string path = EditorUtility.SaveFilePanel("Export Standard Text", "", - "standard.txt", ""); + string path = EditorUtility.SaveFilePanel("Export Standard Text", "Assets/", "standard.txt", ""); if (path.Length == 0) { return; @@ -66,6 +71,19 @@ namespace Fungus string textData = language.GetStandardText(); File.WriteAllText(path, textData); + AssetDatabase.Refresh(); + } + + public virtual void ImportStandardText(Language language) + { + string path = EditorUtility.OpenFilePanel("Import Standard Text", "Assets/", "txt"); + if (path.Length == 0) + { + return; + } + + string textData = File.ReadAllText(path); + language.SetStandardText(textData); } } diff --git a/Assets/Fungus/Flowchart/Scripts/Language.cs b/Assets/Fungus/Flowchart/Scripts/Language.cs index bfdccf33..5062c5f5 100644 --- a/Assets/Fungus/Flowchart/Scripts/Language.cs +++ b/Assets/Fungus/Flowchart/Scripts/Language.cs @@ -437,6 +437,57 @@ namespace Fungus return textData; } + + /** + * Sets standard text on scene objects by parsing a text data file. + */ + public virtual void SetStandardText(string textData) + { + // Cache a lookup table of characters in the scene + Dictionary characterDict = new Dictionary(); + foreach (Character character in GameObject.FindObjectsOfType()) + { + characterDict[character.nameText] = character; + } + + // Cache a lookup table of flowcharts in the scene + Dictionary flowchartDict = new Dictionary(); + foreach (Flowchart flowChart in GameObject.FindObjectsOfType()) + { + flowchartDict[flowChart.localizationId] = flowChart; + } + + string[] lines = textData.Split('\n'); + + string stringId = ""; + string buffer = ""; + foreach (string line in lines) + { + // Check for string id line + if (line.StartsWith("#")) + { + if (stringId.Length > 0) + { + // Write buffered text to the appropriate text property + PopulateTextProperty(stringId, buffer.Trim(), flowchartDict, characterDict); + } + + // Set the string id for the follow text lines + stringId = line.Substring(1, line.Length - 1); + buffer = ""; + } + else + { + buffer += line; + } + } + + // Handle last buffered entry + if (stringId.Length > 0) + { + PopulateTextProperty(stringId, buffer.Trim(), flowchartDict, characterDict); + } + } } } \ No newline at end of file