Add support for copy/pasting script properties

This commit is contained in:
2021-02-17 15:25:45 +01:00
parent ed04eece8a
commit 2609f9c21a
3 changed files with 96 additions and 28 deletions
+60 -6
View File
@@ -520,15 +520,46 @@ namespace FlaxEditor.CustomEditors
try
{
string text;
if (new ScriptType(typeof(FlaxEngine.Object)).IsAssignableFrom(Values.Type))
if (ParentEditor is Dedicated.ScriptsEditor)
{
// Script
text = JsonSerializer.Serialize(Values[0]);
// Remove properties that should be ignored when copy/pasting data
if (text == null)
text = string.Empty;
int idx = text.IndexOf("\"Actor\":");
if (idx != -1)
{
int endIdx = text.IndexOf("\n", idx);
if (endIdx != -1)
text = text.Remove(idx, endIdx - idx);
}
idx = text.IndexOf("\"Parent\":");
if (idx != -1)
{
int endIdx = text.IndexOf("\n", idx);
if (endIdx != -1)
text = text.Remove(idx, endIdx - idx);
}
idx = text.IndexOf("\"OrderInParent\":");
if (idx != -1)
{
int endIdx = text.IndexOf("\n", idx);
if (endIdx != -1)
text = text.Remove(idx, endIdx - idx);
}
}
else if (new ScriptType(typeof(FlaxEngine.Object)).IsAssignableFrom(Values.Type))
{
// Object reference
text = JsonSerializer.GetStringID(Values[0] as FlaxEngine.Object);
}
else
{
// Default
text = JsonSerializer.Serialize(Values[0]);
}
Clipboard.Text = text;
}
catch (Exception ex)
@@ -538,7 +569,7 @@ namespace FlaxEditor.CustomEditors
}
}
private bool GetClipboardObject(out object result)
private bool GetClipboardObject(out object result, bool deserialize)
{
result = null;
var text = Clipboard.Text;
@@ -546,8 +577,30 @@ namespace FlaxEditor.CustomEditors
return false;
object obj;
if (new ScriptType(typeof(FlaxEngine.Object)).IsAssignableFrom(Values.Type))
if (ParentEditor is Dedicated.ScriptsEditor)
{
// Script
obj = Values[0];
if (deserialize)
{
if (Presenter.Undo != null && Presenter.Undo.Enabled)
{
using (new UndoBlock(Presenter.Undo, obj, "Paste values"))
JsonSerializer.Deserialize(obj, text);
}
else
{
JsonSerializer.Deserialize(obj, text);
}
}
else if (Newtonsoft.Json.Schema.JsonSchema.Parse(text) == null)
{
return false;
}
}
else if (new ScriptType(typeof(FlaxEngine.Object)).IsAssignableFrom(Values.Type))
{
// Object reference
if (text.Length != 32)
return false;
JsonSerializer.ParseID(text, out var id);
@@ -555,6 +608,7 @@ namespace FlaxEditor.CustomEditors
}
else
{
// Default
obj = JsonConvert.DeserializeObject(text, TypeUtils.GetType(Values.Type), JsonSerializer.Settings);
}
@@ -576,7 +630,7 @@ namespace FlaxEditor.CustomEditors
{
try
{
return GetClipboardObject(out _);
return GetClipboardObject(out _, false);
}
catch
{
@@ -594,7 +648,7 @@ namespace FlaxEditor.CustomEditors
try
{
if (GetClipboardObject(out var obj))
if (GetClipboardObject(out var obj, true))
{
SetValue(obj);
}
@@ -622,7 +622,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Create group
var title = CustomEditorsUtil.GetPropertyNameUI(scriptType.Name);
var group = layout.Group(title);
var group = layout.Group(title, editor);
if (Presenter.CacheExpandedGroups)
{
if (Editor.Instance.ProjectCache.IsCollapsedGroup(title))
@@ -35,6 +35,38 @@ namespace FlaxEditor.CustomEditors
/// </summary>
public abstract ContainerControl ContainerControl { get; }
/// <summary>
/// Adds new group element.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="linkedEditor">The custom editor to be linked for a group. Used to provide more utility functions for a drop panel UI via context menu.</param>
/// <param name="useTransparentHeader">True if use drop down icon and transparent group header, otherwise use normal style.</param>
/// <returns>The created element.</returns>
public GroupElement Group(string title, CustomEditor linkedEditor, bool useTransparentHeader = false)
{
var element = Group(title, useTransparentHeader);
element.Panel.Tag = linkedEditor;
element.Panel.MouseButtonRightClicked += OnGroupPanelMouseButtonRightClicked;
return element;
}
private void OnGroupPanelMouseButtonRightClicked(DropPanel groupPanel, Vector2 location)
{
var linkedEditor = (CustomEditor)groupPanel.Tag;
var menu = new ContextMenu();
var revertToPrefab = menu.AddButton("Revert to Prefab", linkedEditor.RevertToReferenceValue);
revertToPrefab.Enabled = linkedEditor.CanRevertReferenceValue;
var resetToDefault = menu.AddButton("Reset to default", linkedEditor.RevertToDefaultValue);
resetToDefault.Enabled = linkedEditor.CanRevertDefaultValue;
menu.AddSeparator();
menu.AddButton("Copy", linkedEditor.Copy);
var paste = menu.AddButton("Paste", linkedEditor.Paste);
paste.Enabled = linkedEditor.CanPaste;
menu.Show(groupPanel, location);
}
/// <summary>
/// Adds new group element.
/// </summary>
@@ -551,11 +583,9 @@ namespace FlaxEditor.CustomEditors
if (style == DisplayStyle.Group)
{
var group = Group(name, true);
var group = Group(name, editor, true);
group.Panel.Close(false);
group.Panel.TooltipText = tooltip;
group.Panel.Tag = editor;
group.Panel.MouseButtonRightClicked += OnGroupPanelMouseButtonRightClicked;
return group.Object(values, editor);
}
@@ -563,23 +593,6 @@ namespace FlaxEditor.CustomEditors
return property.Object(values, editor);
}
private void OnGroupPanelMouseButtonRightClicked(DropPanel groupPanel, Vector2 location)
{
var linkedEditor = (CustomEditor)groupPanel.Tag;
var menu = new ContextMenu();
var revertToPrefab = menu.AddButton("Revert to Prefab", linkedEditor.RevertToReferenceValue);
revertToPrefab.Enabled = linkedEditor.CanRevertReferenceValue;
var resetToDefault = menu.AddButton("Reset to default", linkedEditor.RevertToDefaultValue);
resetToDefault.Enabled = linkedEditor.CanRevertDefaultValue;
menu.AddSeparator();
menu.AddButton("Copy", linkedEditor.Copy);
var paste = menu.AddButton("Paste", linkedEditor.Paste);
paste.Enabled = linkedEditor.CanPaste;
menu.Show(groupPanel, location);
}
/// <summary>
/// Adds object property editor. Selects proper <see cref="CustomEditor"/> based on overrides.
/// </summary>
@@ -600,8 +613,9 @@ namespace FlaxEditor.CustomEditors
if (style == DisplayStyle.Group)
{
var group = Group(label.Text, true);
var group = Group(label.Text, editor, true);
group.Panel.Close(false);
group.Panel.TooltipText = tooltip;
return group.Object(values, editor);
}