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"
# 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
// =====================================================
const int ChatManager : : maxTextLenght = 64 ;
ChatManager : : ChatManager ( ) {
console = NULL ;
editEnabled = false ;
teamMode = false ;
thisTeamIndex = - 1 ;
2010-05-15 20:59:17 +02:00
disableTeamMode = false ;
2010-03-20 00:26:00 +01:00
}
2010-06-09 00:50:37 +02:00
void ChatManager : : init ( Console * console , int thisTeamIndex , const bool inMenu ) {
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-03-20 00:26:00 +01:00
}
2010-04-15 23:16:13 +02:00
void ChatManager : : keyUp ( char key ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
try {
if ( editEnabled ) {
if ( key = = vkEscape )
{
text . clear ( ) ;
editEnabled = false ;
}
}
}
catch ( const exception & ex ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
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-03-20 00:26:00 +01:00
void ChatManager : : keyDown ( char key ) {
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-04-11 03:25:06 +02:00
try {
Lang & lang = Lang : : getInstance ( ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
//toggle team mode
2010-05-15 20:59:17 +02:00
if ( editEnabled = = false & & disableTeamMode = = false & & key = = ' H ' ) {
2010-04-11 03:25:06 +02:00
if ( teamMode ) {
teamMode = false ;
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get ( " All " ) ) ;
}
else {
teamMode = true ;
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get ( " Team " ) ) ;
}
2010-03-20 00:26:00 +01:00
}
2010-04-11 03:25:06 +02:00
if ( key = = vkReturn ) {
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-04-11 03:25:06 +02:00
}
2010-06-08 02:00:28 +02:00
else
{
if ( editEnabled ) {
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
if ( ! text . empty ( ) ) {
console - > addLine ( Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " : " + text ) ;
gameNetworkInterface - > sendTextMessage ( Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " : " +
text , teamMode ? thisTeamIndex : - 1 ) ;
2010-06-09 00:50:37 +02:00
if ( ! inMenu ) editEnabled = false ;
2010-06-08 02:00:28 +02:00
}
else
{
editEnabled = false ;
}
text . clear ( ) ;
}
else {
editEnabled = true ;
text . clear ( ) ;
}
2010-03-20 00:26:00 +01:00
}
}
2010-04-11 03:25:06 +02:00
else if ( key = = vkBack ) {
if ( ! text . empty ( ) ) {
text . erase ( text . end ( ) - 1 ) ;
}
2010-03-20 00:26:00 +01:00
}
2010-04-15 23:16:13 +02: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 ( ) ) ;
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
}
void ChatManager : : keyPress ( char c ) {
if ( editEnabled & & text . size ( ) < maxTextLenght ) {
//space is the first meaningful code
if ( c > = ' ' ) {
text + = c ;
}
}
}
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 ( ) ;
string text ;
string sender ;
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
if ( gameNetworkInterface - > getChatText ( ) . empty ( ) = = false ) {
2010-04-11 03:25:06 +02:00
int teamIndex = gameNetworkInterface - > getChatTeamIndex ( ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] got nmtText [%s] for team = %d \n " , __FILE__ , __FUNCTION__ , gameNetworkInterface - > getChatText ( ) . c_str ( ) , teamIndex ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
if ( teamIndex = = - 1 | | teamIndex = = thisTeamIndex ) {
console - > addLine ( gameNetworkInterface - > getChatText ( ) , true ) ;
2010-03-20 00:26:00 +01:00
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Added text to console \n " , __FILE__ , __FUNCTION__ ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 16:59:09 +02:00
gameNetworkInterface - > clearChatInfo ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ( ) ) ;
throw runtime_error ( szBuf ) ;
}
2010-03-20 00:26:00 +01:00
}
} } //end namespace