ShowWindow() public static method

Returns true if the window should show, returns false if not. If false, it will draw an editor label that says the window is unavailable
public static ShowWindow ( ) : bool
return bool
Beispiel #1
0
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            GUILayout.Label("Settings", EditorStyles.boldLabel);
            if (!LocFileUtility.CheckIfRootLanguageFileExists())
            {
                if (GUILayout.Button("Create New Localization System"))
                {
                    LocFileUtility.CreateRootResourceFile();
                }
            }
            else
            {
                undoManager.CheckUndo();
                if (GUILayout.Button("Refresh"))
                {
                    LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
                }
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Microsoft Translator Settings", EditorStyles.boldLabel, GUILayout.Width(200));
                if (microsoftTranslator.IsInitialized)
                {
                    GUILayout.Label(" - Authenticated!", EditorStyles.miniLabel);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Client ID:", GUILayout.Width(70));
                mtCliendID = EditorGUILayout.TextField(mtCliendID);
                GUILayout.Label("Client Secret:", GUILayout.Width(100));
                mtCliendSecret = EditorGUILayout.TextField(mtCliendSecret);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Save", GUILayout.Width(50)))
                {
                    SaveMicrosoftTranslatorSettings();
                    if (!microsoftTranslator.IsInitialized)
                    {
                        microsoftTranslator.GetAccessToken(mtCliendID, mtCliendSecret);
                    }
                }
                if (!microsoftTranslator.IsInitialized)
                {
                    if (GUILayout.Button("Authenticate!", GUILayout.Width(150)))
                    {
                        microsoftTranslator.GetAccessToken(mtCliendID, mtCliendSecret);
                    }
                }
                keepTranslatorAuthenticated = EditorGUILayout.Toggle("Keep Authenticated", keepTranslatorAuthenticated);
                EditorGUILayout.EndHorizontal();

                GUILayout.Label("Edit Root Language File", EditorStyles.boldLabel);
                if (GUILayout.Button("Edit"))
                {
                    ShowRootEditWindow(LocFileUtility.LoadParsedLanguageFile(null));
                }

                GUILayout.Label("Create new language", EditorStyles.boldLabel);
                chosenCreateNewCultureValue = EditorGUILayout.Popup(chosenCreateNewCultureValue, notAvailableLanguagesEnglishNames.ToArray());
                if (GUILayout.Button("Create Language"))
                {
                    CreateNewLanguage(notAvailableLanguagesEnglishNames[chosenCreateNewCultureValue]);
                }

                GUILayout.Label("Translate Languages", EditorStyles.boldLabel);
                //Start the scroll view
                scrollPosition = GUILayout.BeginScrollView(scrollPosition);
                bool languageDeleted = false;
                foreach (CultureInfo info in availableLanguages)
                {
                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button(info.EnglishName + " - " + info.Name))
                    {
                        //Open language edit window
                        ShowTranslateWindow(info);
                    }
                    if (GUILayout.Button("Delete", GUILayout.Width(60)))
                    {
                        LocFileUtility.DeleteLanguage(info);
                        languageDeleted = true;
                        break;
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (languageDeleted)                //Refresh
                {
                    LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
                }

                //End the scroll view
                GUILayout.EndScrollView();
                undoManager.CheckDirty();
            }
        }
    }
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            if (smartLocWindow == null || thisCultureInfo == null)
            {
                this.Close();                // Temp fix
            }
            else if (!rootFileChanged)
            {
                undoManager.CheckUndo();
                GUILayout.Label("Language - " + thisLanguage, EditorStyles.boldLabel, GUILayout.Width(200));

                //Copy all the Base Values
                GUILayout.Label("If you want to copy all the base values from the root file", EditorStyles.miniLabel);
                if (GUILayout.Button("Copy All Base Values", GUILayout.Width(150)))
                {
                    int count = 0;
                    foreach (KeyValuePair <string, LocalizedObject> rootValue in rootValues)
                    {
                        if (rootValue.Value.ObjectType == LocalizedObjectType.STRING)
                        {
                            thisLanguageValues[count].changedValue.TextValue = rootValue.Value.TextValue;
                        }
                        count++;
                    }
                }

                GUILayout.Label("Microsoft Translator", EditorStyles.boldLabel);
                if (!smartLocWindow.MicrosoftTranslator.IsInitialized)
                {
                    GUILayout.Label("Microsoft Translator is not authenticated", EditorStyles.miniLabel);
                }
                else
                {
                    if (canLanguageBeTranslated)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Label("Translate From:", GUILayout.Width(100));
                        translateFromLanguageValue = EditorGUILayout.Popup(translateFromLanguageValue, availableTranslateLangEnglishNames);
                        EditorGUILayout.EndHorizontal();

                        if (oldTranslateFromLanguageValue != translateFromLanguageValue)
                        {
                            oldTranslateFromLanguageValue = translateFromLanguageValue;
                            //The value have been changed, load the language file of the other language that you want to translate from
                            //I load it like this to show the translate buttons only on the ones that can be translated i.e some values
                            //in the "from" language could be an empty string - no use in translating that
                            if (translateFromDictionary != null)
                            {
                                translateFromDictionary.Clear();
                                translateFromDictionary = null;
                            }
                            if (translateFromLanguageValue != 0)
                            {
                                string englishName = availableTranslateLangEnglishNames[translateFromLanguageValue];
                                foreach (CultureInfo info in availableTranslateFromLanguages)
                                {
                                    if (info.EnglishName == englishName)
                                    {
                                        translateFromDictionary = LocFileUtility.LoadParsedLanguageFile(info.Name);
                                        translateFromLanguage   = info.Name;
                                        break;
                                    }
                                }
                            }
                        }

                        //Translate all the available keys
                        if (translateFromLanguageValue != 0 && GUILayout.Button("Translate all text", GUILayout.Width(150)))
                        {
                            List <string> keys             = new List <string>();
                            List <string> textsToTranslate = new List <string>();
                            int           characterCount   = 0;
                            foreach (KeyValuePair <string, LocalizedObject> stringPair in translateFromDictionary)
                            {
                                if (stringPair.Value.ObjectType == LocalizedObjectType.STRING &&
                                    stringPair.Value.TextValue != null && stringPair.Value.TextValue != "")
                                {
                                    int textLength = stringPair.Value.TextValue.Length;
                                    //Microsoft translator only support translations below 1000 character
                                    //I'll cap it to 700, which gives 300 extra if the translated value is longer
                                    if (textLength < 700)
                                    {
                                        characterCount += textLength;
                                        keys.Add(stringPair.Key);
                                        textsToTranslate.Add(stringPair.Value.TextValue);
                                    }
                                }

                                //Microsoft translator only support translations with 100 array values and a total
                                // character cap of 10000,
                                // I'll cap it to 7000, which gives 3000 extra if the translated value is longer
                                if (keys.Count >= 99 || characterCount >= 7000)
                                {
                                    //Create a new reference to the list with keys, because we need it non-cleared in the callback
                                    List <string> keysToSend = new List <string>();
                                    keysToSend.AddRange(keysToSend.ToArray());

                                    //Send the values
                                    smartLocWindow.MicrosoftTranslator.TranslateArray(textsToTranslate, translateFromLanguage,
                                                                                      thisCultureInfo.Name, keysToSend, new TranslateCompleteArrayCallback(TranslatedTextArrayCompleted));

                                    //Reset values
                                    characterCount = 0;
                                    keys.Clear();
                                    textsToTranslate.Clear();
                                }
                            }
                            if (keys.Count != 0)
                            {
                                smartLocWindow.MicrosoftTranslator.TranslateArray(textsToTranslate, translateFromLanguage,
                                                                                  thisCultureInfo.Name, keys, new TranslateCompleteArrayCallback(TranslatedTextArrayCompleted));

                                //Reset values
                                characterCount = 0;
                                keys.Clear();
                                textsToTranslate.Clear();
                            }
                        }
                    }
                    else
                    {
                        GUILayout.Label(thisCultureInfo.EnglishName + " is not available for translation", EditorStyles.miniLabel);
                    }
                }

                GUILayout.Label("Language Values", EditorStyles.boldLabel);
                //Search field
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Search for Key:", GUILayout.Width(100));
                searchText = EditorGUILayout.TextField(searchText);
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Key", EditorStyles.boldLabel, GUILayout.Width(120));
                GUILayout.Label("Base Value", EditorStyles.boldLabel, GUILayout.Width(120));
                GUILayout.Label("Copy Base", EditorStyles.miniLabel, GUILayout.Width(70));
                if (canLanguageBeTranslated)
                {
                    //TODO::Change to small picture
                    GUILayout.Label("T", EditorStyles.miniLabel, GUILayout.Width(20));
                }
                GUILayout.Label(thisLanguage + " Value", EditorStyles.boldLabel);
                EditorGUILayout.EndHorizontal();

                //Check if the user searched for a value
                bool didSearch = false;
                if (searchText != "")
                {
                    didSearch = true;
                    GUILayout.Label("Search Results - \"" + searchText + "\":", EditorStyles.boldLabel);
                }

                //Start the scroll view
                scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
                int iterationCount = 0;
                foreach (KeyValuePair <string, LocalizedObject> rootValue in rootValues)
                {
                    if (didSearch)
                    {
                        //If the name of the key doesn't contain the search value, then skip a value
                        if (!rootValue.Key.ToLower().Contains(searchText.ToLower()))
                        {
                            continue;
                        }
                    }

                    if (rootValue.Value.ObjectType == LocalizedObjectType.STRING)
                    {
                        OnTextFieldGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.AUDIO)
                    {
                        OnAudioGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.GAME_OBJECT)
                    {
                        OnGameObjectGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.TEXTURE)
                    {
                        OnTextureGUI(rootValue, iterationCount);
                    }

                    iterationCount++;
                }
                //End the scroll view
                EditorGUILayout.EndScrollView();

                if (guiChanged)
                {
                    GUILayout.Label("- You have unsaved changes", EditorStyles.miniLabel);
                }

                //If any changes to the gui is made
                if (GUI.changed)
                {
                    guiChanged = true;
                }

                GUILayout.Label("Save Changes", EditorStyles.boldLabel);
                GUILayout.Label("Remember to always press save when you have changed values", EditorStyles.miniLabel);
                if (GUILayout.Button("Save/Rebuild"))
                {
                    //Copy everything into a dictionary
                    Dictionary <string, string> newLanguageValues = new Dictionary <string, string>();
                    foreach (SerializableLocalizationObjectPair objectPair in this.thisLanguageValues)
                    {
                        if (objectPair.changedValue.ObjectType == LocalizedObjectType.STRING)
                        {
                            newLanguageValues.Add(objectPair.changedValue.GetFullKey(objectPair.keyValue), objectPair.changedValue.TextValue);
                        }
                        else
                        {
                            //Delete the file in case there was a file there previously
                            LocFileUtility.DeleteFileFromResources(objectPair.changedValue.GetFullKey(objectPair.keyValue), thisCultureInfo);

                            //Store the path to the file
                            string pathValue = LocFileUtility.CopyFileIntoResources(objectPair, thisCultureInfo);
                            newLanguageValues.Add(objectPair.changedValue.GetFullKey(objectPair.keyValue), pathValue);
                        }
                    }
                    LocFileUtility.SaveLanguageFile(newLanguageValues, LocFileUtility.rootLanguageFilePath + "." + thisCultureInfo.Name + LocFileUtility.resXFileEnding);
                    guiChanged = false;
                }

                undoManager.CheckDirty();
            }
            else
            {
                //The root file did change, which means that you have to reload. A key might have changed
                //We can't have language files with different keys
                GUILayout.Label("The root file might have changed", EditorStyles.boldLabel);
                GUILayout.Label("The root file did save, which means that you have to reload. A key might have changed.", EditorStyles.miniLabel);
                GUILayout.Label("You can't have language files with different keys", EditorStyles.miniLabel);
                if (GUILayout.Button("Reload Language File"))
                {
                    InitializeLanguage(thisCultureInfo, LocFileUtility.LoadParsedLanguageFile(null), LocFileUtility.LoadParsedLanguageFile(thisCultureInfo.Name));
                }
            }
        }
    }
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            undoManager.CheckUndo();
            GUILayout.Label("Root Values", EditorStyles.boldLabel);

            //Search field
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Search for Key:", GUILayout.Width(100));
            searchText = EditorGUILayout.TextField(searchText);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Key Type", GUILayout.Width(100));
            GUILayout.Label("Key");
            GUILayout.Label("Base Value/Comment");
            GUILayout.Label("Delete", EditorStyles.miniLabel, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            //Create the scroll view
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

            //Delete key information
            bool deleteKey     = false;
            int  indexToDelete = 0;

            //Check if the user searched for a value
            bool didSearch = false;
            if (searchText != "")
            {
                didSearch = true;
                GUILayout.Label("Search Results - \"" + searchText + "\":", EditorStyles.boldLabel);
            }

            for (int i = 0; i < changedRootKeys.Count; i++)
            {
                SerializableStringPair rootValue = changedRootKeys[i];
                if (didSearch)
                {
                    //If the name of the key doesn't contain the search value, then skip a value
                    if (!rootValue.originalValue.ToLower().Contains(searchText.ToLower()))
                    {
                        continue;
                    }
                }
                EditorGUILayout.BeginHorizontal();

                //Popup of all the different key values
                changedRootValues[i].changedValue.ObjectType = (LocalizedObjectType)EditorGUILayout.Popup((int)changedRootValues[i].changedValue.ObjectType,
                                                                                                          keyTypes, GUILayout.Width(100));

                rootValue.changedValue = EditorGUILayout.TextField(rootValue.changedValue);
                changedRootValues[i].changedValue.TextValue = EditorGUILayout.TextField(changedRootValues[i].changedValue.TextValue);
                if (GUILayout.Button("Delete", GUILayout.Width(50)))
                {
                    deleteKey     = true;
                    indexToDelete = i;
                }

                EditorGUILayout.EndHorizontal();
            }

            //End the scrollview
            EditorGUILayout.EndScrollView();

            if (GUILayout.Button("Add New Key"))
            {
                AddNewKey();
            }

            //Delete the key outside the foreach loop
            if (deleteKey)
            {
                DeleteKey(indexToDelete);
            }

            if (guiChanged)
            {
                GUILayout.Label("- You have unsaved changes", EditorStyles.miniLabel);
            }

            //If any changes to the gui is made
            if (GUI.changed)
            {
                guiChanged = true;
            }

            GUILayout.Label("Save Changes", EditorStyles.boldLabel);
            GUILayout.Label("Remember to always press save when you have changed values", EditorStyles.miniLabel);
            if (GUILayout.Button("Save Root Language File"))
            {
                Dictionary <string, string> changeNewRootKeys   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValues = new Dictionary <string, string>();

                for (int i = 0; i < changedRootKeys.Count; i++)
                {
                    SerializableStringPair             rootKey   = changedRootKeys[i];
                    SerializableLocalizationObjectPair rootValue = changedRootValues[i];
                    //Check for possible duplicates and rename them
                    string newKeyValue = LocFileUtility.AddNewKeyPersistent(changeNewRootKeys, rootKey.originalValue, rootValue.changedValue.GetFullKey(rootKey.changedValue));

                    //Check for possible duplicates and rename them(same as above)
                    LocFileUtility.AddNewKeyPersistent(changeNewRootValues, newKeyValue, rootValue.changedValue.TextValue);
                }

                //Add the full values before saving
                Dictionary <string, string> changeNewRootKeysToSave   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValuesToSave = new Dictionary <string, string>();

                foreach (KeyValuePair <string, string> rootKey in changeNewRootKeys)
                {
                    LocalizedObject thisLocalizedObject = parsedRootValues[rootKey.Key];
                    changeNewRootKeysToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), rootKey.Value);
                    changeNewRootValuesToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), changeNewRootValues[rootKey.Key]);
                }

                LocFileUtility.SaveRootLanguageFile(changeNewRootKeysToSave, changeNewRootValuesToSave);

                //Fire the root language changed event
                if (OnRootFileChanged != null)
                {
                    OnRootFileChanged();
                }

                //Reload the window(in case of duplicate keys)
                SetRootValues(LocFileUtility.LoadParsedLanguageFile(null));
                guiChanged = false;
            }

            undoManager.CheckDirty();
        }
    }