Text entry on SDL: hint lines come from the catalog, a password shows stars, a surrogate pair goes in and out whole and obeys the length limit, TOTK_SWKBD_TEST=ok presses OK, and the window redraws when exposed (#401)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-24 09:07:09 +03:00
1 parent 37551aedad
commit c3cd1e31c2
3 files changed
+52 -10

No files matched your search

+44 -10
View File
@@ -40,7 +40,14 @@ static void draw(const Box *b) {
while (pl < 0x100 && b->req->prompt[pl]) pl++;
if (pl) swkbd_utf16_to_utf8(b->req->prompt, pl, prompt, sizeof prompt);
else snprintf(prompt, sizeof prompt, "%s", ui_text(UI_SWKBD_PROMPT));
swkbd_utf16_to_utf8(b->text, b->n, text, sizeof text);
if (b->req->password) { // one star per character, not per UTF-16 unit
size_t k = 0;
for (size_t i = 0; i < b->n && k < KB_LIMIT; i++)
if (b->text[i] < 0xdc00 || b->text[i] >= 0xe000) text[k++] = '*';
text[k] = 0;
} else {
swkbd_utf16_to_utf8(b->text, b->n, text, sizeof text);
}
snprintf(line, sizeof line, "%s_", text);
SDL_SetRenderDrawColor(b->ren, 24, 24, 32, 255);
SDL_RenderClear(b->ren);
@@ -60,12 +67,15 @@ static void draw(const Box *b) {
SDL_RenderDebugText(b->ren, x, y, c);
SDL_SetRenderDrawColor(b->ren, 240, 240, 240, 255);
}
SDL_RenderDebugText(b->ren, 4, 116, "Keys: type, Enter OK, Esc cancel");
SDL_RenderDebugText(b->ren, 4, 130, "Pad: D-pad move, bottom add, right del");
SDL_RenderDebugText(b->ren, 4, 144, "Start OK, top cancel");
// the debug font draws ASCII: the hints are the catalog's English text whatever the game's language
SDL_RenderDebugText(b->ren, 4, 116, ui_text_in("en", UI_SWKBD_SDL_KEYS_HINT));
SDL_RenderDebugText(b->ren, 4, 130, ui_text_in("en", UI_SWKBD_SDL_PAD_HINT1));
SDL_RenderDebugText(b->ren, 4, 144, ui_text_in("en", UI_SWKBD_SDL_PAD_HINT2));
SDL_RenderPresent(b->ren);
}
static void close_box(Box *b);
static void open_box(void *arg) {
Box *b = arg;
b->win = SDL_CreateWindow(ui_text(UI_SWKBD_TITLE), 1280, 720, 0);
@@ -86,6 +96,11 @@ static void open_box(void *arg) {
SDL_StartTextInput(b->win);
SDL_RaiseWindow(b->win);
draw(b);
const char *test = cfg_get("TOTK_SWKBD_TEST");
if (test && !strcmp(test, "ok")) { // as the Win32 window: OK pressed at once
b->result = 1;
close_box(b);
}
}
static void close_box(Box *b) {
@@ -98,14 +113,29 @@ static void close_box(Box *b) {
plat_event_set(&b->done);
}
// Appends typed UTF-16 within the game's limits: a surrogate pair goes in whole or not at all (as clean in swkbd.c),
// and a number pad takes digits only.
static void add_text(Box *b, const uint16_t *u, size_t k) {
const SwkbdRequest *r = b->req;
for (size_t i = 0; i < k && b->n < (size_t)r->max_len && b->n < KB_LIMIT; i++) {
int surrogate = u[i] >= 0xd800 && u[i] < 0xe000;
if (surrogate || swkbd_allowed(u[i], r->disabled, r->numeric)) b->text[b->n++] = u[i];
size_t max = (size_t)r->max_len < KB_LIMIT ? (size_t)r->max_len : KB_LIMIT;
for (size_t i = 0; i < k; i++) {
int pair = u[i] >= 0xd800 && u[i] < 0xdc00 && i + 1 < k && u[i + 1] >= 0xdc00 && u[i + 1] < 0xe000;
if (pair) {
if (!r->numeric && b->n + 2 <= max) b->text[b->n++] = u[i], b->text[b->n++] = u[i + 1];
i++;
} else if (b->n < max && swkbd_allowed(u[i], r->disabled, r->numeric)) {
b->text[b->n++] = u[i];
}
}
}
static void delete_last(Box *b) {
if (!b->n) return;
int pair = b->n >= 2 && b->text[b->n - 1] >= 0xdc00 && b->text[b->n - 1] < 0xe000 && b->text[b->n - 2] >= 0xd800 &&
b->text[b->n - 2] < 0xdc00;
b->n -= pair ? 2 : 1;
}
static void pad_button(Box *b, Uint8 button) {
switch (button) {
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
@@ -126,7 +156,7 @@ static void pad_button(Box *b, Uint8 button) {
break;
}
case SDL_GAMEPAD_BUTTON_EAST:
if (b->n) b->n--;
delete_last(b);
break;
case SDL_GAMEPAD_BUTTON_START:
if ((int)b->n >= b->req->min_len) b->result = 1;
@@ -153,13 +183,17 @@ int swkbd_sdl_event(const SDL_Event *e) {
switch (e->type) {
case SDL_EVENT_TEXT_INPUT: id = e->text.windowID; break;
case SDL_EVENT_KEY_DOWN: id = e->key.windowID; break;
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: id = e->window.windowID; break;
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EVENT_WINDOW_EXPOSED: id = e->window.windowID; break;
default: return 0;
}
if (id != b->id) return 0;
const SwkbdRequest *r = b->req;
if (e->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
b->result = -1;
} else if (e->type == SDL_EVENT_WINDOW_EXPOSED) {
draw(b); // the compositor dropped the contents
return 1;
} else if (e->type == SDL_EVENT_TEXT_INPUT) {
uint16_t u[KB_LIMIT + 1];
size_t k = swkbd_utf8_to_utf16(e->text.text, u, KB_LIMIT + 1);
@@ -167,7 +201,7 @@ int swkbd_sdl_event(const SDL_Event *e) {
} else {
switch (e->key.scancode) {
case SDL_SCANCODE_BACKSPACE:
if (b->n) b->n--;
delete_last(b);
break;
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER:
+5
View File
@@ -40,6 +40,11 @@ typedef struct { const char *lang; const char *const *text; } Row;
static const char *const text_en[UI_TEXT_COUNT] = {
SWKBD("Text entry", "Enter a name", "Horse", "OK", "Cancel", "Controller: A = OK, B = Cancel"),
// The SDL text entry draws with SDL's ASCII debug font, so these three exist in English only (the other rows fall
// back to it).
[UI_SWKBD_SDL_KEYS_HINT] = "Keys: type, Enter OK, Esc cancel",
[UI_SWKBD_SDL_PAD_HINT1] = "Pad: D-pad move, bottom add, right del",
[UI_SWKBD_SDL_PAD_HINT2] = "Start OK, top cancel",
[UI_MENU_TITLE] = "Settings",
[UI_MENU_TAB_SCREEN] = "Screen", [UI_MENU_TAB_GAME] = "Game", [UI_MENU_TAB_MODS] = "Mods", [UI_MENU_TAB_SOUND] = "Sound",
+3
View File
@@ -10,6 +10,9 @@ typedef enum {
UI_SWKBD_OK,
UI_SWKBD_CANCEL,
UI_SWKBD_PAD_HINT, // how a controller answers
UI_SWKBD_SDL_KEYS_HINT, // the SDL window's key line; English only: its debug font draws ASCII (swkbd_sdl.c)
UI_SWKBD_SDL_PAD_HINT1, // the SDL window's controller lines, the letter grid's buttons
UI_SWKBD_SDL_PAD_HINT2,
// Menu: window, tabs, bottom row. UI_MENU_FIRST..UI_MENU_LAST_ID carry English, Russian, Brazilian Portuguese,
// Simplified Chinese, German, French, Spanish, Italian, Dutch, Japanese, Korean and Vietnamese texts.