예제 #1
0
                private void RenderBottomBar()
                {
                    EditorGUILayout.Separator();

                    EditorGUILayout.BeginHorizontal(GUILayout.Height(kBottomBarHeight));
                    {
                        if (GUILayout.Button("Add New", EditorStyles.toolbarButton, GUILayout.Width(_editorPrefs._keyWidth)))
                        {
                            if (!Localisation.Exists(_addNewKey) && !string.IsNullOrEmpty(_addNewKey))
                            {
                                Localisation.Set(_addNewKey, Localisation.GetCurrentLanguage(), string.Empty);
                                _keys = GetKeys();
                                SelectKey(_addNewKey);
                                _addNewKey = "";
                            }
                        }

                        string[] folders            = Localisation.GetStringFolders();
                        int      currentFolderIndex = 0;
                        string   keyWithoutFolder;
                        Localisation.GetFolderIndex(_addNewKey, out currentFolderIndex, out keyWithoutFolder);

                        EditorGUI.BeginChangeCheck();
                        int    newFolderIndex = EditorGUILayout.Popup(currentFolderIndex, folders);
                        string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (newFolderIndex != 0)
                            {
                                _addNewKey = currentFolder + "/" + keyWithoutFolder;
                            }
                        }

                        EditorGUILayout.LabelField("/", GUILayout.Width(8));

                        if (newFolderIndex != 0)
                        {
                            keyWithoutFolder = EditorGUILayout.TextField(keyWithoutFolder);
                            _addNewKey       = currentFolder + "/" + keyWithoutFolder;
                        }
                        else
                        {
                            _addNewKey = EditorGUILayout.TextField(_addNewKey);
                        }

                        GUILayout.FlexibleSpace();
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.Separator();
                }
