Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visject: Improved comment renaming + visual fix #2652

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions Source/Editor/Surface/SurfaceComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ public class SurfaceComment : SurfaceNode
private Rectangle _colorButtonRect;
private Rectangle _resizeButtonRect;
private Float2 _startResizingSize;
private readonly TextBox _renameTextBox;

/// <summary>
/// True if sizing tool is in use.
/// </summary>
protected bool _isResizing;

/// <summary>
/// True if rename textbox is active in order to rename comment
/// </summary>
protected bool _isRenaming;

/// <summary>
/// Gets or sets the color of the comment.
/// </summary>
Expand Down Expand Up @@ -63,6 +69,13 @@ private int OrderValue
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
};
}

/// <inheritdoc />
Expand Down Expand Up @@ -149,6 +162,20 @@ protected override void UpdateRectangles()
_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;
}

/// <inheritdoc />
public override void Update(float deltaTime)
{
if (_isRenaming && (!_renameTextBox.IsFocused || !RootWindow.IsFocused))
{
Rename(_renameTextBox.Text);
StopRenaming();
}

base.Update(deltaTime);
}

/// <inheritdoc />
Expand All @@ -158,7 +185,7 @@ public override void Draw()
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
Expand All @@ -169,7 +196,8 @@ public override void Draw()

// 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);
Expand Down Expand Up @@ -213,6 +241,13 @@ public override void OnLostFocus()
EndResizing();
}

// Check if was renaming
if (_isRenaming)
{
Rename(_renameTextBox.Text);
StopRenaming();
}

// Base
base.OnLostFocus();
}
Expand Down Expand Up @@ -294,17 +329,47 @@ public override bool OnMouseDoubleClick(Float2 location, MouseButton button)
/// </summary>
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 StopRenaming()
{
_isRenaming = false;
_renameTextBox.Visible = false;
}

private void OnRenamed(RenamePopup renamePopup)
private void Rename(string newTitle)
{
Title = TitleValue = renamePopup.Text;
if(string.Equals(Title, newTitle, StringComparison.Ordinal))
return;

Title = TitleValue = newTitle;
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 />
public override bool OnMouseUp(Float2 location, MouseButton button)
{
Expand Down
Loading