S
Slothagami
Guest
I'm making my own version of code writer, just for fun, and I am want to make the text color coded for the languages I intend to use it for (html, css, javascript, etc...).
So the problem is that I need to change part of a strings color based on context, (check if it's in brackets and things) automatically at runtime, since I don't know what the files you open contain before the game is run.
so basically the string
would look like:
I want a script to make color coding like this given the string and the type of brackets that surround it.
My first attempt looks like this, and it barely works, and sometimes highlights random parts of the string.
The final script should look something like this:
and would draw the colored part of the text over the top of the original (on a surface).
If you can think of a better, more reliable way of doing this please let me know, I've been trying to do this all day! performance isn't a huge concern anymore thanks to people recommending I use surfaces, now I just need code that works properly!
Thanks in advance!
EDIT: Revised Question
So the problem is that I need to change part of a strings color based on context, (check if it's in brackets and things) automatically at runtime, since I don't know what the files you open contain before the game is run.
so basically the string
Code:
"<title id="the-title"> Title </title>"
HTML:
<title id="the-title"> Title </title>
My first attempt looks like this, and it barely works, and sometimes highlights random parts of the string.
GML:
function color_condition(xx, yy, starts, ends, color, line_num) {
// look for a part of the string that starts with a starts value
// and ends with a ends value, and set the inbetween to a color
if line_num < 0 exit;
var
i = 0,
str = lines[line_num],
start = -1,
ending = -1;
repeat string_length(str) +2 {
for (var j = 0; j <= array_length(starts); j++) {
if string_copy(str, i, string_length(starts[j])) == starts[j] {
// found the start
start = i;
}
}
for (var e = 0; e <= array_length(ends); e++) {
if string_copy(str, i, string_length(ends[e])) == ends[e] {
// found the end
ending = i;
}
}
i++;
}
if start != -1 and ending != -1 {
var
fstr = string_delete(string_copy(str, start, ending - start), 1, 1),
c = color;
draw_text_color(xx + (char_w*start), yy, fstr, c, c, c, c, 1); // char_w is the width of one character (I'm using a monospaced font)
}
}
GML:
color_condition(10, 10, ["<", "</"], [" ", ">", "/>"], c_red, line_num);
GML:
draw_color_coded(x, y, string, color, array_of_opening_conditions, array_of_closing_conditions);
If you can think of a better, more reliable way of doing this please let me know, I've been trying to do this all day! performance isn't a huge concern anymore thanks to people recommending I use surfaces, now I just need code that works properly!
Thanks in advance!
EDIT: Revised Question
Last edited by a moderator: