From f18715a49718d906bb597338bfe461742ec619cd Mon Sep 17 00:00:00 2001 From: Nils Hausfeld Date: Sat, 25 May 2024 13:25:52 +0200 Subject: [PATCH] - Replaced renamepopup with textbox in surface comment --- Source/Editor/Surface/SurfaceComment.cs | 79 ++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/Source/Editor/Surface/SurfaceComment.cs b/Source/Editor/Surface/SurfaceComment.cs index f5179ba9a..e98a94a67 100644 --- a/Source/Editor/Surface/SurfaceComment.cs +++ b/Source/Editor/Surface/SurfaceComment.cs @@ -20,12 +20,18 @@ namespace FlaxEditor.Surface private Rectangle _colorButtonRect; private Rectangle _resizeButtonRect; private Float2 _startResizingSize; + private readonly TextBox _renameTextBox; /// /// True if sizing tool is in use. /// protected bool _isResizing; + /// + /// True if rename textbox is active in order to rename comment + /// + protected bool _isRenaming; + /// /// Gets or sets the color of the comment. /// @@ -63,6 +69,13 @@ namespace FlaxEditor.Surface public SurfaceComment(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) : base(id, context, nodeArch, groupArch) { + _renameTextBox = new TextBox(false, 0,0, Width) + { + Height = Constants.NodeHeaderSize, + Visible = false, + Parent = this, + EndEditOnClick = false, // We have to handle this ourselves, otherwise the textbox instantly loses focus when double-clicking the header + }; } /// @@ -149,6 +162,20 @@ namespace FlaxEditor.Surface _closeButtonRect = new Rectangle(Width - buttonSize - buttonMargin, buttonMargin, buttonSize, buttonSize); _colorButtonRect = new Rectangle(_closeButtonRect.Left - buttonSize - buttonMargin, buttonMargin, buttonSize, buttonSize); _resizeButtonRect = new Rectangle(_closeButtonRect.Left, Height - buttonSize - buttonMargin, buttonSize, buttonSize); + _renameTextBox.Width = Width; + _renameTextBox.Height = headerSize; + } + + /// + public override void Update(float deltaTime) + { + if (_isRenaming && (!_renameTextBox.IsFocused || !RootWindow.IsFocused)) + { + Rename(_renameTextBox.Text); + StopRenaming(); + } + + base.Update(deltaTime); } /// @@ -158,7 +185,7 @@ namespace FlaxEditor.Surface var color = Color; var backgroundRect = new Rectangle(Float2.Zero, Size); var headerColor = new Color(Mathf.Clamp(color.R, 0.1f, 0.3f), Mathf.Clamp(color.G, 0.1f, 0.3f), Mathf.Clamp(color.B, 0.1f, 0.3f), 0.4f); - if (IsSelected) + if (IsSelected && !_isRenaming) headerColor *= 2.0f; // Paint background @@ -169,7 +196,8 @@ namespace FlaxEditor.Surface // Header Render2D.FillRectangle(_headerRect, headerColor); - Render2D.DrawText(style.FontLarge, Title, _headerRect, style.Foreground, TextAlignment.Center, TextAlignment.Center); + if(!_isRenaming) + Render2D.DrawText(style.FontLarge, Title, _headerRect, style.Foreground, TextAlignment.Center, TextAlignment.Center); // Close button Render2D.DrawSprite(style.Cross, _closeButtonRect, _closeButtonRect.Contains(_mousePosition) && Surface.CanEdit ? style.Foreground : style.ForegroundGrey); @@ -213,6 +241,13 @@ namespace FlaxEditor.Surface EndResizing(); } + // Check if was renaming + if (_isRenaming) + { + Rename(_renameTextBox.Text); + StopRenaming(); + } + // Base base.OnLostFocus(); } @@ -294,17 +329,47 @@ namespace FlaxEditor.Surface /// public void StartRenaming() { - Surface.Select(this); - var dialog = RenamePopup.Show(this, _headerRect, Title, false); - dialog.Renamed += OnRenamed; + _isRenaming = true; + _renameTextBox.Visible = true; + _renameTextBox.SetText(Title); + _renameTextBox.Focus(); + _renameTextBox.SelectAll(); } - private void OnRenamed(RenamePopup renamePopup) + private void StopRenaming() { - Title = TitleValue = renamePopup.Text; + _isRenaming = false; + _renameTextBox.Visible = false; + } + + private void Rename(string newTitle) + { + if(string.Equals(Title, newTitle, StringComparison.Ordinal)) + return; + + Title = TitleValue = newTitle; Surface.MarkAsEdited(false); } + /// + public override bool OnKeyDown(KeyboardKeys key) + { + if (key == KeyboardKeys.Return) + { + Rename(_renameTextBox.Text); + StopRenaming(); + return true; + } + + if(key == KeyboardKeys.Escape) + { + StopRenaming(); + return true; + } + + return base.OnKeyDown(key); + } + /// public override bool OnMouseUp(Float2 location, MouseButton button) {