From 4f7559783cafc9744f7de40ff4d42928f758b826 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 15 Oct 2011 09:27:55 +0000 Subject: [PATCH] support backspace for multi-byte characters in chat mode --- source/glest_game/game/chat_manager.cpp | 28 +++++++++++++++++++++++-- source/glest_game/game/chat_manager.h | 3 +++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/source/glest_game/game/chat_manager.cpp b/source/glest_game/game/chat_manager.cpp index 3431414d..dbc9d374 100644 --- a/source/glest_game/game/chat_manager.cpp +++ b/source/glest_game/game/chat_manager.cpp @@ -41,6 +41,8 @@ ChatManager::ChatManager() { xPos=300; yPos=150; maxTextLenght=64; + textCharLength.clear(); + text=""; font=CoreData::getInstance().getConsoleFont(); font3D=CoreData::getInstance().getConsoleFont3D(); inMenu=false; @@ -72,6 +74,7 @@ void ChatManager::keyUp(SDL_KeyboardEvent key) { //if(key == vkEscape || key == SDLK_ESCAPE) { if(isKeyPressed(SDLK_ESCAPE,key,false) == true) { text.clear(); + textCharLength.clear(); editEnabled= false; } } @@ -146,6 +149,7 @@ void ChatManager::keyDown(SDL_KeyboardEvent key) { editEnabled= false; } text.clear(); + textCharLength.clear(); } else { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key.keysym.sym,key.keysym.sym); @@ -158,8 +162,19 @@ void ChatManager::keyDown(SDL_KeyboardEvent key) { 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); - if(!text.empty()) { - text.erase(text.end() -1); + 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(); + } } } @@ -177,6 +192,7 @@ void ChatManager::keyDown(SDL_KeyboardEvent key) { void ChatManager::switchOnEdit() { editEnabled= true; text.clear(); + textCharLength.clear(); } void ChatManager::keyPress(SDL_KeyboardEvent c) { @@ -198,18 +214,26 @@ void ChatManager::keyPress(SDL_KeyboardEvent c) { char buf[4] = {0}; if (key < 0x80) { buf[0] = key; + textCharLength.push_back(1); + //printf("1 char, textCharLength = %d\n",textCharLength.size()); } else if (key < 0x800) { buf[0] = (0xC0 | key >> 6); buf[1] = (0x80 | key & 0x3F); + textCharLength.push_back(2); + //printf("2 char, textCharLength = %d\n",textCharLength.size()); } else { buf[0] = (0xE0 | key >> 12); buf[1] = (0x80 | key >> 6 & 0x3F); buf[2] = (0x80 | key & 0x3F); + textCharLength.push_back(3); + //printf("3 char, textCharLength = %d\n",textCharLength.size()); } text += buf; + printf("text length = %d\n",text.length()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] [%d] szCharText [%s]\n",__FILE__,__FUNCTION__,__LINE__, key,text.c_str()); //delete [] utfStr; diff --git a/source/glest_game/game/chat_manager.h b/source/glest_game/game/chat_manager.h index 07728782..177addef 100644 --- a/source/glest_game/game/chat_manager.h +++ b/source/glest_game/game/chat_manager.h @@ -15,9 +15,11 @@ #include #include "font.h" #include +#include #include "leak_dumper.h" using std::string; +using std::vector; using Shared::Graphics::Font2D; using Shared::Graphics::Font3D; @@ -37,6 +39,7 @@ private: bool disableTeamMode; Console* console; string text; + vector textCharLength; int thisTeamIndex; bool inMenu; string manualPlayerNameOverride;