Exemplo n.º 1
0
        public static void Render()
        {
            if (!WindowManager.LocaleEditor)
            {
                return;
            }

            ImGui.SetNextWindowSize(size, ImGuiCond.Once);

            if (!ImGui.Begin("Locale editor"))
            {
                ImGui.End();
                return;
            }

            if (ImGui.Combo("##locale", ref locale, aviableLocales, aviableLocales.Length))
            {
                Locale.Load(aviableLocales[locale]);
            }

            ImGui.SameLine();

            if (ImGui.Button("Save"))
            {
                Locale.Save();
            }

            ImGui.SameLine();

            if (ImGui.Button("New"))
            {
                ImGui.OpenPopup("New locale");
            }

            if (ImGui.BeginPopupModal("New locale"))
            {
                ImGui.SetItemDefaultFocus();
                var input  = ImGui.InputText("Name", ref newLocaleName, 3, ImGuiInputTextFlags.EnterReturnsTrue);
                var button = ImGui.Button("Create");

                ImGui.SameLine();

                if (ImGui.Button("Cancel"))
                {
                    ImGui.CloseCurrentPopup();
                    newLocaleName = "";
                }
                else
                {
                    if (input || button)
                    {
                        Locale.Current = newLocaleName;
                        Locale.Map     = new Dictionary <string, string>();
                        Locale.Loaded[newLocaleName] = Locale.Map;

                        var list = aviableLocales.ToList();
                        list.Add(newLocaleName);
                        aviableLocales = list.ToArray();
                        locale         = list.Count - 1;

                        newLocaleName = "";
                        ImGui.CloseCurrentPopup();
                    }
                }

                ImGui.EndPopup();
            }

            var notEng = aviableLocales[locale] != "en";

            if (notEng)
            {
                if (ImGui.Button("Clear"))
                {
                    Locale.Map.Clear();
                }

                ImGui.SameLine();

                if (ImGui.Button("Delete"))
                {
                    ImGui.OpenPopup("Delete?");
                }

                if (ImGui.BeginPopupModal("Delete?"))
                {
                    ImGui.Text("This operation can't be undone!");
                    ImGui.Text("Are you sure?");

                    if (ImGui.Button("Yes"))
                    {
                        ImGui.CloseCurrentPopup();
                        var list = aviableLocales.ToList();
                        list.Remove(Locale.Current);
                        aviableLocales = list.ToArray();
                        locale         = 0;
                        Locale.Delete();
                    }

                    ImGui.SameLine();
                    ImGui.SetItemDefaultFocus();

                    if (ImGui.Button("No"))
                    {
                        ImGui.CloseCurrentPopup();
                    }

                    ImGui.EndPopup();
                }
            }

            if (notEng)
            {
                ImGui.SameLine();

                if (ImGui.Button("Add en"))
                {
                    foreach (var t in Locale.Fallback)
                    {
                        if (!Locale.Map.ContainsKey(t.Key))
                        {
                            Locale.Map[t.Key] = t.Value;
                        }
                    }
                }
            }

            ImGui.Text(notEng ? $"{Locale.Map.Count} entries (en has {Locale.Fallback.Count})" : $"{Locale.Map.Count} entries");

            if (notEng)
            {
                ImGui.Checkbox("Show english", ref showEnglish);
            }

            ImGui.Separator();

            filter.Draw("");
            ImGui.SameLine();
            ImGui.Checkbox("By key", ref filterByKey);

            ImGui.Separator();
            var height = ImGui.GetStyle().ItemSpacing.Y + ImGui.GetFrameHeightWithSpacing() + 4;

            ImGui.BeginChild("ScrollingRegionLocale", new System.Numerics.Vector2(0, -height),
                             false);
            ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, spacer);

            var    i      = 0;
            string remove = null;

            foreach (var t in Locale.Map)
            {
                if (filter.PassFilter(filterByKey ? t.Key : t.Value))
                {
                    var key   = t.Key;
                    var value = t.Value;

                    ImGui.PushItemWidth(100);
                    ImGui.PushID($"{i}__key");

                    if (notEng && showEnglish && Locale.Fallback.TryGetValue(t.Key, out var en))
                    {
                        ImGui.InputText("", ref en, 64);
                    }
                    else
                    {
                        ImGui.InputText("", ref key, 64);
                    }

                    ImGui.PopID();
                    ImGui.PopItemWidth();
                    ImGui.SameLine();
                    ImGui.PushID($"{i}__value");
                    ImGui.InputText("", ref value, 256);

                    if (created == t.Key)
                    {
                        ImGui.SetKeyboardFocusHere(-1);
                        ImGui.SetScrollHereY(-1);
                        created = null;
                    }

                    if (key != t.Key || value != t.Value)
                    {
                        modified.Add(new ModifiedInfo {
                            OldKey     = t.Key,
                            OldValue   = t.Value,
                            Key        = key,
                            Value      = value,
                            KeyChanged = key != t.Key
                        });
                    }

                    ImGui.SameLine();

                    if (ImGui.SmallButton("-"))
                    {
                        remove = t.Key;
                    }
                }

                i++;
            }

            if (remove != null)
            {
                Locale.Map.Remove(remove);
            }

            ImGui.PopStyleVar();
            ImGui.EndChild();
            ImGui.Separator();

            var enter = ImGui.InputText("##newkey", ref newKey, 128, ImGuiInputTextFlags.EnterReturnsTrue);

            ImGui.SameLine();

            if ((enter || ImGui.Button("Add")) && newKey.Length > 0)
            {
                modified.Add(new ModifiedInfo {
                    Key   = newKey,
                    Value = newKey
                });

                created = newKey;
                newKey  = "";
            }

            if (modified.Count > 0)
            {
                foreach (var t in modified)
                {
                    if (t.OldKey != null)
                    {
                        Locale.Map.Remove(t.KeyChanged ? t.OldKey : Locale.Map.FirstOrDefault(m => m.Value == t.OldValue).Key);
                    }

                    if (t.Key.Length > 0)
                    {
                        Locale.Map[t.Key] = t.Value;
                    }
                }

                modified.Clear();
            }

            ImGui.End();
        }