From 5d61e45ecde7ced2294cdb8debd1bff077ea978f Mon Sep 17 00:00:00 2001 From: ruan Date: Sun, 4 Feb 2024 10:42:29 -0400 Subject: [PATCH 1/3] Add rename multiple actors --- .../Windows/SceneTreeWindow.ContextMenu.cs | 2 - .../Windows/SceneTreeWindow.RenameWindow.cs | 177 ++++++++++++++++++ Source/Editor/Windows/SceneTreeWindow.cs | 21 ++- 3 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs diff --git a/Source/Editor/Windows/SceneTreeWindow.ContextMenu.cs b/Source/Editor/Windows/SceneTreeWindow.ContextMenu.cs index 0c8e0f283..b37353f8f 100644 --- a/Source/Editor/Windows/SceneTreeWindow.ContextMenu.cs +++ b/Source/Editor/Windows/SceneTreeWindow.ContextMenu.cs @@ -55,8 +55,6 @@ namespace FlaxEditor.Windows // Basic editing options b = contextMenu.AddButton("Rename", inputOptions.Rename, Rename); - b.Enabled = isSingleActorSelected; - b = contextMenu.AddButton("Duplicate", inputOptions.Duplicate, Editor.SceneEditing.Duplicate); b.Enabled = hasSthSelected; diff --git a/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs new file mode 100644 index 000000000..fdef598b2 --- /dev/null +++ b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs @@ -0,0 +1,177 @@ +using System.Text; +using FlaxEditor.CustomEditors; +using FlaxEditor.GUI; +using FlaxEditor.Windows.Assets; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace FlaxEditor.Windows +{ + /// + /// A window used to rename multiple actors. + /// + public class RenameWindow : EditorWindow + { + /// + private class RenameUndoAction : IUndoAction + { + /// + /// The old actors name to use on action. + /// + public string[] OldNames; + + /// + /// The new actors name to use on action. + /// + public string[] NewNames; + + /// + /// All actors to rename. + /// + public Actor[] ActorsToRename; + + /// + /// Create a undo action. + /// + /// + public RenameUndoAction(Actor[] nodes) + { + ActorsToRename = nodes; + OldNames = new string[nodes.Length]; + + for (int i = 0; i < nodes.Length; i++) + OldNames[i] = nodes[i].Name; + } + + /// + public void Do() + { + for (int i = 0; i < ActorsToRename.Length; i++) + ActorsToRename[i].Name = NewNames[i]; + } + + /// + public void Undo() + { + for (int i = 0; i < ActorsToRename.Length; i++) + ActorsToRename[i].Name = OldNames[i]; + } + + /// + public string ActionString => "Renaming actors."; + + /// + public void Dispose() { } + } + + /// + /// Rename options. + /// + private enum RenameOptions + { + OnlyName, + UsePrefix, + UseSufix + } + + private Label _label; + private TextBox _textBox; + private EnumComboBox _renameOptions; + private Button _renameButton; + + private Actor[] _actorsToRename; + + /// + /// Create an instance of the to rename actors. + /// + /// All actors to rename + /// The editor. + public RenameWindow(Actor[] actorsToRename, Editor editor) : base(editor, true, FlaxEngine.GUI.ScrollBars.None) + { + Title = "Rename"; + _actorsToRename = actorsToRename; + + var container = new VerticalPanel + { + Parent = this, + AnchorPreset = AnchorPresets.StretchAll, + Offset = Vector2.Zero, + + }; + + _label = new Label + { + Text = "New Name", + AnchorPreset = AnchorPresets.TopLeft, + Parent = container, + Size = new Float2(100, 25) + }; + + _textBox = new TextBox + { + Text = "Actor", + AnchorPreset = AnchorPresets.TopLeft, + Parent = container, + Size = new Float2(200, 25) + }; + + var renameOptionLabel = new Label + { + Text = "Rename Option", + AnchorPreset = AnchorPresets.TopLeft, + Parent = container, + Size = new Float2(100, 25) + }; + + _renameOptions = new EnumComboBox(typeof(RenameOptions)) + { + Parent = container, + Value = 0 + }; + + _renameButton = new Button + { + Text = "Rename", + AnchorPreset = AnchorPresets.TopLeft, + Parent = container, + Size = new Float2(200, 25), + }; + + _renameButton.Clicked += () => + { + var renameUndoAction = new RenameUndoAction(_actorsToRename); + Editor.Instance.SceneEditing.Undo.AddAction(renameUndoAction); + renameUndoAction.NewNames = new string[_actorsToRename.Length]; + for (int i = 0; i < _actorsToRename.Length; i++) + { + var actor = _actorsToRename[i]; + if (!actor) + continue; + var newName = new StringBuilder(_textBox.Text); + if (_renameOptions.Value == (int)RenameOptions.UsePrefix) + { + newName = new StringBuilder(); + newName.Append(i); + newName.Append(_textBox.Text); + } + else if (_renameOptions.Value == (int)RenameOptions.UseSufix) + newName.Append(i.ToString()); + var newNameStr = newName.ToString(); + actor.Name = newNameStr; + renameUndoAction.NewNames[i] = newNameStr; + } + Editor.Instance.Scene.MarkAllScenesEdited(); + Close(); + }; + } + + ~RenameWindow() + { + _actorsToRename = null; + _renameButton = null; + _label = null; + _textBox = null; + _renameOptions = null; + } + } +} diff --git a/Source/Editor/Windows/SceneTreeWindow.cs b/Source/Editor/Windows/SceneTreeWindow.cs index 63ba7b960..8ebd508a5 100644 --- a/Source/Editor/Windows/SceneTreeWindow.cs +++ b/Source/Editor/Windows/SceneTreeWindow.cs @@ -13,7 +13,6 @@ using FlaxEditor.Scripting; using FlaxEditor.States; using FlaxEngine; using FlaxEngine.GUI; -using static FlaxEditor.GUI.ItemsListContextMenu; namespace FlaxEditor.Windows { @@ -138,10 +137,24 @@ namespace FlaxEditor.Windows private void Rename() { var selection = Editor.SceneEditing.Selection; - if (selection.Count != 0 && selection[0] is ActorNode actor) + var selectionCount = selection.Count; + + // Show a window with options to rename multiple actors. + if (selectionCount > 1) { - if (selection.Count != 0) - Editor.SceneEditing.Select(actor); + var selectedActors = new Actor[selectionCount]; + + for (int i = 0; i < selectionCount; i++) + if (selection[i] is ActorNode actorNode) + selectedActors[i] = actorNode.Actor; + + new RenameWindow(selectedActors, Editor).Show(); + return; + } + + if (selectionCount != 0 && selection[0] is ActorNode actor) + { + Editor.SceneEditing.Select(actor); actor.TreeNode.StartRenaming(this, _sceneTreePanel); } } From c6515da8c9d2cbec8394d92cbe198732fd3a98fe Mon Sep 17 00:00:00 2001 From: ruan Date: Sat, 10 Feb 2024 20:36:42 -0400 Subject: [PATCH 2/3] Improve rename window style and refactor --- .../Windows/SceneTreeWindow.RenameWindow.cs | 195 ++++++++++++------ Source/Editor/Windows/SceneTreeWindow.cs | 2 +- 2 files changed, 133 insertions(+), 64 deletions(-) diff --git a/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs index fdef598b2..597efeb80 100644 --- a/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs +++ b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs @@ -1,9 +1,7 @@ using System.Text; -using FlaxEditor.CustomEditors; -using FlaxEditor.GUI; -using FlaxEditor.Windows.Assets; using FlaxEngine; using FlaxEngine.GUI; +using FlaxEditor.GUI; namespace FlaxEditor.Windows { @@ -74,21 +72,19 @@ namespace FlaxEditor.Windows UseSufix } - private Label _label; - private TextBox _textBox; - private EnumComboBox _renameOptions; - private Button _renameButton; - + private string _newActorsName; + private RenameOptions _renameOption; private Actor[] _actorsToRename; - /// - /// Create an instance of the to rename actors. - /// - /// All actors to rename - /// The editor. - public RenameWindow(Actor[] actorsToRename, Editor editor) : base(editor, true, FlaxEngine.GUI.ScrollBars.None) + private static RenameWindow _currentOpenedWindow; + + private RenameWindow(Actor[] actorsToRename, Editor editor) : base(editor, true, FlaxEngine.GUI.ScrollBars.None) { Title = "Rename"; + Size = new Float2(300, 110); + + _newActorsName = "Actor "; + _renameOption = RenameOptions.UseSufix; _actorsToRename = actorsToRename; var container = new VerticalPanel @@ -96,82 +92,155 @@ namespace FlaxEditor.Windows Parent = this, AnchorPreset = AnchorPresets.StretchAll, Offset = Vector2.Zero, - + AutoSize = false, + Bounds = Rectangle.Empty }; - _label = new Label + var nameContainer = new HorizontalPanel + { + Parent = container, + AnchorPreset = AnchorPresets.TopLeft, + Bounds = new Rectangle(0, 0, 300, 22), + Offset = Vector2.Zero, + AutoSize = false, + Spacing = 2, + CullChildren = false, + ClipChildren = false, + }; + + var optionsContainer = new HorizontalPanel + { + Parent = container, + AnchorPreset = AnchorPresets.TopLeft, + Bounds = new Rectangle(0, 22, 300, 22), + Offset = Vector2.Zero, + AutoSize = false, + Spacing = 2, + CullChildren = false, + ClipChildren = false, + }; + + var renameLabel = new Label { Text = "New Name", - AnchorPreset = AnchorPresets.TopLeft, - Parent = container, - Size = new Float2(100, 25) + AnchorPreset = AnchorPresets.Custom, + AnchorMin = Float2.Zero, + AnchorMax = new Float2(0.5f, 0), + Parent = nameContainer, + HorizontalAlignment = TextAlignment.Near, + Size = new Float2(150, 22), + Offsets = Margin.Zero, }; - _textBox = new TextBox + var newNameTextBox = new TextBox { - Text = "Actor", - AnchorPreset = AnchorPresets.TopLeft, - Parent = container, - Size = new Float2(200, 25) + Text = _newActorsName, + AnchorPreset = AnchorPresets.Custom, + AnchorMin = new Float2(0.5f, 0), + AnchorMax = new Float2(1, 0), + Parent = nameContainer, + Size = new Float2(150, 22), + Offsets = Margin.Zero, }; - var renameOptionLabel = new Label + var optionNameLabel = new Label { Text = "Rename Option", - AnchorPreset = AnchorPresets.TopLeft, - Parent = container, - Size = new Float2(100, 25) + HorizontalAlignment = TextAlignment.Near, + AnchorPreset = AnchorPresets.Custom, + AnchorMin = Float2.Zero, + AnchorMax = new Float2(0.5f, 0), + Parent = optionsContainer, + Size = new Float2(150, 22), + Offsets = Margin.Zero, }; - _renameOptions = new EnumComboBox(typeof(RenameOptions)) + var renameOptions = new EnumComboBox(typeof(RenameOptions)) { - Parent = container, - Value = 0 + Parent = optionsContainer, + Value = (int)_renameOption, + AnchorPreset = AnchorPresets.Custom, + AnchorMin = new Float2(0.5f, 0f), + AnchorMax = new Float2(1, 0), + Size = new Float2(150, 22), + Offsets = Margin.Zero, }; - _renameButton = new Button + var renameButton = new Button { Text = "Rename", AnchorPreset = AnchorPresets.TopLeft, Parent = container, - Size = new Float2(200, 25), }; - _renameButton.Clicked += () => + newNameTextBox.TextBoxEditEnd += textBox => { - var renameUndoAction = new RenameUndoAction(_actorsToRename); - Editor.Instance.SceneEditing.Undo.AddAction(renameUndoAction); - renameUndoAction.NewNames = new string[_actorsToRename.Length]; - for (int i = 0; i < _actorsToRename.Length; i++) - { - var actor = _actorsToRename[i]; - if (!actor) - continue; - var newName = new StringBuilder(_textBox.Text); - if (_renameOptions.Value == (int)RenameOptions.UsePrefix) - { - newName = new StringBuilder(); - newName.Append(i); - newName.Append(_textBox.Text); - } - else if (_renameOptions.Value == (int)RenameOptions.UseSufix) - newName.Append(i.ToString()); - var newNameStr = newName.ToString(); - actor.Name = newNameStr; - renameUndoAction.NewNames[i] = newNameStr; - } - Editor.Instance.Scene.MarkAllScenesEdited(); - Close(); + _newActorsName = textBox.Text; }; + + renameOptions.EnumValueChanged += combo => + { + _renameOption = (RenameOptions)combo.Value; + }; + + newNameTextBox.Focus(); + newNameTextBox.KeyDown += k => + { + if (k == KeyboardKeys.Return) + { + _newActorsName = newNameTextBox.Text; + RenameActors(); + } + }; + + renameButton.Clicked += RenameActors; } - ~RenameWindow() + private void RenameActors() { - _actorsToRename = null; - _renameButton = null; - _label = null; - _textBox = null; - _renameOptions = null; + var renameUndoAction = new RenameUndoAction(_actorsToRename); + Editor.Instance.SceneEditing.Undo.AddAction(renameUndoAction); + renameUndoAction.NewNames = new string[_actorsToRename.Length]; + for (int i = 0; i < _actorsToRename.Length; i++) + { + var actor = _actorsToRename[i]; + if (!actor) + continue; + var newName = new StringBuilder(_newActorsName); + if (_renameOption == RenameOptions.UsePrefix) + { + newName = new StringBuilder(); + newName.Append(i); + newName.Append(_newActorsName); + } + else if (_renameOption == RenameOptions.UseSufix) + newName.Append(i.ToString()); + + var newNameStr = newName.ToString(); + actor.Name = newNameStr; + renameUndoAction.NewNames[i] = newNameStr; + } + Editor.Instance.Scene.MarkAllScenesEdited(); + Close(); + } + + /// + /// Create an instance of the to rename actors and show the window. + /// + /// All actors to rename + /// The editor. + public static void Show(Actor[] actorsToRename, Editor editor) + { + // Can only one window opened. + if (_currentOpenedWindow != null) + _currentOpenedWindow.Close(ClosingReason.CloseEvent); + + _currentOpenedWindow = new RenameWindow(actorsToRename, editor); + _currentOpenedWindow.ShowFloating(new Float2(300, 110)); + _currentOpenedWindow.RootWindow.Window.Closed += () => + { + _currentOpenedWindow = null; + }; } } } diff --git a/Source/Editor/Windows/SceneTreeWindow.cs b/Source/Editor/Windows/SceneTreeWindow.cs index 8ebd508a5..b44b838cf 100644 --- a/Source/Editor/Windows/SceneTreeWindow.cs +++ b/Source/Editor/Windows/SceneTreeWindow.cs @@ -148,7 +148,7 @@ namespace FlaxEditor.Windows if (selection[i] is ActorNode actorNode) selectedActors[i] = actorNode.Actor; - new RenameWindow(selectedActors, Editor).Show(); + RenameWindow.Show(selectedActors, Editor); return; } From 203f5d06d192ad72ce46043ee429d16471f5e1ee Mon Sep 17 00:00:00 2001 From: ruan Date: Sat, 10 Feb 2024 20:46:05 -0400 Subject: [PATCH 3/3] Fix typo --- Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs index 597efeb80..6278a5f3b 100644 --- a/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs +++ b/Source/Editor/Windows/SceneTreeWindow.RenameWindow.cs @@ -69,7 +69,7 @@ namespace FlaxEditor.Windows { OnlyName, UsePrefix, - UseSufix + UseSuffix } private string _newActorsName; @@ -84,7 +84,7 @@ namespace FlaxEditor.Windows Size = new Float2(300, 110); _newActorsName = "Actor "; - _renameOption = RenameOptions.UseSufix; + _renameOption = RenameOptions.UseSuffix; _actorsToRename = actorsToRename; var container = new VerticalPanel @@ -213,7 +213,7 @@ namespace FlaxEditor.Windows newName.Append(i); newName.Append(_newActorsName); } - else if (_renameOption == RenameOptions.UseSufix) + else if (_renameOption == RenameOptions.UseSuffix) newName.Append(i.ToString()); var newNameStr = newName.ToString();