- fix text entry so unicode characters work properly

This commit is contained in:
Mark Vejvoda
2012-09-21 03:41:10 +00:00
parent c50469acd7
commit 684c35e25a
4 changed files with 116 additions and 7 deletions

View File

@@ -347,6 +347,7 @@ void MenuState::setActiveInputLabel(GraphicLabel *newLabel, GraphicLabel **activ
text += "_"; text += "_";
} }
newLabel->setText(text); newLabel->setText(text);
textCharLength = extractKeyPressedUnicodeLength(text);
} }
if(activeInputLabelEdit != NULL && activeInputLabelEdit->getText().empty() == false) { if(activeInputLabelEdit != NULL && activeInputLabelEdit->getText().empty() == false) {
string text= activeInputLabelEdit->getText(); string text= activeInputLabelEdit->getText();
@@ -356,6 +357,7 @@ void MenuState::setActiveInputLabel(GraphicLabel *newLabel, GraphicLabel **activ
text = text.substr(0,found); text = text.substr(0,found);
} }
activeInputLabelEdit->setText(text); activeInputLabelEdit->setText(text);
textCharLength = extractKeyPressedUnicodeLength(text);
activeInputLabelEdit->setEditModeEnabled(false); activeInputLabelEdit->setEditModeEnabled(false);
} }
if(newLabel != NULL) { if(newLabel != NULL) {
@@ -377,6 +379,7 @@ bool MenuState::keyPressEditLabel(SDL_KeyboardEvent c, GraphicLabel **activeInpu
if(isKeyPressed(SDLK_ESCAPE,c,false) == true || if(isKeyPressed(SDLK_ESCAPE,c,false) == true ||
isKeyPressed(SDLK_RETURN,c,false) == true ) { isKeyPressed(SDLK_RETURN,c,false) == true ) {
setActiveInputLabel(NULL,activeInputLabelPtr); setActiveInputLabel(NULL,activeInputLabelPtr);
textCharLength.clear();
eventHandled = true; eventHandled = true;
return eventHandled; return eventHandled;
} }
@@ -386,22 +389,51 @@ bool MenuState::keyPressEditLabel(SDL_KeyboardEvent c, GraphicLabel **activeInpu
if(activeInputLabel->getText().size() < maxTextSize) { if(activeInputLabel->getText().size() < maxTextSize) {
string text= activeInputLabel->getText(); string text= activeInputLabel->getText();
//text.insert(text.end()-1, key); //text.insert(text.end()-1, key);
char szCharText[20]="";
sprintf(szCharText,"%c",key); //char szCharText[20]="";
char *utfStr = String::ConvertToUTF8(&szCharText[0]); //sprintf(szCharText,"%c",key);
//char *utfStr = String::ConvertToUTF8(&szCharText[0]);
wchar_t keyW = extractKeyPressedUnicode(c);
wchar_t textAppend[] = { keyW, 0 };
wchar_t newKey = textAppend[0];
char buf[4] = {0};
if (newKey < 0x80) {
buf[0] = newKey;
textCharLength.push_back(1);
//printf("1 char, textCharLength = %d\n",textCharLength.size());
}
else if (newKey < 0x800) {
buf[0] = (0xC0 | newKey >> 6);
buf[1] = (0x80 | newKey & 0x3F);
textCharLength.push_back(2);
//printf("2 char, textCharLength = %d\n",textCharLength.size());
}
else {
buf[0] = (0xE0 | newKey >> 12);
buf[1] = (0x80 | newKey >> 6 & 0x3F);
buf[2] = (0x80 | newKey & 0x3F);
textCharLength.push_back(3);
//printf("3 char, textCharLength = %d\n",textCharLength.size());
}
if(text.size() > 0) { if(text.size() > 0) {
size_t found = text.find_last_of("_"); size_t found = text.find_last_of("_");
if (found == string::npos || found != text.length()-1) { if (found == string::npos || found != text.length()-1) {
text.insert(text.end(), utfStr[0]); //text.insert(text.end(), utfStr[0]);
//text.insert(text.end(), buf);
text += buf;
} }
else { else {
text.insert(text.end() -1, utfStr[0]); //text.insert(text.end() -1, utfStr[0]);
text.insert(text.end() -1, buf[0]);
} }
} }
else { else {
text = utfStr[0]; text = buf;
} }
delete [] utfStr; //delete [] utfStr;
activeInputLabel->setText(text); activeInputLabel->setText(text);
@@ -418,6 +450,9 @@ bool MenuState::keyDownEditLabel(SDL_KeyboardEvent c, GraphicLabel **activeInput
if(activeInputLabel != NULL) { if(activeInputLabel != NULL) {
string text = activeInputLabel->getText(); string text = activeInputLabel->getText();
if(isKeyPressed(SDLK_BACKSPACE,c) == true && text.length() > 0) { if(isKeyPressed(SDLK_BACKSPACE,c) == true && text.length() > 0) {
//printf("BSPACE text [%s]\n",text.c_str());
/*
size_t found = text.find_last_of("_"); size_t found = text.find_last_of("_");
if (found == string::npos || found != text.length()-1) { if (found == string::npos || found != text.length()-1) {
text.erase(text.end() - 1); text.erase(text.end() - 1);
@@ -427,7 +462,48 @@ bool MenuState::keyDownEditLabel(SDL_KeyboardEvent c, GraphicLabel **activeInput
text.erase(text.end() - 2); text.erase(text.end() - 2);
} }
} }
*/
bool hasUnderscore = false;
bool delChar = false;
size_t found = text.find_last_of("_");
if (found == string::npos || found != text.length()-1) {
//printf("A text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size());
if(textCharLength[textCharLength.size()-1] >= 1) {
//textCharLength[textCharLength.size()-1] = text.length();
delChar = true;
}
}
else {
//printf("B text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size());
hasUnderscore = true;
if(textCharLength.size() >= 2 && textCharLength[textCharLength.size()-2] >= 1) {
//textCharLength[textCharLength.size()-2] = text.length();
delChar = true;
}
}
if(delChar == true) {
if(hasUnderscore) {
if(textCharLength.size() > 1) {
for(unsigned int i = 0; i < textCharLength[textCharLength.size()-2]; ++i) {
text.erase(text.end() -2);
}
//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();
textCharLength.pop_back();
textCharLength.push_back(1);
}
}
else {
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();
}
}
activeInputLabel->setText(text); activeInputLabel->setText(text);
eventHandled = true; eventHandled = true;
} }

View File

@@ -103,6 +103,8 @@ protected:
const char *containerName; const char *containerName;
Console console; Console console;
vector<int> textCharLength;
protected: protected:
void setActiveInputLabel(GraphicLabel *newLabel, GraphicLabel **activeInputLabelPtr); void setActiveInputLabel(GraphicLabel *newLabel, GraphicLabel **activeInputLabelPtr);

View File

@@ -19,9 +19,11 @@
#include <cassert> #include <cassert>
#include "data_types.h" #include "data_types.h"
#include "vec.h" #include "vec.h"
#include <vector>
#include "leak_dumper.h" #include "leak_dumper.h"
using std::map; using std::map;
using std::vector;
using std::string; using std::string;
using Shared::Graphics::Vec2i; using Shared::Graphics::Vec2i;
@@ -213,6 +215,7 @@ SDLKey extractKeyPressed(SDL_KeyboardEvent input);
bool isAllowedInputTextKey(SDLKey key); bool isAllowedInputTextKey(SDLKey key);
wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input); wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input);
vector<int> extractKeyPressedUnicodeLength(string text);
bool isAllowedInputTextKey(wchar_t &key); bool isAllowedInputTextKey(wchar_t &key);
}}//end namespace }}//end namespace

View File

@@ -936,6 +936,34 @@ wchar_t extractKeyPressedUnicode(SDL_KeyboardEvent input) {
return c; return c;
} }
vector<int> extractKeyPressedUnicodeLength(string text) {
vector<int> result;
unsigned int i = 0;
for(i = 0; i < text.length();) {
char c = text[i];
wchar_t keyW = c;
wchar_t textAppend[] = { keyW, 0 };
if(textAppend) {
wchar_t newKey = textAppend[0];
if (newKey < 0x80) {
result.push_back(1);
//printf("1 char, textCharLength = %d\n",textCharLength.size());
}
else if (newKey < 0x800) {
result.push_back(2);
//printf("2 char, textCharLength = %d\n",textCharLength.size());
}
else {
result.push_back(3);
//printf("3 char, textCharLength = %d\n",textCharLength.size());
}
i += result[result.size()-1];
}
}
return result;
}
SDLKey extractKeyPressed(SDL_KeyboardEvent input) { SDLKey extractKeyPressed(SDL_KeyboardEvent input) {
SDLKey c = SDLK_UNKNOWN; SDLKey c = SDLK_UNKNOWN;
//if(input.keysym.unicode > 0 && input.keysym.unicode < 0x80) { //if(input.keysym.unicode > 0 && input.keysym.unicode < 0x80) {