Table of Contents

Class TextBox

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

A single-line text field: typing, a caret, a selection, the clipboard, and scrolling when the text is wider than the box.

public class TextBox : InteractiveControl, IUpdateable, IEquatable<IUpdateable>, IDrawable, IIdentifiable<Guid>, IEquatable<IDrawable>, IDisposable
Inheritance
TextBox
Implements
Inherited Members

Remarks

The editing rules - what a key does to the text, the caret and the selection - live in TextEditBuffer; this class turns input into calls on it and draws the result.

Keys: arrows move, Shift extends a selection, Ctrl (or Option on macOS) moves by word, Cmd+Left/Right go to either end on macOS, Home/End too. Ctrl/Cmd with A selects all, C copies, X cuts, V pastes over the selection. Ctrl+Backspace and Ctrl+Delete remove a word. The mouse places the caret, drags a selection, and double-click selects a word. A password box never hands its text to the clipboard and moves by the whole field rather than by word.

There is no clipping in the renderer, so a long line is not drawn and trimmed: only the characters that fit are drawn, starting from BSGameLib.UI.Controls.TextBox._firstVisible, which follows the caret so it stays in view.

Constructors

TextBox(ILogger, View, string)

Creates a text box in view.

public TextBox(ILogger logger, View view, string id = null)

Parameters

logger ILogger
view View
id string

Properties

Clipboard

Where copy and paste read and write. Defaults to the system clipboard.

public IClipboard Clipboard { get; set; }

Property Value

IClipboard

Remarks

Settable so a test can say what is on the clipboard without a window, and so a host that wants its own (a sandboxed build, a replay harness) can supply one.

Color

The tint applied to Texture, from the current state.

public Color Color { get; }

Property Value

Color

Font

The font the text is drawn in. Set by LoadContent().

public IFont Font { get; }

Property Value

IFont

FontColor

The colour of the text.

public Color FontColor { get; set; }

Property Value

Color

FontFile

The font to draw the text in, or the name of a built-in font. Null for the built-in default.

public string FontFile { get; set; }

Property Value

string

FontSize

The size of the text, in pixels. Zero draws the font at its own size.

public float FontSize { get; set; }

Property Value

float

IsPasswordBox

Whether the box is a password box: its text is drawn as PasswordChar, cannot be copied or cut, and words do not break inside it.

public bool IsPasswordBox { get; set; }

Property Value

bool

Label

Text shown beside the box.

public string Label { get; set; }

Property Value

string

PasswordChar

The character drawn in place of each character of a password box. An asterisk by default.

public char PasswordChar { get; set; }

Property Value

char

RequiresRawKeyboard

True, so that typed keys reach the box while it has focus.

public override bool RequiresRawKeyboard { get; }

Property Value

bool

RestrictToNumbers

Whether only digits can be typed or pasted.

public bool RestrictToNumbers { get; set; }

Property Value

bool

SelectionColor

Behind selected text. Defaults to the font color at low opacity, which reads as a selection in any palette without a theme having to say so.

public Color? SelectionColor { get; set; }

Property Value

Color?

Text

The text in the box. A bound view-model property is updated as the player types.

public string Text { get; set; }

Property Value

string

Texture

The texture behind the box in its current state.

public ITexture Texture { get; }

Property Value

ITexture

Methods

Dispose()

Stops reading keyboard input, then disposes the control.

public override void Dispose()

Draw(IGameTime)

Draws the box, its text (as PasswordChar for a password box), the selection and the caret.

public override void Draw(IGameTime gameTime)

Parameters

gameTime IGameTime

GainFocus()

Shows the focused state, starts the caret blinking and moves the caret to the end of the text.

public override void GainFocus()

LoadContent()

Loads the font and the built-in art for each state that sets neither a texture nor a fill or border, and starts reading keyboard input. A failure to start keyboard input is logged rather than thrown.

public override void LoadContent()

LoseFocus()

Shows the hover or normal state, stops the caret blinking and drops any selection.

public override void LoseFocus()

Update(IGameTime)

Blinks the caret while the box has focus.

public override void Update(IGameTime gameTime)

Parameters

gameTime IGameTime