예제 #2
0
                private void RenderBottomBar()
                {
                    EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
                    {
                        GUILayout.Button("Add New Key:", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false));

                        string[] folders = Localisation.GetStringFolders();
                        Localisation.GetFolderIndex(_addNewKey, out int currentFolderIndex, out string keyWithoutFolder);

                        EditorGUI.BeginChangeCheck();
                        int    newFolderIndex = EditorGUILayout.Popup(currentFolderIndex, folders, EditorStyles.toolbarDropDown);
                        string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (newFolderIndex != 0)
                            {
                                _addNewKey = currentFolder + "/" + keyWithoutFolder;
                            }
                        }

                        EditorGUILayout.LabelField("/", GUILayout.Width(8));

                        if (newFolderIndex != 0)
                        {
                            keyWithoutFolder = EditorGUILayout.TextField(keyWithoutFolder, GUILayout.ExpandWidth(true));
                            _addNewKey       = currentFolder + "/" + keyWithoutFolder;
                        }
                        else
                        {
                            _addNewKey = EditorGUILayout.TextField(_addNewKey, GUILayout.ExpandWidth(true));
                        }

                        if (GUILayout.Button(" Ok ", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
                        {
                            if (!Localisation.Exists(_addNewKey) && !string.IsNullOrEmpty(_addNewKey))
                            {
                                Localisation.Set(_addNewKey, Localisation.GetCurrentLanguage(), string.Empty);
                                UpdateKeys();
                                SelectKey(_addNewKey);
                                _addNewKey = "";
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
                {
                    if (!property.isExpanded)
                    {
                        SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");

                        float height = EditorGUIUtility.singleLineHeight * 3;

                        if (Localisation.Exists(localisationkeyProperty.stringValue))
                        {
                            string text     = Localisation.GetRawString(localisationkeyProperty.stringValue);
                            int    numLines = StringUtils.GetNumberOfLines(text);
                            height += (EditorGUIUtility.singleLineHeight - 2.0f) * numLines + 4.0f;
                        }

                        return(height);
                    }

                    return(EditorGUIUtility.singleLineHeight);
                }
예제 #4
0
            public string GetAutoKey()
            {
                string autoKey = null;

                if (!string.IsNullOrEmpty(_editorAutoNameParentName))
                {
                    string autoNameParent = _editorAutoNameParentName;

                    //Replace _ with / so each bit will appear in separate dropdown menu (eg TextConv_Hath_Birthday will go to TextConv, Hath, Birthday)
                    autoNameParent = autoNameParent.Replace('_', '/');

                    //Find first free key
                    int index = 0;
                    while (Localisation.Exists(autoKey = autoNameParent + "/" + index.ToString("000")))
                    {
                        index++;
                    }
                }

                return(autoKey);
            }
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");
                    string             localisationkey         = localisationkeyProperty.stringValue;

                    float yPos = position.y;

                    Rect foldoutPosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);

                    property.isExpanded = !EditorGUI.Foldout(foldoutPosition, !property.isExpanded, property.displayName + " (Localised String)");
                    yPos += EditorGUIUtility.singleLineHeight;

                    if (!property.isExpanded)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        //Draw list of possible keys
                        int currentKey = 0;
                        {
                            string[] keys = Localisation.GetStringKeys();

                            for (int i = 0; i < keys.Length; i++)
                            {
                                if (keys[i] == localisationkey)
                                {
                                    currentKey = i;
                                    break;
                                }
                            }

                            Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                            yPos += EditorGUIUtility.singleLineHeight;

                            EditorGUI.BeginChangeCheck();
                            currentKey = EditorGUI.Popup(typePosition, "Localisation Key", currentKey, keys);

                            if (EditorGUI.EndChangeCheck())
                            {
                                if (currentKey == 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, null);
                                }
                                else
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, keys[currentKey]);
                                }
                            }
                        }

                        //Draw button for adding new key
                        if (currentKey == 0)
                        {
                            string[] folders            = Localisation.GetStringFolders();
                            int      currentFolderIndex = 0;
                            string   keyWithoutFolder;
                            Localisation.GetFolderIndex(localisationkey, out currentFolderIndex, out keyWithoutFolder);


                            float keyTextWidth = position.width - EditorUtils.GetLabelWidth() - (folderNameWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth);
                            float buttonWidth  = autoKeySlashFakeWidth + keyTextWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth;


                            //Get list of current folder options
                            Rect folderText = new Rect(position.x, yPos, position.width - buttonWidth, EditorGUIUtility.singleLineHeight);

                            EditorGUI.BeginChangeCheck();
                            int    newFolderIndex = EditorGUI.Popup(folderText, "New Key", currentFolderIndex, folders);
                            string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (newFolderIndex != 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }

                            Rect addKeySlash = new Rect(position.x + (position.width - buttonWidth) - fudge, yPos, autoKeySlashWidth, EditorGUIUtility.singleLineHeight);
                            EditorGUI.LabelField(addKeySlash, new GUIContent("/"));

                            Rect addKeyText = new Rect(position.x + (position.width - buttonWidth) - fudge + autoKeySlashFakeWidth, yPos, keyTextWidth + fudge, EditorGUIUtility.singleLineHeight);
                            if (newFolderIndex != 0)
                            {
                                EditorGUI.BeginChangeCheck();
                                keyWithoutFolder = EditorGUI.TextField(addKeyText, keyWithoutFolder);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }
                            else
                            {
                                EditorGUI.BeginChangeCheck();
                                localisationkey = EditorGUI.TextField(addKeyText, localisationkey);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    UpdateNewLocalisedStringRef(property, localisationkey);
                                }
                            }

                            Rect autoKeyButton = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth, yPos, autoKeybuttonWidth, EditorGUIUtility.singleLineHeight);
                            Rect addKeyButton  = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth + buttonSpace + autoKeybuttonWidth, yPos, addButtonWidth, EditorGUIUtility.singleLineHeight);

                            yPos += addKeyButton.height;

                            if (GUI.Button(autoKeyButton, "Auto"))
                            {
                                LocalisedStringRef localisedStringRef = SerializedPropertyUtils.GetSerializedPropertyValue <LocalisedStringRef>(property);

                                Component component = property.serializedObject.targetObject as Component;
                                if (component != null)
                                {
                                    string parentName = component.gameObject.name;

                                    if (component.gameObject.scene.IsValid())
                                    {
                                        parentName = component.gameObject.scene.name + "_" + parentName;
                                    }

                                    localisedStringRef.SetAutoNameParentName(parentName);
                                }

                                localisationkey = UpdateNewLocalisedStringRef(property, localisedStringRef.GetAutoKey());
                            }

                            if (GUI.Button(addKeyButton, "Add") && !string.IsNullOrEmpty(localisationkey))
                            {
                                if (!Localisation.Exists(localisationkey))
                                {
                                    Localisation.Set(localisationkey, Localisation.GetCurrentLanguage(), string.Empty);
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }
                            }
                        }

                        //Draw displayed text (can be edited to update localization file)
                        {
                            //Only display if have a valid key
                            if (!string.IsNullOrEmpty(localisationkey) && Localisation.Exists(localisationkey))
                            {
                                string text       = StringUtils.GetFirstLine(Localisation.GetRawString(localisationkey));
                                float  height     = EditorGUIUtility.singleLineHeight;
                                float  labelWidth = EditorUtils.GetLabelWidth();

                                Rect textPosition = new Rect(position.x + labelWidth + 2.0f, yPos, position.width - labelWidth - 2.0f - editbuttonWidth - buttonSpace, height);
                                EditorGUI.LabelField(textPosition, text, EditorUtils.ReadonlyTextBoxStyle);
                                Rect editTextPosition = new Rect(textPosition.x + textPosition.width + buttonSpace, yPos, editbuttonWidth, EditorGUIUtility.singleLineHeight);

                                if (GUI.Button(editTextPosition, "Edit"))
                                {
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }

                                yPos += height;
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    EditorGUI.EndProperty();
                }
                public static object PropertyField(object obj, GUIContent label, ref bool dataChanged, GUIStyle style, params GUILayoutOption[] options)
                {
                    LocalisedStringRef localisedString = (LocalisedStringRef)obj;

                    bool editorCollapsed = !EditorGUILayout.Foldout(!localisedString._editorCollapsed, label);

                    if (editorCollapsed != localisedString._editorCollapsed)
                    {
                        localisedString._editorCollapsed = editorCollapsed;
                        dataChanged = true;
                    }

                    if (!editorCollapsed)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        //Draw list of possible keys
                        int currentKeyIndex = 0;
                        {
                            string[] keys       = Localisation.GetStringKeys();
                            string   currentKey = localisedString.GetLocalisationKey();

                            for (int i = 0; i < keys.Length; i++)
                            {
                                if (keys[i] == currentKey)
                                {
                                    currentKeyIndex = i;
                                    break;
                                }
                            }

                            EditorGUI.BeginChangeCheck();
                            currentKeyIndex = EditorGUILayout.Popup("Localisation Key", currentKeyIndex, keys);

                            //If key has changed
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (currentKeyIndex == 0)
                                {
                                    localisedString = new LocalisedStringRef();
                                }
                                else
                                {
                                    localisedString = new LocalisedStringRef(keys[currentKeyIndex]);
                                }

                                dataChanged = true;
                            }
                        }

                        //Draw buttons for adding new key
                        if (currentKeyIndex == 0)
                        {
                            string[] folders            = Localisation.GetStringFolders();
                            int      currentFolderIndex = 0;
                            string   keyWithoutFolder;
                            Localisation.GetFolderIndex(localisedString.GetLocalisationKey(), out currentFolderIndex, out keyWithoutFolder);

                            EditorGUILayout.BeginHorizontal();
                            {
                                EditorGUILayout.LabelField(new GUIContent("New Key"), GUILayout.Width(EditorUtils.GetLabelWidth()));

                                string editorParentName = localisedString.GetAutoNameParentName();

                                EditorGUI.BeginChangeCheck();
                                int    newFolderIndex = EditorGUILayout.Popup(currentFolderIndex, folders);
                                string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                                if (EditorGUI.EndChangeCheck())
                                {
                                    if (newFolderIndex != 0)
                                    {
                                        localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
                                        localisedString.SetAutoNameParentName(editorParentName);
                                        dataChanged = true;
                                    }
                                }

                                EditorGUILayout.LabelField(new GUIContent("/"), GUILayout.Width(44));

                                EditorGUI.BeginChangeCheck();
                                keyWithoutFolder = EditorGUILayout.TextField(keyWithoutFolder);
                                if (EditorGUI.EndChangeCheck())
                                {
                                    localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
                                    localisedString.SetAutoNameParentName(editorParentName);
                                    dataChanged = true;
                                }

                                if (GUILayout.Button("Auto", GUILayout.Width(36)))
                                {
                                    string newKey = localisedString.GetAutoKey();
                                    localisedString = new LocalisedStringRef(newKey);
                                    localisedString.SetAutoNameParentName(editorParentName);
                                    dataChanged = true;
                                }

                                if (GUILayout.Button("Add", GUILayout.Width(32)) && !string.IsNullOrEmpty(localisedString.GetLocalisationKey()))
                                {
                                    if (!Localisation.Exists(localisedString.GetLocalisationKey()))
                                    {
                                        Localisation.Set(localisedString.GetLocalisationKey(), Localisation.GetCurrentLanguage(), string.Empty);
                                        LocalisationEditorWindow.EditString(localisedString.GetLocalisationKey());
                                    }
                                    dataChanged = true;
                                }
                            }
                            EditorGUILayout.EndHorizontal();
                        }

                        //Draw actual localised text (can be edited to update localisation file)
                        {
                            string currentKey = localisedString.GetLocalisationKey();

                            //Only display if have a valid key
                            if (!string.IsNullOrEmpty(currentKey) && Localisation.Exists(currentKey))
                            {
                                EditorGUI.BeginChangeCheck();
                                string text;
                                if (style != null)
                                {
                                    text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey, Localisation.GetCurrentLanguage()), style);
                                }
                                else
                                {
                                    text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey, Localisation.GetCurrentLanguage()));
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    Localisation.Set(currentKey, Localisation.GetCurrentLanguage(), text);
                                }
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    return(localisedString);
                }
