From 1fb6586dff6c37a245f40865adbc0f214a7121ca Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 19 Jul 2025 16:09:33 -0500 Subject: [PATCH 1/2] Add collection item duplication. --- .../CustomEditors/Editors/CollectionEditor.cs | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/CollectionEditor.cs b/Source/Editor/CustomEditors/Editors/CollectionEditor.cs index cfc11d5a5..50b3e47d5 100644 --- a/Source/Editor/CustomEditors/Editors/CollectionEditor.cs +++ b/Source/Editor/CustomEditors/Editors/CollectionEditor.cs @@ -70,7 +70,9 @@ namespace FlaxEditor.CustomEditors.Editors menu.ItemsContainer.RemoveChildren(); menu.AddButton("Copy", linkedEditor.Copy); - var b = menu.AddButton("Paste", linkedEditor.Paste); + var b = menu.AddButton("Duplicate", () => Editor.Duplicate(Index)); + b.Enabled = linkedEditor.CanPaste && !Editor._readOnly; + b = menu.AddButton("Paste", linkedEditor.Paste); b.Enabled = linkedEditor.CanPaste && !Editor._readOnly; menu.AddSeparator(); @@ -404,8 +406,10 @@ namespace FlaxEditor.CustomEditors.Editors var menu = new ContextMenu(); menu.AddButton("Copy", linkedEditor.Copy); + var b = menu.AddButton("Duplicate", () => Editor.Duplicate(Index)); + b.Enabled = linkedEditor.CanPaste && !Editor._readOnly; var paste = menu.AddButton("Paste", linkedEditor.Paste); - paste.Enabled = linkedEditor.CanPaste; + paste.Enabled = linkedEditor.CanPaste && !Editor._readOnly; if (_canReorder) { @@ -741,6 +745,25 @@ namespace FlaxEditor.CustomEditors.Editors cloned[srcIndex] = tmp; SetValue(cloned); } + + /// + /// Duplicates the list item. + /// + /// The index to duplicate. + public void Duplicate(int index) + { + if (IsSetBlocked) + return; + + var count = Count; + Resize(count + 1); + RefreshInternal(); // Force update values. + Shift(count, index + 1); + RefreshInternal(); // Force update values. + var cloned = CloneValues(); + cloned[index + 1] = Utilities.Utils.CloneValue(cloned[index]); + SetValue(cloned); + } /// /// Shifts the specified item at the given index and moves it through the list to the other item. It supports undo. From 824b49dd883443c7a20384de4aa83bedb43f3483 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Aug 2025 20:25:02 -0500 Subject: [PATCH 2/2] Better duplication of collection. --- .../CustomEditors/Editors/CollectionEditor.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/CollectionEditor.cs b/Source/Editor/CustomEditors/Editors/CollectionEditor.cs index 50b3e47d5..a0ee8d3dc 100644 --- a/Source/Editor/CustomEditors/Editors/CollectionEditor.cs +++ b/Source/Editor/CustomEditors/Editors/CollectionEditor.cs @@ -756,13 +756,22 @@ namespace FlaxEditor.CustomEditors.Editors return; var count = Count; - Resize(count + 1); - RefreshInternal(); // Force update values. - Shift(count, index + 1); - RefreshInternal(); // Force update values. - var cloned = CloneValues(); - cloned[index + 1] = Utilities.Utils.CloneValue(cloned[index]); - SetValue(cloned); + var newValues = Allocate(count + 1); + var oldValues = (IList)Values[0]; + + for (int i = 0; i <= index; i++) + { + newValues[i] = oldValues[i]; + } + + newValues[index + 1] = Utilities.Utils.CloneValue(oldValues[index]); + + for (int i = index + 1; i < count; i++) + { + newValues[i + 1] = oldValues[i]; + } + + SetValue(newValues); } ///