Skip to main content

UiTextbox

A single-line editable text input component supporting selection, copy/paste, undo/redo, cursor blinking, and numeric constraints.

Constructor

UiTextbox(style = {}, props = {})
ParameterTypeDescription
stylestructUI styling overrides.
propsstructComponent properties (see below).

Macros

MacroDefaultDescription
TEXTBOX_INITIAL_DELAY400 msInitial delay before key repeat starts.
TEXTBOX_REPEAT_DELAY50 msDelay between each key repeat.
TEXTBOX_CURSOR_BLINK500 msCursor blinking interval.
TEXTBOX_UNDO_STACK_SIZE100Maximum number of undo/redo states stored.

Properties

PropertyTypeDefaultDescription
labelstringundefinedOptional label drawn before the textbox.
valuestring""The initial value of the textbox.
valueGetterfunctionundefinedFunction returning a dynamic external value to sync with.
onChangefunction(value, input)empty functionCalled whenever text value changes.
onBlurfunction(value, input)empty functionCalled when textbox loses focus.
maxLengthreal255Maximum allowed text length.
format"string", "integer", "float""string"Input format constraint.
minrealundefinedMinimum numeric value (if numeric format).
maxrealundefinedMaximum numeric value (if numeric format).
negativeboolfalseWhether negative values are allowed (numeric).
placeholderstringundefinedPlaceholder text shown when empty/unfocused.

Internal Node: Input

The textbox creates an internal node self.Input which handles input logic, rendering, and state management (focus, selection, cursor, undo stack, etc).

State Variables (Input)

VariableTypeDescription
focusedboolWhether the textbox is focused.
cursorPosrealCurrent cursor index.
selectionStart, selectionEndrealSelection range in characters.
scrollOffsetrealHorizontal scroll offset for long text.
undoStack, redoStackarrayUndo/redo history.
keyRepeatstructState for key repeat timing.
isDraggingboolWhether the user is dragging to select text.
showCursorboolWhether the cursor is currently visible (for blinking).

Methods (Main)

MethodDescription
focus()Focuses the textbox and prepares to receive input.
blur()Removes focus and validates value (for numeric fields).
updateScrollOffset()Updates scroll offset to keep cursor visible.
getSelectedText()Returns the currently selected text.
deleteSelected()Deletes selected text and updates cursor.
insertText(newText)Inserts text at the current cursor position, respecting max length and format.
handleKeyInput()Processes keyboard input and shortcuts.
handleMouseDrag()Updates selection while dragging.
handleKeyRepeat()Checks if the current key should repeat.
updateKeyRepeat()Updates repeat key state based on input.
saveUndoState()Saves the current input state into the undo stack.
performUndo()Restores the last undo state.
performRedo()Restores the last redo state.
getMouseCursorPos(mouseX)Returns cursor index for a given mouse X coordinate.
findWordStart(pos)Finds the start index of the current word.
findWordEnd(pos)Finds the end index of the current word.
resetCursorBlink()Resets the cursor blinking timer.
isValidCharacter(char, currentText, cursorPos)Checks if a character is valid given the input format.
validateValue(value)Validates an entire value against min/max and format rules.

Keyboard shortcuts

ShortcutAction
Ctrl + ZUndo
Ctrl + Shift + Z or Ctrl + YRedo
Ctrl + ASelect all
Ctrl + CCopy selection
Ctrl + XCut selection
Ctrl + VPaste clipboard text
/ Move cursor
Shift + ←/→Extend selection
Ctrl + ←/→Move cursor by word
Home / EndMove to start or end
Backspace / DeleteDelete text before or after cursor

Drawing

LayerDescription
BackgroundFilled rectangle using global.UI_COL_INPUT_BG.
BorderOutline using global.UI_COL_BOX.
SelectionSemi-transparent highlight when selecting text.
TextDrawn with fText font, horizontally scrollable.
PlaceholderFaded text when empty/unfocused.
CursorVertical line when focused and visible.

Behavior Summary

  • Focus handling: gains focus on click, loses it on outside click.
  • Undo/Redo system: keeps a capped stack for state restoration.
  • Selection logic: supports click-drag, double-click word select, and keyboard extension.
  • Key repeat system: mimics native OS key repeat delays.
  • Numeric validation: enforces min, max, integer, and float formats.
  • Clipboard integration: supports cut/copy/paste with system clipboard.
  • Placeholder rendering: semi-transparent when inactive.