예제 #7
0
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    bool hasChanges = false;

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");
                    string             localisationkey         = localisationkeyProperty.stringValue;

                    float yPos = position.y;

                    //Draw list of possible keys
                    int currentKey = 0;

                    {
                        string[] keys = Localisation.GetStringKeys();

                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i] == localisationkey)
                            {
                                currentKey = i;
                                break;
                            }
                        }

                        Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                        yPos += EditorGUIUtility.singleLineHeight;

                        EditorUtils.SetBoldDefaultFont(localisationkeyProperty.prefabOverride);

                        EditorGUI.BeginChangeCheck();
                        currentKey = EditorGUI.Popup(typePosition, property.displayName + " (Localised String)", currentKey, keys);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (currentKey == 0)
                            {
                                localisationkey = UpdateNewLocalisedStringRef(property, null);
                                hasChanges      = true;
                            }
                            else
                            {
                                localisationkey = UpdateNewLocalisedStringRef(property, keys[currentKey]);
                                hasChanges      = true;
                            }
                        }

                        EditorUtils.SetBoldDefaultFont(false);
                    }

                    //Draw text preview (can be edited to update localization file)
                    if (!string.IsNullOrEmpty(localisationkey) && Localisation.Exists(localisationkey) && !localisationkeyProperty.hasMultipleDifferentValues)
                    {
                        string text       = StringUtils.GetFirstLine(Localisation.GetRawString(localisationkey, Localisation.GetCurrentLanguage()));
                        float  height     = EditorGUIUtility.singleLineHeight;
                        float  labelWidth = EditorUtils.GetLabelWidth();

                        Rect textPosition = new Rect(position.x + labelWidth + 2.0f, yPos, position.width - labelWidth - 2.0f - editbuttonWidth - buttonSpace, height);
                        EditorGUI.LabelField(textPosition, text, EditorUtils.ReadOnlyTextBoxStyle);
                        Rect editTextPosition = new Rect(textPosition.x + textPosition.width + buttonSpace, yPos, editbuttonWidth, EditorGUIUtility.singleLineHeight);

                        if (GUI.Button(editTextPosition, "Edit"))
                        {
                            LocalisationEditorWindow.EditString(localisationkey);
                        }
                    }

                    EditorGUI.EndProperty();

                    if (hasChanges)
                    {
                        property.serializedObject.ApplyModifiedProperties();
                        property.serializedObject.Update();
                    }
                }
