GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2021-04-27 17:59:07

maras
Member
Registered: 2021-04-25
Posts: 22
Website

BB like text formatter draw_text_bb

Expand///draw_text_bb(x, y, str)
//
// draws a formatted text
//
//     x          x position, real
//     y          y postition, real
//     str        string to draw, string
//
// current options:
// [+B,<width>]...[-B] bold text
// [+NL,<pixels>] inserts a new line x pixels below the old line
// [+C,<hex>] changes the text color
// [+SP,<pixels>]...[-SP] changes the spacing between characters
// [+SH,<amount>]...[-SH] this will make the text shake
// [+RB]...[-RB] rainbow effect (overrides +C)
// [+O,<hex>,<width>]...[-O] adds an outline
// [+F,<font>] changes the font 
// [+A,<alpha>]...[-A] changes the text alpha
// [+W,<amount>,<chain_speed>]...[-W] adds a wave effect
// 
// [-/]...[+/] any option between these will be drawn as regular text
// [--]... resets all formatting
// 
// Options can be combined 
// 
/// GMLscripts.com/license

function draw_text_bb(_x, _y, str) {
	
	var text = string(str);
	var ch_x = 0;
	var line = 0;
	
	var ignore_bb = false;
	
	// formatting variables
	var bold = 0;
	var col = draw_get_colour();
	var spacing = 0;
	var line_h = 0;
	var shake_amount = 0;
	var rainbow = 0;
	var outline_col = c_black;
	var outline_w = 0;
	var font = draw_get_font();
	var alpha = 1;
	var wave_chain = 0;
	var wave_y = 0;
	
	// loop through every character
	var str_len = string_length(text);
	for(var i = 1; i <= str_len; i++)
		{
		var ch = string_char_at(text, i);
		
		// if this variable is true the current char will be skipped
		var is_bb = false;
		
		// check for brackets and get what's between them
		if ch == "["
		    {
		    var next_char_check = "";
		    if (i < str_len) next_char_check = string_char_at(text, i+1);
        
		    if next_char_check == "+" or next_char_check == "-"
		        {
		        var bb_len = 1;
		        var str_between = "";
            
		        while(i+bb_len < str_len)
		            {
		            var this_char = string_char_at(text, i+bb_len);
		            if this_char == "]"
		                {
		                if str_between != "" is_bb = true;
		                break;
		                }
		            else str_between += this_char;
		            bb_len++;
		            }
				
				// both brackets and something between them?
		        if is_bb
		            {
		            var arr_args = string_split(string_replace_all(str_between, " ", ""), ",");
					var args_count = array_length(arr_args);
					
		            if ignore_bb is_bb = false;
					else
						{
						i += bb_len;
						
						// (+) sets the formatting
						// (-) resets the formatting
						switch(arr_args[0])
			                {
					case "--": // reset all formatting
						bold = 0;
						col = c_white;
						spacing = 0;
						line_h = 0;
						shake_amount = 0;
						rainbow = 0;
						outline_col = c_black;
						outline_w = 0;
						font = -1;
						alpha = 1;
						wave_chain = 0;
						wave_y = 0;
					break;
			                case "+B": // bold
			                    if args_count > 1 bold = real(arr_args[1]);
			                break;
			                case "-B":
			                    bold = 0;
			                break;
			                case "+NL": // new line
								line_h = 32;
								if args_count > 1 line_h = real(arr_args[1]);
			                    line += line_h;
			                    ch_x = 0;
			                break;
			                case "+C": // colour
			                    if args_count > 1
			                        if string_pos("#", arr_args[1]) == 1
			                            col = real(hex2dec(arr_args[1]));
			                break;
			                case "+SP": // space between chars
			                    if args_count > 1 spacing = real(arr_args[1]);
			                break;
			                case "-SP":
			                    spacing = 0;
			                break;
			                case "+SH": // shake
			                    if args_count > 1 shake_amount = real(arr_args[1]);
			                break;
			                case "-SH":
			                    shake_amount = 0;
			                break;
			                case "+RB": // rainbow
			                    rainbow = 1;
			                break;
			                case "-RB":
			                    rainbow = 0;
			                break;
			                case "+O": // outline
			                    outline_w = 1;
			                    if args_count > 1 outline_col = real(hex2dec(arr_args[1]));
			                    if args_count > 2 outline_w = real(arr_args[2]);
			                break;
			                case "-O":
			                    outline_w = 0;
			                break;
			                case "+F": // font
			                    if args_count > 1 font = asset_get_index(arr_args[1]);
			                break;
			                case "+A": // alpha
			                    if args_count > 1 alpha = real(arr_args[1]);
			                break;
			                case "-A":
			                    alpha = 1;
			                break;
							case "+W": // wave
			                    if args_count > 1 wave_y = real(arr_args[1]);
								if args_count > 2 wave_chain = real(arr_args[2]);
			                break;
			                case "-W":
								wave_y = 0;
			                    wave_chain = 0;
			                break;
							default: // invalid tag. just draw it
								i -= bb_len;
								is_bb = false;
			                }
						}
					
					// BB on/off switch
					if ignore_bb and arr_args[0] == "+/"
						{
						ignore_bb = false;
						i += bb_len;
						}
					else if !ignore_bb and arr_args[0] == "-/"
						{
						ignore_bb = true;
						i += bb_len;
						}
		            }
		        }
		    }
		
		// draw it!
		if !is_bb
		    {
			// current char default pos
			var pos_x = _x + ch_x;
			var pos_y = _y;
			
			pos_y += line;
			
			///// POSOTION CHANGERS /////
			
			// shaking
		    if shake_amount != 0
		        {
		        pos_x += random_range(-shake_amount, shake_amount);
		        pos_y += random_range(-shake_amount, shake_amount);
		        }
			
			// wave
			if wave_y != 0
				{
				pos_y += sin(get_timer() * 0.00001 - i * wave_chain) * wave_y;
				}
			
			///// VISUAL CHANGERS /////
			
			// set font
		    if font != -1 draw_set_font(font);
			
			// outline
		    if outline_w != 0
		        {
		        if alpha != 1 draw_set_alpha(alpha/5);
		        draw_set_colour(outline_col);
				var o_offset = bold+outline_w;
		        draw_text(pos_x+o_offset, pos_y+o_offset, ch);
		        draw_text(pos_x-o_offset, pos_y-o_offset, ch);
		        draw_text(pos_x+o_offset, pos_y-o_offset, ch);
		        draw_text(pos_x-o_offset, pos_y+o_offset, ch);
		        }
			
			// rainbow effect
		    if rainbow != 0
		        {
		        rainbow -= 5;
		        draw_set_colour(make_color_hsv((abs((get_timer() * 0.0002)+rainbow) mod 255),255,255));
		        }
		    else
				{ // set color
				draw_set_colour(col);
				}
			
			// bold
		    if bold != 0
		        {
		        if alpha != 1 draw_set_alpha(alpha/3);
		        draw_text(pos_x-bold, pos_y+bold, ch);
		        draw_text(pos_x+bold, pos_y+bold, ch);
		        }
			
			// alpha
		    draw_set_alpha(alpha);
			
			// draw the character finally
		    draw_text(pos_x, pos_y, ch);
			
			// next char position
		    ch_x += string_width(ch) + spacing;
		    }
		}
	}

