private void Import()
        {
            if (!EditorUtility.DisplayDialog("Import CSV?",
                                             "Importing from CSV will overwrite the current contents. Are you sure?",
                                             "Import", "Cancel"))
            {
                return;
            }
            string newFilename = EditorUtility.OpenFilePanel("Import from CSV", EditorWindowTools.GetDirectoryName(csvFilename), "csv");

            if (!string.IsNullOrEmpty(newFilename))
            {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                try {
                    using (StreamReader file = new StreamReader(csvFilename, Encoding.UTF8)) {
                        // Work with a temporary, new table:
                        LocalizedTextTable newTable = ScriptableObject.CreateInstance <LocalizedTextTable>();

                        // Read heading:
                        string[] values = CSVExporter.GetValues(file.ReadLine());
                        newTable.languages = new List <string>(values);
                        newTable.languages.RemoveAt(0);

                        // Read fields:
                        newTable.fields.Clear();
                        while (!file.EndOfStream)
                        {
                            values = CSVExporter.GetValues(file.ReadLine());
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name = values[0];
                            for (int i = 1; i < values.Length; i++)
                            {
                                field.values.Add(values[i]);
                            }
                            newTable.fields.Add(field);
                        }

                        // If we got to the end, use the new table:
                        table.languages.Clear();
                        foreach (var newLanguage in newTable.languages)
                        {
                            table.languages.Add(newLanguage);
                        }
                        table.fields.Clear();
                        foreach (var newField in newTable.fields)
                        {
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name   = newField.name;
                            field.values = new List <string>(newField.values);
                            table.fields.Add(field);
                        }
                        DestroyImmediate(newTable);
                    }
                } catch (System.Exception e) {
                    Debug.LogError(e.Message);
                    EditorUtility.DisplayDialog("Import Failed", "There was an error importing the CSV file.", "OK");
                }
                EditorUtility.DisplayDialog("Export Complete", "The localized text table was exported to CSV (comma-separated values) format. ", "OK");
            }
        }
        private void DrawFields()
        {
            // Draw Fields foldout and "+" button:
            EditorGUILayout.BeginHorizontal();
            fieldsFoldout = EditorGUILayout.Foldout(fieldsFoldout, "Fields");
            if (GUILayout.Button("Sort", EditorStyles.miniButton, GUILayout.Width(44)))
            {
                SortFields();
                return;
            }
            if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.Width(22)))
            {
                fieldsFoldout = true;
                table.fields.Add(new LocalizedTextTable.LocalizedTextField());
                if (!fieldFoldouts.ContainsKey(table.fields.Count - 1))
                {
                    fieldFoldouts.Add(table.fields.Count - 1, true);
                }
            }
            EditorGUILayout.EndHorizontal();

            // Draw fields:
            if (fieldsFoldout)
            {
                int fieldIndexToDelete = -1;
                EditorWindowTools.StartIndentedSection();
                for (int i = 0; i < table.fields.Count; i++)
                {
                    LocalizedTextTable.LocalizedTextField field = table.fields[i];
                    if (!fieldFoldouts.ContainsKey(i))
                    {
                        fieldFoldouts.Add(i, false);
                    }
                    EditorGUILayout.BeginHorizontal();
                    fieldFoldouts[i] = EditorGUILayout.Foldout(fieldFoldouts[i], string.IsNullOrEmpty(field.name) ? string.Format("Field {0}", i) : field.name);
                    if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(22)))
                    {
                        fieldIndexToDelete = i;
                    }
                    EditorGUILayout.EndHorizontal();
                    if (fieldFoldouts[i])
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("Field", GUILayout.Width(60));
                        field.name = EditorGUILayout.TextField(field.name);
                        EditorGUILayout.LabelField(string.Empty, GUILayout.Width(22));
                        EditorGUILayout.EndHorizontal();
                        EditorWindowTools.StartIndentedSection();
                        for (int j = 0; j < table.languages.Count; j++)
                        {
                            if (j >= field.values.Count)
                            {
                                field.values.Add(string.Empty);
                            }
                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.LabelField(table.languages[j], GUILayout.Width(60));
                            field.values[j] = EditorGUILayout.TextField(field.values[j]);
                            EditorGUILayout.LabelField(string.Empty, GUILayout.Width(22));
                            EditorGUILayout.EndHorizontal();
                        }
                        EditorWindowTools.EndIndentedSection();
                    }
                }
                EditorWindowTools.EndIndentedSection();
                if (fieldIndexToDelete != -1)
                {
                    DeleteField(fieldIndexToDelete);
                }
            }
        }
