2010-03-20 00:26:00 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño 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"
2012-03-03 09:45:20 +01:00
# include "sound_renderer.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 ;
2015-02-07 02:24:30 +01:00
xPos = 75 ;
2015-02-06 01:02:34 +01:00
yPos = 155 ;
maxTextLenght = 90 ;
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 ;
2012-09-20 06:10:34 +02:00
customCB = NULL ;
this - > maxCustomTextLength = maxTextLenght ;
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 {
2012-09-20 06:10:34 +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
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 ;
2012-09-20 06:10:34 +02:00
if ( customCB ! = NULL ) {
customCB - > processInputText ( text , true ) ;
customCB = NULL ;
}
2010-04-15 23:16:13 +02:00
}
}
}
catch ( const exception & ex ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " In [%s::%s Line: %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-10-06 22:22:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , szBuf ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-04-15 23:16:13 +02:00
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2015-10-25 00:06:29 +02:00
bool ChatManager : : textInput ( std : : string inputText ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] inputText [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , inputText . c_str ( ) ) ;
2015-10-24 18:02:23 +02:00
int maxTextLenAllowed = ( customCB ! = NULL ? this - > maxCustomTextLength : maxTextLenght ) ;
2015-11-12 00:03:17 +01:00
string textToAdd = getTextWithLengthCheck ( inputText , textCharLength . size ( ) , maxTextLenAllowed ) ;
if ( editEnabled & & ( int ) textCharLength . size ( ) < maxTextLenAllowed & & textToAdd . size ( ) > 0 ) {
2015-10-24 18:02:23 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2015-11-09 23:55:13 +01:00
2015-11-12 06:52:22 +01:00
appendText ( textToAdd , true , true ) ;
2015-10-25 00:06:29 +02:00
updateAutoCompleteBuffer ( ) ;
2015-10-24 18:02:23 +02:00
return true ;
}
return false ;
}
2015-11-10 01:55:52 +01:00
string ChatManager : : getTextWithLengthCheck ( string addText , int currentLength , int maxLength ) {
string resultText = " " ;
if ( addText . empty ( ) = = false ) {
2015-11-12 00:03:17 +01:00
int utf8CharsAdded = 0 ;
2015-11-12 06:52:22 +01:00
for ( unsigned int index = 0 ; index < addText . size ( ) ; ) {
int len = getUTF8_Width ( & addText [ index ] ) ;
utf8CharsAdded + + ;
if ( currentLength + utf8CharsAdded > maxLength ) {
break ;
2015-11-10 01:55:52 +01:00
}
2015-11-12 06:52:22 +01:00
resultText + = addText . substr ( index , len ) ;
index + = len ;
2015-11-10 01:55:52 +01:00
}
}
return resultText ;
}
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
2014-09-22 03:31:35 +02:00
if ( editEnabled = = false & &
2011-06-26 03:55:08 +02:00
isKeyPressed ( configKeys . getSDLKey ( " ChatTeamMode " ) , key ) = = true ) {
2014-09-22 03:31:35 +02:00
if ( disableTeamMode = = true ) {
if ( ! inMenu ) {
console - > addLine ( lang . getString ( " ChatModeDisabledToAvoidCheating " ) ) ;
}
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
if ( ! inMenu ) {
if ( teamMode = = true ) {
teamMode = false ;
console - > addLine ( lang . getString ( " ChatMode " ) + " : " + lang . getString ( " All " ) ) ;
} else {
teamMode = true ;
console - > addLine ( lang . getString ( " ChatMode " ) + " : " + lang . getString ( " 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
}
2014-09-22 03:31:35 +02:00
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 = 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 ) {
2010-12-24 03:10:31 +01:00
2012-09-20 06:10:34 +02:00
if ( customCB = = NULL ) {
2012-10-06 09:06:40 +02:00
//string playerName = gameNetworkInterface->getHumanPlayerName();
2012-09-20 06:10:34 +02:00
int playerIndex = gameNetworkInterface - > getHumanPlayerIndex ( ) ;
if ( this - > manualPlayerNameOverride ! = " " ) {
console - > addLine ( text , false , this - > manualPlayerNameOverride , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
}
else {
console - > addLine ( text , false , playerIndex , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
}
2010-08-21 15:04:52 +02:00
2012-09-20 06:10:34 +02:00
gameNetworkInterface - > sendTextMessage ( " * " + text , teamMode ? thisTeamIndex : - 1 , false , " " ) ;
2012-10-12 00:10:31 +02:00
if ( inMenu = = false & & Config : : getInstance ( ) . getBool ( " ChatStaysActive " , " false " ) = = false ) {
editEnabled = false ;
}
2012-09-20 06:10:34 +02:00
}
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
}
2012-09-20 06:10:34 +02:00
if ( customCB ! = NULL ) {
customCB - > processInputText ( text , false ) ;
editEnabled = false ;
customCB = NULL ;
}
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-11-25 00:15:21 +01:00
else if ( isKeyPressed ( SDLK_TAB , key , false ) = = true ) {
if ( text . empty ( ) = = false ) {
2011-11-25 10:12:53 +01:00
// First find the prefix characters to auto-complete
string currentAutoCompleteName = " " ;
2011-11-25 00:15:21 +01:00
int startPos = - 1 ;
2013-11-02 12:04:52 +01:00
for ( int i = ( int ) text . size ( ) - 1 ; i > = 0 ; - - i ) {
2011-11-25 00:15:21 +01:00
if ( text [ i ] ! = ' ' ) {
startPos = i ;
}
else {
break ;
}
}
if ( startPos > = 0 ) {
2011-11-25 10:12:53 +01:00
currentAutoCompleteName = text . substr ( startPos ) ;
}
2011-11-25 00:15:21 +01:00
2011-11-25 10:12:53 +01:00
//printf("TAB currentAutoCompleteName [%s] lastAutoCompleteSearchText [%s]\n",currentAutoCompleteName.c_str(),lastAutoCompleteSearchText.c_str());
string autoCompleteName = lastAutoCompleteSearchText ;
// Now lookup the prefix for a match in playernames
string autoCompleteResult = " " ;
int replaceCurrentAutoCompleteName = - 1 ;
vector < int > matchedIndexes ;
GameNetworkInterface * gameNetworkInterface = NetworkManager : : getInstance ( ) . getGameNetworkInterface ( ) ;
const GameSettings * settings = gameNetworkInterface - > getGameSettings ( ) ;
2013-11-19 07:14:06 +01:00
for ( unsigned int factionIndex = 0 ; factionIndex < ( unsigned int ) settings - > getFactionCount ( ) ; + + factionIndex ) {
2011-11-25 10:12:53 +01:00
string playerName = settings - > getNetworkPlayerName ( factionIndex ) ;
if ( playerName . length ( ) > autoCompleteName . length ( ) & &
StartsWith ( toLower ( playerName ) , toLower ( autoCompleteName ) ) = = true ) {
if ( toLower ( playerName ) = = toLower ( currentAutoCompleteName ) ) {
replaceCurrentAutoCompleteName = factionIndex ;
}
else {
2011-11-25 00:15:21 +01:00
autoCompleteResult = playerName . substr ( autoCompleteName . length ( ) ) ;
2011-11-25 10:12:53 +01:00
matchedIndexes . push_back ( factionIndex ) ;
}
}
}
2011-12-02 17:07:59 +01:00
if ( matchedIndexes . empty ( ) = = false ) {
2011-11-25 10:12:53 +01:00
int newMatchedIndex = - 1 ;
2013-11-19 07:14:06 +01:00
for ( unsigned int index = 0 ; index < ( unsigned int ) matchedIndexes . size ( ) ; + + index ) {
2011-11-25 10:12:53 +01:00
int possibleMatchIndex = matchedIndexes [ index ] ;
2017-10-10 04:09:36 +02:00
if ( replaceCurrentAutoCompleteName < 0 | | possibleMatchIndex > replaceCurrentAutoCompleteName ) {
2011-11-25 10:12:53 +01:00
newMatchedIndex = possibleMatchIndex ;
2011-11-25 00:15:21 +01:00
break ;
}
}
2011-11-25 10:12:53 +01:00
if ( newMatchedIndex < 0 ) {
2013-11-19 07:14:06 +01:00
for ( unsigned int index = 0 ; index < ( unsigned int ) matchedIndexes . size ( ) ; + + index ) {
2011-11-25 10:12:53 +01:00
int possibleMatchIndex = matchedIndexes [ index ] ;
2017-10-10 04:09:36 +02:00
if ( replaceCurrentAutoCompleteName < 0 | | possibleMatchIndex > replaceCurrentAutoCompleteName ) {
2011-11-25 10:12:53 +01:00
newMatchedIndex = possibleMatchIndex ;
break ;
}
}
}
2011-11-25 00:15:21 +01:00
2011-11-25 10:12:53 +01:00
if ( newMatchedIndex > = 0 ) {
autoCompleteResult = settings - > getNetworkPlayerName ( newMatchedIndex ) . substr ( autoCompleteName . length ( ) ) ;
2011-11-25 00:15:21 +01:00
}
}
2010-07-29 17:49:06 +02:00
2011-11-25 10:12:53 +01:00
if ( autoCompleteResult = = " " ) {
replaceCurrentAutoCompleteName = - 1 ;
matchedIndexes . clear ( ) ;
2013-11-19 07:14:06 +01:00
for ( unsigned int index = 0 ; index < ( unsigned int ) autoCompleteTextList . size ( ) ; + + index ) {
2011-11-25 10:12:53 +01:00
string autoText = autoCompleteTextList [ index ] ;
//printf("CHECKING #2 autoText.length() = %d [%s] autoCompleteName.length() = %d [%s]\n",autoText.length(),autoText.c_str(),autoCompleteName.length(),currentAutoCompleteName.c_str());
if ( autoText . length ( ) > autoCompleteName . length ( ) & &
StartsWith ( toLower ( autoText ) , toLower ( autoCompleteName ) ) = = true ) {
if ( toLower ( autoText ) = = toLower ( currentAutoCompleteName ) ) {
replaceCurrentAutoCompleteName = index ;
//printf("CHECKING #2 REPLACE\n");
}
else {
autoCompleteResult = autoText . substr ( autoCompleteName . length ( ) ) ;
//printf("CHECKING #2 autoCompleteResult [%s] autoCompleteName [%s]\n",autoCompleteResult.c_str(),autoCompleteName.c_str());
matchedIndexes . push_back ( index ) ;
}
}
}
2011-12-02 17:07:59 +01:00
if ( matchedIndexes . empty ( ) = = false ) {
2011-11-25 10:12:53 +01:00
int newMatchedIndex = - 1 ;
2013-11-19 07:14:06 +01:00
for ( unsigned int index = 0 ; index < ( unsigned int ) matchedIndexes . size ( ) ; + + index ) {
2011-11-25 10:12:53 +01:00
int possibleMatchIndex = matchedIndexes [ index ] ;
2017-10-10 04:09:36 +02:00
if ( replaceCurrentAutoCompleteName < 0 | | possibleMatchIndex > replaceCurrentAutoCompleteName ) {
2011-11-25 10:12:53 +01:00
newMatchedIndex = possibleMatchIndex ;
break ;
}
}
if ( newMatchedIndex < 0 ) {
2013-11-19 07:14:06 +01:00
for ( unsigned int index = 0 ; index < ( unsigned int ) matchedIndexes . size ( ) ; + + index ) {
2011-11-25 10:12:53 +01:00
int possibleMatchIndex = matchedIndexes [ index ] ;
2017-10-10 04:09:36 +02:00
if ( replaceCurrentAutoCompleteName < 0 | | possibleMatchIndex > replaceCurrentAutoCompleteName ) {
2011-11-25 10:12:53 +01:00
newMatchedIndex = possibleMatchIndex ;
break ;
}
}
}
2011-10-15 11:27:55 +02:00
2011-11-25 10:12:53 +01:00
if ( newMatchedIndex > = 0 ) {
autoCompleteResult = autoCompleteTextList [ newMatchedIndex ] . substr ( autoCompleteName . length ( ) ) ;
}
2011-10-15 11:27:55 +02:00
}
2011-11-25 10:12:53 +01:00
}
if ( autoCompleteResult ! = " " ) {
if ( replaceCurrentAutoCompleteName > = 0 ) {
2013-11-02 12:04:52 +01:00
deleteText ( ( int ) currentAutoCompleteName . length ( ) , false ) ;
2011-11-25 10:12:53 +01:00
autoCompleteResult = autoCompleteName + autoCompleteResult ;
//printf("REPLACE: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str());
}
else {
//printf("ADD: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str());
2011-10-15 11:27:55 +02:00
}
2015-11-12 06:52:22 +01:00
appendText ( autoCompleteResult , false , false ) ;
2011-10-15 11:27:55 +02:00
}
2010-04-11 03:25:06 +02:00
}
2010-03-20 00:26:00 +01:00
}
2011-11-25 10:12:53 +01:00
else if ( isKeyPressed ( SDLK_BACKSPACE , key , false ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] key = [%c] [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , key . keysym . sym , key . keysym . sym ) ;
deleteText ( 1 ) ;
}
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 ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " 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 ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_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
}
2012-03-19 06:51:35 +01: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 ) ;
2015-10-25 00:06:29 +02:00
// no more textinput with keyPress in SDL2!
// int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght);
// if(editEnabled && (int)text.size() < maxTextLenAllowed) {
// SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,c.keysym.sym,c.keysym.sym);
// //space is the first meaningful code
// wchar_t key = extractKeyPressedUnicode(c);
// wchar_t textAppend[] = { key, 0 };
// appendText(textAppend);
// }
2012-03-19 06:51:35 +01:00
}
2012-09-20 06:10:34 +02:00
void ChatManager : : switchOnEdit ( CustomInputCallbackInterface * customCB , int maxCustomTextLength ) {
2010-12-27 01:59:57 +01:00
editEnabled = true ;
text . clear ( ) ;
2011-10-15 11:27:55 +02:00
textCharLength . clear ( ) ;
2012-09-20 06:10:34 +02:00
this - > customCB = customCB ;
if ( maxCustomTextLength > 0 ) {
this - > maxCustomTextLength = maxCustomTextLength ;
}
else {
this - > maxCustomTextLength = maxTextLenght ;
}
2010-12-27 01:59:57 +01:00
}
2011-11-25 10:12:53 +01:00
void ChatManager : : deleteText ( int deleteCount , bool addToAutoCompleteBuffer ) {
2013-11-19 07:14:06 +01:00
if ( text . empty ( ) = = false & & deleteCount > = 0 ) {
for ( unsigned int i = 0 ; i < ( unsigned int ) deleteCount ; + + i ) {
2011-12-02 17:07:59 +01:00
if ( textCharLength . empty ( ) = = false ) {
2011-11-25 10:12:53 +01:00
//printf("BEFORE DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length());
2013-11-02 12:04:52 +01:00
if ( textCharLength [ textCharLength . size ( ) - 1 ] > ( int ) text . length ( ) ) {
textCharLength [ ( int ) textCharLength . size ( ) - 1 ] = ( int ) text . length ( ) ;
2011-11-25 10:12:53 +01:00
}
2013-11-19 07:14:06 +01:00
for ( unsigned int i = 0 ; i < ( unsigned int ) textCharLength [ textCharLength . size ( ) - 1 ] ; + + i ) {
2011-11-25 10:12:53 +01:00
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 ( ) ;
if ( addToAutoCompleteBuffer = = true ) {
updateAutoCompleteBuffer ( ) ;
}
}
}
}
}
2015-11-12 06:52:22 +01:00
void ChatManager : : appendText ( string addText , bool validateChars , bool addToAutoCompleteBuffer ) {
2011-11-25 10:12:53 +01:00
2015-11-12 06:52:22 +01:00
for ( unsigned int index = 0 ; index < addText . size ( ) ; ) {
int len = getUTF8_Width ( & addText [ index ] ) ;
textCharLength . push_back ( len ) ;
text + = addText . substr ( index , len ) ;
index + = len ;
if ( addToAutoCompleteBuffer = = true ) {
updateAutoCompleteBuffer ( ) ;
2011-11-25 10:12:53 +01:00
}
}
}
void ChatManager : : updateAutoCompleteBuffer ( ) {
if ( text . empty ( ) = = false ) {
int startPos = - 1 ;
2013-11-02 12:04:52 +01:00
for ( int i = ( int ) text . size ( ) - 1 ; i > = 0 ; - - i ) {
2011-11-25 10:12:53 +01:00
if ( text [ i ] ! = ' ' ) {
startPos = i ;
}
else {
break ;
}
}
if ( startPos > = 0 ) {
lastAutoCompleteSearchText = text . substr ( startPos ) ;
2011-11-25 00:15:21 +01:00
}
}
}
2011-06-26 03:55:08 +02:00
void ChatManager : : addText ( string text ) {
2012-09-20 06:10:34 +02:00
int maxTextLenAllowed = ( customCB ! = NULL ? this - > maxCustomTextLength : maxTextLenght ) ;
2013-12-10 01:52:08 +01:00
if ( editEnabled & & ( int ) text . size ( ) + ( int ) this - > text . size ( ) < = maxTextLenAllowed ) {
2011-06-26 03:55:08 +02:00
this - > text + = text ;
2013-12-10 01:52:08 +01:00
for ( int i = 0 ; i < ( int ) text . size ( ) ; i + + ) {
textCharLength . push_back ( 1 ) ;
}
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
2011-11-25 18:01:35 +01:00
if ( gameNetworkInterface ! = NULL & &
gameNetworkInterface - > getChatTextList ( false ) . empty ( ) = = false ) {
2011-04-05 22:19:25 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2011-11-25 18:01:35 +01:00
std : : vector < ChatMsgInfo > chatList = gameNetworkInterface - > getChatTextList ( true ) ;
2013-11-19 07:14:06 +01:00
for ( int idx = 0 ; idx < ( int ) chatList . size ( ) ; idx + + ) {
2011-11-25 18:01:35 +01:00
const ChatMsgInfo msg = chatList [ idx ] ;
2010-07-01 02:08:59 +02:00
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 ) ;
2012-03-03 09:45:20 +01:00
string playerName = gameNetworkInterface - > getHumanPlayerName ( ) ;
if ( this - > manualPlayerNameOverride ! = " " ) {
playerName = this - > manualPlayerNameOverride ;
}
2012-06-03 19:08:14 +02:00
2012-10-06 19:50:27 +02:00
//printf("Network chat msg from: [%d - %s] [%s]\n",msg.chatPlayerIndex,gameNetworkInterface->getHumanPlayerName().c_str(),this->manualPlayerNameOverride.c_str());
2012-06-04 23:53:20 +02:00
if ( StartsWith ( msg . chatText , " * " ) ) {
if ( msg . chatText . find ( playerName ) ! = string : : npos ) {
CoreData & coreData = CoreData : : getInstance ( ) ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
2017-02-02 02:54:29 +01:00
soundRenderer . playFx ( coreData . getHighlightSound ( ) , true ) ;
2012-06-04 23:53:20 +02:00
}
2012-06-03 19:08:14 +02:00
console - > addLine ( msg . chatText . substr ( 1 , msg . chatText . size ( ) ) , true , msg . chatPlayerIndex , Vec3f ( 1.f , 1.f , 1.f ) , teamMode ) ;
2012-03-03 09:45:20 +01:00
}
2012-06-03 19:08:14 +02:00
else {
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__ ) ;
2011-11-25 18:01:35 +01:00
//gameNetworkInterface->clearChatInfo();
2010-03-20 00:26:00 +01:00
}
}
2010-04-11 03:25:06 +02:00
catch ( const std : : exception & ex ) {
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " 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 ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2010-04-11 03:25:06 +02:00
}
2010-03-20 00:26:00 +01:00
}
} } //end namespace