• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

string_width and string_width_ext inverse function ?

DaveInDev

Member
Hi there,

I know string_width and string_width _ext that are very handy, but I'm writing a texteditor,
and I wonder if some kind of "inverse" functions exists : I mean giving a x (relative to the left position of the draw_text)
and getting the position in the string ? Like a string_pos_x(s,x) ?
Because I want my user to click anywhere in teh edit field and find what is the corresponding position in the string...

I don't ask for an _ext version, it would be huge ;)

Otherwise, I need to make repeated incremented calls to string_width until I find the good position... :(
 

obscene

Member
I can't think of any way other than when the user clicks, get the x and y of the line, loop through a series of string_width() calls adding that value to your line start x and determining the position when that value is greater than the cursor x. Which by the way would only work if the text was aligned left. Pretty sloppy solution, but using GM to write a text editor is definitely going to be a beast.
 

DaveInDev

Member
I can't think of any way other than when the user clicks, get the x and y of the line, loop through a series of string_width() calls adding that value to your line start x and determining the position when that value is greater than the cursor x. Which by the way would only work if the text was aligned left. Pretty sloppy solution, but using GM to write a text editor is definitely going to be a beast.
Yes, tx for your answer. That's finally what I did. My goal was not to write a full texteditor, but a UI Textbox where user can input a name or a simple string.
 

HalRiyami

Member
Yes, tx for your answer. That's finally what I did. My goal was not to write a full texteditor, but a UI Textbox where user can input a name or a simple string.
I've done this in my app (search box where you can tap anywhere on the text to insert/delete characters). I don't remember exactly how I did it but I don't think I used a loop. I don't have access to my PC right now but I'll look up how I did it when I do.
 
Top