Skip to content

Dynamic Text

TechnologicalTurtle edited this page Jul 22, 2026 · 8 revisions

Dynamic Text

DynamicText class is used to quickly render text on screen.

Initialization

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

// create the text         font  | position
LibGui::DynamicText MyText(roboto, {0, 0});
// set text value
MyText.text = "Hello world!";

// you can add some parameters
//                         window  | font  | pos   | text color| back color
LibGui::DynamicText MyText(MyWindow, roboto, {0, 0}, Color::Red, Color::Blue);

Usage

After initialization, use Render() in a loop to render.

Methods

float RenderChar(char character, Vec2 add_pos, Color color, DT_TextureAtlas& font, const Color char_highlight)

Used to render single character character from font font with color color and highlight char_highlight. add_pos is literally added to MyText.pos.
If you want to use it, you should set font to *ThisDynamicTextObject.atlas.
Returns width of character image.

void Render(std::vector<DT_TextureAtlas*> fonts, Vec2 selection)

If you want to just render normal text, you can leave fonts empty as they are used in formatting. Selection isn't actually position, but two [source indexes](#source indexes vs rich text indexes), determinig start and end of text seletion, so all character with source indexes < selection.y and >= selection.x, will be rendered with selectionColor highlight. Renders whole text.

int SourceIndexToRichTextIndex(int in) const

Translates source type index in to rich text type; more here.

int RichTextIndexToSourceIndex(int in) const

Translates rich text type index in to source type; more here.

Vec2 GetFinalSize(std::vector<DT_TextureAtlas*> fonts)

Returns final size of DynamicText (final size -> minimum size of rectangle that would cover whole field + backgroundAddSize).
fonts list additional fonts used in text formatting, which can have different sizes that normal fonts, so they are needed in size calculation, but if you just want to render non-formatted text, you can leave this optional parameter empty.

bool Rect_OnMouseEnter(...)

Returns if mouse cursor is on this dynamic text, ignore anchor parameter, as it's override of RectangleInput::Rect_OnMouseEnter.

int GetTouchedCharIndex(std::vector<DT_TextureAtlas*> fonts = {})

If Rect_OnMouseEnter() then it should return index of character mouse is on, else returns -1.
As this function also uses size calculation it too needs fonts parameter, I explained it like two or three times, so you likely read it now.

Vec2 GetPositionOnIndex(int character_index, bool left_side, std::vector<DT_TextureAtlas*> fonts = {})

If left_side is true returns position of lower left corner of character on character_index,
else it ________________ returns position of lower right corner of character on character_index.
As this function also uses size calculation it too needs fonts parameter, I explained it like two or three times, so you likely read it now.

Properties

Type Name Description
DT_TextureAtlas* atlas Font of this text.
Vec2i pos Position of upper left corner of object.
float fontSize Multiplies with size of font.
Vec2i textFieldSize Size of smallest rectangle to cover rendered text.
Vec2 backgroundAddSize Added with textFieldSize determines background 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
Color selectionColor The text selection color (the one that's usually blue)

Formatting

You can chage text color, fonts and highlighting through you text using text formatting.

Changing color

Using \033c with hex color code, you can set texts color, for hex code you can use #RRGGBB or $RRGGBBAA, for example change to red \033c#FF0000, or semi-transparent red \033c$FF000090. To Returns to previous color use \033ce (abbreviation for "color end").
Example

This text is \033c#0B000Corange\033ce
This text is \033c$0000FF90transparent\033ce

Warning

Don't forget to use correct symbols for hex codes: # for RRGGBB and $ for RRGGBBAA!

Tip

Think about color starting and ending sequences as brackets, the ending ones close the smallest scope:

"\033c#00FF00some \033c#FF000text\033ce"
is basically "[some [text]]"

Changing highlighting

Works same way as color changing, but instead of c, use h.

Note

Highlighting can be overridden by text selection, as it has higher priority.

Fonts

Fonts can be changed by using \033f with index (more later) of font an returned with \033fe (again standing for "font end").
But, what are these indexes?
Well, they are literal indexes of yours fonts you can pass to dynamic text in optional parameter std::vector<DT_TextureAtlas*> fonts of Render() and other functions.
Example

LibGui::DT_TextureAtlas roboto("fonts/Roboto-Regular.ttf");
// bold roboto => roboldo
LibGui::DT_TextureAtlas roboldo("fonts/Roboto-Bold.ttf");

// creating dynamic text object with default font roboto
LibGui::DynamicText MyText(roboto);
// set text to This is bold; with 'bold' in font roboldo.
MyText.text = "This is \033f0bold\033fe.";
...

while (!MyWindow.ShouldClose())
{
   ...
   // render MyText with additional fonts list
   MyText.Render({&roboldo});
   ...
}

Note

You may have found that in TextInput you can't pass fonts parameter anywhere, thats because there is property called rich_text_font_list, more here

Source indexes vs Rich text indexes

As I added text formatting, I realized that now, there are two ways to index characters, source indexes (indexes of characters excluding formatting sequences), and rich text indexes (literal character indexes including formatting sequences). After while I decided to try to use source indexes everywhere as TextInput (which has user written text and rendered text, here) would be very annoying to work with.

Tip

To change indexes from one type to other you can use SourceIndexToRichTextIndex() and RichTextIndexToSourceIndex().

Limitations

To not go insane, I implemented rendering of only 256 characters (not all are printable, so actually little bit less).

DT_TextureAtlas

DynamicText_TextureAtlas handles font loading and storing. I used this name because there was older implementation of text rendering (which used Font class), that rendered into an image that was later displayed. It obviously had more cons than pros, so I removed it.

Clone this wiki locally