Jump to content


Photo

Change color of certain words in a drawn text


  • Please log in to reply
19 replies to this topic

#1 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 08 January 2010 - 03:41 PM

This script works exactly like draw_text, except you can change the color of certain words in the drawn text using simple color tags, much like in BBcode. This script is very useful, and can be used in almost any type of game. For instance in a dialogue system, where the characters' names are written in a different color. Works in any Game Maker version and for unregistered users, and no lag whatsoever.

NOTE: The output will not be affected by any horizontal or vertical aligns.

Example:
draw_text_colortags(x, y, "[c=255]Red text[/c], [c=$00FF00]green text[/c] and [c=c_blue]blue text[/c]!")
Result:
 

Red text, green text and blue text!


Downloads:The script:
GM8/8.1:
Spoiler

 
GM:Studio:
Spoiler

Note: In GM:Studio, use "[c="+string(c_yellow)+"]" rather than "[c=c_yellow]"

Edited by Davve, 18 October 2015 - 02:44 PM.

  • 4

#2 emanuel87

emanuel87

    GMC Member

  • New Member
  • 176 posts

Posted 08 January 2010 - 04:15 PM

thanks!"
  • 0

#3 Poddington

Poddington

    Lucky To Reach 30

  • GMC Member
  • 267 posts

Posted 24 March 2010 - 12:01 PM

Decent script, thanks Davve941018
  • 0

#4 firefly431

firefly431

    GMC Member

  • New Member
  • 45 posts

Posted 17 July 2010 - 05:07 PM

FYI: to make simple colorcodes like: #00FF00 you could replace the # char with make_color_rgb( and parse the hex code. i think you're good enough to code that.
  • 0

#5 firefly431

firefly431

    GMC Member

  • New Member
  • 45 posts

Posted 17 July 2010 - 05:53 PM

I made a hex number example. if u want it:
PS: use it like: [c_red]Red[$77FF22]I dunno what color its color $77FF22
will make something like:
*red text*Red*light like text*2293623
OK...
/*
    CALL:
            draw_text_colorcodes(x,y,str)

    BY:
            Davve941018

    EXAMPLE:
            draw_text_colorcodes(16,16,"[col=c_white]White text[/col][col=255]Red text[/col]")
*/
var xx,yy,str,str2,drawcol,a,b,drawx,drawy,start,start2,close;

start="["
start2="]"
close="[c_black]"

xx=argument0
yy=argument1
str=argument2
drawcol=c_black
drawx=0
drawy=0
_col=drawcol
while (string_count("$",str) != 0) {
    pos = string_pos("$",str);
    cstring = "$";
    pos += 1;
    r = 0;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        r = real(s);
    } else {
        r = ord(s)-ord('A');
        r += 10;
    }
    pos += 1;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        r += real(s)*16;
    } else {
        r += ((ord(s)-ord('A'))+10)*16;
    }
    pos += 1;
    g = 0;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        g = real(s);
    } else {
        g = ord(s)-ord('A');
        g += 10;
    }
    pos += 1;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        g += real(s)*16;
    } else {
        g += ((ord(s)-ord('A'))+10)*16;
    }
    pos += 1;
    b = 0;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        b = real(s);
    } else {
        b = ord(s)-ord('A');
        b += 10;
    }
    pos += 1;
    s = string_char_at(str,pos);
    cstring += s;
    s = string_upper(s);
    if (s != 'A' && s != 'B' && s != 'C' && s != 'D' && s != 'E' && s != 'F') {
        b += real(s)*16;
    } else {
        b += ((ord(s)-ord('A'))+10)*16;
    }
    col = make_color_rgb(r,g,b);
    str = string_replace_all(str,cstring,string(col));
}
for (a=1 a<=string_length(str) a+=1) {
    if (string_char_at(str,a)="\" && (string_copy(str,a+1,string_length(start))=start || string_copy(str,a+1,string_length(close))=close)) {
        a+=1
    }
    if (string_copy(str,a,string_length(start))=start && string_char_at(str,a-1)!="\") {
        a+=string_length(start)
        for (b=a b<=string_length(str) b+=1) {
            if (string_char_at(str,b)=start2) {
                execute_string("_col="+string_copy(str,a,b-a),0)
                a=b+string_length(start2)
                b=string_length(str)
            }
        }
    }
    str2=string_char_at(str,a)
    if (string_copy(str,a,string_length(close))=close && string_char_at(str,a-1)!="\") {
        a+=string_length(close)
        _col=drawcol
        str2=string_char_at(str,a)
        if (string_copy(str,a,string_length(start))=start) {
            a-=1
            str2=""
        }
    }
    if (string_char_at(str,a)="\" && string_char_at(str,a+1)="#") {
        str2=""
    }
    if (string_char_at(str,a)="#") {
        if (string_char_at(str,a-1)="\") {
            str2="\#"
        } else {
            str2=""
            drawy+=string_height(string_char_at(str,a))
            drawx=0
        }
    }
    draw_set_color(_col)
    draw_text(xx+drawx,yy+drawy,str2)
    drawx+=string_width(str2)
}
draw_set_color(drawcol)

  • 0

