Improve keyboard input (#132)

This commit is contained in:
Pk11 2022-01-02 13:35:21 -06:00 committed by GitHub
parent 60736a5fcd
commit b3bdcabec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 158 additions and 89 deletions

View File

@ -40,6 +40,7 @@
#include "dumpOperations.h"
#include "font.h"
#include "hexEditor.h"
#include "keyboard.h"
#include "ndsInfo.h"
#include "startMenu.h"
#include "nitrofs.h"
@ -62,11 +63,6 @@ bool extension(const std::string_view filename, const std::vector<std::string_vi
return false;
}
void OnKeyPressed(int key) {
if(key > 0)
iprintf("%c", key);
}
bool dirEntryPredicate (const DirEntry& lhs, const DirEntry& rhs) {
if (!lhs.isDirectory && rhs.isDirectory) {
@ -757,39 +753,26 @@ std::string browseForFile (void) {
font->update(true);
pressed = 0;
consoleDemoInit();
Keyboard *kbd = keyboardDemoInit();
char newName[256];
kbd->OnKeyPressed = OnKeyPressed;
keyboardShow();
iprintf(STR_RENAME_TO.c_str());
fgets(newName, 256, stdin);
newName[strlen(newName)-1] = 0;
keyboardHide();
std::string newName = kbdGetString(STR_RENAME_TO, -1, entry->name);
videoSetModeSub(MODE_5_2D);
bgShow(bgInitSub(2, BgType_Bmp8, BgSize_B8_256x256, 3, 0));
BG_PALETTE_SUB[0] = 0x0000;
BG_PALETTE_SUB[1] = 0x7FFF;
if (newName[0] != '\0') {
if (newName.length() > 0) {
// Check for unsupported characters
for (int i = 0; i < (int)sizeof(newName); i++) {
if (newName[i] == '>'
|| newName[i] == '<'
|| newName[i] == ':'
|| newName[i] == '"'
|| newName[i] == '/'
|| newName[i] == '\x5C'
|| newName[i] == '|'
|| newName[i] == '?'
|| newName[i] == '*')
{
newName[i] = '_'; // Remove unsupported character
for (uint i = 0; i < newName.length(); i++) {
switch(newName[i]) {
case '>':
case '<':
case ':':
case '"':
case '/':
case '\\':
case '|':
case '?':
case '*':
newName[i] = '_'; // Remove unsupported character
}
}
if (rename(entry->name.c_str(), newName) == 0) {
if (rename(entry->name.c_str(), newName.c_str()) == 0) {
getDirectoryContents(dirContents);
}
}
@ -889,39 +872,26 @@ std::string browseForFile (void) {
font->update(true);
pressed = 0;
consoleDemoInit();
Keyboard *kbd = keyboardDemoInit();
char newName[256];
kbd->OnKeyPressed = OnKeyPressed;
keyboardShow();
iprintf(STR_NAME_FOR_NEW_FOLDER.c_str());
fgets(newName, 256, stdin);
newName[strlen(newName)-1] = 0;
keyboardHide();
std::string newName = kbdGetString(STR_NAME_FOR_NEW_FOLDER);
videoSetModeSub(MODE_5_2D);
bgShow(bgInitSub(2, BgType_Bmp8, BgSize_B8_256x256, 3, 0));
BG_PALETTE_SUB[0] = 0x0000;
BG_PALETTE_SUB[1] = 0x7FFF;
if (newName[0] != '\0') {
if (newName.length() > 0) {
// Check for unsupported characters
for (int i = 0; i < (int)sizeof(newName); i++) {
if (newName[i] == '>'
|| newName[i] == '<'
|| newName[i] == ':'
|| newName[i] == '"'
|| newName[i] == '/'
|| newName[i] == '\x5C'
|| newName[i] == '|'
|| newName[i] == '?'
|| newName[i] == '*')
{
newName[i] = '_'; // Remove unsupported character
for (uint i = 0; i < newName.length(); i++) {
switch(newName[i]) {
case '>':
case '<':
case ':':
case '"':
case '/':
case '\\':
case '|':
case '?':
case '*':
newName[i] = '_'; // Remove unsupported character
}
}
if (mkdir(newName, 0777) == 0) {
if (mkdir(newName.c_str(), 0777) == 0) {
getDirectoryContents (dirContents);
}
}

View File

@ -54,7 +54,6 @@ enum class FileOperation {
};
bool extension(const std::string_view filename, const std::vector<std::string_view> &extensions);
void OnKeyPressed(int key);
std::string browseForFile (void);
void getDirectoryContents (std::vector<DirEntry>& dirContents);

View File

@ -28,6 +28,7 @@ enum class Palette : u8 {
greenAlt,
blue,
yellow,
blackWhite,
blackRed,
blackGreen,
blackBlue,
@ -94,6 +95,7 @@ class Font {
{0x0000, 0x02E0}, // Green (alt)
{0x0000, 0x656A}, // Blue
{0x0000, 0x3339}, // Yellow
{0x7FFF, 0x0000}, // Black on white
{0x001F, 0x0000}, // Black on red
{0x03E0, 0x0000}, // Black on green
{0x656A, 0x0000}, // Black on blue

View File

@ -3,6 +3,7 @@
#include "date.h"
#include "file_browse.h"
#include "font.h"
#include "keyboard.h"
#include "language.h"
#include "screenshot.h"
#include "tonccpy.h"
@ -91,30 +92,11 @@ u32 search(u32 offset, FILE *file) {
size_t strLen = 1;
if(cursorPosition == 0) {
consoleDemoInit();
Keyboard *kbd = keyboardDemoInit();
kbd->OnKeyPressed = OnKeyPressed;
strcpy(str, kbdGetString(STR_SEARCH_FOR, sizeof(str)).c_str());
// keyboardShow();
iprintf(STR_SEARCH_FOR.c_str());
fgets(str, sizeof(str), stdin);
keyboardHide();
videoSetModeSub(MODE_5_2D);
bgShow(bgInitSub(2, BgType_Bmp8, BgSize_B8_256x256, 3, 0));
BG_PALETTE_SUB[0] = 0x0000;
BG_PALETTE_SUB[1] = 0x7FFF;
BG_PALETTE_SUB[0x1F] = 0x9CF7;
BG_PALETTE_SUB[0x2F] = 0xB710;
BG_PALETTE_SUB[0x3F] = 0xAE8D;
BG_PALETTE_SUB[0x7F] = 0xEA2D;
strLen = strlen(str) - 1;
strLen = strlen(str);
if(strLen == 0)
return offset;
str[strLen] = 0; // Remove ending \n that fgets has
} else {
cursorPosition = 0;
while(1) {

106
arm9/source/keyboard.cpp Normal file
View File

@ -0,0 +1,106 @@
#include "keyboard.h"
#include "date.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 {
// Print time
font->print(-1, 0, true, RetTime(), Alignment::right, Palette::blackGreen);
font->update(true);
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;
}

8
arm9/source/keyboard.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <string>
std::string kbdGetString(std::string label, int maxSize = -1, std::string oldStr = "");
#endif // KEYBOARD_H

View File

@ -17,8 +17,8 @@ STRING(N_MORE_FILES, "%d more files...")
STRING(PASTE_CLIPBOARD_HERE, "Paste clipboard here?")
STRING(COPY_FILES, "Copy files")
STRING(MOVE_FILES, "Move files")
STRING(RENAME_TO, "Rename to:\n") // ASCII only
STRING(NAME_FOR_NEW_FOLDER, "Name for new folder:\n") // ASCII only
STRING(RENAME_TO, "Rename to:")
STRING(NAME_FOR_NEW_FOLDER, "Name for new folder:")
STRING(DELETE_N_PATHS, "Delete %d paths?")
STRING(AND_1_MORE, "- and %d more...")
STRING(AND_N_MORE, "- and %d more...")
@ -120,7 +120,7 @@ STRING(HEX_EDITOR, "Hex Editor")
STRING(JUMP_TO_OFFSET, "Jump to Offset")
STRING(SEARCH_STRING, "Search for String")
STRING(SEARCH_DATA, "Search for Data")
STRING(SEARCH_FOR, "Search for:\n") // ASCII only
STRING(SEARCH_FOR, "Search for:")
STRING(ENTER_VALUE, "Enter value:")
STRING(SEARCHING, "Searching")
STRING(PRESS_B_TO_CANCEL, "Press \\B to cancel")
@ -162,6 +162,7 @@ STRING(A_SELECT_B_CANCEL, "(\\A select, \\B cancel)")
STRING(START_CANCEL, "(START cancel)")
STRING(UDLR_CHANGE_ATTRIBUTES, "(\\D change attributes)")
STRING(A_APPLY_B_CANCEL, "(\\A apply, \\B cancel)")
STRING(START_RETURN_B_BACKSPACE, "(START Return, \\B Backspace)")
// Byte counts
STRING(1_BYTE, "1 Byte")

View File

@ -17,8 +17,8 @@ N_MORE_FILES=%d more files...
PASTE_CLIPBOARD_HERE=Paste clipboard here?
COPY_FILES=Copy files
MOVE_FILES=Move files
RENAME_TO=Rename to:\n
NAME_FOR_NEW_FOLDER=Name for new folder:\n
RENAME_TO=Rename to:
NAME_FOR_NEW_FOLDER=Name for new folder:
DELETE_N_PATHS=Delete %d paths?
AND_1_MORE=- and %d more...
AND_N_MORE=- and %d more...
@ -113,7 +113,7 @@ HEX_EDITOR=Hex Editor
JUMP_TO_OFFSET=Jump to Offset
SEARCH_STRING=Search for String
SEARCH_DATA=Search for Data
SEARCH_FOR=Search for:\n
SEARCH_FOR=Search for:
ENTER_VALUE=Enter value:
SEARCHING=Searching
PRESS_B_TO_CANCEL=Press \B to cancel
@ -153,6 +153,7 @@ A_SELECT_B_CANCEL=(\A select, \B cancel)
START_CANCEL=(START cancel)
UDLR_CHANGE_ATTRIBUTES=(\D change attributes)
A_APPLY_B_CANCEL=(\A apply, \B cancel)
START_RETURN_B_BACKSPACE=(START Return, \B Backspace)
1_BYTE=1 Byte
N_BYTES=%d Bytes