Skip to content

TextInput

TechnologicalTurtle edited this page Jul 22, 2026 · 13 revisions

TextInput

Wrapper for LibGui::DynamicText, that adds text editing.

Initialization

// load font
LibGui::DT_TextureAtlas roboto("roboto.ttf");

// initialize textinput
LibGui::TextInput MyInput(roboto);
// or initialize textinput with window
LibGui::TextInput MyInput(MyWindow, roboto);

// its good to set some properties
MyInput.minSize = {100, 64};

Usage

First initialize the text input, then just Update() it in while loop.

// initialize textinput
LibGui::TextInput MyInput(MyWindow, roboto);

// its good to set some properties
MyInput.minSize = {100, 64};

while (!MyWindow.ShouldClose())
{
   MyWindow.Draw();

   MyInput.Update();

   MyWindow.PushDraw();
}

Methods

void Update(bool render_afterwards)

Updates input, if render_afterwards is true, it calls Render() to render.

void Render()

Renders TextInput.

bool Rect_OnMouseEnter(Vec2 anchor)

Returns if user mouse is on TextInput. Ignore anchor parameter, as the function is override for RectangleInput::Rect_OnMouseEnter().

Properties

Type Name Description
bool active Can user interact with TextInput?
bool multiline Can user type more lines using Enter?
string text Text in this TextInput
function pointer string(*preprocessText)(const string&) Pointer to function that can process text before rendering. (useful f. e. for syntax highlighting)
DT_TextureAtlas*& atlas Text input's default font
std::vector<DT_TextureAtlas*> rich_text_font_list Text input's additional fonts list
float fontSize Text input's text size
Vec2 minSize Minimum background size. Set any axis to -1 to have no minimum size
Vec2 maxSize Maximum background size. Set any axis to -1 to have no maximum size
Color textColor text color
Color backColor background color
Vec2i pos Position of upper left corner
int cursor_pos Index of character, where is currently text cursor.
int select_start Index of start of selected text, if none selected => equals -1
int select_end Index of end of selected text, if none selected => equals -1
float textCursorTickRate text cursor blink length

Formatting

Because TextInput is wrapper for DynamicText, it has DynamicText's formatting too, I just had to move fonts to TextInput property called rich_text_font_list.

preprocess function

With this function pointer, you can make your custom preprocessing function, just remember to only add formatting sequences and not to add/remove any rendered text as that will break rendering system. Example:

// this will make first word "red" actually red
static std::string preprocess(const std::string& in)
{
   std::string cpy = in;
   // find it
   int first_bold = in.find("red");
   if (first_bold != std::string::npos) {
      // add ending sequence first (because its later and we dont have to move indexes)
      cpy.insert(first_bold+4, "\033ce");
      // add stating sequence
      cpy.insert(first_bold, "\033c#FF0000");
   }
   return cpy;
}

int main()
{
   ...
   LibGui::TextInput MyInput(roboto);
   MyInput.preprocessText = preprocess;
   ...//set better position and stuff
   while (!MyWindow.ShouldClose())
   {
      MyWindow.Draw();

      MyInput.Update();

      MyWindow.PushDraw();
   }
   ...//terminate

}

And it should look like this (after clicking on the rectangle and typing "Thi is red", and no, I dind't want to type "Thi"):
red_formatting_example_window

Clone this wiki locally