Share chat between frontend and engine
authorunc0rr
Wed, 15 Oct 2008 15:02:39 +0000
changeset 1356 77ab8735401d
parent 1355 8e167aa3b2e4
child 1357 5b9012dd4a05
Share chat between frontend and engine
QTfrontend/chatwidget.cpp
QTfrontend/chatwidget.h
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hwform.cpp
QTfrontend/newnetclient.cpp
QTfrontend/newnetclient.h
hedgewars/CCHandlers.inc
hedgewars/uTeams.pas
--- a/QTfrontend/chatwidget.cpp	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/chatwidget.cpp	Wed Oct 15 15:02:39 2008 +0000
@@ -55,11 +55,10 @@
   chatEditLine->clear();
 }
 
-void HWChatWidget::onChatStringFromNet(const QStringList& str)
+void HWChatWidget::onChatStringFromNet(const QString& str)
 {
   if (str.size() < 2) return;
   QListWidget* w = chatText;
-  w->addItem(str[0]+": "+str[1]);
   w->scrollToBottom();
   w->setSelectionMode(QAbstractItemView::NoSelection);
 }
@@ -67,8 +66,6 @@
 void HWChatWidget::nickAdded(const QString& nick)
 {
   chatNicks->addItem(nick);
-  chatText->addItem(QString(tr("* %1 joined")).arg(nick));
-  chatText->scrollToBottom();
 }
 
 void HWChatWidget::nickRemoved(const QString& nick)
@@ -78,8 +75,6 @@
     chatNicks->takeItem(chatNicks->row(*it));
     ++it;
   }
-  chatText->addItem(QString(tr("* %1 left")).arg(nick));
-  chatText->scrollToBottom();
 }
 
 void HWChatWidget::clear()
--- a/QTfrontend/chatwidget.h	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/chatwidget.h	Wed Oct 15 15:02:39 2008 +0000
@@ -34,7 +34,7 @@
   HWChatWidget(QWidget* parent=0);
 
  public slots:
-  void onChatStringFromNet(const QStringList& str);
+  void onChatStringFromNet(const QString& str);
   void nickAdded(const QString& nick);
   void nickRemoved(const QString& nick);
   void clear();
--- a/QTfrontend/game.cpp	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/game.cpp	Wed Oct 15 15:02:39 2008 +0000
@@ -201,13 +201,6 @@
 			}
 			return;
 		}
-		case '+': {
-			if (gameType == gtNet)
-			{
-				emit SendNet(msg);
-			}
-			break;
-		}
 		case 'i': {
 			int size = msg.size();
 			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5)));
@@ -221,6 +214,10 @@
 			SetGameState(gsFinished);
 			break;
 		}
+		case 's': {
+			int size = msg.size();
+			emit SendChat(QString::fromUtf8(msg.mid(2).left(size - 4)));
+		}
 		default: {
 			if (gameType == gtNet)
 			{
@@ -236,6 +233,13 @@
 	RawSendIPC(msg);
 }
 
+void HWGame::FromNetChat(const QString & msg)
+{
+	QByteArray buf;
+	HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20");
+	RawSendIPC(buf);
+}
+
 void HWGame::onClientRead()
 {
 	quint8 msglen;
--- a/QTfrontend/game.h	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/game.h	Wed Oct 15 15:02:39 2008 +0000
@@ -58,6 +58,7 @@
 
 signals:
 	void SendNet(const QByteArray & msg);
+	void SendChat(const QString & msg);
 	void GameStateChanged(GameState gameState);
 	void GameStats(char type, const QString & info);
 	void HaveRecord(bool isDemo, const QByteArray & record);
@@ -65,6 +66,7 @@
 
 public slots:
 	void FromNet(const QByteArray & msg);
+	void FromNetChat(const QString & msg);
 
 private:
     enum GameType {
--- a/QTfrontend/hwform.cpp	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/hwform.cpp	Wed Oct 15 15:02:39 2008 +0000
@@ -702,7 +702,9 @@
 	CreateGame(ui.pageNetGame->pGameCFG, ui.pageNetGame->pNetTeamsWidget, ammo);
 
 	connect(game, SIGNAL(SendNet(const QByteArray &)), hwnet, SLOT(SendNet(const QByteArray &)));
+	connect(game, SIGNAL(SendChat(const QString &)), hwnet, SLOT(chatLineToNet(const QString &)));
 	connect(hwnet, SIGNAL(FromNet(const QByteArray &)), game, SLOT(FromNet(const QByteArray &)));
+	connect(hwnet, SIGNAL(chatStringFromNet(const QString &)), game, SLOT(FromNetChat(const QString &)));
 
 	game->StartNet();
 }
--- a/QTfrontend/newnetclient.cpp	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/newnetclient.cpp	Wed Oct 15 15:02:39 2008 +0000
@@ -239,9 +239,7 @@
 		qWarning("Net: Empty CHAT_STRING message");
 		return;
 		}
-		QStringList tmp = lst;
-		tmp.removeFirst();
-		emit chatStringFromNet(tmp);
+		emit chatStringFromNet(QString("%1: %2").arg(lst[1]).arg(lst[2]));
 		return;
 	}
 
