Fix several keyboard problems (#160)

- Longer than 1 row broke
- Unicode was broken
- Lots of unicode problems
This commit is contained in:
Pk11 2022-04-05 00:23:13 -05:00 committed by GitHub
parent 6015596593
commit 7ade5f7d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,15 +22,43 @@ std::string kbdGetString(std::string label, int maxSize, std::string oldStr) {
std::string output(oldStr); std::string output(oldStr);
int stringPosition = output.size(), scrollPosition = stringPosition;
for(int i = 0; i < SCREEN_COLS - 4 && scrollPosition > 0; i++) {
scrollPosition--;
while((output[scrollPosition] & 0xC0) == 0x80) // UTF-8
scrollPosition--;
}
u16 pressed; u16 pressed;
int key, cursorPosition = output.length(); int key;
int labelHeight = font->calcHeight(label); int labelHeight = font->calcHeight(label);
bool done = false; bool done = false;
while(!done) { while(!done) {
font->clear(false); font->clear(false);
font->print(0, 0, false, label); font->print(0, 0, false, label);
font->printf(0, labelHeight, false, Alignment::left, Palette::white, "> %s", output.c_str());
font->printf(2 + cursorPosition, labelHeight, false, Alignment::left, Palette::blackWhite, "%c", cursorPosition < (int)output.length() ? output[cursorPosition] : ' '); int strSize = 0;
for(int i = 0; strSize < (int)output.size() && (i < SCREEN_COLS - 3 || (output[scrollPosition + strSize] & 0xC0) == 0x80); strSize++) {
if((output[scrollPosition + strSize] & 0xC0) != 0x80)
i++;
}
font->printf(0, labelHeight, false, Alignment::left, Palette::white, "> %s", output.substr(scrollPosition, strSize).c_str());
if(scrollPosition + strSize < (int)output.size())
font->print(-1, labelHeight, false, "");
if(scrollPosition > 0)
font->print(1, labelHeight, false, "");
int charLen = 1;
while((output[stringPosition + charLen] & 0xC0) == 0x80)
charLen++;
int cursorPosition = 0;
for(int i = 0; scrollPosition + i < stringPosition; i++) {
if((output[scrollPosition + i] & 0xC0) != 0x80)
cursorPosition++;
}
font->printf(2 + cursorPosition, labelHeight, false, Alignment::left, Palette::blackWhite, "%s", stringPosition < (int)output.size() ? output.substr(stringPosition, charLen).c_str() : " ");
font->print(0, labelHeight + 2, false, STR_START_RETURN_B_BACKSPACE); font->print(0, labelHeight + 2, false, STR_START_RETURN_B_BACKSPACE);
font->update(false); font->update(false);
@ -53,44 +81,67 @@ std::string kbdGetString(std::string label, int maxSize, std::string oldStr) {
case DVK_TAB: // tab case DVK_TAB: // tab
break; break;
case DVK_RIGHT: // Right case DVK_RIGHT: // Right
if(cursorPosition < (int)output.length()) pressed |= KEY_RIGHT;
cursorPosition++;
break; break;
case DVK_LEFT: // Left case DVK_LEFT: // Left
if(cursorPosition > 0) pressed |= KEY_LEFT;
cursorPosition--;
break; break;
case DVK_FOLD: // (using as esc) case DVK_FOLD: // (using as esc)
output = oldStr; output = oldStr;
done = true; done = true;
break; break;
case DVK_BACKSPACE: // Backspace case DVK_BACKSPACE: // Backspace
if(cursorPosition > 0) { pressed |= KEY_B;
output.erase(output.begin() + cursorPosition - 1);
cursorPosition--;
}
break; break;
case DVK_ENTER: // Return case DVK_ENTER: // Return
done = true; done = true;
break; break;
default: // Letter default: // Letter
if(output.size() < (uint)maxSize) { if(output.size() < (uint)maxSize) {
output.insert(output.begin() + cursorPosition, key); output.insert(output.begin() + stringPosition, key);
cursorPosition++; stringPosition++;
if(cursorPosition + 1 >= (SCREEN_COLS - 3) && stringPosition <= (int)output.size()) {
scrollPosition++;
while((output[scrollPosition] & 0xC0) == 0x80) // UTF-8
scrollPosition++;
}
} }
break; break;
} }
if(pressed & KEY_LEFT) { if(pressed & KEY_LEFT) {
if(cursorPosition > 0) if(stringPosition > 0) {
cursorPosition--; stringPosition--;
while((output[stringPosition] & 0xC0) == 0x80) // UTF-8
stringPosition--;
if(cursorPosition - 1 < 0) {
scrollPosition--;
while((output[scrollPosition] & 0xC0) == 0x80) // UTF-8
scrollPosition--;
}
}
} else if(pressed & KEY_RIGHT) { } else if(pressed & KEY_RIGHT) {
if(cursorPosition < (int)output.length()) if(stringPosition < (int)output.size()) {
cursorPosition++; stringPosition++;
while((output[stringPosition] & 0xC0) == 0x80) // UTF-8
stringPosition++;
if(cursorPosition + 1 >= (SCREEN_COLS - 3)) {
scrollPosition++;
while((output[scrollPosition] & 0xC0) == 0x80) // UTF-8
scrollPosition++;
}
}
} else if(pressed & KEY_B) { } else if(pressed & KEY_B) {
if(cursorPosition > 0) { if(stringPosition > 0) {
output.erase(output.begin() + cursorPosition - 1); stringPosition--;
cursorPosition--; while((output[stringPosition] & 0xC0) == 0x80) {
output.erase(output.begin() + stringPosition);
stringPosition--;
}
output.erase(output.begin() + stringPosition);
} }
} else if(pressed & KEY_START) { } else if(pressed & KEY_START) {
done = true; done = true;