Ejemplo n.º 1
0
        void LoadItem(TextAsset text, int lang, bool isPlatform, RuntimePlatform platform)
        {
            Item item = null;

            if (lang == 0 && !isPlatform)
            {
                if (mEditItemBase == null)
                {
                    mEditItemBase = new Item {
                        header = new ItemHeader {
                            text = text, language = lang, isPlatform = isPlatform
                        }
                    }
                }
                ;

                item = mEditItemBase;
            }
            else
            {
                //grab existing item. if not, create it
                bool itemFound = false;
                for (int i = 0; i < mEditItems.Count; i++)
                {
                    if (mEditItems[i].header.IsEqualTo(lang, isPlatform, platform))
                    {
                        item      = mEditItems[i];
                        itemFound = true;
                    }
                }

                if (!itemFound)
                {
                    item = new Item {
                        header = new ItemHeader {
                            text = text, language = lang, isPlatform = isPlatform, platform = platform
                        }
                    };
                    mEditItems.Add(item);
                }
            }

            LoadItem(item);
        }

        void SaveItem(Item item)
        {
            if (item.header.text)
            {
                //generate entries
                List <Localize.Entry> tableEntries = new List <Localize.Entry>();

                foreach (var pair in item.items)
                {
                    //if text is empty, do not save if we are not base or platform specific
                    if ((item.header.language == 0 && !item.header.isPlatform) || !string.IsNullOrEmpty(pair.Value.value))
                    {
                        Localize.Entry entry = new Localize.Entry {
                            key = pair.Key, text = pair.Value.value, param = item != mEditItemBase && pair.Value.paramIsRef ? null : pair.Value.param
                        };
                        tableEntries.Add(entry);
                    }
                }

                //save
                fastJSON.JSON.Parameters.UseExtensions = false;
                string output = fastJSON.JSON.ToNiceJSON(tableEntries, fastJSON.JSON.Parameters);

                string path = AssetDatabase.GetAssetPath(item.header.text);
                File.WriteAllText(path, output);
            }

            item.changed = false;
        }

        bool RemoveInvalidItems()
        {
            bool ret = false;

            for (int i = 0; i < mEditItems.Count; i++)
            {
                if (mEditItems[i].header.language == 0)
                {
                    if (mEditItems[i].header.isPlatform)
                    {
                        bool found = false;
                        for (int p = 0; p < mLocalizeTable[0].platforms.Length; p++)
                        {
                            if (mLocalizeTable[0].platforms[p].platform == mEditItems[i].header.platform)
                            {
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            ret = true;
                            mEditItems.RemoveAt(i);
                            i--;
                        }
                    }
                }
                else
                {
                    bool found = false;
                    for (int l = 0; l < mLocalizeTable.Length; l++)
                    {
                        if (mEditItems[i].header.language == l)
                        {
                            if (mEditItems[i].header.isPlatform)
                            {
                                for (int p = 0; p < mLocalizeTable[l].platforms.Length; p++)
                                {
                                    if (mLocalizeTable[l].platforms[p].platform == mEditItems[i].header.platform)
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                found = true;
                            }
                            break;
                        }
                    }
                    if (!found)
                    {
                        ret = true;
                        mEditItems.RemoveAt(i);
                        i--;
                    }
                }
            }

            return(ret);
        }

        void GenerateLocalizedUIItems()
        {
            int c = mEditItems.Count;

            mEditItemTexts = new string[c];
            mEditItemInds  = new int[c];
            for (int i = 0; i < c; i++)
            {
                mEditItemTexts[i] = mEditItems[i].header.isPlatform ?
                                    string.Format("{0} ({1})", mLocalizeTable[mEditItems[i].header.language].language, mEditItems[i].header.platform.ToString())
                              : mLocalizeTable[mEditItems[i].header.language].language;

                mEditItemInds[i] = i;
            }

            mEditItemInd = 0;
        }

        bool LoadItems()
        {
            bool ret = false;

            if (mEditItemLoads.Count > 0)
            {
                //load
                foreach (ItemHeader header in mEditItemLoads)
                {
                    //load actual data from file
                    if (header.text)
                    {
                        LoadItem(header.text, header.language, header.isPlatform, header.platform);
                    }
                    else   //remove
                    {
                        for (int i = 0; i < mEditItems.Count; i++)
                        {
                            if (mEditItems[i].header.IsEqualTo(header))
                            {
                                mEditItems.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }

                mEditItemLoads.Clear();

                ret = true;
            }


            return(ret);
        }

        void LoadAllItems()
        {
            mEditItemBase         = null;
            mEditItemBaseKeyTexts = null;
            mEditItemBaseKeyInds  = null;

            mEditItems.Clear();

            if (mLocalize)
            {
                if (mLocalizeTable != null)
                {
                    for (int i = 0; i < mLocalizeTable.Length; i++)
                    {
                        if (mLocalizeTable[i].file)
                        {
                            LoadItem(mLocalizeTable[i].file, i, false, RuntimePlatform.WindowsEditor);
                        }
                        if (mLocalizeTable[i].platforms != null)
                        {
                            for (int p = 0; p < mLocalizeTable[i].platforms.Length; p++)
                            {
                                if (mLocalizeTable[i].platforms[p].file)
                                {
                                    LoadItem(mLocalizeTable[i].platforms[p].file, i, true, mLocalizeTable[i].platforms[p].platform);
                                }
                            }
                        }
                    }
                }
            }
        }

        void AddNewBaseKey()
        {
            string newKey = mEditItemBase.GenerateNewItem(ref mEditItemsCurKeyGen);

            int newCount = mEditItemBaseKeyTexts.Length + 1;

            System.Array.Resize(ref mEditItemBaseKeyTexts, newCount);
            System.Array.Resize(ref mEditItemBaseKeyInds, newCount);

            mEditItemBaseKeyTexts[newCount - 1] = newKey;
            mEditItemBaseKeyInds[newCount - 1]  = newCount - 1;

            System.Array.Sort(mEditItemBaseKeyTexts);

            mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, newKey);
        }

        void DrawItemData(string controlName, Item item, Data dat, ref string paramLastText)
        {
            //content
            GUILayout.Label("Value");

            GUI.SetNextControlName(controlName);

            if (dat != null)
            {
                string editText = EditorGUILayout.TextArea(dat.value, GUILayout.MinHeight(valueTextAreaMinHeight), GUILayout.MaxHeight(valueTextAreaMaxHeight));
                if (editText != dat.value)
                {
                    dat.value = editText;

                    item.changed = true;
                }

                //params
                GUILayout.BeginVertical(GUI.skin.box);

                item.paramFoldout = EditorGUILayout.Foldout(item.paramFoldout, "Params");
                if (item.paramFoldout)
                {
                    bool isRef;

                    if (item != mEditItemBase)
                    {
                        isRef = GUILayout.Toggle(dat.paramIsRef, "Use Base");
                        if (dat.paramIsRef != isRef)
                        {
                            dat.paramIsRef = isRef;
                            item.changed   = true;
                        }
                    }
                    else
                    {
                        isRef = false;
                    }

                    if (!isRef)
                    {
                        if (EditorExt.Utility.DrawStringArrayAlt(ref dat.param, ref paramLastText))
                        {
                            item.changed = true;
                        }
                    }
                }

                GUILayout.EndVertical();
            }
            else
            {
                EditorGUILayout.TextArea("", GUILayout.MinHeight(valueTextAreaMinHeight), GUILayout.MaxHeight(valueTextAreaMaxHeight));

                GUILayout.BeginVertical(GUI.skin.box);

                EditorGUILayout.Foldout(false, "Params");

                GUILayout.EndVertical();
            }
        }

        void AddNewItem(string key, Data copyFrom)
        {
            mEditItemBase.AddNewItem(key, copyFrom);

            int newCount = mEditItemBaseKeyTexts.Length + 1;

            System.Array.Resize(ref mEditItemBaseKeyTexts, newCount);
            System.Array.Resize(ref mEditItemBaseKeyInds, newCount);

            mEditItemBaseKeyTexts[newCount - 1] = key;
            mEditItemBaseKeyInds[newCount - 1]  = newCount - 1;

            System.Array.Sort(mEditItemBaseKeyTexts);

            mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, key);
        }

        void DoEditItems()
        {
            if (mLocalizeTable == null || mLocalizeTable.Length == 0 || mLocalizeTable[0].file == null || mEditItemBase == null)
            {
                GUILayout.Label("Base File is not set!");
                return;
            }

            mEditItemsScroll = GUILayout.BeginScrollView(mEditItemsScroll);

            string baseCurKey = null;

            //display base
            if (mEditItemBaseKeyTexts.Length > 0)
            {
                baseCurKey = mEditItemBaseKeyTexts[mEditItemBaseKeyInd];

                Data baseCurDat = mEditItemBase.items[baseCurKey];

                //key
                GUILayout.Label("Key");

                if (mEditItemsKeyEdit)
                {
                    GUILayout.BeginHorizontal();

                    GUI.SetNextControlName(editKeyControl);

                    mEditItemsKeyText = EditorGUILayout.TextField(mEditItemsKeyText);

                    GUI.enabled = !string.IsNullOrEmpty(mEditItemsKeyText) && !mEditItemBase.items.ContainsKey(mEditItemsKeyText);

                    //change key
                    if (EditorExt.Utility.DrawSimpleButton("√", "Accept"))
                    {
                        if (mEditItemsKeyEditIsNew)
                        {
                            AddNewItem(mEditItemsKeyText, null);
                        }
                        else
                        {
                            //remove old key
                            mEditItemBase.items.Remove(baseCurKey);

                            //regenerate keys
                            mEditItemBaseKeyTexts[mEditItemBaseKeyInd] = mEditItemsKeyText;
                            System.Array.Sort(mEditItemBaseKeyTexts);
                            mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, mEditItemsKeyText);

                            //re-add to items
                            mEditItemBase.items.Add(mEditItemsKeyText, baseCurDat);

                            mEditItemBase.changed = true;
                        }

                        mEditItemsKeyEdit      = false;
                        mEditItemsKeyEditIsNew = false;

                        EditorGUI.FocusTextInControl(editItemControl1);
                    }

                    GUI.enabled = true;

                    //cancel
                    if (EditorExt.Utility.DrawSimpleButton("X", "Cancel"))
                    {
                        mEditItemsKeyEdit = false;

                        EditorGUI.FocusTextInControl(editItemControl1);
                    }

                    GUILayout.EndHorizontal();
                }
                else
                {
                    GUILayout.BeginHorizontal();

                    int ind = EditorGUILayout.IntPopup(mEditItemBaseKeyInd, mEditItemBaseKeyTexts, mEditItemBaseKeyInds);
                    if (mEditItemBaseKeyInd != ind)
                    {
                        mEditItemBaseKeyInd = Mathf.Clamp(ind, 0, mEditItemBaseKeyTexts.Length - 1);
                        mEditItemParamText  = "";
                    }

                    //edit key
                    if (EditorExt.Utility.DrawSimpleButton("E", "Edit key text"))
                    {
                        mEditItemsKeyText = mEditItemBaseKeyTexts[mEditItemBaseKeyInd];
                        mEditItemsKeyEdit = true;
                        EditorGUI.FocusTextInControl(editKeyControl);
                    }

                    //duplicate
                    if (EditorExt.Utility.DrawSimpleButton("D", "Duplicate"))
                    {
                        //generate unique key
                        string key = mEditItemBaseKeyTexts[mEditItemBaseKeyInd];

                        string newKey;
                        int    dupInd = 1;
                        do
                        {
                            newKey = string.Format("{0}({1})", key, dupInd++);
                        } while(mEditItemBase.items.ContainsKey(newKey));

                        AddNewItem(newKey, mEditItemBase.items[key]);
                    }

                    //add
                    if (EditorExt.Utility.DrawAddButton())
                    {
                        mEditItemsKeyText      = "";
                        mEditItemsKeyEdit      = true;
                        mEditItemsKeyEditIsNew = true;
                        EditorGUI.FocusTextInControl(editKeyControl);
                    }

                    //remove
                    if (EditorExt.Utility.DrawRemoveButton())
                    {
                        M8.ArrayUtil.RemoveAt(ref mEditItemBaseKeyTexts, mEditItemBaseKeyInd);
                        M8.ArrayUtil.RemoveAt(ref mEditItemBaseKeyInds, mEditItemBaseKeyInd);

                        for (int i = mEditItemBaseKeyInd; i < mEditItemBaseKeyInds.Length; i++)
                        {
                            mEditItemBaseKeyInds[i] = i;
                        }

                        if (mEditItemBaseKeyInd > 0 && mEditItemBaseKeyInd >= mEditItemBaseKeyInds.Length)
                        {
                            mEditItemBaseKeyInd--;
                        }

                        mEditItemBase.items.Remove(baseCurKey);

                        mEditItemBase.changed = true;
                    }

                    GUILayout.EndHorizontal();
                }

                GUI.enabled = !mEditItemsKeyEdit;

                DrawItemData(editItemControl1, mEditItemBase, baseCurDat, ref mEditItemParamText);

                GUI.enabled = true;
            }
            else if (GUILayout.Button("Add new item"))
            {
                AddNewBaseKey();

                mEditItemsKeyText = "";
                mEditItemsKeyEdit = true;
                EditorGUI.FocusTextInControl(editKeyControl);
            }

            //Localize

            mEditItemsLocalizeFoldout = EditorGUILayout.Foldout(mEditItemsLocalizeFoldout, "Localize");
            if (mEditItemsLocalizeFoldout && mEditItems.Count > 0)
            {
                GUI.enabled = !(mEditItemsKeyEdit || string.IsNullOrEmpty(baseCurKey));

                if (mEditItemInd >= mEditItemTexts.Length)
                {
                    mEditItemInd = 0;
                }

                int ind = EditorGUILayout.IntPopup(mEditItemInd, mEditItemTexts, mEditItemInds);
                if (mEditItemInd != ind)
                {
                    mEditItemInd = Mathf.Clamp(ind, 0, mEditItemTexts.Length - 1);
                    mEditItemLocalizeParamText = "";
                }

                Item localizeItem = mEditItems[mEditItemInd];

                Data localizeDat = null;

                if (GUI.enabled)
                {
                    if (!localizeItem.items.TryGetValue(baseCurKey, out localizeDat))
                    {
                        localizeDat = new Data {
                            value = "", param = null, paramIsRef = true
                        };
                        localizeItem.items.Add(baseCurKey, localizeDat);
                    }
                }

                DrawItemData(editItemControl2, localizeItem, localizeDat, ref mEditItemLocalizeParamText);

                GUI.enabled = true;
            }

            GUILayout.EndScrollView();

            EditorExt.Utility.DrawSeparator();

            //save, revert
            bool hasChanges;

            if (mEditItemsKeyEdit)
            {
                hasChanges = false;
            }
            else
            {
                hasChanges = mEditItemBase.changed;
                if (!hasChanges)
                {
                    foreach (Item item in mEditItems)
                    {
                        hasChanges = item.changed;
                        if (hasChanges)
                        {
                            break;
                        }
                    }
                }
            }

            GUI.enabled = hasChanges;

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            GUI.backgroundColor = Color.green;
            if (GUILayout.Button("Save", GUILayout.Width(100f)))
            {
                if (mEditItemBase.changed)
                {
                    SaveItem(mEditItemBase);
                }

                foreach (Item item in mEditItems)
                {
                    if (item.changed)
                    {
                        SaveItem(item);
                    }
                }

                AssetDatabase.Refresh();
            }

            GUILayout.Space(8f);

            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Revert", GUILayout.Width(100f)))
            {
                if (mEditItemBase.changed)
                {
                    LoadItem(mEditItemBase);
                }


                foreach (Item item in mEditItems)
                {
                    if (item.changed)
                    {
                        LoadItem(item);
                    }
                }

                GUIUtility.keyboardControl = 0;
                GUIUtility.hotControl      = 0;
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUI.enabled = true;
        }

        void DoSelectComponent()
        {
            GUILayout.Label("Grab from a core prefab.");
            Localize localize = EditorGUILayout.ObjectField(mLocalize, typeof(Localize), false) as Localize;

            if (mLocalize != localize)
            {
                SetLocalize(localize, true);

                LoadAllItems();
                GenerateBaseKeyItems();

                Repaint();
            }
        }
    }