Colorize chat messages in frontend and engine
authorunc0rr
Sun, 27 Sep 2009 10:26:36 +0000
changeset 2396 e13a1117152b
parent 2395 d01d3bf3e1de
child 2397 2ca4ca6b4bab
Colorize chat messages in frontend and engine
QTfrontend/chatwidget.cpp
QTfrontend/newnetclient.cpp
hedgewars/CCHandlers.inc
hedgewars/uChat.pas
hedgewars/uIO.pas
--- a/QTfrontend/chatwidget.cpp	Sat Sep 26 20:54:13 2009 +0000
+++ b/QTfrontend/chatwidget.cpp	Sun Sep 27 10:26:36 2009 +0000
@@ -78,9 +78,12 @@
 	if (chatStrings.size() > 250)
 		chatStrings.removeFirst();
 
-	QString formattedStr = Qt::escape(str);
-	if (formattedStr.startsWith("["))
+	QString formattedStr = Qt::escape(str.mid(1));
+	if (str.startsWith("\x03"))
 		formattedStr = QString("<font color=grey>%1</font>").arg(formattedStr);
+	else if (str.startsWith("\x02"))
+		formattedStr = QString("<font color=magenta>%1</font>").arg(formattedStr);
+
 
 	chatStrings.append(formattedStr);
 
--- a/QTfrontend/newnetclient.cpp	Sat Sep 26 20:54:13 2009 +0000
+++ b/QTfrontend/newnetclient.cpp	Sun Sep 27 10:26:36 2009 +0000
@@ -375,7 +375,7 @@
 					emit configAsked();
 			}
 			emit nickAdded(lst[i]);
-			emit chatStringFromNet(QString(tr("[ %1 has joined the room ]")).arg(lst[i]));
+			emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
 		}
 		return;
 	}
@@ -397,7 +397,7 @@
 			}
 
 			emit nickAddedLobby(lst[i]);
-			emit chatStringLobby(QString(tr("[ %1 has joined ]")).arg(lst[i]));
+			emit chatStringLobby(tr("%1 *** %2 has joined").arg('\x03').arg(lst[i]));
 		}
 		return;
 	}
@@ -410,9 +410,9 @@
 		}
 		emit nickRemoved(lst[1]);
 		if (lst.size() < 3)
-			emit chatStringFromNet(QString(tr("[ %1 has left ]")).arg(lst[1]));
+			emit chatStringFromNet(tr("%1 *** %2 has left ]").arg('\x03').arg(lst[1]));
 		else
-			emit chatStringFromNet(QString(tr("[ %1 has left (%2) ]")).arg(lst[1], lst[2]));
+			emit chatStringFromNet(tr("%1 *** %2 has left (%3) ]").arg('\x03').arg(lst[1], lst[2]));
 		return;
 	}
 
@@ -434,9 +434,9 @@
 		}
 		emit nickRemovedLobby(lst[1]);
 		if (lst.size() < 3)
-			emit chatStringLobby(QString(tr("[ %1 has left ]")).arg(lst[1]));
+			emit chatStringLobby(tr("%1 *** %2 has left ]").arg('\x03').arg(lst[1]));
 		else
-			emit chatStringLobby(QString(tr("[ %1 has left (%2) ]")).arg(lst[1], lst[2]));
+			emit chatStringLobby(tr("%1 *** %2 has left (%3) ]").arg('\x03').arg(lst[1], lst[2]));
 		return;
 	}
 
@@ -629,9 +629,9 @@
 QString HWNewNet::formatChatMsg(const QString & nick, const QString & msg)
 {
 	if(msg.left(4) == "/me ")
-		return QString("* %1 %2").arg(nick).arg(msg.mid(4));
+		return QString("\x02* %1 %2").arg(nick).arg(msg.mid(4));
 	else
-		return QString("%1: %2").arg(nick).arg(msg);
+		return QString("\x01%1: %2").arg(nick).arg(msg);
 }
 
 void HWNewNet::banPlayer(const QString & nick)
--- a/hedgewars/CCHandlers.inc	Sat Sep 26 20:54:13 2009 +0000
+++ b/hedgewars/CCHandlers.inc	Sun Sep 27 10:26:36 2009 +0000
@@ -336,21 +336,20 @@
 SendIPC('s' + s);
 
 if copy(s, 1, 4) = '/me ' then
-	s:= '* ' + UserNick + ' ' + copy(s, 5, Length(s) - 4)
+	s:= #2'* ' + UserNick + ' ' + copy(s, 5, Length(s) - 4)
 else
-	s:= UserNick + ': ' + s;
+	s:= #1 + UserNick + ': ' + s;
 
 AddChatString(s)
 end;
 
 procedure chTeamSay(var s: shortstring);
-var text: shortstring;
 begin
 SendIPC('b' + s);
 
-text:= copy(s, 2, Length(s)-1);
+s[1]:= #4;
 
-AddChatString(text)
+AddChatString(s)
 end;
 
 procedure chTimer(var s: shortstring);
--- a/hedgewars/uChat.pas	Sat Sep 26 20:54:13 2009 +0000
+++ b/hedgewars/uChat.pas	Sun Sep 27 10:26:36 2009 +0000
@@ -46,16 +46,34 @@
 	InputStr: TChatLine;
 	InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
 
+const colors: array[#1..#4] of Longword = (
+	$FFFFFF, // chat message
+	$FF00FF, // action message
+	$00B000, // join/leave message
+	$AFFFAF  // team message
+	);
+
 procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean);
 var strSurface, resSurface: PSDL_Surface;
 	w, h: LongInt;
+	color: Longword;
 begin
 if cl.Tex <> nil then
 	FreeTexture(cl.Tex);
 
+
 cl.s:= str;
 
-if isInput then str:= UserNick + '> ' + str + '_';
+if isInput then
+	begin
+	color:= $00FFFF;
+	str:= UserNick + '> ' + str + '_'
+	end
+	else begin
+	color:= colors[str[1]];
+	delete(str, 1, 1)
+	end;
+
 
 TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h);
 
@@ -65,7 +83,7 @@
 		32,
 		RMask, GMask, BMask, AMask);
 
-strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF);
+strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), color);
 cl.Width:= w + 4;
 SDL_UpperBlit(strSurface, nil, resSurface, nil);
 SDL_FreeSurface(strSurface);
--- a/hedgewars/uIO.pas	Sat Sep 26 20:54:13 2009 +0000
+++ b/hedgewars/uIO.pas	Sun Sep 27 10:26:36 2009 +0000
@@ -315,7 +315,7 @@
 		'b': if LocalClan = byte(headcmd^.str[2]) then
                begin
                s:= copy(headcmd^.str, 3, Pred(headcmd^.len));
-               AddChatString(s);
+               AddChatString(#4 + s);
                WriteLnToConsole(s)
                end;
 		'1'..'5': ParseCommand('timer ' + headcmd^.cmd, true);