Skip to content

Commit

Permalink
Merge pull request #557 from ZenithMDC/text-input
Browse files Browse the repository at this point in the history
Allow typing with keyboard in menuitemKeyboard
  • Loading branch information
fgsfdsfgs authored Jan 5, 2025
2 parents 49cd478 + 8945809 commit a05c429
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 43 deletions.
26 changes: 26 additions & 0 deletions port/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum virtkey {
VK_KEYBOARD_BEGIN = 0,
VK_RETURN = 40,
VK_ESCAPE = 41,
VK_BACKSPACE = 42,
VK_DELETE = 76,

/* same order as SDL mouse buttons */
Expand Down Expand Up @@ -87,6 +88,14 @@ enum mouselockmode {
MLOCK_AUTO = 2
};

enum keymod {
/* same order as SDL keymods */
KM_LSHIFT = 0x0001,
KM_RSHIFT = 0x0002,
KM_CAPS = 0x2000,
KM_SHIFT = KM_LSHIFT | KM_RSHIFT
};

// returns bitmask of connected controllers or -1 if failed
s32 inputInit(void);

Expand Down Expand Up @@ -140,6 +149,9 @@ s32 inputAssignController(s32 cidx, s32 id);
s32 inputKeyPressed(u32 vk);
s32 inputKeyJustPressed(u32 vk);

// same as inputKeyJustPressed but returns true again if key repeat activates
s32 inputKeyJustPressedWithRepeat(u32 vk);

// idx is controller index, contbtn is one of the CONT_ constants
s32 inputButtonPressed(s32 idx, u32 contbtn);

Expand Down Expand Up @@ -217,4 +229,18 @@ s32 inputAutoLockMouse(s32 wantlock);
// show/hide mouse cursor; if mouse lock is on the cursor is always hidden
void inputMouseShowCursor(s32 show);

// enables/disables/queries text input event processing, respectively
void inputStartTextInput(void);
void inputStopTextInput(void);
s32 inputIsTextInputActive(void);

// fills textInputBuffer with textInput chars
void inputSetTextInput(char *textInput);

// returns pointer to textInputBuffer
char *inputGetTextInput(void);

// returns keymod values
u32 inputGetModState(void);

#endif
54 changes: 54 additions & 0 deletions port/src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ static f32 mouseSensX = 1.5f;
static f32 mouseSensY = 1.5f;

static s32 lastKey = 0;
static s32 keyRepeat = 0;

// buffer used to store text input during a text input event
static char textInputBuffer[11] = {0};

static const char *ckNames[CK_TOTAL_COUNT] = {
"R_CBUTTONS",
Expand Down Expand Up @@ -496,6 +500,7 @@ static int inputEventFilter(void *data, SDL_Event *event)
if (!lastKey) {
lastKey = VK_KEYBOARD_BEGIN + event->key.keysym.scancode;
}
keyRepeat = event->key.repeat;
break;

case SDL_CONTROLLERBUTTONDOWN:
Expand All @@ -521,6 +526,9 @@ static int inputEventFilter(void *data, SDL_Event *event)
}
}
break;
case SDL_TEXTINPUT:
inputSetTextInput(event->text.text);
break;

default:
break;
Expand Down Expand Up @@ -658,6 +666,9 @@ s32 inputInit(void)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
}

// SDL enables text input event handling by default
inputStopTextInput();

// try to load controller db from an external file in the save folder
if (fsFileSize("$S/" CONTROLLERDB_FNAME)) {
const char *dbpath = fsFullPath("$S/" CONTROLLERDB_FNAME);
Expand Down Expand Up @@ -1114,6 +1125,11 @@ s32 inputKeyJustPressed(u32 vk)
return result;
}

s32 inputKeyJustPressedWithRepeat(u32 vk)
{
return inputKeyJustPressed(vk) || (keyRepeat && inputKeyPressed(vk));
}

