From 1a9c3ba3ffc01c6759be72d7dfb2740732adc9f3 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 27 Jul 2024 09:28:56 -0500 Subject: [PATCH 1/4] Add options menu to json assets --- .../Editor/Windows/Assets/JsonAssetWindow.cs | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Windows/Assets/JsonAssetWindow.cs b/Source/Editor/Windows/Assets/JsonAssetWindow.cs index 86cf78ffc..c86846f7c 100644 --- a/Source/Editor/Windows/Assets/JsonAssetWindow.cs +++ b/Source/Editor/Windows/Assets/JsonAssetWindow.cs @@ -4,6 +4,7 @@ using System; using FlaxEditor.Content; using FlaxEditor.CustomEditors; using FlaxEditor.GUI; +using FlaxEditor.GUI.ContextMenu; using FlaxEngine; using FlaxEngine.GUI; @@ -24,6 +25,7 @@ namespace FlaxEditor.Windows.Assets private object _object; private bool _isRegisteredForScriptsReload; private Label _typeText; + private ToolStripButton _optionsButton; /// /// Gets the instance of the Json asset object that is being edited. @@ -139,6 +141,29 @@ namespace FlaxEditor.Windows.Assets if (_typeText != null) _typeText.Dispose(); + + // Get content item for options button + object buttonTag = null; + var allTypes = Editor.CodeEditing.All.Get(); + foreach (var type in allTypes) + { + if (type.TypeName.Equals(Asset.DataTypeName, StringComparison.Ordinal)) + { + buttonTag = type.ContentItem; + break; + } + } + + _optionsButton = new ToolStripButton(_toolstrip.ItemsHeight, ref Editor.Icons.Settings12) + { + AnchorPreset = AnchorPresets.TopRight, + Tag = buttonTag, + Parent = this, + }; + _optionsButton.LocalX -= (_optionsButton.Width + 4); + _optionsButton.LocalY += (_toolstrip.Height - _optionsButton.Height) * 0.5f; + _optionsButton.Clicked += OpenOptionsContextMenu; + var typeText = new ClickableLabel { Text = $"{Asset.DataTypeName}", @@ -147,9 +172,8 @@ namespace FlaxEditor.Windows.Assets AutoWidth = true, Parent = this, }; - typeText.LocalX += -(typeText.Width + 4); + typeText.LocalX += -(typeText.Width + _optionsButton.Width + 8); typeText.LocalY += (_toolstrip.Height - typeText.Height) * 0.5f; - typeText.RightClick = () => Clipboard.Text = Asset.DataTypeName; _typeText = typeText; _undo.Clear(); @@ -165,6 +189,27 @@ namespace FlaxEditor.Windows.Assets base.OnAssetLoaded(); } + private void OpenOptionsContextMenu() + { + var cm = new ContextMenu(); + cm.AddButton("Copy type name", () => Clipboard.Text = Asset.DataTypeName); + cm.AddButton("Copy Asset data", () => Clipboard.Text = Asset.Data); + cm.AddSeparator(); + if (_optionsButton.Tag is ContentItem item) + { + cm.AddButton("Edit Asset code", () => + { + Editor.Instance.ContentEditing.Open(item); + }); + cm.AddButton("Show Asset code item in content window", () => + { + Editor.Instance.Windows.ContentWin.Select(item); + }); + } + + cm.Show(_optionsButton, _optionsButton.PointFromScreen(Input.MouseScreenPosition)); + } + /// protected override void OnAssetLoadFailed() { From 728401b67c30daa2914cfa20fe84291500ef90c3 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 27 Jul 2024 10:30:05 -0500 Subject: [PATCH 2/4] Add pasting. --- .../Editor/Windows/Assets/JsonAssetWindow.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/Source/Editor/Windows/Assets/JsonAssetWindow.cs b/Source/Editor/Windows/Assets/JsonAssetWindow.cs index c86846f7c..7ca5250e6 100644 --- a/Source/Editor/Windows/Assets/JsonAssetWindow.cs +++ b/Source/Editor/Windows/Assets/JsonAssetWindow.cs @@ -7,6 +7,8 @@ using FlaxEditor.GUI; using FlaxEditor.GUI.ContextMenu; using FlaxEngine; using FlaxEngine.GUI; +using FlaxEngine.Json; +using FlaxEngine.Utilities; namespace FlaxEditor.Windows.Assets { @@ -17,6 +19,53 @@ namespace FlaxEditor.Windows.Assets /// public sealed class JsonAssetWindow : AssetEditorWindowBase { + private class ObjectPasteUndo : IUndoAction + { + /// + public string ActionString => "Object Paste Undo"; + + private JsonAssetWindow _window; + private object _oldObject; + private object _newObject; + + public ObjectPasteUndo(object oldObject, object newObject, JsonAssetWindow window) + { + _oldObject = oldObject; + _newObject = newObject; + _window = window; + } + + /// + public void Dispose() + { + _oldObject = null; + _newObject = null; + _window = null; + } + + /// + public void Do() + { + if (_newObject != null) + { + _window._object = _newObject; + _window.MarkAsEdited(); + _window._presenter.Select(_window._object); + } + } + + /// + public void Undo() + { + if (_oldObject != null) + { + _window._object = _oldObject; + _window.MarkAsEdited(); + _window._presenter.Select(_window._object); + } + } + } + private readonly CustomEditorPresenter _presenter; private readonly ToolStripButton _saveButton; private readonly ToolStripButton _undoButton; @@ -194,6 +243,38 @@ namespace FlaxEditor.Windows.Assets var cm = new ContextMenu(); cm.AddButton("Copy type name", () => Clipboard.Text = Asset.DataTypeName); cm.AddButton("Copy Asset data", () => Clipboard.Text = Asset.Data); + cm.AddButton("Paste Asset data", () => + { + if (!string.IsNullOrEmpty(Clipboard.Text)) + { + var dataTypeName = Asset.DataTypeName; + var type = TypeUtils.GetType(dataTypeName); + if (type != null) + { + try + { + var obj = Activator.CreateInstance(type.Type); + var data = Clipboard.Text; + JsonSerializer.Deserialize(obj, data); + if (obj != null) + { + var undoAction = new ObjectPasteUndo(_object, obj, this); + undoAction.Do(); + _undo.AddAction(undoAction); + } + else + { + Editor.LogWarning("Pasted data is not the correct data type or has incomplete data"); + } + } + catch (Exception ex) + { + Editor.LogWarning("Pasted data is not the correct data type or has incomplete data"); + } + } + } + }); + cm.Enabled = !string.IsNullOrEmpty(Clipboard.Text); cm.AddSeparator(); if (_optionsButton.Tag is ContentItem item) { From 5f5b0485b5009515109aeded14d819848d1ac699 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 27 Jul 2024 10:42:51 -0500 Subject: [PATCH 3/4] Cache context menu and add check to prevent accidental duplication. --- .../Editor/Windows/Assets/JsonAssetWindow.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Source/Editor/Windows/Assets/JsonAssetWindow.cs b/Source/Editor/Windows/Assets/JsonAssetWindow.cs index 7ca5250e6..7fd2fc84c 100644 --- a/Source/Editor/Windows/Assets/JsonAssetWindow.cs +++ b/Source/Editor/Windows/Assets/JsonAssetWindow.cs @@ -75,6 +75,7 @@ namespace FlaxEditor.Windows.Assets private bool _isRegisteredForScriptsReload; private Label _typeText; private ToolStripButton _optionsButton; + private ContextMenu _optionsCM; /// /// Gets the instance of the Json asset object that is being edited. @@ -240,10 +241,13 @@ namespace FlaxEditor.Windows.Assets private void OpenOptionsContextMenu() { - var cm = new ContextMenu(); - cm.AddButton("Copy type name", () => Clipboard.Text = Asset.DataTypeName); - cm.AddButton("Copy Asset data", () => Clipboard.Text = Asset.Data); - cm.AddButton("Paste Asset data", () => + if (_optionsCM != null && _optionsCM.ContainsFocus) + return; + + _optionsCM = new ContextMenu(); + _optionsCM.AddButton("Copy type name", () => Clipboard.Text = Asset.DataTypeName); + _optionsCM.AddButton("Copy Asset data", () => Clipboard.Text = Asset.Data); + _optionsCM.AddButton("Paste Asset data", () => { if (!string.IsNullOrEmpty(Clipboard.Text)) { @@ -274,21 +278,21 @@ namespace FlaxEditor.Windows.Assets } } }); - cm.Enabled = !string.IsNullOrEmpty(Clipboard.Text); - cm.AddSeparator(); + _optionsCM.Enabled = !string.IsNullOrEmpty(Clipboard.Text); + _optionsCM.AddSeparator(); if (_optionsButton.Tag is ContentItem item) { - cm.AddButton("Edit Asset code", () => + _optionsCM.AddButton("Edit Asset code", () => { Editor.Instance.ContentEditing.Open(item); }); - cm.AddButton("Show Asset code item in content window", () => + _optionsCM.AddButton("Show Asset code item in content window", () => { Editor.Instance.Windows.ContentWin.Select(item); }); } - cm.Show(_optionsButton, _optionsButton.PointFromScreen(Input.MouseScreenPosition)); + _optionsCM.Show(_optionsButton, _optionsButton.PointFromScreen(Input.MouseScreenPosition)); } /// From 265e34bd04849aa222a40b0b20194a32e21cc8a1 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 5 Aug 2024 12:13:44 -0500 Subject: [PATCH 4/4] Serialize json asset copy and paste data and code style fixes. --- .../Editor/Windows/Assets/JsonAssetWindow.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Source/Editor/Windows/Assets/JsonAssetWindow.cs b/Source/Editor/Windows/Assets/JsonAssetWindow.cs index 7fd2fc84c..43b34a8c6 100644 --- a/Source/Editor/Windows/Assets/JsonAssetWindow.cs +++ b/Source/Editor/Windows/Assets/JsonAssetWindow.cs @@ -25,13 +25,13 @@ namespace FlaxEditor.Windows.Assets public string ActionString => "Object Paste Undo"; private JsonAssetWindow _window; - private object _oldObject; - private object _newObject; + private string _oldObject; + private string _newObject; public ObjectPasteUndo(object oldObject, object newObject, JsonAssetWindow window) { - _oldObject = oldObject; - _newObject = newObject; + _oldObject = JsonSerializer.Serialize(oldObject); + _newObject = JsonSerializer.Serialize(newObject); _window = window; } @@ -46,9 +46,9 @@ namespace FlaxEditor.Windows.Assets /// public void Do() { - if (_newObject != null) + if (!string.IsNullOrEmpty(_newObject)) { - _window._object = _newObject; + _window._object = JsonSerializer.Deserialize(_newObject, TypeUtils.GetType(_window.Asset.DataTypeName).Type); _window.MarkAsEdited(); _window._presenter.Select(_window._object); } @@ -57,9 +57,9 @@ namespace FlaxEditor.Windows.Assets /// public void Undo() { - if (_oldObject != null) + if (!string.IsNullOrEmpty(_oldObject)) { - _window._object = _oldObject; + _window._object = JsonSerializer.Deserialize(_oldObject, TypeUtils.GetType(_window.Asset.DataTypeName).Type); _window.MarkAsEdited(); _window._presenter.Select(_window._object); } @@ -203,11 +203,12 @@ namespace FlaxEditor.Windows.Assets break; } } - + _optionsButton = new ToolStripButton(_toolstrip.ItemsHeight, ref Editor.Icons.Settings12) { AnchorPreset = AnchorPresets.TopRight, Tag = buttonTag, + Size = new Float2(18), Parent = this, }; _optionsButton.LocalX -= (_optionsButton.Width + 4); @@ -246,8 +247,8 @@ namespace FlaxEditor.Windows.Assets _optionsCM = new ContextMenu(); _optionsCM.AddButton("Copy type name", () => Clipboard.Text = Asset.DataTypeName); - _optionsCM.AddButton("Copy Asset data", () => Clipboard.Text = Asset.Data); - _optionsCM.AddButton("Paste Asset data", () => + _optionsCM.AddButton("Copy asset data", () => Clipboard.Text = Asset.Data); + _optionsCM.AddButton("Paste asset data", () => { if (!string.IsNullOrEmpty(Clipboard.Text)) { @@ -273,7 +274,7 @@ namespace FlaxEditor.Windows.Assets } catch (Exception ex) { - Editor.LogWarning("Pasted data is not the correct data type or has incomplete data"); + Editor.LogWarning($"Pasted data is not the correct data type or has incomplete data. Exception: {ex}"); } } } @@ -282,11 +283,11 @@ namespace FlaxEditor.Windows.Assets _optionsCM.AddSeparator(); if (_optionsButton.Tag is ContentItem item) { - _optionsCM.AddButton("Edit Asset code", () => + _optionsCM.AddButton("Edit asset code", () => { Editor.Instance.ContentEditing.Open(item); }); - _optionsCM.AddButton("Show Asset code item in content window", () => + _optionsCM.AddButton("Show asset code item in content window", () => { Editor.Instance.Windows.ContentWin.Select(item); });