From 805f862af6ab5f56df48aeb30dd9ca6d20ce47e7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 29 Sep 2021 10:06:44 +0200 Subject: [PATCH] Add copy/paste for group panel with multiple custom editors nested inside --- Source/Editor/CustomEditors/CustomEditor.cs | 4 - .../CustomEditors/Editors/GenericEditor.cs | 139 ++++++++++++++++++ 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/Source/Editor/CustomEditors/CustomEditor.cs b/Source/Editor/CustomEditors/CustomEditor.cs index e00971cbf..dd921b4ee 100644 --- a/Source/Editor/CustomEditors/CustomEditor.cs +++ b/Source/Editor/CustomEditors/CustomEditor.cs @@ -520,8 +520,6 @@ namespace FlaxEditor.CustomEditors /// public void Copy() { - Editor.Log("Copy custom editor value"); - try { string text; @@ -658,8 +656,6 @@ namespace FlaxEditor.CustomEditors /// public void Paste() { - Editor.Log("Paste custom editor value"); - try { if (GetClipboardObject(out var obj, true)) diff --git a/Source/Editor/CustomEditors/Editors/GenericEditor.cs b/Source/Editor/CustomEditors/Editors/GenericEditor.cs index ca4ae5dfc..395c9529a 100644 --- a/Source/Editor/CustomEditors/Editors/GenericEditor.cs +++ b/Source/Editor/CustomEditors/Editors/GenericEditor.cs @@ -4,12 +4,14 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Text; using FlaxEditor.CustomEditors.Elements; using FlaxEditor.CustomEditors.GUI; using FlaxEditor.GUI.ContextMenu; using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.GUI; +using FlaxEngine.Json; namespace FlaxEditor.CustomEditors.Editors { @@ -365,6 +367,137 @@ namespace FlaxEditor.CustomEditors.Editors OnGroupPanelRevert(child as LayoutElementsContainer, toDefault); } + private void OnGroupPanelCopy(LayoutElementsContainer layout) + { + if (layout.Editors.Count == 1) + { + layout.Editors[0].Copy(); + } + else if (layout.Editors.Count != 0) + { + var data = new string[layout.Editors.Count]; + var sb = new StringBuilder(); + sb.Append("[\n"); + for (var i = 0; i < layout.Editors.Count; i++) + { + layout.Editors[i].Copy(); + if (i != 0) + sb.Append(",\n"); + sb.Append(Clipboard.Text); + data[i] = Clipboard.Text; + } + sb.Append("\n]"); + Clipboard.Text = sb.ToString(); + Clipboard.Text = JsonSerializer.Serialize(data); + } + else if (layout.Children.Any(x => x is LayoutElementsContainer)) + { + foreach (var child in layout.Children) + { + if (child is LayoutElementsContainer childContainer) + { + OnGroupPanelCopy(childContainer); + break; + } + } + } + } + + private bool OnGroupPanelCanCopy(LayoutElementsContainer layout) + { + return layout.Editors.Count != 0 || layout.Children.Any(x => x is LayoutElementsContainer); + } + + private void OnGroupPanelPaste(LayoutElementsContainer layout) + { + if (layout.Editors.Count == 1) + { + layout.Editors[0].Paste(); + } + else if (layout.Editors.Count != 0) + { + var sb = Clipboard.Text; + if (!string.IsNullOrEmpty(sb)) + { + try + { + var data = JsonSerializer.Deserialize(sb); + if (data == null || data.Length != layout.Editors.Count) + return; + for (var i = 0; i < layout.Editors.Count; i++) + { + Clipboard.Text = data[i]; + layout.Editors[i].Paste(); + } + } + catch + { + } + finally + { + Clipboard.Text = sb; + } + } + } + else if (layout.Children.Any(x => x is LayoutElementsContainer)) + { + foreach (var child in layout.Children) + { + if (child is LayoutElementsContainer childContainer) + { + OnGroupPanelPaste(childContainer); + break; + } + } + } + } + + private bool OnGroupPanelCanPaste(LayoutElementsContainer layout) + { + if (layout.Editors.Count == 1) + { + return layout.Editors[0].CanPaste; + } + if (layout.Editors.Count != 0) + { + var sb = Clipboard.Text; + if (!string.IsNullOrEmpty(sb)) + { + try + { + var data = JsonSerializer.Deserialize(sb); + if (data == null || data.Length != layout.Editors.Count) + return false; + for (var i = 0; i < layout.Editors.Count; i++) + { + Clipboard.Text = data[i]; + if (!layout.Editors[i].CanPaste) + return false; + } + return true; + } + catch + { + return false; + } + finally + { + Clipboard.Text = sb; + } + } + return false; + } + if (layout.Children.Any(x => x is LayoutElementsContainer)) + { + foreach (var child in layout.Children) + { + if (child is LayoutElementsContainer childContainer) + return OnGroupPanelCanPaste(childContainer); + } + } + return false; + } + private void OnGroupPanelMouseButtonRightClicked(DropPanel groupPanel, Vector2 location) { var group = (GroupElement)groupPanel.Tag; @@ -376,6 +509,12 @@ namespace FlaxEditor.CustomEditors.Editors revertToPrefab.Enabled = canRevertReference; var resetToDefault = menu.AddButton("Reset to default", () => OnGroupPanelRevert(group, true)); resetToDefault.Enabled = canRevertDefault; + menu.AddSeparator(); + var copy = menu.AddButton("Copy", () => OnGroupPanelCopy(group)); + copy.Enabled = OnGroupPanelCanCopy(group); + var paste = menu.AddButton("Paste", () => OnGroupPanelPaste(group)); + paste.Enabled = OnGroupPanelCanPaste(group); + menu.Show(groupPanel, location); }