diff --git a/source/glest_game/game/chat_manager.cpp b/source/glest_game/game/chat_manager.cpp index aa0e8c5c..59399d51 100644 --- a/source/glest_game/game/chat_manager.cpp +++ b/source/glest_game/game/chat_manager.cpp @@ -99,15 +99,14 @@ bool ChatManager::textInput(std::string inputText) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] inputText [%s]\n",__FILE__,__FUNCTION__,__LINE__,inputText.c_str()); int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); - int maxpaste=0; - maxpaste=maxTextLenAllowed-text.length(); - string textToAdd=inputText.substr (0,maxpaste); - if(editEnabled && (int)text.length() < maxTextLenAllowed) { + //int maxpaste = 0; + //maxpaste = maxTextLenAllowed - text.length(); + //maxpaste = maxTextLenAllowed - getTextByteLength(); + //string textToAdd = inputText.substr (0,maxpaste); + string textToAdd = getTextWithLengthCheck(inputText,getTextByteLength(),maxTextLenAllowed); + + if(editEnabled && (int)text.length() < maxTextLenAllowed && textToAdd.size() > 0) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -// for(unsigned int idx = 0; idx < textToAdd.size(); ++idx) { -// textCharLength.push_back(1); -// } -// this->text +=textToAdd; WString addText(textToAdd); appendText(addText.cw_str(), true, true); @@ -118,6 +117,72 @@ bool ChatManager::textInput(std::string inputText) { return false; } +string ChatManager::getTextWithLengthCheck(string addText, int currentLength, int maxLength) { + + //printf("len check text [%s] curlen: %d maxlen: %d\n",addText.c_str(),currentLength,maxLength); + + string resultText = ""; + if(addText.empty() == false) { + int bytesToAdd = 0; + WString addTextW(addText); + const wchar_t *addTextPtr = addTextW.cw_str(); + for(unsigned int i = 0; i < wcslen(addTextPtr); ++i) { + wchar_t key = addTextPtr[i]; + if(isAllowedInputTextKey(key) == true && key != 10) { + char buf[4] = {0}; + if (key < 0x80) { + bytesToAdd++; + //printf("#1 len check cur: %d max: %d\n",currentLength + bytesToAdd,maxLength); + if(currentLength + bytesToAdd > maxLength) { + break; + } + buf[0] = key; + + resultText += buf; + } + else if (key < 0x800) { + bytesToAdd+=2; + //printf("#2 len check cur: %d max: %d\n",currentLength + bytesToAdd,maxLength); + if(currentLength + bytesToAdd > maxLength) { + break; + } + buf[0] = (0xC0 | key >> 6); + buf[1] = (0x80 | (key & 0x3F)); + + resultText += buf; + } + else { + bytesToAdd+=3; + //printf("#3 len check cur: %d max: %d\n",currentLength + bytesToAdd,maxLength); + if(currentLength + bytesToAdd > maxLength) { + break; + } + buf[0] = (0xE0 | key >> 12); + buf[1] = (0x80 | (key >> 6 & 0x3F)); + buf[2] = (0x80 | (key & 0x3F)); + + resultText += buf; + } + } + else { + //printf("len check char NOT ALLOWED at pos: %d\n",i); + } + } + } + //printf("len check resultText: %s\n",resultText.c_str()); + return resultText; +} + +int ChatManager::getTextByteLength() { + int byteLength = 0; + if(text.empty() == false && textCharLength.empty() == false) { + for(unsigned int i = 0; i < textCharLength.size(); ++i) { + byteLength+= textCharLength[i]; + } + } + return byteLength; +} + 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); diff --git a/source/glest_game/game/chat_manager.h b/source/glest_game/game/chat_manager.h index 11afc4ac..3d3048fb 100644 --- a/source/glest_game/game/chat_manager.h +++ b/source/glest_game/game/chat_manager.h @@ -69,6 +69,8 @@ private: CustomInputCallbackInterface *customCB; int maxCustomTextLength; + string getTextWithLengthCheck(string text, int currentLength, int maxLength); + int getTextByteLength(); void appendText(const wchar_t *addText, bool validateChars=true,bool addToAutoCompleteBuffer=true); void deleteText(int deleteCount,bool addToAutoCompleteBuffer=true); void updateAutoCompleteBuffer();