Example #3
0
        private void DrawFields()
        {
            // Draw Fields foldout and "+" button:
            EditorGUILayout.BeginHorizontal();
            fieldsFoldout = EditorGUILayout.Foldout(fieldsFoldout, "Fields");
            if (GUILayout.Button("Sort", EditorStyles.miniButton, GUILayout.Width(44)))
            {
                SortFields();
                return;
            }
            if (GUILayout.Button("+", EditorStyles.miniButton, GUILayout.Width(22)))
            {
                fieldsFoldout = true;
                var index = table.fields.Count;
                if (!fieldFoldouts.ContainsKey(index))
                {
                    fieldFoldouts.Add(index, false);
                }
                if (addFieldsAtTop)
                {
                    table.fields.Insert(0, new LocalizedTextTable.LocalizedTextField());
                    for (int i = index; i > 0; i--)
                    {
                        if (!fieldFoldouts.ContainsKey(i))
                        {
                            fieldFoldouts.Add(i, false);
                        }
                        if (!fieldFoldouts.ContainsKey(i - 1))
                        {
                            fieldFoldouts.Add(i - 1, false);
                        }
                        fieldFoldouts[i] = fieldFoldouts[i - 1];
                    }
                    fieldFoldouts[0] = true;
                }
                else
                {
                    table.fields.Add(new LocalizedTextTable.LocalizedTextField());
                    fieldFoldouts[index] = true;
                }
            }
            EditorGUILayout.EndHorizontal();

            // Draw fields:
            if (fieldsFoldout)
            {
                int fieldIndexToDelete = -1;
                EditorWindowTools.StartIndentedSection();
                for (int i = 0; i < table.fields.Count; i++)
                {
                    LocalizedTextTable.LocalizedTextField field = table.fields[i];
                    if (!fieldFoldouts.ContainsKey(i))
                    {
                        fieldFoldouts.Add(i, false);
                    }
                    EditorGUILayout.BeginHorizontal();
                    fieldFoldouts[i] = EditorGUILayout.Foldout(fieldFoldouts[i], string.IsNullOrEmpty(field.name) ? string.Format("Field {0}", i) : field.name);
                    if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(22)))
                    {
                        fieldIndexToDelete = i;
                    }
                    EditorGUILayout.EndHorizontal();
                    if (fieldFoldouts[i])
                    {
                        EditorGUILayout.BeginHorizontal();

                        if (currentSearchResultIndex == i && currentSearchResultValueIndex == -1)
                        {
                            GUI.SetNextControlName("Match");
                        }

                        EditorGUILayout.LabelField("Field", GUILayout.Width(60));
                        field.name = EditorGUILayout.TextField(field.name);
                        EditorGUILayout.LabelField(string.Empty, GUILayout.Width(22));
                        EditorGUILayout.EndHorizontal();
                        EditorWindowTools.StartIndentedSection();
                        for (int j = 0; j < table.languages.Count; j++)
                        {
                            if (j >= field.values.Count)
                            {
                                field.values.Add(string.Empty);
                            }
                            EditorGUILayout.BeginHorizontal();

                            if (currentSearchResultIndex == i && currentSearchResultValueIndex == j)
                            {
                                GUI.SetNextControlName("Match");
                            }

                            EditorGUILayout.LabelField(table.languages[j], GUILayout.Width(60));
                            //---Was: field.values[j] = EditorGUILayout.TextField(field.values[j]);
                            field.values[j] = EditorGUILayout.TextArea(field.values[j]);
                            EditorGUILayout.LabelField(string.Empty, GUILayout.Width(22));
                            EditorGUILayout.EndHorizontal();
                        }
                        EditorWindowTools.EndIndentedSection();
                    }

                    if (currentSearchResultIndex == i && needToFocusOnSearchResult)
                    {
                        GUI.FocusControl("Match");
                        needToFocusOnSearchResult = false;
                    }
                }
                EditorWindowTools.EndIndentedSection();
                if (fieldIndexToDelete != -1)
                {
                    DeleteField(fieldIndexToDelete);
                }
            }
        }
