Beispiel #1
0
        public void OnGUI(Rect r)
        {
            var   line    = EditorGUIUtility.singleLineHeight + 2;
            var   cHeight = EditorGUIUtility.singleLineHeight;
            float b       = r.y + 5;
            int   i       = 0;
            LocalizationUnitHandle handleToRemove = null;

            foreach (var h in Handles)
            {
                GUI.Label(new Rect(r.x, b + line * i * 2, 150f, cHeight), h.Pack.Name);

                var oldValue = h.GetText(Key);
                var newValue =
                    EditorGUI.TextArea(new Rect(r.x + 150f, b + line * i * 2, r.width - 150f - 60f, cHeight * 2), oldValue);
                if (oldValue != newValue)
                {
                    h.SetText(Key, newValue);
                    h.SaveUnit();
                }

                if (GUI.Button(new Rect(r.x + r.width - 60f, b + line * i * 2, 60f, cHeight * 2), "Remove"))
                {
                    handleToRemove = h;
                }

                i++;
            }


            if (handleToRemove != null)
            {
                handleToRemove.RemoveKey(Key);
                handleToRemove.SaveUnit();
                Refresh();
            }

            int selected = EditorGUI.Popup(new Rect(r.x, b + line * i * 2, 100f, cHeight), 0,
                                           new string[] { "Add localization" }.Concat(
                                               UnaddedPacks.Select(_ => $"{_.Name} ({_.Guid})")).ToArray())
            ;

            if (selected != 0)
            {
                var packToAdd = UnaddedPacks[selected - 1];

                packToAdd.RequestUnitHandle(UnitPath, true).AddKey(Key);

                Refresh();
            }
        }
Beispiel #2
0
        private void Refresh()
        {
            if (!preventAutoGUI)
            {
                Clear();

                if (Localizer.Instance == null)
                {
                    Add(new Label("Localizer is not loaded. Try refresh."));
                    return;
                }
            }

            foreach (var handle in Handles)
            {
                handle.Dispose();
            }

            Handles.Clear();
            UnaddedPacks.Clear();

            if (!string.IsNullOrEmpty(UnitPath) && !string.IsNullOrEmpty(Key))
            {
                foreach (var pack in Localizer.Instance.Packs)
                {
                    var handle = pack.RequestUnitHandle(UnitPath);
                    if (!handle.IsEmpty())
                    {
                        if (handle.TryGetText(Key, out string value))
                        {
                            Handles.Add(handle);
                            continue;
                        }
                    }

                    UnaddedPacks.Add(pack);
                    handle.Dispose();
                }
            }

            if (!preventAutoGUI)
            {
                ImguiContainer = new IMGUIContainer(() =>
                {
                    var r    = this.ImguiContainer.contentRect;
                    r.width -= 20f;
                    r.x     += 20f;
                    OnGUI(r);
                    this.ImguiContainer.style.height = GetPropertyHeight();
                });
                Add(ImguiContainer);
            }

            return;

            //以下、UIElements用コード

            /*
             *
             * var visualTree =
             *  AssetDatabase.LoadAssetAtPath(
             *      "Assets/Ruccho/ORLL/Unity/Scripts/Editor/LocalizedTextField/LocalizedTextField.uxml",
             *      typeof(VisualTreeAsset)) as VisualTreeAsset;
             * VisualElement uxml = visualTree.CloneTree();
             * this.Add(uxml);
             *
             * foreach (var handle in Handles)
             * {
             *  AddLocalizationElement(handle, Key);
             * }
             *
             *
             * refreshButton = this.Q<Button>("refresh");
             * refreshButton.clickable.clickedWithEventInfo += RefreshButton_Clicked;
             *
             * var addButton = this.Q<Button>("add");
             * var m = new ContextualMenuManipulator((e) =>
             * {
             *  if (!string.IsNullOrEmpty(UnitPath) && !string.IsNullOrEmpty(Key))
             *      foreach (var pack in Localizer.Instance.Packs)
             *      {
             *          var handle = pack.RequestUnitHandle(UnitPath);
             *          if (!handle.IsEmpty())
             *          {
             *              if (handle.TryGetText(Key, out string value))
             *              {
             *                  handle.Dispose();
             *                  continue;
             *              }
             *          }
             *
             *          handle.Dispose();
             *          e.menu.AppendAction($"{pack.Name} ({pack.Guid})", a =>
             *              {
             *                  pack.RequestUnitHandle(UnitPath, true).AddKey(Key);
             *
             *                  Refresh();
             *              },
             *              a => DropdownMenuAction.AlwaysEnabled(a));
             *      }
             * });
             * m.target = addButton;
             * m.activators.Clear();
             *
             * m.activators.Add(new ManipulatorActivationFilter() {button = MouseButton.LeftMouse, clickCount = 1});
             */
        }