public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var key       = Localizator.ConvertRawToKey(property.stringValue);
            var keyExists = DoesKeyExists(key);

            // Rects
            var keyRect     = new Rect(position.x, position.y, position.width, 30f);
            var helpBoxRect = new Rect(keyRect.x, keyRect.y + (keyExists ? 0f : 40f), position.width, 35f);
            var editKeyRect = new Rect(keyRect.x, helpBoxRect.y + 40f, position.width, 35f);

            // Drawing Property
            EditorGUI.PropertyField(keyRect, property, label);
            property.stringValue = Localizator.ConvertRawToKey(property.stringValue);

            if (!keyExists)
            {
                EditorGUI.HelpBox(helpBoxRect, "The key does not exist in the current context", MessageType.Warning);
            }

            if (GUI.Button(editKeyRect, Localizator.DoesContainKey(key) ? $"Edit the '{key}' key" : $"Create a key named '{key}'"))
            {
                OpenKeyOnEditor(key);
            }
        }
Esempio n. 2
0
        private void OnGUI()
        {
            var keySearchBarStyle = new GUIStyle(EditorStyles.textField)
            {
                fixedWidth  = position.width * .99f,
                fixedHeight = 23f,
                alignment   = TextAnchor.MiddleCenter,
                fontSize    = 12
            };

            key = GUILayout.TextField(key, keySearchBarStyle);
            key = Localizator.ConvertRawToKey(key);

            GUILayout.Space(10f);
            Localizator.UpdateKeyFile();

            if (key != keyLastValue || values.Length != Localizator.GetLanguageCount())
            {
                values = new string[Localizator.GetLanguageCount()];
            }

            if (Localizator.DoesContainKey(key))
            {
                scrollPos = GUILayout.BeginScrollView(scrollPos, false, true);
                for (var i = 0; i < values.Length; i++)
                {
                    if (values[i] == null)
                    {
                        values[i] = Localizator.GetString(key, Localizator.GetAvailableLanguages()[i], false);
                    }
                    GUILayout.BeginHorizontal();

                    var isSameAsTheSavedValue = values[i] == Localizator.GetString(key, Localizator.GetAvailableLanguages()[i], false);
                    GUI.color = isSameAsTheSavedValue ? Color.green : Color.red;
                    EditorGUILayout.LabelField($"{Localizator.GetAvailableLanguages()[i]}: ",
                                               GUILayout.MaxWidth(50));
                    GUI.color = Color.white;

                    var customTextAreaStyle = EditorStyles.textArea;
                    customTextAreaStyle.wordWrap      = true;
                    customTextAreaStyle.fixedHeight   = 0;
                    customTextAreaStyle.stretchHeight = true;

                    values[i] = EditorGUILayout.TextArea(
                        values[i], customTextAreaStyle);

                    GUILayout.EndHorizontal();
                }

                GUILayout.EndScrollView();

                if (GUILayout.Button("Update Values"))
                {
                    for (var i = 0; i < values.Length; i++)
                    {
                        Localizator.AddLocalizedValue(key, values[i], Localizator.GetAvailableLanguages()[i]);
                    }

                    Localizator.RefreshAll();
                    RefreshKeyBrowser();
                }

                if (GUILayout.Button("Remove Key"))
                {
                    var dialogOutput = EditorUtility.DisplayDialog(
                        $"{key} will be removed permanently",
                        "Are you sure you want to remove this key?",
                        "Remove",
                        "Cancel");
                    if (dialogOutput)
                    {
                        Localizator.RemoveKey(key);
                        Localizator.RefreshAll();

                        RefreshKeyBrowser();
                    }
                }

                // Renaming
                EditorScriptingUtils.BeginCenter();

                var canRename = !Localizator.DoesContainKey(newName.Trim()) &&
                                !string.IsNullOrWhiteSpace(newName.Trim());
                if (GUILayout.Button("Rename Key", GUILayout.Width(this.position.width * .5f)))
                {
                    if (canRename)
                    {
                        Localizator.AddKey(newName.Trim());
                        foreach (var language in Localizator.GetAvailableLanguages())
                        {
                            Localizator.AddLocalizedValue(newName.Trim(), Localizator.GetString(key, language, returnErrorString: false), language);
                        }

                        Localizator.RemoveKey(key);

                        key     = newName.Trim();
                        newName = "";
                        RefreshKeyBrowser();
                    }
                }

                newName = Localizator.ConvertRawToKey(GUILayout.TextField(newName, GUILayout.Width(this.position.width * .5f)));
                EditorScriptingUtils.EndCenter();
                if (Localizator.DoesContainKey(newName.Trim()))
                {
                    EditorGUILayout.HelpBox("The key name entered is already in use.",
                                            MessageType.Warning);
                }
            }
            else if (GUILayout.Button("Add New Key") && !string.IsNullOrEmpty(key) && !string.IsNullOrWhiteSpace(key))
            {
                Localizator.AddKey(key);
                RefreshKeyBrowser();
            }

            keyLastValue = key;
        }
 private static bool DoesKeyExists(string key) => Localizator.DoesContainKey(key);