Example #4
0
        private void Import()
        {
            if (!EditorUtility.DisplayDialog("Import CSV?",
                                             "Importing from CSV will overwrite the current contents. Are you sure?",
                                             "Import", "Cancel"))
            {
                return;
            }
            string newFilename = EditorUtility.OpenFilePanel("Import from CSV", EditorWindowTools.GetDirectoryName(csvFilename), "csv");

            if (!string.IsNullOrEmpty(newFilename))
            {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                try
                {
                    // Read the source file and combine multiline rows:
                    var    sourceLines = new List <string>();
                    var    file        = new StreamReader(csvFilename, EncodingTypeTools.GetEncoding(encodingType));
                    string line;
                    while ((line = file.ReadLine()) != null)
                    {
                        sourceLines.Add(line.TrimEnd());
                    }
                    file.Close();
                    CombineMultilineSourceLines(sourceLines);
                    if (sourceLines.Count < 1)
                    {
                        throw new System.Exception("No lines read from CSV file.");
                    }

                    // Work with a temporary, new table:
                    LocalizedTextTable newTable = ScriptableObject.CreateInstance <LocalizedTextTable>();

                    // Read heading:
                    string[] values = CSVExporter.GetValues(sourceLines[0]);
                    sourceLines.RemoveAt(0);
                    newTable.languages = new List <string>(values);
                    newTable.languages.RemoveAt(0);

                    // Read fields:
                    newTable.fields.Clear();
                    while (sourceLines.Count > 0)
                    {
                        values = CSVExporter.GetValues(sourceLines[0]);
                        sourceLines.RemoveAt(0);
                        LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                        field.name = values[0];
                        for (int i = 1; i < values.Length; i++)
                        {
                            field.values.Add(values[i]);
                        }
                        newTable.fields.Add(field);
                    }

                    // If we got to the end, use the new table:
                    table.languages.Clear();
                    foreach (var newLanguage in newTable.languages)
                    {
                        table.languages.Add(newLanguage);
                    }
                    table.fields.Clear();
                    foreach (var newField in newTable.fields)
                    {
                        LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                        field.name   = newField.name;
                        field.values = new List <string>(newField.values);
                        table.fields.Add(field);
                    }
                    DestroyImmediate(newTable);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e.Message);
                    EditorUtility.DisplayDialog("Import Failed", "There was an error importing the CSV file.", "OK");
                }
                EditorUtility.DisplayDialog("Import Complete", "The localized text table was imported from " + newFilename + ".", "OK");
            }
        }
        private void Import()
        {
            if (!EditorUtility.DisplayDialog("Import CSV?",
                                            "Importing from CSV will overwrite the current contents. Are you sure?",
                                            "Import", "Cancel")) {
                return;
            }
            string newFilename = EditorUtility.OpenFilePanel("Import from CSV", EditorWindowTools.GetDirectoryName(csvFilename), "csv");
            if (!string.IsNullOrEmpty(newFilename)) {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor) {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                try {
                    using (StreamReader file = new StreamReader(csvFilename, Encoding.UTF8)) {

                        // Work with a temporary, new table:
                        LocalizedTextTable newTable = ScriptableObject.CreateInstance<LocalizedTextTable>();

                        // Read heading:
                        string[] values = CSVExporter.GetValues(file.ReadLine());
                        newTable.languages = new List<string>(values);
                        newTable.languages.RemoveAt(0);

                        // Read fields:
                        newTable.fields.Clear();
                        while (!file.EndOfStream) {
                            values = CSVExporter.GetValues(file.ReadLine());
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name = values[0];
                            for (int i = 1; i < values.Length; i++) {
                                field.values.Add(values[i]);
                            }
                            newTable.fields.Add(field);
                        }

                        // If we got to the end, use the new table:
                        table.languages.Clear();
                        foreach (var newLanguage in newTable.languages) {
                            table.languages.Add(newLanguage);
                        }
                        table.fields.Clear();
                        foreach (var newField in newTable.fields) {
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name = newField.name;
                            field.values = new List<string>(newField.values);
                            table.fields.Add(field);
                        }
                        DestroyImmediate(newTable);
                    }
                } catch (System.Exception e) {
                    Debug.LogError(e.Message);
                    EditorUtility.DisplayDialog("Import Failed", "There was an error importing the CSV file.", "OK");
                }
                EditorUtility.DisplayDialog("Export Complete", "The localized text table was exported to CSV (comma-separated values) format. ", "OK");
            }
        }