Detect text selection with SDL_ttf 2

603 views Asked by At

I would like to implement a simple input box with SDL and SDL_ttf. Just like the address bar in your browser.

I'm guessing what is the easiest and most efficient way to detect a mouse selection with SDL_ttf.

Let say the box display text like this:

+--------+
| abcdef |
+--------+

The user clicks between 'c' and 'd', then it drags the mouse to the left by several pixels. Now I need to detect that it has selected the 'c' character.

The SDL_ttf library has several functions to determine the metrics of a text. You can compute the whole text size with TTF_SizeUTF8 and you can also determine a glyph size for an individual character with TTF_GlyphMetrics.

Rough idea would be to create a special array with all individual characters with their respective size just to determine their positions in the box.

Do you have better ideas on how this can be implemented easily?

1

There are 1 answers

0
Dhia Hassen On

Text selection in immediate rendering mode should be worked on from zero , which is alot of work to do. there is the legendary stb_textedit used by many immediate gui libraries like imgui , you can find it here https://github.com/nothings/stb/blob/master/stb_textedit.h