- Replaced renamepopup with textbox in surface comment

This commit is contained in:
Nils Hausfeld
2024-05-25 13:25:52 +02:00
parent 9731437717
commit f18715a497

View File

@@ -20,12 +20,18 @@ namespace FlaxEditor.Surface
private Rectangle _colorButtonRect; private Rectangle _colorButtonRect;
private Rectangle _resizeButtonRect; private Rectangle _resizeButtonRect;
private Float2 _startResizingSize; private Float2 _startResizingSize;
private readonly TextBox _renameTextBox;
/// <summary> /// <summary>
/// True if sizing tool is in use. /// True if sizing tool is in use.
/// </summary> /// </summary>
protected bool _isResizing; protected bool _isResizing;
/// <summary>
/// True if rename textbox is active in order to rename comment
/// </summary>
protected bool _isRenaming;
/// <summary> /// <summary>
/// Gets or sets the color of the comment. /// Gets or sets the color of the comment.
/// </summary> /// </summary>
@@ -63,6 +69,13 @@ namespace FlaxEditor.Surface
public SurfaceComment(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) public SurfaceComment(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch)
: base(id, context, nodeArch, 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
};
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -149,6 +162,20 @@ namespace FlaxEditor.Surface
_closeButtonRect = new Rectangle(Width - buttonSize - buttonMargin, buttonMargin, buttonSize, buttonSize); _closeButtonRect = new Rectangle(Width - buttonSize - buttonMargin, buttonMargin, buttonSize, buttonSize);
_colorButtonRect = new Rectangle(_closeButtonRect.Left - 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); _resizeButtonRect = new Rectangle(_closeButtonRect.Left, Height - buttonSize - buttonMargin, buttonSize, buttonSize);
_renameTextBox.Width = Width;
_renameTextBox.Height = headerSize;
}
/// <inheritdoc />
public override void Update(float deltaTime)
{
if (_isRenaming && (!_renameTextBox.IsFocused || !RootWindow.IsFocused))
{
Rename(_renameTextBox.Text);
StopRenaming();
}
base.Update(deltaTime);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -158,7 +185,7 @@ namespace FlaxEditor.Surface
var color = Color; var color = Color;
var backgroundRect = new Rectangle(Float2.Zero, Size); 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); 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; headerColor *= 2.0f;
// Paint background // Paint background
@@ -169,7 +196,8 @@ namespace FlaxEditor.Surface
// Header // Header
Render2D.FillRectangle(_headerRect, headerColor); 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 // Close button
Render2D.DrawSprite(style.Cross, _closeButtonRect, _closeButtonRect.Contains(_mousePosition) && Surface.CanEdit ? style.Foreground : style.ForegroundGrey); Render2D.DrawSprite(style.Cross, _closeButtonRect, _closeButtonRect.Contains(_mousePosition) && Surface.CanEdit ? style.Foreground : style.ForegroundGrey);
@@ -213,6 +241,13 @@ namespace FlaxEditor.Surface
EndResizing(); EndResizing();
} }
// Check if was renaming
if (_isRenaming)
{
Rename(_renameTextBox.Text);
StopRenaming();
}
// Base // Base
base.OnLostFocus(); base.OnLostFocus();
} }
@@ -294,17 +329,47 @@ namespace FlaxEditor.Surface
/// </summary> /// </summary>
public void StartRenaming() public void StartRenaming()
{ {
Surface.Select(this); _isRenaming = true;
var dialog = RenamePopup.Show(this, _headerRect, Title, false); _renameTextBox.Visible = true;
dialog.Renamed += OnRenamed; _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); Surface.MarkAsEdited(false);
} }
/// <inheritdoc />
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);
}
/// <inheritdoc /> /// <inheritdoc />
public override bool OnMouseUp(Float2 location, MouseButton button) public override bool OnMouseUp(Float2 location, MouseButton button)
{ {