2010-03-20 00:26:00 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-04-11 03:25:06 +02:00
// Copyright (C) 2001-2008 Martio Figueroa
2010-03-20 00:26:00 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "chat_manager.h"
# include "window.h"
# include "console.h"
2010-03-23 02:38:19 +01:00
# include "config.h"
2010-03-20 00:26:00 +01:00
# include "network_manager.h"
# include "lang.h"
2010-12-27 01:59:57 +01:00
# include "core_data.h"
2010-03-20 00:26:00 +01:00
# include "util.h"
2010-04-11 03:25:06 +02:00
# include <stdexcept>
2010-03-20 00:26:00 +01:00
# include "leak_dumper.h"
2010-04-11 03:25:06 +02:00
using namespace std ;
2010-03-20 00:26:00 +01:00
using namespace Shared : : Platform ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
// =====================================================
// class ChatManager
// =====================================================
2010-10-22 09:28:55 +02:00
ChatManager : : ChatManager ( ) {
2010-03-20 00:26:00 +01:00
console = NULL ;
editEnabled = false ;
teamMode = false ;
thisTeamIndex = - 1 ;
2010-05-15 20:59:17 +02:00
disableTeamMode = false ;
2010-12-27 01:59:57 +01:00
xPos = 300 ;
yPos = 150 ;
maxTextLenght = 64 ;
font = CoreData : : getInstance ( ) . getConsoleFont ( ) ;
2010-03-20 00:26:00 +01:00
}
2010-12-24 03:10:31 +01:00
void ChatManager : : init ( Console * console , int thisTeamIndex , const bool inMenu , string manualPlayerNameOverride ) {
2010-03-20 00:26:00 +01:00
this - > console = console ;
this - > thisTeamIndex = thisTeamIndex ;
2010-05-15 20:59:17 +02:00
this - > disableTeamMode = false ;
2010-06-09 00:50:37 +02:00
this - > inMenu = inMenu ;
2010-12-24 03:10:31 +01:00
this - > manualPlayerNameOverride = manualPlayerNameOverride ;
2010-03-20 00:26:00 +01:00
}
2010-10-22 09:28:55 +02:00
void ChatManager : : keyUp ( char key ) {
2010-04-15 23:16:13 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-12-24 03:10:31 +01:00
2010-04-15 23:16:13 +02:00
try {
2010-10-22 09:28:55 +02:00
if ( editEnabled ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-10-22 09:28:55 +02:00
if ( key = = vkEscape )
2010-04-15 23:16:13 +02:00
{
text . clear ( ) ;
editEnabled = false ;
}
}
}
catch ( const exception & ex ) {
char szBuf [ 1024 ] = " " ;
2010-10-06 22:22:06 +02:00
sprintf ( szBuf , " In [%s::%s Line: %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
2010-04-15 23:16:13 +02:00
throw runtime_error ( szBuf ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-05-15 20:59:17 +02:00
void ChatManager : : setDisableTeamMode ( bool value ) {
disableTeamMode = value ;
if ( disableTeamMode = = true ) {
teamMode = false ;
}
}
2010-04-15 23:16:13 +02:00
2010-10-22 09:28:55 +02:00
void ChatManager : : keyDown ( char key ) {
2010-07-06 07:30:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
try {
Lang & lang = Lang : : getInstance ( ) ;
2010-08-27 23:43:47 +02:00
Config & configKeys = Config : : getInstance ( std : : pair < ConfigType , ConfigType > ( cfgMainKeys , cfgUserKeys ) ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
//toggle team mode
2010-08-27 23:43:47 +02:00
if ( editEnabled = = false & & disableTeamMode = = false & &
key = = configKeys . getCharKey ( " ChatTeamMode " ) ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2011-01-01 21:31:25 +01:00
if ( ! inMenu ) {
if ( teamMode = = true ) {
teamMode = false ;
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get (
" All " ) ) ;
} else {
teamMode = true ;
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get (
" Team " ) ) ;
}
2010-04-11 03:25:06 +02:00
}
2010-03-20 00:26:00 +01:00
}
2010-04-11 03:25:06 +02:00
if ( key = = vkReturn ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-06-08 02:00:28 +02:00
SDL_keysym keystate = Window : : getKeystate ( ) ;
if ( keystate . mod & ( KMOD_LALT | KMOD_RALT ) ) {
// alt+enter is ignored
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-04-11 03:25:06 +02:00
}
2010-06-08 02:00:28 +02:00
else
{
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-10-22 09:28:55 +02:00
if ( editEnabled = = true ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-06-08 02:00:28 +02:00
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2010-10-22 09:28:55 +02:00
if ( text . empty ( ) = = false ) {
string playerName = gameNetworkInterface - > getHumanPlayerName ( ) ;
int playerIndex = gameNetworkInterface - > getHumanPlayerIndex ( ) ;
2010-12-24 03:10:31 +01:00
if ( this - > manualPlayerNameOverride ! = " " ) {
console - > addLine ( text , false , this - > manualPlayerNameOverride ) ;
}
else {
console - > addLine ( text , false , playerIndex ) ;
}
2010-08-21 15:04:52 +02:00
gameNetworkInterface - > sendTextMessage ( text , teamMode ? thisTeamIndex : - 1 ) ;
2010-10-22 09:28:55 +02:00
if ( inMenu = = false ) {
editEnabled = false ;
}
2010-06-08 02:00:28 +02:00
}
2010-10-22 09:28:55 +02:00
else {
2010-06-08 02:00:28 +02:00
editEnabled = false ;
2010-12-24 03:10:31 +01:00
}
2010-06-08 02:00:28 +02:00
text . clear ( ) ;
}
2010-07-29 17:49:06 +02:00
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
2010-12-27 01:59:57 +01:00
switchOnEdit ( ) ;
2010-06-08 02:00:28 +02:00
}
2010-03-20 00:26:00 +01:00
}
}
2010-07-29 17:49:06 +02:00
else if ( key = = vkBack ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , key ) ;
if ( ! text . empty ( ) ) {
2010-04-11 03:25:06 +02:00
text . erase ( text . end ( ) - 1 ) ;
}
2010-03-20 00:26:00 +01:00
}
2010-12-24 03:10:31 +01:00
2010-03-20 00:26:00 +01:00
}
2010-04-11 03:25:06 +02:00
catch ( const exception & ex ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-10-06 22:22:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
2010-04-11 03:25:06 +02:00
throw runtime_error ( szBuf ) ;
2010-03-20 00:26:00 +01:00
}
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-20 00:26:00 +01:00
}
2010-12-27 01:59:57 +01:00
void ChatManager : : switchOnEdit ( ) {
editEnabled = true ;
text . clear ( ) ;
}
2010-03-20 00:26:00 +01:00
void ChatManager : : keyPress ( char c ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , c , c ) ;
2010-03-20 00:26:00 +01:00
if ( editEnabled & & text . size ( ) < maxTextLenght ) {
2010-07-29 17:49:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , c , c ) ;
2010-03-20 00:26:00 +01:00
//space is the first meaningful code
if ( c > = ' ' ) {
text + = c ;
}
}
}
2010-12-27 01:59:57 +01:00
void ChatManager : : addText ( string text ) {
if ( editEnabled & & text . size ( ) + this - > text . size ( ) < maxTextLenght ) {
this - > text + = text ;
}
}
2010-05-28 16:59:09 +02:00
void ChatManager : : updateNetwork ( ) {
2010-04-11 03:25:06 +02:00
try {
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
2010-07-01 02:08:59 +02:00
//string text;
//string sender;
2010-04-11 03:25:06 +02:00
Config & config = Config : : getInstance ( ) ;
2010-03-20 00:26:00 +01:00
2010-05-29 13:12:45 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameNetworkInterface->getChatText() [%s]\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface->getChatText().c_str());
2010-05-28 16:59:09 +02:00
2010-07-08 10:29:51 +02:00
if ( gameNetworkInterface ! = NULL & & gameNetworkInterface - > getChatTextList ( ) . empty ( ) = = false ) {
2010-07-01 02:08:59 +02:00
for ( int idx = 0 ; idx < gameNetworkInterface - > getChatTextList ( ) . size ( ) ; idx + + ) {
const ChatMsgInfo & msg = gameNetworkInterface - > getChatTextList ( ) [ idx ] ;
int teamIndex = msg . chatTeamIndex ;
2010-03-20 00:26:00 +01:00
2010-07-01 02:08:59 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] got nmtText [%s] for team = %d \n " , __FILE__ , __FUNCTION__ , msg . chatText . c_str ( ) , teamIndex ) ;
2010-03-20 00:26:00 +01:00
2010-10-22 09:28:55 +02:00
if ( teamIndex = = - 1 | | teamIndex = = thisTeamIndex ) {
2010-10-23 06:00:39 +02:00
//console->addLine(msg.chatSender + ": " + msg.chatText, true, msg.chatPlayerIndex);
console - > addLine ( msg . chatText , true , msg . chatPlayerIndex ) ;
2010-03-20 00:26:00 +01:00
2010-07-01 02:08:59 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Added text to console \n " , __FILE__ , __FUNCTION__ ) ;
}
2010-04-11 03:25:06 +02:00
2010-07-01 02:08:59 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 16:59:09 +02:00
gameNetworkInterface - > clearChatInfo ( ) ;
2010-03-20 00:26:00 +01:00
}
}
2010-04-11 03:25:06 +02:00
catch ( const std : : exception & ex ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-10-06 22:22:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
2010-04-11 03:25:06 +02:00
throw runtime_error ( szBuf ) ;
}
2010-03-20 00:26:00 +01:00
}
} } //end namespace