mirror of
https://github.com/perfect-dark-pc-port/perfect_dark.git
synced 2026-10-23 15:52:41 +02:00
port: net: unify console and join address input routines
This commit is contained in:
1 parent
a1af5b655a
commit
6a03e8d9cf
4 files changed
+62
-61
No files matched your search
@@ -225,6 +225,8 @@ void inputStopTextInput(void);
|
||||
void inputClearLastTextChar(void);
|
||||
char inputGetLastTextChar(void);
|
||||
|
||||
s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol);
|
||||
|
||||
void inputClearClipboard(void);
|
||||
const char *inputGetClipboard(void);
|
||||
|
||||
|
||||
+3
-21
@@ -202,30 +202,12 @@ void conTick(void)
|
||||
conButton = button;
|
||||
|
||||
if (conOpen) {
|
||||
char chr = inputGetLastTextChar();
|
||||
if (chr && isprint(chr)) {
|
||||
if (conInputCol < CON_COLS) {
|
||||
conInput[conInputCol++] = chr;
|
||||
conInput[conInputCol] = '\0';
|
||||
}
|
||||
}
|
||||
const s32 key = inputGetLastKey();
|
||||
if (key == VK_BACKSPACE) {
|
||||
if (conInputCol) {
|
||||
conInput[--conInputCol] = '\0';
|
||||
} else {
|
||||
conInput[0] = '\0';
|
||||
}
|
||||
} else if (key == VK_RETURN) {
|
||||
if (conInput[0] && conInputCol) {
|
||||
if (g_NetMode) {
|
||||
netChat(NULL, conInput);
|
||||
}
|
||||
if (inputTextHandler(conInput, CON_COLS, &conInputCol)) {
|
||||
if (g_NetMode) {
|
||||
netChat(NULL, conInput);
|
||||
}
|
||||
conInput[0] = '\0';
|
||||
conInputCol = 0;
|
||||
}
|
||||
inputClearLastTextChar();
|
||||
inputClearLastKey();
|
||||
}
|
||||
}
|
||||
@@ -1287,6 +1287,51 @@ char inputGetLastTextChar(void)
|
||||
return lastChar;
|
||||
}
|
||||
|
||||
s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol)
|
||||
{
|
||||
const s32 ctrlHeld = inputKeyPressed(VK_LCTRL) || inputKeyPressed(VK_RCTRL);
|
||||
|
||||
if (!ctrlHeld) {
|
||||
const char chr = inputGetLastTextChar();
|
||||
inputClearLastTextChar();
|
||||
if (chr && isprint(chr)) {
|
||||
if (*curCol < outSize - 1) {
|
||||
out[(*curCol)++] = chr;
|
||||
out[*curCol] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const s32 key = inputGetLastKey();
|
||||
inputClearLastKey();
|
||||
if (ctrlHeld && (key == VK_A + ('v' - 'a'))) {
|
||||
// CTRL+V; paste from clipboard
|
||||
const char *clip = inputGetClipboard();
|
||||
if (clip) {
|
||||
const s32 remain = outSize - *curCol - 1;
|
||||
inputClearClipboard();
|
||||
*curCol += snprintf(out + *curCol, remain, "%s", clip);
|
||||
if (*curCol > outSize) {
|
||||
*curCol = outSize;
|
||||
}
|
||||
}
|
||||
} else if (key == VK_BACKSPACE) {
|
||||
if (*curCol) {
|
||||
out[--*curCol] = '\0';
|
||||
} else {
|
||||
out[0] = '\0';
|
||||
}
|
||||
} else if (key == VK_RETURN) {
|
||||
if (out[0] && *curCol) {
|
||||
return 1;
|
||||
}
|
||||
} else if (key == VK_ESCAPE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void inputClearClipboard(void)
|
||||
{
|
||||
if (clipboardText) {
|
||||
@@ -1301,6 +1346,12 @@ const char *inputGetClipboard(void)
|
||||
char *text = SDL_GetClipboardText();
|
||||
if (text) {
|
||||
clipboardText = text;
|
||||
// remove non-printable and multibyte chars
|
||||
for (; *text; ++text) {
|
||||
if ((u8)*text < 0x20 || (u8)*text >= 0x7F) {
|
||||
*text = '?';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return clipboardText;
|
||||
|
||||
+6
-40
@@ -236,48 +236,12 @@ static MenuItemHandlerResult menuhandlerEnterJoinAddress(s32 operation, struct m
|
||||
return 0;
|
||||
}
|
||||
|
||||
const s32 key = inputGetLastKey();
|
||||
char chr = '\0';
|
||||
switch (key) {
|
||||
case 0:
|
||||
return 0;
|
||||
case VK_ESCAPE:
|
||||
menuPopDialog();
|
||||
break;
|
||||
case VK_BACKSPACE:
|
||||
if (g_NetJoinAddrPtr) {
|
||||
g_NetJoinAddr[--g_NetJoinAddrPtr] = '\0';
|
||||
} else {
|
||||
g_NetJoinAddr[0] = '\0';
|
||||
}
|
||||
break;
|
||||
case VK_A ... VK_Z: chr = 'a' + key - VK_A; break;
|
||||
case VK_1 ... VK_9: chr = '1' + key - VK_1; break;
|
||||
case VK_0: chr = '0'; break;
|
||||
case VK_PERIOD: chr = '.'; break;
|
||||
case VK_SEMICOLON: chr = ':'; break;
|
||||
case VK_LEFTBRACKET: chr = '['; break;
|
||||
case VK_RIGHTBRACKET: chr = ']'; break;
|
||||
case VK_MINUS: chr = '-'; break;
|
||||
default:
|
||||
break;
|
||||
if (inputTextHandler(g_NetJoinAddr, NET_MAX_ADDR, &g_NetJoinAddrPtr) < 0) {
|
||||
// escape has been pressed, stop editing
|
||||
inputStopTextInput();
|
||||
menuPopDialog();
|
||||
}
|
||||
|
||||
if (chr == 'v' && (inputKeyPressed(VK_LCTRL) || inputKeyPressed(VK_RCTRL))) {
|
||||
// try to paste in from clipboard
|
||||
const char *clip = inputGetClipboard();
|
||||
if (clip) {
|
||||
strncpy(g_NetJoinAddr, clip, sizeof(g_NetJoinAddr) - 1);
|
||||
g_NetJoinAddrPtr = strlen(g_NetJoinAddr);
|
||||
inputClearClipboard();
|
||||
}
|
||||
} else if (chr && g_NetJoinAddrPtr < sizeof(g_NetJoinAddr) - 1) {
|
||||
g_NetJoinAddr[g_NetJoinAddrPtr++] = chr;
|
||||
g_NetJoinAddr[g_NetJoinAddrPtr + 1] = '\0';
|
||||
}
|
||||
|
||||
inputClearLastKey();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -285,6 +249,8 @@ static MenuItemHandlerResult menuhandlerJoinAddress(s32 operation, struct menuit
|
||||
{
|
||||
if (operation == MENUOP_SET) {
|
||||
inputClearLastKey();
|
||||
inputClearLastTextChar();
|
||||
inputStartTextInput();
|
||||
g_NetJoinAddrPtr = strlen(g_NetJoinAddr);
|
||||
menuPushDialog(&g_NetJoinAddressDialog);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user