GodMode9i/arm9/source/keyboard.cpp
Pk11 d8bf2447ec
Clear cart info on eject outside of drive menu and detect ramdrive eject (#146)
* Fix cart info if ejected not in drive menu

* Fix a couple ramdrive issues

- Unmount ramdrive if Slot-2 RAM is ejected
- Fix potentially reading from the wrong location if numSectors > 1 and crosses between locations
- free ramdLoc on regular DS too
- Use calloc to 0 initialize, I saw it mess up occasionally on DSi too

* Update time in the Vblank handler
2022-01-07 20:35:49 -07:00

102 lines
2.5 KiB
C++

#include "keyboard.h"
#include "font.h"
#include "language.h"
#include <nds.h>
#include <string.h>
std::string kbdGetString(std::string label, int maxSize, std::string oldStr) {
font->clear(false);
font->update(false);
bgInit(0, BgType_Text4bpp, BgSize_T_256x512, 20, 0);
keyboardInit(nullptr, 0, BgType_Text4bpp, BgSize_T_256x512, 20, 0, false, true);
BG_PALETTE_SUB[0] = 0x0000;
BG_PALETTE_SUB[1] = 0x7FFF;
keyboardShow();
std::string output(oldStr);
u16 pressed;
int key, cursorPosition = output.length();
int labelHeight = font->calcHeight(label);
bool done = false;
while(!done) {
font->clear(false);
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] : ' ');
font->print(0, labelHeight + 2, false, STR_START_RETURN_B_BACKSPACE);
font->update(false);
do {
scanKeys();
pressed = keysDownRepeat();
key = keyboardUpdate();
swiWaitForVBlank();
} while (!((pressed & (KEY_LEFT | KEY_RIGHT | KEY_B | KEY_START
#ifdef SCREENSWAP
&& !(pressed & KEY_TOUCH)
#endif
)) || (key != -1)));
switch(key) {
case NOKEY:
case DVK_MENU:
case DVK_CAPS: // Caps
case DVK_SHIFT: // Shift
case DVK_CTRL: // Ctrl
case DVK_UP: // Up
case DVK_DOWN: // Down
case DVK_ALT: // Alt
case DVK_TAB: // tab
break;
case DVK_RIGHT: // Right
if(cursorPosition < (int)output.length())
cursorPosition++;
break;
case DVK_LEFT: // Left
if(cursorPosition > 0)
cursorPosition--;
break;
case DVK_FOLD: // (using as esc)
output = oldStr;
done = true;
break;
case DVK_BACKSPACE: // Backspace
if(cursorPosition > 0) {
output.erase(output.begin() + cursorPosition - 1);
cursorPosition--;
}
break;
case DVK_ENTER: // Return
done = true;
break;
default: // Letter
if(output.size() < (uint)maxSize) {
output.insert(output.begin() + cursorPosition, key);
cursorPosition++;
}
break;
}
if(pressed & KEY_LEFT) {
if(cursorPosition > 0)
cursorPosition--;
} else if(pressed & KEY_RIGHT) {
if(cursorPosition < (int)output.length())
cursorPosition++;
} else if(pressed & KEY_B) {
if(cursorPosition > 0) {
output.erase(output.begin() + cursorPosition - 1);
cursorPosition--;
}
} else if(pressed & KEY_START) {
done = true;
}
}
keyboardHide();
return output;
}