Implement deleting a full word at a time (#3725)

This commit is contained in:
Visne
2023-04-03 04:50:30 +02:00
committed by GitHub
parent 68fe69ef24
commit eb65a45ed4
5 changed files with 136 additions and 8 deletions

View File

@@ -56,6 +56,9 @@ namespace Robust.Client.Input
common.AddFunction(EngineKeyFunctions.TextCursorSelectEnd);
common.AddFunction(EngineKeyFunctions.TextBackspace);
common.AddFunction(EngineKeyFunctions.TextDelete);
common.AddFunction(EngineKeyFunctions.TextWordBackspace);
common.AddFunction(EngineKeyFunctions.TextWordDelete);
common.AddFunction(EngineKeyFunctions.TextNewline);
common.AddFunction(EngineKeyFunctions.TextSubmit);
common.AddFunction(EngineKeyFunctions.TextCopy);
@@ -66,7 +69,6 @@ namespace Robust.Client.Input
common.AddFunction(EngineKeyFunctions.TextHistoryNext);
common.AddFunction(EngineKeyFunctions.TextReleaseFocus);
common.AddFunction(EngineKeyFunctions.TextScrollToBottom);
common.AddFunction(EngineKeyFunctions.TextDelete);
common.AddFunction(EngineKeyFunctions.TextTabComplete);
common.AddFunction(EngineKeyFunctions.TextCompleteNext);
common.AddFunction(EngineKeyFunctions.TextCompletePrev);

View File

@@ -368,7 +368,7 @@ namespace Robust.Client.UserInterface.Controls
_imeData = null;
}
public event Action<LineEditBackspaceEventArgs>? OnBackspace;
public event Action<LineEditTextRemovedEventArgs>? OnTextRemoved;
protected internal override void KeyBindDown(GUIBoundKeyEventArgs args)
{
@@ -415,7 +415,7 @@ namespace Robust.Client.UserInterface.Controls
_selectionStart = _cursorPosition;
OnTextChanged?.Invoke(new LineEditEventArgs(this, _text));
_updatePseudoClass();
OnBackspace?.Invoke(new LineEditBackspaceEventArgs(oldText, _text, cursor, selectStart));
OnTextRemoved?.Invoke(new LineEditTextRemovedEventArgs(oldText, _text, cursor, selectStart));
}
}
@@ -446,6 +446,71 @@ namespace Robust.Client.UserInterface.Controls
_selectionStart = _cursorPosition;
OnTextChanged?.Invoke(new LineEditEventArgs(this, _text));
_updatePseudoClass();
OnTextRemoved?.Invoke(new LineEditTextRemovedEventArgs(_text, _text, _cursorPosition, _selectionStart));
}
}
args.Handle();
}
else if (args.Function == EngineKeyFunctions.TextWordBackspace)
{
if (Editable)
{
var changed = false;
// If there is a selection, we just delete the selection. Otherwise we delete the previous word
if (_selectionStart != _cursorPosition)
{
_text = _text.Remove(SelectionLower, SelectionLength);
_cursorPosition = SelectionLower;
changed = true;
}
else if (_cursorPosition != 0)
{
int remAmt = _cursorPosition - TextEditShared.PrevWordPosition(_text, _cursorPosition);
_text = _text.Remove(_cursorPosition - remAmt, remAmt);
_cursorPosition -= remAmt;
changed = true;
}
if (changed)
{
_selectionStart = _cursorPosition;
OnTextChanged?.Invoke(new LineEditEventArgs(this, _text));
_updatePseudoClass();
OnTextRemoved?.Invoke(new LineEditTextRemovedEventArgs(_text, _text, _cursorPosition, _selectionStart));
}
}
args.Handle();
}
else if (args.Function == EngineKeyFunctions.TextWordDelete)
{
if (Editable)
{
var changed = false;
// If there is a selection, we just delete the selection. Otherwise we delete the next word
if (_selectionStart != _cursorPosition)
{
_text = _text.Remove(SelectionLower, SelectionLength);
_cursorPosition = SelectionLower;
changed = true;
}
else if (_cursorPosition < _text.Length)
{
int nextWord = TextEditShared.EndWordPosition(_text, _cursorPosition);
_text = _text.Remove(_cursorPosition, nextWord - _cursorPosition);
changed = true;
}
if (changed)
{
_selectionStart = _cursorPosition;
OnTextChanged?.Invoke(new LineEditEventArgs(this, _text));
_updatePseudoClass();
OnTextRemoved?.Invoke(new LineEditTextRemovedEventArgs(_text, _text, _cursorPosition, _selectionStart));
}
}
@@ -839,14 +904,14 @@ namespace Robust.Client.UserInterface.Controls
}
}
public sealed class LineEditBackspaceEventArgs : EventArgs
public sealed class LineEditTextRemovedEventArgs : EventArgs
{
public string OldText { get; }
public string NewText { get; }
public int OldCursorPosition { get; }
public int OldSelectionStart { get; }
public LineEditBackspaceEventArgs(
public LineEditTextRemovedEventArgs(
string oldText,
string newText,
int oldCursorPosition,

View File

@@ -357,6 +357,65 @@ public sealed class TextEdit : Control
args.Handle();
}
}
else if (args.Function == EngineKeyFunctions.TextWordBackspace)
{
if (Editable)
{
var changed = false;
// If there is a selection, we just delete the selection. Otherwise we delete the previous word
if (_selectionStart != _cursorPosition)
{
TextRope = Rope.Delete(TextRope, SelectionLower.Index, SelectionLength);
_cursorPosition = SelectionLower;
changed = true;
}
else if (_cursorPosition.Index < TextLength)
{
var runes = Rope.EnumerateRunesReverse(TextRope, _cursorPosition.Index);
int remAmt = -TextEditShared.PrevWordPosition(runes.GetEnumerator());
TextRope = Rope.Delete(TextRope, _cursorPosition.Index - remAmt, remAmt);
_cursorPosition.Index -= remAmt;
changed = true;
}
if (changed)
_selectionStart = _cursorPosition;
InvalidateHorizontalCursorPos();
args.Handle();
}
}
else if (args.Function == EngineKeyFunctions.TextWordDelete)
{
if (Editable)
{
var changed = false;
// If there is a selection, we just delete the selection. Otherwise we delete the next word
if (_selectionStart != _cursorPosition)
{
TextRope = Rope.Delete(TextRope, SelectionLower.Index, SelectionLength);
_cursorPosition = SelectionLower;
changed = true;
}
else if (_cursorPosition.Index < TextLength)
{
var runes = Rope.EnumerateRunes(TextRope, _cursorPosition.Index);
int endWord = _cursorPosition.Index + TextEditShared.EndWordPosition(runes.GetEnumerator());
TextRope = Rope.Delete(TextRope, _cursorPosition.Index, endWord - _cursorPosition.Index);
changed = true;
}
if (changed)
_selectionStart = _cursorPosition;
InvalidateHorizontalCursorPos();
args.Handle();
}
}
else if (args.Function == EngineKeyFunctions.TextNewline)
{
InsertAtCursor("\n");

View File

@@ -43,7 +43,7 @@ public sealed partial class DebugConsole
{
CommandBar.OnTextTyped += CommandBarOnOnTextTyped;
CommandBar.OnFocusExit += CommandBarOnOnFocusExit;
CommandBar.OnBackspace += CommandBarOnOnBackspace;
CommandBar.OnTextRemoved += CommandBarOnTextRemoved;
}
private void CommandBarOnOnFocusExit(LineEdit.LineEditEventArgs obj)
@@ -56,7 +56,7 @@ public sealed partial class DebugConsole
TypeUpdateCompletions(true);
}
private void CommandBarOnOnBackspace(LineEdit.LineEditBackspaceEventArgs eventArgs)
private void CommandBarOnTextRemoved(LineEdit.LineEditTextRemovedEventArgs eventArgs)
{
if (eventArgs.OldCursorPosition == 0 || eventArgs.OldSelectionStart != eventArgs.OldCursorPosition)
{

View File

@@ -65,6 +65,9 @@ namespace Robust.Shared.Input
public static readonly BoundKeyFunction TextCursorSelectEnd = "TextCursorSelectEnd";
public static readonly BoundKeyFunction TextBackspace = "TextBackspace";
public static readonly BoundKeyFunction TextDelete = "TextDelete";
public static readonly BoundKeyFunction TextWordBackspace = "TextWordBackspace";
public static readonly BoundKeyFunction TextWordDelete = "TextWordDelete";
public static readonly BoundKeyFunction TextNewline = "TextNewline";
public static readonly BoundKeyFunction TextSubmit = "TextSubmit";
public static readonly BoundKeyFunction TextSelectAll = "TextSelectAll";
@@ -75,7 +78,6 @@ namespace Robust.Shared.Input
public static readonly BoundKeyFunction TextHistoryNext = "TextHistoryNext";
public static readonly BoundKeyFunction TextReleaseFocus = "TextReleaseFocus";
public static readonly BoundKeyFunction TextScrollToBottom = "TextScrollToBottom";
public static readonly BoundKeyFunction TextDelete = "TextDelete";
public static readonly BoundKeyFunction TextTabComplete = "TextTabComplete";
public static readonly BoundKeyFunction TextCompleteNext = "TextCompleteNext";
public static readonly BoundKeyFunction TextCompletePrev = "TextCompletePrev";