@@ -290,6 +288,7 @@
 					ConfigAsked();
 			}
 			emit nickAdded(lst[i]);
+			emit chatStringFromNet(QString(tr("* %1 joined")).arg(lst[i]));
 		}
 		return;
 	}
@@ -301,6 +300,7 @@
 			return;
 		}
 		emit nickRemoved(lst[1]);
+		emit chatStringFromNet(QString(tr("* %1 left")).arg(lst[1]));
 		return;
 	}
 
@@ -482,7 +482,7 @@
 {
   if(str!="") {
     RawSendNet(QString("CHAT_STRING")+delimeter+str);
-    emit(chatStringFromNet(QStringList(mynick) << str));
+    emit(chatStringFromNet(QString("%1: %2").arg(mynick).arg(str)));
   }
 }
 
--- a/QTfrontend/newnetclient.h	Wed Oct 15 11:54:06 2008 +0000
+++ b/QTfrontend/newnetclient.h	Wed Oct 15 15:02:39 2008 +0000
@@ -104,7 +104,7 @@
   void fortsModeChanged(bool value);
   void hhnumChanged(const HWTeam&);
   void teamColorChanged(const HWTeam&);
-  void chatStringFromNet(const QStringList&);
+  void chatStringFromNet(const QString&);
   void ammoChanged(const QString& name, const QString& ammo);
   void roomsList(const QStringList&);
 
--- a/hedgewars/CCHandlers.inc	Wed Oct 15 11:54:06 2008 +0000
+++ b/hedgewars/CCHandlers.inc	Wed Oct 15 15:02:39 2008 +0000
@@ -296,9 +296,9 @@
 
 procedure chSay(var s: shortstring);
 begin
+SendIPC('s' + s);
 s:= UserNick + ': ' + s;
-AddChatString(s);
-SendIPC('s' + s)
+AddChatString(s)
 end;
 
 procedure chTimer(var s: shortstring);
--- a/hedgewars/uTeams.pas	Wed Oct 15 11:54:06 2008 +0000
+++ b/hedgewars/uTeams.pas	Wed Oct 15 15:02:39 2008 +0000
@@ -358,16 +358,16 @@
 end;
 
 procedure TeamGone(s: shortstring);
-var i: integer;
+var i, t: LongInt;
 begin
 i:= 0;
 
-while (i < cMaxTeams)
-	and (TeamsArray[i] <> nil)
-	and (TeamsArray[i]^.TeamName <> s) do inc(i);
-if (i = cMaxTeams) or (TeamsArray[i] = nil) then exit;
+while (t < cMaxTeams)
+	and (TeamsArray[t] <> nil)
+	and (TeamsArray[t]^.TeamName <> s) do inc(t);
+if (t = cMaxTeams) or (TeamsArray[t] = nil) then exit;
 
-with TeamsArray[i]^ do
+with TeamsArray[t]^ do
 	begin
 	AddChatString('* '+ TeamName + ' is gone');
 	for i:= 0 to cMaxHHIndex do