示例#1
0
void put_colored_text_in_buffer (Uint8 color, Uint8 channel, const Uint8 *text_to_add, int len)
{
	text_message *msg;
	int minlen, text_color;
	Uint32 cnr = 0, ibreak = -1, jbreak = -1;
	char time_stamp[12];
	struct tm *l_time; time_t c_time;

	check_chat_text_to_overtext (text_to_add, len, channel);

	// check for auto-length
	if (len < 0)
		len = strlen ((char*)text_to_add);

	// set the time when we got this message
	last_server_message_time = cur_time;

	// if the buffer is full, delete some old lines and move the remainder to the front
	if (++last_message >= DISPLAY_TEXT_BUFFER_SIZE)
	{
		const size_t num_move = DISPLAY_TEXT_BUFFER_SIZE - DISPLAY_TEXT_BUFFER_DEL;
		size_t i;
		for (i=0; i<DISPLAY_TEXT_BUFFER_DEL; i++)
		{
			msg = &(display_text_buffer[i]);
			if (msg->data)
			{
				msg->deleted = 1;
				update_text_windows(msg);
				free_text_message_data (msg);
			}
		}
		memmove(display_text_buffer, &display_text_buffer[DISPLAY_TEXT_BUFFER_DEL], sizeof(text_message)*num_move);
		last_message -= DISPLAY_TEXT_BUFFER_DEL;
		for (i = num_move; i < DISPLAY_TEXT_BUFFER_SIZE; i++)
			init_text_message (display_text_buffer + i, 0);
	}

	msg = &(display_text_buffer[last_message]);

	// Try to make a guess at the number of wrapping newlines required,
	// but allow al least for a null byte and up to 8 extra newlines and
	// colour codes
	minlen = len + 18 + (len/60);
	if (show_timestamp)
	{
		minlen += 12;
		time (&c_time);
		l_time = localtime (&c_time);
		strftime (time_stamp, sizeof(time_stamp), "[%H:%M:%S] ", l_time);
	}
	cnr = get_active_channel (channel);
	if (cnr != 0)
		// allow some space for the channel number
		minlen += 20;
	if (msg->data == NULL)
		alloc_text_message_data (msg, minlen);
	else
		resize_text_message_data (msg, minlen);

	if (cnr != 0)
	{
		for (ibreak = 0; ibreak < len; ibreak++)
		{
			if (text_to_add[ibreak] == ']') break;
		}
	}

	if (channel == CHAT_LOCAL)
	{
		for (jbreak = 0; jbreak < len; jbreak++)
		{
			if (text_to_add[jbreak] == ':' && text_to_add[jbreak+1] == ' ') break;
		}
	}

	if (dark_channeltext==1)
		text_color = c_grey2;
	else if (dark_channeltext==2)
		text_color = c_grey4;

	if (ibreak >= len)
	{
		// not a channel, or something's messed up
		if(!is_color (text_to_add[0]))
		{
			// force the color
			if (show_timestamp)
			{
				safe_snprintf (msg->data, msg->size, "%c%s%.*s", to_color_char (color), time_stamp, len, text_to_add);
			}
			else
			{
				safe_snprintf (msg->data, msg->size, "%c%.*s", to_color_char (color), len, text_to_add);
			}
		}
		else
		{
			// color set by server
			if (show_timestamp)
			{
				if(dark_channeltext && channel==CHAT_LOCAL && from_color_char(text_to_add[0])==c_grey1 && jbreak < (len-3))
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s%.*s", to_color_char (text_color), time_stamp, jbreak+1, &text_to_add[1], len-jbreak-3, &text_to_add[jbreak+3]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s", text_to_add[0], time_stamp, len-1, &text_to_add[1]);
				}
			}
			else
			{
				if(dark_channeltext && channel==CHAT_LOCAL && from_color_char(text_to_add[0])==c_grey1 && jbreak < (len-3))
				{
					safe_snprintf (msg->data, msg->size, "%c%.*s%.*s", to_color_char (text_color), jbreak+1, &text_to_add[1], len-jbreak-3, &text_to_add[jbreak+3]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%.*s", len, text_to_add);
				}
			}
		}
	}
	else
	{
		char nr_str[16];
		int has_additional_color = is_color(text_to_add[ibreak+3]);
		if (cnr >= 1000000000)
			safe_snprintf (nr_str, sizeof (nr_str), "guild");
		else
			safe_snprintf (nr_str, sizeof (nr_str), "%u", cnr);

		if(!is_color (text_to_add[0]))
		{
			// force the color
			if (show_timestamp)
			{
				if (dark_channeltext)
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s @ %s%.*s%c%.*s", to_color_char (color), time_stamp, ibreak, text_to_add, nr_str, 3, &text_to_add[ibreak], to_color_char (text_color), len-ibreak-3-has_additional_color, &text_to_add[ibreak+3+has_additional_color]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s @ %s%.*s", to_color_char (color), time_stamp, ibreak, text_to_add, nr_str, len-ibreak, &text_to_add[ibreak]);
				}
			}
			else
			{
				if (dark_channeltext)
				{
					safe_snprintf (msg->data, msg->size, "%c%.*s @ %s%.*s%c%.*s", to_color_char (color), ibreak, text_to_add, nr_str, 3, &text_to_add[ibreak], to_color_char (text_color), len-ibreak-3-has_additional_color, &text_to_add[ibreak+3+has_additional_color]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%c%.*s @ %s%.*s", to_color_char (color), ibreak, text_to_add, nr_str, len-ibreak, &text_to_add[ibreak]);
				}
			}
		}
		else
		{
			// color set by server
			if (show_timestamp)
			{
				if (dark_channeltext)
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s @ %s%.*s%c%.*s", text_to_add[0], time_stamp, ibreak-1, &text_to_add[1], nr_str, 3, &text_to_add[ibreak], to_color_char (text_color), len-ibreak-3-has_additional_color, &text_to_add[ibreak+3+has_additional_color]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%c%s%.*s @ %s%.*s", text_to_add[0], time_stamp, ibreak-1, &text_to_add[1], nr_str, len-ibreak, &text_to_add[ibreak]);
				}
			}
			else
			{
				if (dark_channeltext)
				{
					safe_snprintf (msg->data, msg->size, "%.*s @ %s%.*s%c%.*s", ibreak, text_to_add, nr_str, 3, &text_to_add[ibreak], to_color_char (text_color), len-ibreak-3-has_additional_color, &text_to_add[ibreak+3+has_additional_color]);
				}
				else
				{
					safe_snprintf (msg->data, msg->size, "%.*s @ %s%.*s", ibreak, text_to_add, nr_str, len-ibreak, &text_to_add[ibreak]);
				}
			}
		}
	}

	msg->len = strlen (msg->data);
	msg->chan_idx = channel;
	msg->channel = cnr;

	// set invalid wrap data to force rewrapping
	msg->wrap_lines = 0;
	msg->wrap_zoom = 0.0f;
	msg->wrap_width = 0;

	msg->deleted = 0;
	recolour_message(msg);
	update_text_windows(msg);

	// log the message
	write_to_log (channel, (unsigned char*)msg->data, msg->len);

	return;
}
示例#2
0
void put_colored_text_in_buffer(Uint8 color, unsigned char *text_to_add, int len,
								int x_chars_limit)
{
	int i;
	Uint8 cur_char;

	check_chat_text_to_overtext( text_to_add, len );

	// check for auto-length
	if(len<0)len=strlen(text_to_add);
	//set the time when we got this line
	last_server_message_time=cur_time;
	if(lines_to_show<max_lines_no)lines_to_show++;
	//watch for the end of buffer!
	while(display_text_buffer_last+len+8 >= max_display_text_buffer_lenght)
		{
			memmove(display_text_buffer, display_text_buffer+1024, display_text_buffer_last-1024);
			display_text_buffer_last-=1024;
			display_text_buffer_first-=1024;
			if(display_console_text_buffer_first<0)
				{
					display_console_text_buffer_first=0;
				}
		}

	// force the color
	if(*text_to_add <= 127 || *text_to_add > 127+c_grey4)
		{
			display_text_buffer[display_text_buffer_last]=127+color;
			display_text_buffer_last++;
		}

	//see if the text fits on the screen
	if(!x_chars_limit)x_chars_limit=(window_width-hud_x)/11;
	if(len<=x_chars_limit)
		{
			for(i=0;i<len;i++)
				{
					cur_char=text_to_add[i];

					if(!cur_char)
						{
							i--;
							break;
						}

					display_text_buffer[i+display_text_buffer_last]=cur_char;
				}
			display_text_buffer[display_text_buffer_last+i]='\n';
			display_text_buffer[display_text_buffer_last+i+1]=0;
			display_text_buffer_last+=i+1;
		}
	else//we have to add new lines to our text...
		{
			int line=0;
			int k,j;
			int new_line_pos=0;
			int text_lines;
			char semaphore=0;
			unsigned char current_color=127+color;

			//how many lines of text do we have?
			text_lines=len/x_chars_limit;
			//go trought all the text
			j=0;
			for(i=0;i<len;i++)
				{
					if(!semaphore && new_line_pos+x_chars_limit<len)//don't go trough the last line
						{
							//find the closest space from the end of this line
							//if we have one really big word, then parse the string from the
							//end of the line backwards, untill the beginning of the line +2
							//the +2 is so we avoid parsing the ": " thing...
							for(k=new_line_pos+x_chars_limit-1;k>new_line_pos+2;k--)
								{
									cur_char=text_to_add[k];
									if(k>len)continue;
									if(cur_char==' ')
										{
											k++;//let the space on the previous line
											break;
										}
								}
							if(k==new_line_pos+2)
								new_line_pos=new_line_pos+x_chars_limit;
							else new_line_pos=k;
							line++;
							semaphore=1;
						}

					cur_char=text_to_add[i];

					if(!cur_char)
						{
							j--;
							break;
						}

					if(cur_char>=127)	//we have a color, save it
						current_color=cur_char;
					if(cur_char=='\n')
						{
							new_line_pos=i;
						}



					if(i==new_line_pos)
						{
							display_text_buffer[j+display_text_buffer_last]='\n';
							j++;
							display_text_buffer[j+display_text_buffer_last]=current_color;
							j++;
							semaphore=0;
							if(lines_to_show<max_lines_no)lines_to_show++;
						}
					//don't add another new line, if the current char is already a new line...
					if(cur_char!='\n')
						{
							display_text_buffer[j+display_text_buffer_last]=cur_char;
							j++;
						}

				}
			display_text_buffer[display_text_buffer_last+j]='\n';
			display_text_buffer[display_text_buffer_last+j+1]=0;
			display_text_buffer_last+=j+1;
		}
}