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>
2011-06-26 05:38:28 +02:00
# include "string_utils.h"
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 ;
2011-10-15 11:27:55 +02:00
textCharLength . clear ( ) ;
text = " " ;
2010-12-27 01:59:57 +01:00
font = CoreData : : getInstance ( ) . getConsoleFont ( ) ;
2011-06-10 05:09:19 +02:00
font3D = CoreData : : getInstance ( ) . getConsoleFont3D ( ) ;
2011-09-01 01:10:43 +02:00
inMenu = false ;
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
}
2011-06-26 03:55:08 +02:00
void ChatManager : : setDisableTeamMode ( bool value ) {
disableTeamMode = value ;
if ( disableTeamMode = = true ) {
teamMode = false ;
}
}
void ChatManager : : keyUp ( SDL_KeyboardEvent 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 ) {
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2011-06-26 03:55:08 +02:00
//if(key == vkEscape || key == SDLK_ESCAPE) {
2011-07-05 07:40:14 +02:00
if ( isKeyPressed ( SDLK_ESCAPE , key , false ) = = true ) {
2010-04-15 23:16:13 +02:00
text . clear ( ) ;
2011-10-15 11:27:55 +02:00
textCharLength . clear ( ) ;
2010-04-15 23:16:13 +02:00
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__ ) ;
}
2011-06-26 03:55:08 +02:00
void ChatManager : : keyDown ( SDL_KeyboardEvent key ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
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 & &
2011-06-26 03:55:08 +02:00
//key == configKeys.getCharKey("ChatTeamMode")) {
isKeyPressed ( configKeys . getSDLKey ( " ChatTeamMode " ) , key ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2011-01-01 21:31:25 +01:00
if ( ! inMenu ) {
if ( teamMode = = true ) {
teamMode = false ;
2011-10-03 02:36:28 +02:00
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get ( " All " ) ) ;
2011-01-01 21:31:25 +01:00
} else {
teamMode = true ;
2011-06-26 03:55:08 +02:00
console - > addLine ( lang . get ( " ChatMode " ) + " : " + lang . get ( " Team " ) ) ;
2011-01-01 21:31:25 +01:00
}
2010-04-11 03:25:06 +02:00
}
2010-03-20 00:26:00 +01:00
}
2011-06-26 03:55:08 +02:00
//if(key==vkReturn) {
2011-07-05 07:40:14 +02:00
if ( isKeyPressed ( SDLK_RETURN , key , false ) = = true ) {
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2011-06-28 03:25:14 +02:00
//SDL_keysym keystate = Window::getKeystate();
SDL_keysym keystate = key . keysym ;
2010-06-08 02:00:28 +02:00
if ( keystate . mod & ( KMOD_LALT | KMOD_RALT ) ) {
// alt+enter is ignored
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-04-11 03:25:06 +02:00
}
2011-06-26 03:55:08 +02:00
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2010-10-22 09:28:55 +02:00
if ( editEnabled = = true ) {
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
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 ! = " " ) {
2011-10-03 02:36:28 +02:00
console - > addLine ( text , false , this - > manualPlayerNameOverride , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
2010-12-24 03:10:31 +01:00
}
else {
2011-10-03 02:36:28 +02:00
console - > addLine ( text , false , playerIndex , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
2010-12-24 03:10:31 +01:00
}
2010-08-21 15:04:52 +02:00
2011-04-05 20:39:47 +02:00
gameNetworkInterface - > sendTextMessage ( text , teamMode ? thisTeamIndex : - 1 , false , " " ) ;
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 ( ) ;
2011-10-15 11:27:55 +02:00
textCharLength . clear ( ) ;
2010-06-08 02:00:28 +02:00
}
2010-07-29 17:49:06 +02:00
else {
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
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
}
}
2011-06-26 03:55:08 +02:00
//else if(key==vkBack) {
2011-07-05 07:40:14 +02:00
else if ( isKeyPressed ( SDLK_BACKSPACE , key , false ) = = true ) {
2011-06-26 03:55:08 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2011-10-15 11:27:55 +02:00
if ( text . empty ( ) = = false ) {
if ( textCharLength . size ( ) > 0 ) {
//printf("BEFORE DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length());
if ( textCharLength [ textCharLength . size ( ) - 1 ] > text . length ( ) ) {
textCharLength [ textCharLength . size ( ) - 1 ] = text . length ( ) ;
}
for ( unsigned int i = 0 ; i < textCharLength [ textCharLength . size ( ) - 1 ] ; + + i ) {
text . erase ( text . end ( ) - 1 ) ;
}
//printf("AFTER DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length());
textCharLength . pop_back ( ) ;
}
2010-04-11 03:25:06 +02:00
}
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
}
2011-06-26 03:55:08 +02:00
void ChatManager : : switchOnEdit ( ) {
2010-12-27 01:59:57 +01:00
editEnabled = true ;
text . clear ( ) ;
2011-10-15 11:27:55 +02:00
textCharLength . clear ( ) ;
2010-12-27 01:59:57 +01:00
}
2011-06-26 03:55:08 +02:00
void ChatManager : : keyPress ( SDL_KeyboardEvent c ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , c . keysym . sym , c . keysym . sym ) ;
2010-07-29 17:49:06 +02:00
2011-06-26 03:55:08 +02:00
if ( editEnabled & & text . size ( ) < maxTextLenght ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , c . keysym . sym , c . keysym . sym ) ;
2010-03-20 00:26:00 +01:00
//space is the first meaningful code
2011-09-16 19:35:07 +02:00
// SDLKey key = extractKeyPressed(c);
// if(isAllowedInputTextKey(key)) {
wchar_t key = extractKeyPressedUnicode ( c ) ;
2011-06-28 03:25:14 +02:00
if ( isAllowedInputTextKey ( key ) ) {
2011-09-16 19:35:07 +02:00
//char szCharText[20]="";
//sprintf(szCharText,"%lc",key);
//char *utfStr = String::ConvertToUTF8(&szCharText[0]);
//text += utfStr;
//wchar_t wc = 0x1234;
char buf [ 4 ] = { 0 } ;
if ( key < 0x80 ) {
buf [ 0 ] = key ;
2011-10-15 11:27:55 +02:00
textCharLength . push_back ( 1 ) ;
//printf("1 char, textCharLength = %d\n",textCharLength.size());
2011-09-16 19:35:07 +02:00
}
else if ( key < 0x800 ) {
buf [ 0 ] = ( 0xC0 | key > > 6 ) ;
buf [ 1 ] = ( 0x80 | key & 0x3F ) ;
2011-10-15 11:27:55 +02:00
textCharLength . push_back ( 2 ) ;
//printf("2 char, textCharLength = %d\n",textCharLength.size());
2011-09-16 19:35:07 +02:00
}
else {
buf [ 0 ] = ( 0xE0 | key > > 12 ) ;
buf [ 1 ] = ( 0x80 | key > > 6 & 0x3F ) ;
buf [ 2 ] = ( 0x80 | key & 0x3F ) ;
2011-10-15 11:27:55 +02:00
textCharLength . push_back ( 3 ) ;
//printf("3 char, textCharLength = %d\n",textCharLength.size());
2011-09-16 19:35:07 +02:00
}
text + = buf ;
2011-10-17 22:56:11 +02:00
//printf("text length = %d\n",text.length());
2011-10-15 11:27:55 +02:00
2011-09-16 19:35:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] [%d] szCharText [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key , text . c_str ( ) ) ;
//delete [] utfStr;
2010-03-20 00:26:00 +01:00
}
}
}
2011-06-26 03:55:08 +02:00
void ChatManager : : addText ( string text ) {
if ( editEnabled & & text . size ( ) + this - > text . size ( ) < maxTextLenght ) {
this - > text + = text ;
2010-12-27 01:59:57 +01:00
}
}
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;
2011-09-01 01:10:43 +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 ) {
2011-04-05 22:19:25 +02:00
Lang & lang = Lang : : getInstance ( ) ;
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 ) {
2011-04-05 22:19:25 +02:00
if ( msg . targetLanguage = = " " | | lang . isLanguageLocal ( msg . targetLanguage ) = = true ) {
2011-10-12 05:07:00 +02:00
bool teamMode = ( teamIndex ! = - 1 & & teamIndex = = thisTeamIndex ) ;
console - > addLine ( msg . chatText , true , msg . chatPlayerIndex , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
2011-04-05 22:19:25 +02:00
}
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