Could anyone give me some insights on how to get the position of a text?

FoxyOfJungle

Kazan Games
Hello!

I'm making a text editor in GML (for Utiki), and I need to color the text.
I'm wanting to do this via shader (as it's much lighter than using many draw_text and draw_set_color. Scribble would not be an option too):



I want to color the text using the fragment's position, which is based on the GUI's (or room's) position. The shader part is already ok, so skip it for now.

So what I thought, I need:
  1. Get all the words, numbers, symbols, etc and put them in an array.
  2. Acquire the position of the words and then their size and put them in an array.
  3. Get the room position of these words, so I can use it in the shader.
  4. Check and compare these positions in the shader, so I can color the words correctly depending on their type (variable, keyword, value, strings...).

Don't worry about how I'm going to do it, I just need to get the position of a specific word, example:




(Orange rectangle size: 48x16)


Any help/idea is appreciated! Thanks for listening. :)
 
Last edited:

NightFrost

Member
Wouldn't string_width and string_height help here? Loop through the string, search for spaces, and when found grab a substring since previous space, then get that substring's width and height, keep track of line length so you know when to simulate linefeed? All that usual drudge work since GML doesn't have anything like string_split. You'll just have to keep track of position inside the loop, starting from whatever you need to upper left to be, and add everything to an array. Then the array can be sent to shader as-is.
 

Gamebot

Member
Are you using an array or drawing text? In either case,

If someone else is typing "for" you could just save the last "word" based on spaces and keep track of position and line. Using a monospaced font you could then use X as pos[ n ] and Y as line[ n ]. You would then using the example from above get "for" as the range of ( x + fourth place * font width ) to ( x + fourth place * font width * number of letters ) for xpos. For ypos would be ( y + ( line * font_height ).

With a struct/ds_map is there a way then to set the color for the word "for".
 

FoxyOfJungle

Kazan Games
I ended up giving up on using the actual pixel position of the text. Instead, the shader creates an imaginary grid, using mod(), with the same size as the font character (width and height), then it goes through each letter + the length of the word, coloring only that part. This happens 1 time per line, so I have to send a vec3 to the shader with this information:

vec3() -> x: Position (0, 1, 2...) | y: Word Length (0, 1, 2...) | z: Current Line (0, 1, 2...)

I do all this inside a for loop, for every word that I want to colorize.
I think I got what I wanted, basically:





The shader is perfect now. But now I need to rack my brains to interpret the strings of the code and separate the words correctly (the hardest part)... So I can send the word array with other information to the shader, and then color it correctly.
I still need to analyze how it will be done exactly, I hope I can do it...


Wouldn't string_width and string_height help here?
Yes, it will, but I will only use it when interpreting the text. In reality, I will only need the number of characters, not the size in pixels.

Are you using an array or drawing text? In either case,
Currently only drawing normal text. In fact, you even gave me an interesting idea... 🤔 But I think if I use arrays, I'll have to use draw_text() for each line, so it's not feasible. So I'll use "\n" to skip lines anyway, and just 1 draw_text() for everything.

If someone else is typing "for" you could just save the last "word" based on spaces and keep track of position and line. Using a monospaced font you could then use X as pos[ n ] and Y as line[ n ]. You would then using the example from above get "for" as the range of ( x + fourth place * font width ) to ( x + fourth place * font width * number of letters ) for xpos. For ypos would be ( y + ( line * font_height ).
I'm using the Consolas font, which has a fixed size, so it was helpful :D


Thanks for the help!
 
Last edited:
Top