#6 Drara

Drara

    GMC Member

  • GMC Member
  • 325 posts

Posted 18 July 2010 - 05:56 PM

Well, nice scripts but it uses a lot of string funktions which are slow.
I've found this script and I think they are faster and simple to use too:

http://www.gamemaker...p?topic=40901.0
  • 0

#7 margoose

margoose

    GMC Member

  • GMC Member
  • 277 posts

Posted 10 August 2010 - 11:05 PM

I think this is a pretty cool idea...

Why don't you expand it to draw different fonts, sizes, and styles in the same string?

Here are some other ideas to add:

  • Smileys
  • Flashing text
  • Scrolling text
  • Text background color (like a highlighter)
  • Hyperlinks
  • Pictures
  • Bordered text

I think it could be a pretty useful script if you expanded it a little...think about how forums work ;)
  • 0
My Creations:


#8 Nubcake

Nubcake

    Nubcake

  • GMC Member
  • 1320 posts
  • Version:Unknown

Posted 27 August 2010 - 09:37 PM

This doesn't seem to work for me , i'm using it inside another scipt inside another script and all it draws is 0.
  • 0
Nubcake ~ Former GM'er '10 ~

#9 evilgenus

evilgenus

    GMC Member

  • New Member
  • 2 posts

Posted 04 October 2010 - 09:00 PM

Hi,

Thanks for the amazing script! I will be using it for a multi-color text engine sort of like in zelda. will definitly be mentioning your username in the credits if I ever finish it! ;)
I've tried (and failed multiple times!) to modify the script to work using draw_text_ext, because it would cause much pain to add all the # pound signs manually into the npc text scripts.
Does anyone know how to modify this script to work similar to draw_text_ext instead of draw_text?

-evilgenus
  • 0

#10 paul23

paul23

    GMC Member

  • Global Moderators
  • 4147 posts
  • Version:GM:Studio

Posted 04 October 2010 - 09:48 PM

neat idea. But, it'll be very very slow I fear.
execute_string("_col="+string_copy(str,a,b-a),0)
Ugh, try to circumvent this.
-You know you can get the number of (build in) constant by calling "variable_local_get()"?
-or better yet: parse the string yourself!
-
  • 0

#11 Nubcake

Nubcake

    Nubcake

  • GMC Member
  • 1320 posts
  • Version:Unknown

Posted 27 October 2010 - 09:56 AM

There seems to be a problem with this when using scrolling text , the words [col= ] and [/col] appear and then change into the coloured words.
  • 0
Nubcake ~ Former GM'er '10 ~

#12 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 27 October 2010 - 11:17 AM

There seems to be a problem with this when using scrolling text , the words [col= ] and [/col] appear and then change into the coloured words.

Using string_copy with this script will cut the color codes and make them invalid.
  • 0

#13 Nubcake

Nubcake

    Nubcake

  • GMC Member
  • 1320 posts
  • Version:Unknown

Posted 28 October 2010 - 08:28 AM

There seems to be a problem with this when using scrolling text , the words [col= ] and [/col] appear and then change into the coloured words.

Using string_copy with this script will cut the color codes and make them invalid.

What ? Where would i use that ?
currently i use 2

