예제 #1
0
파일: cg_chat.cpp 프로젝트: Picmip/qfusion
/*
** CG_DrawChat
*/
void CG_DrawChat( cg_gamechat_t *chat, int x, int y, char *fontName, struct qfontface_s *font, int fontSize,
				  int width, int height, int padding_x, int padding_y, vec4_t backColor, struct shader_s *backShader ) {
	int i, j;
	int s, e, w;
	int utf_len;
	int l, total_lines, lines;
	int x_offset, y_offset;
	int font_height;
	int pass;
	int lastcolor;
	int message_mode;
	int wait_time, fade_time;
	const cg_gamemessage_t *msg;
	const char *text;
	char tstr[GAMECHAT_STRING_SIZE];
	vec4_t fontColor;
	bool chat_active = false;
	bool background_drawn = false;
	int corner_radius = 12 * cgs.vidHeight / 600;
	int background_y;
	int first_candidate;

	font_height = trap_SCR_FontHeight( font );
	message_mode = (int)trap_Cvar_Value( "con_messageMode" );
	chat_active = ( chat->lastMsgTime + GAMECHAT_WAIT_IN_TIME + GAMECHAT_FADE_IN_TIME > cg.realTime || message_mode );
	lines = 0;
	total_lines = /*!message_mode ? 0 : */ 1;

	if( chat_active ) {
		wait_time = GAMECHAT_WAIT_IN_TIME;
		fade_time = GAMECHAT_FADE_IN_TIME;
	} else {
		wait_time = GAMECHAT_WAIT_OUT_TIME;
		fade_time = GAMECHAT_FADE_OUT_TIME;
	}

	if( chat_active != chat->lastActive ) {
		// smooth fade ins and fade outs
		chat->lastActiveChangeTime = cg.realTime - ( 1.0 - chat->activeFrac ) * ( wait_time + fade_time );
	}

	if( cg.realTime >= chat->lastActiveChangeTime + wait_time ) {
		int time_diff, time_interval;

		time_diff = cg.realTime - ( chat->lastActiveChangeTime + wait_time );
		time_interval = fade_time;

		if( time_diff <= time_interval ) {
			chat->activeFrac = (float)time_diff / time_interval;
		} else {
			chat->activeFrac = 1;
		}
	} else {
		chat->activeFrac = 0;
	}

	if( chat_active ) {
		backColor[3] *= chat->activeFrac;
	} else {
		backColor[3] *= ( 1.0 - chat->activeFrac );
	}

	for( i = 0; i < GAMECHAT_STACK_SIZE; i++ ) {
		bool old_msg;

		l = chat->nextMsg - 1 - i;
		if( l < 0 ) {
			l = GAMECHAT_STACK_SIZE + l;
		}

		msg = &chat->messages[l];
		text = msg->text;
		old_msg = !message_mode && ( cg.realTime > msg->time + GAMECHAT_NOTIFY_TIME );

		if( !background_drawn && backColor[3] ) {
			if( old_msg ) {
				// keep the box being drawn for a while to prevent it from flickering
				// upon arrival of the possibly entered chat message
				if( !( !chat_active && cg.realTime <= chat->lastActiveChangeTime + 200 ) ) {
					break;
				}
			}

			background_y = y;
			trap_R_DrawStretchPic( x, background_y, width, height - corner_radius,
								   0.0f, 0.0f, 1.0f, 0.5f, backColor, backShader );
			background_y += height - corner_radius;

			if( trap_IN_IME_GetCandidates( NULL, 0, 10, NULL, &first_candidate ) ) {
				int candidates_height = ( first_candidate ? 3 : 5 ) * font_height;
				trap_R_DrawStretchPic( x, background_y, width, candidates_height,
									   0.0f, 0.5f, 1.0f, 0.5f, backColor, backShader );
				background_y += candidates_height;
			}

			trap_R_DrawStretchPic( x, background_y, corner_radius, corner_radius,
								   0.0f, 0.5f, 0.5f, 1.0f, backColor, backShader );
			trap_R_DrawStretchPic( x + corner_radius, background_y, width - corner_radius * 2, corner_radius,
								   0.5f, 0.5f, 0.5f, 1.0f, backColor, backShader );
			trap_R_DrawStretchPic( x + width - corner_radius, background_y, corner_radius, corner_radius,
								   0.5f, 0.5f, 1.0f, 1.0f, backColor, backShader );

			background_drawn = true;
		}

		// unless user is typing something, only display recent messages
		if( old_msg ) {
			break;
		}

		pass = 0;
		lines = 0;
		lastcolor = ColorIndex( COLOR_WHITE );

parse_string:
		l = 1;
		s = e = 0;
		while( 1 ) {
			int len;

			memset( tstr, 0, sizeof( tstr ) );

			// skip whitespaces at start
			for( ; text[s] == '\n' || Q_IsBreakingSpace( text + s ); s = Q_Utf8SyncPos( text, s + 1, UTF8SYNC_RIGHT ) ) ;

			// empty string
			if( !text[s] ) {
				break;
			}

			w = -1;
			len = trap_SCR_StrlenForWidth( text + s, font, width - padding_x * 2 );
			clamp_low( len, 1 );

			for( j = s; ( j < ( s + len ) ) && text[j] != '\0'; j += utf_len ) {
				utf_len = Q_Utf8SyncPos( text + j, 1, UTF8SYNC_RIGHT );
				memcpy( tstr + j - s, text + j, utf_len );

				if( text[j] == '\n' || Q_IsBreakingSpace( text + j ) ) {
					w = j; // last whitespace
				}
				if( text[j] == '\n' ) {
					break;
				}
			}
			e = j; // end

			// try to word avoid splitting words, unless no other options
			if( text[j] != '\0' && w > 0 ) {
				// stop at the last encountered whitespace
				j = w;
			}

			tstr[j - s] = '\0';

			Vector4Copy( color_table[lastcolor], fontColor );
			fontColor[3] = chat_active ? chat->activeFrac : 1.0 - chat->activeFrac;

			if( pass ) {
				// now actually render the line
				x_offset = padding_x;
				y_offset = height - padding_y - font_height - ( total_lines + lines - l ) * ( font_height + 2 );
				if( y_offset < padding_y ) {
					break;
				}

				trap_SCR_DrawClampString( x + x_offset, y + y_offset, tstr,
										  x + padding_x, y + padding_y, x - padding_x + width, y - padding_y + height, font, fontColor );

				l++;
			} else {
				// increase the lines counter
				lines++;
			}

			if( !text[j] ) {
				// fast path: we don't need two passes in case of one-liners..
				if( lines == 1 ) {
					x_offset = padding_x;
					y_offset = height - font_height - total_lines * ( font_height + 2 );
					if( y_offset < padding_y ) {
						break;
					}

					trap_SCR_DrawClampString( x + x_offset, y + y_offset, tstr,
											  x + padding_x, y + padding_y, x - padding_x + width, y - padding_y + height, font, fontColor );

					total_lines++;
					pass++;
				}
				break;
			}

			if( pass ) {
				// grab the last color token to carry it over to the next line
				lastcolor = Q_ColorStrLastColor( lastcolor, tstr, j - s );
			}

			s = j;
		}

		if( !pass ) {
			pass++;
			goto parse_string;
		} else {
			total_lines += lines;
		}
	}

	// let the engine know where the input line should be drawn
	trap_SCR_DrawChat( x + padding_x, y + height - padding_y - font_height, width - padding_x, font );

	chat->lastActive = chat_active;
}
예제 #2
0
파일: ui_menu.c 프로젝트: Kaperstone/warsow
/*
* UI_DrawConnectScreen
*/
void UI_DrawConnectScreen( const char *serverName, const char *rejectmessage, int downloadType, const char *downloadFilename,
						  float downloadPercent, int downloadSpeed, int connectCount, qboolean demoplaying, qboolean backGround )
{
	qboolean localhost, design = qfalse;
	char str[MAX_QPATH];
	int x, y, xoffset, yoffset, width, height;
	unsigned int maxwidth;
	qboolean downloadFromWeb;
	char hostName[MAX_CONFIGSTRING_CHARS], mapname[MAX_CONFIGSTRING_CHARS], mapmessage[MAX_CONFIGSTRING_CHARS],
		gametype[MAX_CONFIGSTRING_CHARS], gametypeTitle[MAX_CONFIGSTRING_CHARS],
		gametypeVersion[MAX_CONFIGSTRING_CHARS], gametypeAuthor[MAX_CONFIGSTRING_CHARS],
		matchName[MAX_CONFIGSTRING_CHARS];

	uis.demoplaying = demoplaying;

	//trap_S_StopBackgroundTrack();

	localhost = (qboolean)( !serverName || !serverName[0] || !Q_stricmp( serverName, "localhost" ) );

	trap_GetConfigString( CS_MAPNAME, mapname, sizeof( mapname ) );
	trap_GetConfigString( CS_MESSAGE, mapmessage, sizeof( mapmessage ) );

	if( backGround )
	{
		Q_snprintfz( str, sizeof( str ), UI_SHADER_BACKGROUND, uis.backgroundNum );
		trap_R_DrawStretchPic( 0, 0, uis.vidWidth, uis.vidHeight, 0, 0, 1, 1, colorWhite, trap_R_RegisterPic( str ) );
	}

	//
	// not yet connected
	//

	x = 64;
	y = 64;
	xoffset = yoffset = 0;

	if( demoplaying )
		Q_snprintfz( str, sizeof( str ), "Loading demo: %s", serverName );
	else if( localhost )
		Q_strncpyz( str, "Loading...", sizeof( str ) );
	else if( mapname[0] )
		Q_snprintfz( str, sizeof( str ), "Connecting to %s", serverName );
	else
		Q_snprintfz( str, sizeof( str ), "Awaiting connection... %i", connectCount );

	trap_SCR_DrawString( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, uis.fontSystemBig, colorWhite );
	yoffset += trap_SCR_strHeight( uis.fontSystemBig );

	if( design && !rejectmessage )
		rejectmessage = "Connection was interrupted because the weather sux :P";

	if( rejectmessage )
	{
		x = uis.vidWidth / 2;
		y = uis.vidHeight / 3;

		height = trap_SCR_strHeight( uis.fontSystemMedium ) * 4;
		Q_strncpyz( str, "Refused: ", sizeof( str ) );
		width = max( trap_SCR_strWidth( str, uis.fontSystemMedium, 0 ), trap_SCR_strWidth( rejectmessage, uis.fontSystemSmall, 0 ) );
		width += 32 * 2;

		xoffset = UISCR_HorizontalAlignOffset( ALIGN_CENTER_MIDDLE, width );
		yoffset = UISCR_VerticalAlignOffset( ALIGN_CENTER_MIDDLE, height );

		UI_DrawBox( x + xoffset, y + yoffset, width, height, colorWarsowOrange, colorWhite, NULL, colorDkGrey );

		yoffset += trap_SCR_strHeight( uis.fontSystemMedium );
		xoffset += 32;

		trap_SCR_DrawString( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, uis.fontSystemMedium, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemMedium );

		trap_SCR_DrawString( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, rejectmessage, uis.fontSystemSmall, colorBlack );

		return;
	}

	if( mapname[0] )
	{
		char levelshot[MAX_QPATH];
		struct shader_s *levelshotShader;
		qboolean isDefaultlevelshot = qtrue;

		//
		// connected
		//

		x = 64;
		y = uis.vidHeight - 300;
		xoffset = yoffset = 0;
		width = uis.vidWidth;
		height = 200;

		UI_DrawBox( x + xoffset, y + yoffset, width, height, colorWarsowPurple, colorWhite, NULL, colorDkGrey );
		xoffset += 16;
		yoffset += 16 + 4;

		maxwidth = uis.vidWidth - ( x + xoffset );

		trap_GetConfigString( CS_HOSTNAME, hostName, sizeof( hostName ) );
		trap_GetConfigString( CS_GAMETYPENAME, gametype, sizeof( gametype ) );
		trap_GetConfigString( CS_GAMETYPETITLE, gametypeTitle, sizeof( gametypeTitle ) );
		trap_GetConfigString( CS_GAMETYPEVERSION, gametypeVersion, sizeof( gametypeVersion ) );
		trap_GetConfigString( CS_GAMETYPEAUTHOR, gametypeAuthor, sizeof( gametypeAuthor ) );
		trap_GetConfigString( CS_MATCHNAME, matchName, sizeof( matchName ) );

		Q_snprintfz( levelshot, sizeof( levelshot ), "levelshots/%s.jpg", mapname );

		levelshotShader = trap_R_RegisterLevelshot( levelshot, uis.whiteShader, &isDefaultlevelshot );

		if( !isDefaultlevelshot )
		{
			int lw, lh, lx, ly;

			lh = height - 8;
			lw = lh * ( 4.0f/3.0f );
			lx = uis.vidWidth - lw;
			ly = y + 4;

			trap_R_DrawStretchPic( lx, ly, lw, lh, 0, 0, 1, 1, colorWhite, levelshotShader );
		}

		if( !localhost && !demoplaying )
		{
			Q_snprintfz( str, sizeof( str ), "Server: %s", hostName );
			trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
			yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;
		}

		if( mapmessage[0] && Q_stricmp( mapname, mapmessage ) )
		{
			Q_snprintfz( str, sizeof( str ), "Level: "S_COLOR_ORANGE"%s", COM_RemoveColorTokens( mapmessage ) );
			trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
			yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;
		}

		Q_snprintfz( str, sizeof( str ), "Map: %s", mapname );
		trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;

		if( matchName[0] )
		{
			Q_snprintfz( str, sizeof( str ), "Match: %s", matchName );
			trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
			yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;
		}
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;

		Q_snprintfz( str, sizeof( str ), "Gametype: "S_COLOR_ORANGE"%s", COM_RemoveColorTokens( gametypeTitle ) );
		trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;

		Q_snprintfz( str, sizeof( str ), "Version: %s", COM_RemoveColorTokens( gametypeVersion ) );
		trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;

		Q_snprintfz( str, sizeof( str ), "Author: %s", gametypeAuthor );
		trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, str, maxwidth, uis.fontSystemSmall, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall ) + 8;
	}

	// downloading

	if( design && !downloadFilename )
	{
		downloadFilename = "http://www.warsow.net/autoupdate/basewsw/map_wdm9a.pk3";
		downloadType = 2;
		downloadPercent = 65.8;
		downloadSpeed = 325;
	}

	if( downloadType && downloadFilename )
	{
		size_t len;
		const char *s;

		downloadFromWeb = ( downloadType == 2 );

		x = uis.vidWidth / 2;
		y = uis.vidHeight / 3;
		width = 400;
		height = 128 - trap_SCR_strHeight( uis.fontSystemSmall );
		if( uis.vidWidth <= width )
			width = uis.vidWidth - 64;

		maxwidth = width - 48;

		xoffset = UISCR_HorizontalAlignOffset( ALIGN_CENTER_MIDDLE, width );
		yoffset = UISCR_VerticalAlignOffset( ALIGN_CENTER_MIDDLE, height );

		// adjust the box size for the extra number of lines needed to draw the file path
		s = downloadFilename;
		while( ( len = trap_SCR_StrlenForWidth( s, uis.fontSystemSmall, maxwidth ) ) > 0 )
		{
			s += len;
			height += trap_SCR_strHeight( uis.fontSystemSmall );
		}

		UI_DrawBox( x + xoffset, y + yoffset, width, height, colorWarsowPurple, colorWhite, NULL, colorDkGrey );

		xoffset += 24;
		yoffset += 24;

		trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, downloadFromWeb ? "Downloading from web" : "Downloading from server", maxwidth, uis.fontSystemSmall, colorWhite );
		yoffset += trap_SCR_strHeight( uis.fontSystemSmall );

		s = downloadFilename;
		while( ( len = trap_SCR_DrawStringWidth( x + xoffset, y + yoffset, ALIGN_LEFT_TOP, s, maxwidth, uis.fontSystemSmall, colorWhite ) ) > 0 )
		{
			s += len;
			yoffset += trap_SCR_strHeight( uis.fontSystemSmall );
		}

		yoffset += 16;

		UI_DrawPicBar( x + xoffset, y + yoffset, maxwidth, 24, ALIGN_LEFT_TOP, downloadPercent, trap_R_RegisterPic( "gfx/ui/progressbar" ),colorDkGrey, colorOrange );
		Q_snprintfz( str, sizeof( str ), "%3.1f%c", downloadPercent, '%' );
		trap_SCR_DrawStringWidth( x + xoffset + 12, y + yoffset + 12, ALIGN_LEFT_MIDDLE, str, maxwidth, uis.fontSystemSmall, colorWhite );
		Q_snprintfz( str, sizeof( str ), "%ik/s", downloadSpeed );
		trap_SCR_DrawStringWidth( x + xoffset + maxwidth - 12, y + yoffset + 12, ALIGN_RIGHT_MIDDLE, str, maxwidth, uis.fontSystemSmall, colorWhite );

		yoffset += 24 + 8;
	}
}