Table of Contents

Class TextEditBuffer

Namespace
BSGameLib.UI.Controls
Assembly
BSGameLib.UI.dll

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

maxLength int

Properties

Anchor

The index the selection starts from. The same as Caret when nothing is selected.

public int Anchor { get; }

Property Value

int

Caret

The index the caret is at, from 0 to the length of the text.

public int Caret { get; }

Property Value

int

DigitsOnly

Only digits may be typed or pasted.

public bool DigitsOnly { get; set; }

Property Value

bool

HasSelection

Whether any text is selected.

public bool HasSelection { get; }

Property Value

bool

MaxLength

The most characters the buffer holds.

public int MaxLength { get; }

Property Value

int

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

bool

SelectedText

The selected text, or an empty string when nothing is selected.

public string SelectedText { get; }

Property Value

string

SelectionEnd

The end of the selection, or the caret when nothing is selected.

public int SelectionEnd { get; }

Property Value

int

SelectionStart

The start of the selection, or the caret when nothing is selected.

public int SelectionStart { get; }

Property Value

int

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

string

Methods

Backspace(bool)

Backspace: the selection if there is one, else a character or a word back.

public bool Backspace(bool byWord)

Parameters

byWord bool

Returns

bool

Delete(bool)

Delete: the selection if there is one, else a character or a word forward.

public bool Delete(bool byWord)

Parameters

byWord bool

Returns

bool

DeleteSelection()

Deletes the selected text. Returns whether anything was deleted.

public bool DeleteSelection()

Returns

bool

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

input string

Returns

bool

MoveEnd(bool)

Moves the caret to the end of the text, extending the selection when extend is set.

public void MoveEnd(bool extend)

Parameters

extend bool

MoveHome(bool)

Moves the caret to the start of the text, extending the selection when extend is set.

public void MoveHome(bool extend)

Parameters

extend bool

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

extend bool
byWord bool

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

extend bool
byWord bool

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

index int

SetCaret(int, bool)

Puts the caret at index, extending the selection or not.

public void SetCaret(int index, bool extend = false)

Parameters

index int
extend bool