Class TextEditBuffer
The editing half of a text box: the text, the caret, the selection, and every change a keystroke can make to them - with none of the drawing, so the rules can be tested alone.
public sealed class TextEditBuffer
- Inheritance
-
TextEditBuffer
- Inherited Members
Remarks
A selection is the span between Anchor and Caret; they are equal when nothing is selected. Moving with "extend" (Shift held) moves the caret and leaves the anchor; moving without it collapses the selection first, the way every desktop text field does - Left with a selection lands at its start rather than one character before the caret.
A word is a run of letters, digits or underscores. Moving by word left goes to the start of the word before the caret; right goes past the end of the word after it and the space that follows, landing at the start of the next one. That is the Windows and Linux behaviour; macOS stops at the end of the word instead, which is not modelled yet.
Public so a game can give a line it draws itself -- a chat line in the HUD -- the same editing a TextBox has, without the line having to be a control in a view.
Constructors
TextEditBuffer(int)
Creates an empty buffer that holds at most maxLength characters.
public TextEditBuffer(int maxLength)
Parameters
maxLengthint
Properties
Anchor
The index the selection starts from. The same as Caret when nothing is selected.
public int Anchor { get; }
Property Value
Caret
The index the caret is at, from 0 to the length of the text.
public int Caret { get; }
Property Value
DigitsOnly
Only digits may be typed or pasted.
public bool DigitsOnly { get; set; }
Property Value
HasSelection
Whether any text is selected.
public bool HasSelection { get; }
Property Value
MaxLength
The most characters the buffer holds.
public int MaxLength { get; }
Property Value
OpaqueWords
Word movement treats the whole text as one word - a password box, whose characters are hidden, must not reveal where its words break.
public bool OpaqueWords { get; set; }
Property Value
SelectedText
The selected text, or an empty string when nothing is selected.
public string SelectedText { get; }
Property Value
SelectionEnd
The end of the selection, or the caret when nothing is selected.
public int SelectionEnd { get; }
Property Value
SelectionStart
The start of the selection, or the caret when nothing is selected.
public int SelectionStart { get; }
Property Value
Text
The text. Setting it keeps the caret and anchor within the new text; null is treated as empty.
public string Text { get; set; }
Property Value
Methods
Backspace(bool)
Backspace: the selection if there is one, else a character or a word back.
public bool Backspace(bool byWord)
Parameters
byWordbool
Returns
Delete(bool)
Delete: the selection if there is one, else a character or a word forward.
public bool Delete(bool byWord)
Parameters
byWordbool
Returns
DeleteSelection()
Deletes the selected text. Returns whether anything was deleted.
public bool DeleteSelection()
Returns
Insert(string)
Types or pastes input over the selection, keeping only what the field
accepts and stopping at MaxLength. Returns whether anything changed.
public bool Insert(string input)
Parameters
inputstring
Returns
MoveEnd(bool)
Moves the caret to the end of the text, extending the selection when extend is set.
public void MoveEnd(bool extend)
Parameters
extendbool
MoveHome(bool)
Moves the caret to the start of the text, extending the selection when extend is set.
public void MoveHome(bool extend)
Parameters
extendbool
MoveLeft(bool, bool)
Moves the caret one character left, or one word when byWord is set. With
extend the selection grows; without it the selection is dropped. Without
extend, a selection collapses to its start.
public void MoveLeft(bool extend, bool byWord)
Parameters
MoveRight(bool, bool)
Moves the caret one character right, or one word when byWord is set. With
extend the selection grows; without it the selection is dropped. Without
extend, a selection collapses to its end.
public void MoveRight(bool extend, bool byWord)
Parameters
SelectAll()
Selects all the text, with the caret at its end.
public void SelectAll()
SelectWordAt(int)
Selects the word around index, for a double-click.
public void SelectWordAt(int index)
Parameters
indexint
SetCaret(int, bool)
Puts the caret at index, extending the selection or not.
public void SetCaret(int index, bool extend = false)