static inline u32 inputContToContKey(const u32 cont)
{
if (cont == 0) {
Expand Down Expand Up @@ -1316,6 +1332,44 @@ s32 inputGetLastKey(void)
return lastKey;
}

void inputStartTextInput(void)
{
SDL_StartTextInput();
}

void inputStopTextInput(void)
{
SDL_StopTextInput();
textInputBuffer[0] = '\0';
}

s32 inputIsTextInputActive(void)
{
return SDL_IsTextInputActive();
}

void inputSetTextInput(char *textInput)
{
for (size_t i = 0; i < sizeof textInputBuffer - 1; ++i) {
if (textInput && i < strlen(textInput)) {
textInputBuffer[i] = textInput[i];
} else {
textInputBuffer[i] = '\0';
}
}
}

char *inputGetTextInput(void)
{
return textInputBuffer;
}


u32 inputGetModState(void)
{
return SDL_GetModState();
}

PD_CONSTRUCTOR static void inputConfigInit(void)
{
configRegisterInt("Input.MouseEnabled", &mouseEnabled, 0, 1);
Expand Down
29 changes: 29 additions & 0 deletions src/game/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ s32 g_MpPlayerNum = 0;
#ifndef PLATFORM_N64
s32 g_MenuMouseControl = true;
s32 g_MenuUsingMouse = false;
s32 g_MenuTypeWithKeyboard = false;
s32 g_MenuTypeBackspace = false;
#endif

void menuPlaySound(s32 menusound)
Expand Down Expand Up @@ -602,7 +604,11 @@ void menuCalculateItemSize(struct menuitem *item, s16 *width, s16 *height, struc
break;
case MENUITEMTYPE_KEYBOARD:
*width = 130;
#ifndef PLATFORM_N64
*height = inputGetAssignedControllerId(g_MpPlayerNum) == -1 ? 84 : 73;
#else
*height = 73;
#endif
break;
case MENUITEMTYPE_LIST:
if (item->param2 > 0) {
Expand Down Expand Up @@ -4695,6 +4701,8 @@ void menuProcessInput(void)
inputs.back2 = 0;

#ifndef PLATFORM_N64
s32 usingcontroller = inputGetAssignedControllerId(g_MpPlayerNum) != -1;

inputs.mousemoved = false;
inputs.mousescroll = 0;
inputs.mousex = 0;
Expand All @@ -4703,6 +4711,11 @@ void menuProcessInput(void)
if (menu->playernum == 0) {
// ESC always acts as back
inputs.back = inputKeyJustPressed(VK_ESCAPE);
if (inputs.back && g_MenuTypeWithKeyboard) {
if (inputIsTextInputActive()) {
inputStopTextInput();
}
}
if (inputMouseIsEnabled() && !inputMouseIsLocked() && g_MenuMouseControl) {
inputs.mousemoved = inputMouseGetPosition(&inputs.mousex, &inputs.mousey);
inputs.mousescroll = inputKeyPressed(VK_MOUSE_WHEEL_DN) - inputKeyPressed(VK_MOUSE_WHEEL_UP);
Expand All @@ -4713,6 +4726,14 @@ void menuProcessInput(void)
if (dialog && inputs.mousemoved) {
g_MenuUsingMouse = true;
}
g_MenuTypeBackspace = inputKeyJustPressedWithRepeat(VK_BACKSPACE);

if (usingcontroller && g_MenuTypeWithKeyboard) {
g_MenuTypeWithKeyboard = false;
if (inputIsTextInputActive()) {
inputStopTextInput();
}
}
}
#endif

Expand Down Expand Up @@ -4783,7 +4804,11 @@ void menuProcessInput(void)
#endif

if (buttonsnow & B_BUTTON) {
#ifndef PLATFORM_N64
inputs.back = inputIsTextInputActive() && !usingcontroller ? 0 : 1;
#else
inputs.back = 1;
#endif
}

if (buttonsnow & Z_TRIG) {
Expand All @@ -4795,7 +4820,11 @@ void menuProcessInput(void)
}

if (buttons & R_TRIG) {
#ifndef PLATFORM_N64
inputs.shoulder = inputIsTextInputActive() && !usingcontroller ? 0 : 1;
#else
inputs.shoulder = 1;
#endif
}

if ((stickx < 0 ? -stickx : stickx) < (thisstickx < 0 ? -thisstickx : thisstickx)) {
Expand Down
Loading

0 comments on commit a05c429

Please sign in to comment.