private static bool DrawItemSelector(SerializedProperty p, SerializedProperty serializedList, string label)
        {
            var temp = p.intValue;

            GUILayout.BeginVertical();
            {
                var size   = 14;
                int height = (int)(size * 1.5f);

                List <string> list = new List <string>();
                if (serializedList.arraySize > 0)
                {
                    for (int i = 0; i < serializedList.arraySize; i++)
                    {
                        var el = serializedList.GetArrayElementAtIndex(i);
                        if (!string.IsNullOrEmpty(el?.objectReferenceValue?.name))
                        {
                            list.Add(el.objectReferenceValue.name);
                        }
                    }
                }

                var backColor = p.intValue != -1 ? LocLayout.BACK_COLOR : LocLayout.EMPTY_COLOR;
                LocLayout.Background(backColor, height);
                LocLayout.Space(-height);
                GUILayout.BeginVertical(GUILayout.Height(size));
                if (list.Count > 0)
                {
                    if (p.intValue == -1)
                    {
                        p.intValue = 0;
                    }
                    p.intValue = EditorGUILayout.Popup(label, p.intValue, list.ToArray());
                }
                else
                {
                    p.intValue = -1;
                    EditorGUILayout.LabelField("Selected Language \t\t\t - NONE -");
                }

                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();
            return(temp != p.intValue ? true : false);
        }
Beispiel #2
0
        private static string[] ReadFile(string filepath)
        {
            if (!File.Exists(filepath))
            {
                LocLayout.Error($"The file path {filepath} doesn't exist!");
                PlayerPrefs.DeleteKey(PrefsKey);
                return(null);
            }

            var data = File.ReadAllLines(filepath);

            if (data.Length <= 0 || data[0] != FILE_HEADER)
            {
                LocLayout.Error($"The file path {filepath} is corrupted!");
                PlayerPrefs.DeleteKey(PrefsKey);
                return(null);
            }

            return(data);
        }
Beispiel #3
0
        public static void SaveData(string filepath, List <LocalizationElement> dataList)
        {
            if (!File.Exists(filepath))
            {
                LocLayout.Error($"The file path {filepath} doesn't exist!");
                return;
            }

            string data = "";

            void AddLine(string line) => data += line + "\n";

            AddLine(FILE_HEADER);
            if (dataList != null)
            {
                foreach (string group in LOCALIZATION_GROUPS)
                {
                    if (group != "Default")
                    {
                        AddLine(LocalizationManager.DIVISION_CHAR + group);
                    }
                    foreach (var t in dataList)
                    {
                        var element = t;
                        if (group != LOCALIZATION_GROUPS[element.Group])
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(element.Key))
                        {
                            continue;
                        }

                        element.Key = element.Key.Replace(LocalizationManager.DIVISION_CHAR, "");
                        AddLine(element.Key + LocalizationManager.DIVISION_CHAR + element.Text);
                    }
                }
            }

            File.WriteAllText(filepath, data);
        }
        private void OnGUI()
        {
            EditorGUI.indentLevel++;
            LocLayout.Space();

            LocLayout.Label("Localization Manager");

            LocLayout.Space();

            //---Not ready:
            if (!readySteady)
            {
                GUILayout.BeginVertical("Box");

                EditorGUILayout.HelpBox(
                    "There is no Localization Manager file. To set up keys structure and language system, select or create a Localization Manager file.",
                    MessageType.Info);
                GUILayout.BeginHorizontal("Box");
                if (GUILayout.Button("Select Localization Manager file", GUILayout.Width(250)))
                {
                    string f = EditorUtility.OpenFilePanel("Select Localization Manager file", Application.dataPath,
                                                           "loc");
                    if (string.IsNullOrEmpty(f))
                    {
                        return;
                    }
                    path = f;
                    PlayerPrefs.SetString(LocalizationController.PrefsKey, path);
                    LocLayout.Message("Great! The Localization Manager is now ready.");
                    GetManagerPath();
                    return;
                }

                if (GUILayout.Button("Create Localization Manager file"))
                {
                    string f = EditorUtility.SaveFilePanel("Create Localization Manager file", Application.dataPath,
                                                           "LocalizationManager", "loc");
                    if (string.IsNullOrEmpty(f))
                    {
                        return;
                    }
                    File.Create(f).Dispose();
                    path = f;
                    PlayerPrefs.SetString(LocalizationController.PrefsKey, path);
                    LocLayout.Message("Great! The Localization Manager is now ready.");
                    Save();
                    GetManagerPath();
                    return;
                }

                GUILayout.EndHorizontal();
                GUILayout.EndVertical();

                return;
            }
            //---Else...:

            #region SECTION__UPPER

            GUILayout.BeginHorizontal("Box");
            if (GUILayout.Button("Save System"))
            {
                Save();
            }
            LocLayout.Space();
            if (GUILayout.Button("Reset Manager Path"))
            {
                if (EditorUtility.DisplayDialog("Question",
                                                "You are about to reset the Localization Manager path... Are you sure?", "Yes", "No"))
                {
                    PlayerPrefs.DeleteKey(LocalizationController.PrefsKey);
                    this.Close();
                }
            }

            GUILayout.EndHorizontal();

            LocLayout.Space(5);


            GUILayout.BeginHorizontal("Box");
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Open Language"))
            {
                OpenLang();
            }

            if (GUILayout.Button("Create Language"))
            {
                CreateLang();
            }

            GUILayout.EndHorizontal();

            #endregion

            LocLayout.Space(5);

            GUILayout.BeginVertical("Box");

            #region SECTION_GROUPS

            GUILayout.BeginHorizontal("Box");
            EditorGUIUtility.labelWidth -= 70;
            selectAllKeys = EditorGUILayout.ToggleLeft("Show all keys", selectAllKeys);
            if (!selectAllKeys)
            {
                groupSelected = EditorGUILayout.Popup("Group:", groupSelected,
                                                      LocalizationController.LOCALIZATION_GROUPS.ToArray(), GUILayout.MaxWidth(300),
                                                      GUILayout.MinWidth(50));
            }
            else
            {
                EditorGUILayout.LabelField("Group: BLOCKED", GUILayout.MaxWidth(300),
                                           GUILayout.MinWidth(50));
            }

            EditorGUIUtility.labelWidth += 70;

            LocLayout.Space();
            grpoup_Name = EditorGUILayout.TextField(grpoup_Name);
            if (GUILayout.Button("+ Group"))
            {
                if (string.IsNullOrEmpty(grpoup_Name))
                {
                    LocLayout.Error("Please fill the required field! [Group Name]");
                    return;
                }

                LocalizationController.LOCALIZATION_GROUPS.Add(grpoup_Name);
                grpoup_Name = "";
                GUI.FocusControl("Set");
                return;
            }

            if (GUILayout.Button("- Group") && LocalizationController.LOCALIZATION_GROUPS.Count > 1)
            {
                if (EditorUtility.DisplayDialog("Question", "You are going to remove category... Are you sure?",
                                                "Yes", "No"))
                {
                    if (string.IsNullOrEmpty(grpoup_Name))
                    {
                        LocalizationController.LOCALIZATION_GROUPS.RemoveAt(
                            LocalizationController.LOCALIZATION_GROUPS.Count - 1);
                        groupSelected = 0;
                    }
                    else
                    {
                        int  cc       = 0;
                        bool notfound = true;
                        foreach (string cat in LocalizationController.LOCALIZATION_GROUPS)
                        {
                            if (grpoup_Name == cat)
                            {
                                LocalizationController.LOCALIZATION_GROUPS.RemoveAt(cc);
                                groupSelected = 0;
                                notfound      = false;
                                break;
                            }

                            cc++;
                        }

                        if (notfound)
                        {
                            LocLayout.Error("The category couldn't be found.");
                        }
                        grpoup_Name = "";
                    }

                    return;
                }
            }


            GUILayout.EndHorizontal();

            #endregion

            LocLayout.Space();

            #region SECTION__LOCALIZATION_ARRAY

            searchField.OnGUI();

            GUILayout.BeginHorizontal();
            LocLayout.Label("Localization Keys & Translations");
            if (GUILayout.Button("+"))
            {
                ElementsList.Insert(0, new LocalizationElement()
                {
                    Group = groupSelected
                });
            }
            GUILayout.EndHorizontal();

            if (ElementsList.Count == 0)
            {
                GUILayout.Label("- - Empty - -", EditorStyles.boldLabel);
            }
            else
            {
                scrollRect = EditorGUILayout.BeginScrollView(scrollRect);


                if (string.IsNullOrEmpty(searchField.SearchString))
                {
                    tempElementsList = ElementsList;
                }
                else if (searchField.IsChanged)
                {
                    tempElementsList = new List <LocalizationElement>();
                    foreach (LocalizationElement locA in ElementsList)
                    {
                        if (locA.Key.IndexOf(searchField.SearchString, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            tempElementsList.Add(locA);
                        }
                    }
                }
                foreach (var locEl in tempElementsList)
                {
                    if (locEl.Group >= LocalizationController.LOCALIZATION_GROUPS.Count)
                    {
                        locEl.Group = 0;
                        break;
                    }

                    if (!selectAllKeys && LocalizationController.LOCALIZATION_GROUPS[locEl.Group] !=
                        LocalizationController.LOCALIZATION_GROUPS[groupSelected])
                    {
                        continue;
                    }

                    EditorGUIUtility.labelWidth -= 100;
                    EditorGUILayout.BeginHorizontal("Box");
                    {
                        EditorGUILayout.LabelField("Key:", GUILayout.Width(45));

                        locEl.Key = EditorGUILayout.TextField(locEl.Key, GUILayout.MaxWidth(150),
                                                              GUILayout.MinWidth(100));
                        EditorGUILayout.LabelField("Group:", GUILayout.Width(75));
                        locEl.Group = EditorGUILayout.Popup(locEl.Group,
                                                            LocalizationController.LOCALIZATION_GROUPS.ToArray());
                        if (GUILayout.Button("-", GUILayout.Width(30)))
                        {
                            ElementsList.Remove(locEl);
                            return;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                    EditorGUIUtility.labelWidth += 100;
                }

                EditorGUILayout.EndScrollView();
            }

            GUILayout.EndVertical();

            #endregion

            EditorGUI.indentLevel--;
        }
        public override void OnInspectorGUI()
        {
            if (target == null)
            {
                return;
            }
            serializedObject.Update();
            DrawHeader();

            LocLayout.Space();

            //LANGUAGE SETTINGS

            if (LocLayout.Bar("Show language settings", ref isShowLang, _selectedLanguage.intValue == -1))
            {
                GUILayout.BeginVertical(blockStyle);

                LocLayout.List(_languageFiles, "Language Files");
                LocLayout.Space(5);
                serializedObject.ApplyModifiedProperties();

                if (DrawItemSelector(_selectedLanguage, _languageFiles, "Selected Language") &&
                    _selectedLanguage.intValue != -1)
                {
                    l.LoadLanguage(_selectedLanguage.intValue);
                }

                GUILayout.EndVertical();
            }

            LocLayout.Space();
            serializedObject.ApplyModifiedProperties();

            // CONTENT SETTINGS
            LocLayout.Bar("Show content settings", ref isShowContent);
            if (!isShowContent)
            {
                return;
            }

            GUILayout.BeginVertical(blockStyle);
            {
                LocLayout.Property(_gameObjectChildsRoot, "GameObject Root",
                                   "Starting find root for keys containing 'GameObjectChild' assignation type", isColor: true,
                                   isNullColor: true);
                LocLayout.Space(5);
                GUILayout.BeginHorizontal();
                {
                    LocLayout.ToggleLabel(_loadLanguageOnStart, "Load language On Start",
                                          "Update all GameObjects on scene load");
                    LocLayout.Space(3);
                    if (_selectedLanguage.intValue != -1 && LocLayout.Button("Load selected language"))
                    {
                        l.LoadLanguage(_selectedLanguage.intValue);
                    }
                }
                GUILayout.EndHorizontal();
                LocLayout.Space();
                serializedObject.ApplyModifiedProperties();

                //ADD NEW KEY
                if (LocLayout.Bar("Add Key", ref addKey, false, 20))
                {
                    LocalizationWindow.Init();
                    Refresh();
                    DrawAddKeyMenu();
                }

                LocLayout.Space();
                serializedObject.Update();

                //KEY LIST
                GUILayout.BeginVertical();
                {
                    if (l.objectSelectorList.Count > 0)
                    {
                        DrawKeyList(_localizationSelector);
                    }
                    else
                    {
                        LocLayout.Label("- - Key list is empty - -");
                    }
                }
                GUILayout.EndVertical();
            }

            GUILayout.EndVertical();
            serializedObject.ApplyModifiedProperties();
        }
 private new static void DrawHeader()
 {
     LocLayout.Space();
     GUILayout.Label(GUIContent.none, LocLayout.HEADER, GUILayout.ExpandWidth(true));
     LocLayout.Space(20);
 }
        private void DrawKeyList(SerializedProperty p)
        {
            for (int i = 0; i < l.objectSelectorList.Count; i++)
            {
                GUILayout.BeginVertical(blockStyle);
                SerializedProperty item  = _localizationSelector.GetArrayElementAtIndex(i);
                var foundObjectsProperty = item.FindPropertyRelative("foundObjects");

                GUILayout.BeginHorizontal();
                {
                    LocLayout.Property(item, l.objectSelectorList[i].key);

                    if (Application.isPlaying)
                    {
                        GUILayout.BeginVertical();
                        {
                            for (int j = 0; j < foundObjectsProperty.arraySize; j++)
                            {
                                LocLayout.Property(foundObjectsProperty.GetArrayElementAtIndex(j));
                            }
                        }
                        GUILayout.EndVertical();
                    }

                    if (LocLayout.Button("Delete", 30, 40, LocLayout.BACK_COLOR))
                    {
                        l.objectSelectorList.RemoveAt(i);
                        return;
                    }
                }
                GUILayout.EndHorizontal();
                if (!item.isExpanded)
                {
                    GUILayout.EndVertical();
                    continue;
                }

                LocalizationManager.ObjectSelector sec = l.objectSelectorList[i];

                LocLayout.Space(5);


                EditorGUI.indentLevel += 1;
                GUILayout.BeginHorizontal("Box");
                LocLayout.Label("Key: " + sec.key);
                LocLayout.Label("Group: " + l.groups[sec.@group]);
                GUILayout.EndHorizontal();
                LocLayout.Space();


                LocLayout.Property(item.FindPropertyRelative("assignationType"), "Assignation Type", isColor: true);

                switch (sec.assignationType)
                {
                case LocalizationManager.ObjectSelector.AssignationType.GameObjectChild:

                    if (!LocLayout.ToggleLabel(item.FindPropertyRelative("refindOnStart"), "Refind On Start"))
                    {
                        LocLayout.Space(5);

                        GUILayout.BeginHorizontal();
                        {
                            if (LocLayout.Button("Search"))
                            {
                                sec.foundObjects = l.FindGameObject(sec);
                            }
                            LocLayout.Space(3);

                            LocLayout.List(foundObjectsProperty, "Objects");
                        }
                        GUILayout.EndHorizontal();
                    }
                    LocLayout.Space(5);

                    LocLayout.ToggleLabel(item.FindPropertyRelative("findChildByKeyName"),
                                          "Find Child By Key Name",
                                          "If enabled, the system will find the child of the selected component type [below] by the key name");
                    if (sec.findChildByKeyName)
                    {
                        LocLayout.Property(item.FindPropertyRelative("childName"), "Child Name");
                    }

                    LocLayout.Space(3);

                    LocLayout.ToggleLabel(item.FindPropertyRelative("сhildsRootObject"),
                                          "Use Custom Childs Root Object");
                    if (sec.сhildsRootObject)
                    {
                        LocLayout.Property(item.FindPropertyRelative("customChildsRootObject"),
                                           "Custom Childs Root Object", isColor: true, isNullColor: true);
                    }

                    LocLayout.Space(3);

                    LocLayout.ToggleLabel(item.FindPropertyRelative("multipleObjectAllowed"),
                                          "Allow Multiple Objects");

                    LocLayout.Space(5);
                    LocLayout.Label("Allow Component Object");
                    GUILayout.BeginHorizontal();
                    {
                        LocLayout.ToggleLabel(item.FindPropertyRelative("textComponentAllowed"),
                                              "UIText");
                        LocLayout.ToggleLabel(item.FindPropertyRelative("textMeshComponentAllowed"),
                                              "TextMesh");
                        LocLayout.ToggleLabel(item.FindPropertyRelative("textMeshProComponentAllowed"),
                                              "UITextMeshPro");
                        LocLayout.Space(3);
                    }
                    GUILayout.EndHorizontal();


                    break;

                case LocalizationManager.ObjectSelector.AssignationType.SpecificTextMeshPro:
                    LocLayout.Property(item.FindPropertyRelative("textMeshProObject"),
                                       "Specific UI TextMeshPro",
                                       "Assign specific UI TextMeshPro object", isColor: true, isNullColor: true);
                    break;

                case LocalizationManager.ObjectSelector.AssignationType.SpecificText:
                    LocLayout.Property(item.FindPropertyRelative("textObject"), "Specific UI Text",
                                       "Assign specific UI Text object", isColor: true, isNullColor: true);
                    break;

                case LocalizationManager.ObjectSelector.AssignationType.SpecificTextMesh:
                    LocLayout.Property(item.FindPropertyRelative("textMeshbject"), "Specific Text Mesh",
                                       "Assign specific Text Mesh object", isColor: true, isNullColor: true);
                    break;
                }

                EditorGUI.indentLevel -= 1;
                GUILayout.EndVertical();
            }

            serializedObject.ApplyModifiedProperties();
        }
        private void DrawAddKeyMenu()
        {
            bool addAll = false;

            GUILayout.BeginVertical(blockStyle);
            {
                LocLayout.Label("Choose Group:");
                LocLayout.Space();
                GUILayout.BeginHorizontal();
                {
                    //TODO too much elements
                    for (int i = 0; i < l.groups.Count; i++)
                    {
                        if (LocLayout.Button(l.groups[i]))
                        {
                            _group = i;
                        }

                        LocLayout.Space();
                    }
                }
                GUILayout.EndHorizontal();
                if (_group != -1)
                {
                    LocLayout.Space(40);

                    GUILayout.BeginVertical(blockStyle);
                    {
                        GUILayout.BeginHorizontal();
                        {
                            LocLayout.Label("Choose Key:");
                            GUILayout.FlexibleSpace();
                            LocLayout.Button("Add All", ref addAll, 120);
                        }
                        GUILayout.EndHorizontal();
                        LocLayout.Space();
                        GUILayout.BeginVertical();
                        {
                            ushort counter = 0;
                            LocalizationWindow.ElementsList.Sort((x, y)
                                                                 => String.Compare(x.Key, y.Key, StringComparison.OrdinalIgnoreCase));
                            for (var i = 0; i < LocalizationWindow.ElementsList.Count; i++)
                            {
                                var el = LocalizationWindow.ElementsList[i];
                                if (el.Group != _group)
                                {
                                    continue;
                                }
                                bool passed = true;
                                foreach (LocalizationManager.ObjectSelector sel in l.objectSelectorList)
                                {
                                    if (sel.key == el.Key)
                                    {
                                        passed = false;
                                        break;
                                    }
                                }

                                if (!passed)
                                {
                                    continue;
                                }
                                if (addAll)
                                {
                                    AddKey(el.Key);
                                }

                                //DRAW ELEMENTS
                                if (counter % 3 == 0)
                                {
                                    GUILayout.BeginHorizontal();
                                }

                                if (LocLayout.Button(el.Key, width: 100, color: LocLayout.BACK_COLOR))
                                {
                                    AddKey(el.Key);
                                    addKey = false;
                                    _group = -1;
                                    return;
                                }

                                GUILayout.FlexibleSpace();
                                counter++;
                                if (counter % 3 == 0)
                                {
                                    GUILayout.EndHorizontal();
                                    LocLayout.Space();
                                }
                            }

                            if (counter % 3 != 0)
                            {
                                GUILayout.EndHorizontal();
                            }
                            if (counter == 0)
                            {
                                LocLayout.Label("- - Empty - -");
                            }
                        }
                        GUILayout.EndVertical();
                    }
                    GUILayout.EndVertical();
                }
            }
            GUILayout.EndVertical();
            serializedObject.ApplyModifiedProperties();
        }
Beispiel #9
0
        private void OnGUI()
        {
            LocLayout.Label($"Language: {Language}");

            LocLayout.Space();

            #region SECTION__UPPER

            GUILayout.BeginHorizontal("Box");
            if (GUILayout.Button("Save"))
            {
                Save();
            }

            LocLayout.Space();

            GUILayout.EndHorizontal();

            LocLayout.Space(5);

            #endregion

            LocLayout.Space(5);

            GUILayout.BeginVertical("Box");

            #region SECTION_GROUPS

            GUILayout.BeginHorizontal("Box");

            EditorGUIUtility.labelWidth -= 70;
            selectAllKeys = EditorGUILayout.ToggleLeft("Show all keys", selectAllKeys);
            if (!selectAllKeys)
            {
                selectedGroup = EditorGUILayout.Popup("Group:", selectedGroup,
                                                      LocalizationController.LOCALIZATION_GROUPS.ToArray(), GUILayout.MaxWidth(300),
                                                      GUILayout.MinWidth(50));
            }
            else
            {
                EditorGUILayout.LabelField("Group: BLOCKED", GUILayout.MaxWidth(300),
                                           GUILayout.MinWidth(50));
            }

            EditorGUIUtility.labelWidth += 70;

            GUILayout.EndHorizontal();

            #endregion

            LocLayout.Space();

            #region SECTION__LOCALIZATION_ARRAY

            searchField.OnGUI();
            List <LocalizationElement> tempLocalizationArray = null;

            if (elementsList?.Count == 0)
            {
                GUILayout.Label("- - Empty - -", EditorStyles.boldLabel);
            }
            else
            {
                scrollRect = EditorGUILayout.BeginScrollView(scrollRect);


                if (string.IsNullOrEmpty(searchField.SearchString))
                {
                    tempLocalizationArray = elementsList;
                }
                else if (searchField.IsChanged)

                {
                    tempLocalizationArray = new List <LocalizationElement>();
                    foreach (LocalizationElement locA in elementsList)
                    {
                        if (locA.Key.IndexOf(searchField.SearchString, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            tempLocalizationArray.Add(locA);
                        }
                    }
                }


                if (tempLocalizationArray != null)
                {
                    for (var i = 0; i < tempLocalizationArray.Count; i++)
                    {
                        LocalizationElement locEl = tempLocalizationArray[i];
                        if (locEl.Group >= LocalizationController.LOCALIZATION_GROUPS.Count)
                        {
                            locEl.Group = 0;
                            break;
                        }

                        if (!selectAllKeys && LocalizationController.LOCALIZATION_GROUPS[locEl.Group] !=
                            LocalizationController.LOCALIZATION_GROUPS[selectedGroup])
                        {
                            continue;
                        }

                        EditorGUIUtility.labelWidth -= 100;
                        EditorGUILayout.BeginHorizontal("Box");

                        EditorGUILayout.LabelField(locEl.Key, GUILayout.MinWidth(100), GUILayout.MaxWidth(200));

                        EditorGUILayout.LabelField("Text:", GUILayout.Width(50));
                        locEl.Text = EditorGUILayout.TextField(locEl.Text,
                                                               GUILayout.MinWidth(100));


                        EditorGUILayout.EndHorizontal();
                        EditorGUIUtility.labelWidth += 100;
                    }
                }
                else
                {
                    GUILayout.Label("- - Empty - -", EditorStyles.boldLabel);
                }

                EditorGUILayout.EndScrollView();
            }

            GUILayout.EndVertical();

            #endregion

            EditorGUI.indentLevel--;
        }