Browse Source

Import standard text file format #8

master
chrisgregan 10 years ago
parent
commit
2cb88cc634
  1. 30
      Assets/Fungus/Flowchart/Editor/LanguageEditor.cs
  2. 51
      Assets/Fungus/Flowchart/Scripts/Language.cs

30
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);
}
}

51
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<string, Character> characterDict = new Dictionary<string, Character>();
foreach (Character character in GameObject.FindObjectsOfType<Character>())
{
characterDict[character.nameText] = character;
}
// Cache a lookup table of flowcharts in the scene
Dictionary<string, Flowchart> flowchartDict = new Dictionary<string, Flowchart>();
foreach (Flowchart flowChart in GameObject.FindObjectsOfType<Flowchart>())
{
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);
}
}
}
}
Loading…
Cancel
Save