예제 #8
0
                public static object PropertyField(object obj, GUIContent label, ref bool dataChanged, GUIStyle style, params GUILayoutOption[] options)
                {
                    LocalisedString localisedString = (LocalisedString)obj;

                    //Draw list of possible keys
                    int currentKeyIndex = 0;

                    {
                        string[] keys       = Localisation.GetStringKeys();
                        string   currentKey = localisedString.GetLocalisationKey();

                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i] == currentKey)
                            {
                                currentKeyIndex = i;
                                break;
                            }
                        }

                        EditorGUI.BeginChangeCheck();
                        currentKeyIndex = EditorGUILayout.Popup("Localisation Key", currentKeyIndex, keys);

                        //If key has changed
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (currentKeyIndex == 0)
                            {
                                localisedString = new LocalisedString();
                            }
                            else
                            {
                                localisedString = keys[currentKeyIndex];
                            }

                            dataChanged = true;
                        }
                    }

                    //Draw actual localised text (can be edited to update localisation file)
                    {
                        string currentKey = localisedString.GetLocalisationKey();

                        //Only display if have a valid key
                        if (!string.IsNullOrEmpty(currentKey) && Localisation.Exists(currentKey))
                        {
                            EditorGUI.BeginChangeCheck();
                            string text;
                            if (style != null)
                            {
                                text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey, Localisation.GetCurrentLanguage()), style);
                            }
                            else
                            {
                                text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey, Localisation.GetCurrentLanguage()));
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                Localisation.Set(currentKey, Localisation.GetCurrentLanguage(), text);
                            }
                        }
                    }

                    return(localisedString);
                }