draw_text_colored(argument0-view_xview,argument1-view_yview,string_copy(typing,1,i),16,-1); //draw_text_ext
//Draw it shifted 
draw_text_colored(argument0-view_xview, argument1+i-view_yview,typing,16,-1);//draw_text_ext

Edited by Nubcake, 28 October 2010 - 08:31 AM.

  • 0
Nubcake ~ Former GM'er '10 ~

#14 Davve

Davve

    GMC Member

  • GMC Member
  • 4091 posts
  • Version:GM:Studio

Posted 10 December 2010 - 06:39 PM

I rewrote the script. It now draws line-by-line instead of every character, which cuts the lag significantly. And it is much shorter :]

Edited by Davve, 11 December 2010 - 01:12 PM.

  • 0

#15 BluePotato1994

BluePotato1994

    GMC Member

  • GMC Member
  • 185 posts
  • Version:GM8

Posted 21 December 2010 - 12:24 PM

lol @ Davve's sig :lol:
  • 0
Posted Image
Stick Legends Online - Networking Programmer / GML Programmer Alpha out NOW!

Was this post helpful? if so click (+)


#16 AlexTM

AlexTM

    GMC Member?

  • GMC:Member
  • 1437 posts
  • Version:Unknown

Posted 01 April 2013 - 04:27 PM

Davve, is there any way you could make this go along the aligns like this:

 

draw_set_halign(fa_right)
draw_set_valign(fa_right) 


#17 tserek

tserek

    GMC Member

  • GMC Member
  • 598 posts
  • Version:GM8.1

Posted 31 July 2014 - 08:57 PM

Any way to draw variable strings?


Edited by tserek, 31 July 2014 - 09:01 PM.

  • 0

#18 Colnusca

Colnusca

    GMC Member

  • GMC Member
  • 462 posts
  • Version:GM:Studio

Posted 30 May 2015 - 11:37 PM

For anyone wanting to use this with draw_text_ext, i have the solution but without the "sep" paramater. Here's a script to insert line breaks in a string, not to draw it, then I will give an example of how it will work with the above script.

 

 

 

string_linebreak(str,w)

///string_linebreak(str,w)
var str, w, pos, cut;
str = argument0;
w = argument1;
pos = 0;
cut = 0;

if string_width(str) <= w then return str;
else
{
while (string_count(" ",str) > 0)
    {
    pos = string_pos(" ", str);
    if string_width(string_copy(str,1,pos)) > w
        {
            str = string_replace(str, " ", "#");
        }
    else
        str = string_replace(str," ","@__");
    }
}
str = string_replace_all(str,"@__"," ");
return str;

Example:

draw_text_colortags(4,4,string_linebreak("Hello, [c="+string(c_yellow)+"]World[/c]! It's fantastic!",128));

This would draw it as:

Hello, World!

It's

fantastic!

 

at 128px width max, and default font styling.


  • 0

I'll help any way I can ^.^


#19 chreechokash007

chreechokash007

    GMC Member

  • GMC Member
  • 230 posts
  • Version:GM:Studio

Posted 14 July 2015 - 07:09 AM

Colnusca the script is not working correctly as the way it should like draw_text_ext(). It should draw the whole text in continuation without breaking it to the next line until it reaches the width limit. But what it does is it takes random width for the first line and then keep drawing every single word leaving one line. Please fix it.


Edited by chreechokash007, 14 July 2015 - 07:47 AM.

  • 0

#20 Colnusca

Colnusca

    GMC Member

  • GMC Member
  • 462 posts
  • Version:GM:Studio

Posted 20 July 2015 - 01:04 PM

Colnusca the script is not working correctly as the way it should like draw_text_ext(). It should draw the whole text in continuation without breaking it to the next line until it reaches the width limit. But what it does is it takes random width for the first line and then keep drawing every single word leaving one line. Please fix it.

I'm sure that my "@__" swap makes the string larger, line breaking earlier than normal. To fix this, either change the "@__" to a non-space character that is space-width (like an underscore), or save each string_pos of the spaces that are turned into "@__" into some data structure or array, then delete the space. Then, when returning the string, loop through the array adding a space at each saved position in the array. Hope that fixes it. If not, I'll look into it more when I have time.


  • 1

I'll help any way I can ^.^