REQUIRED SCRIPT:
>> https://www.gmlscripts.com/script/hex_to_dec

Expandfunction hex2dec(hex) {
	
	var dec = 0;
	var dig = "0123456789ABCDEF";
	var len = string_length(hex);
	
	for(var pos = 1; pos <= len; pos++) dec = dec << 4 | (string_pos(string_char_at(hex, pos), dig) - 1);

	return real(dec);
}

REQUIRED SCRIPT:
not uploaded here
yet(?)

Expand///string_split(str, substr)
//
// returns an array of strings separated from a string with substrings
//
// 	str		string to split, string
// 	substr	substring to split the string with, string
//
/// GMLscripts.com/license

function string_split(str, substr) {
	
	var sub_len = string_length(substr);
	var n = string_count(substr, str);
	var arr = [];
	
	// no substrings? return the whole thing
	if n == 0 return [str];
	
	// splitting process
	for(var i = 0; i < n; i++)
	    {
	    var p = string_pos(substr, str) - 1; // get substr position
	    arr[i] = string_copy(str, 1, p); // copy everything before the substr pos
	    str = string_delete(str, 1, p + sub_len); // delete everything before the substr pos + the substr
	    }
	if str != "" arr[i] = str; // add the rest after the last substring (if there is anything)
	
	return arr;
}

kEHAlo7.gif

Last edited by maras (2021-05-18 04:14:26)

Offline

#2 2021-05-25 14:26:26

xot
Administrator
Registered: 2007-08-18
Posts: 1,238

Re: BB like text formatter draw_text_bb

This seems like an extremely useful script. It will require a lot of testing and maybe some tweaks. Thank you.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB