Skip to content

Commit

Permalink
Merge branch 'port' of https://github.com/fgsfdsfgs/perfect_dark into…
Browse files Browse the repository at this point in the history
… port-debugger
  • Loading branch information
fgsfdsfgs committed Jan 5, 2025
2 parents 85af838 + 4f7817a commit 7cfead4
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 5 deletions.
46 changes: 46 additions & 0 deletions port/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,29 @@
enum virtkey {
/* same order as SDL scancodes */
VK_KEYBOARD_BEGIN = 0,
VK_A = 4,
VK_Z = 29,
VK_1 = 30,
VK_9 = 38,
VK_0 = 39,
VK_RETURN = 40,
VK_ESCAPE = 41,
VK_BACKSPACE = 42,
VK_SPACE = 44,
VK_MINUS = 45,
VK_LEFTBRACKET = 47,
VK_RIGHTBRACKET = 48,
VK_SEMICOLON = 51,
VK_GRAVE = 53,
VK_COMMA = 54,
VK_PERIOD = 55,
VK_F1 = 58,
VK_F9 = 66,
VK_DELETE = 76,
VK_LCTRL = 224,
VK_LSHIFT = 225,
VK_RCTRL = 228,
VK_RSHIFT = 229,

/* same order as SDL mouse buttons */
VK_MOUSE_BEGIN = 512,
Expand All @@ -45,6 +65,17 @@ enum virtkey {
VK_TOTAL_COUNT = VK_JOY_BEGIN + INPUT_MAX_CONTROLLERS * INPUT_MAX_CONTROLLER_BUTTONS,
};

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

enum contkey {
CK_C_R,
CK_C_L,
Expand Down Expand Up @@ -217,4 +248,19 @@ s32 inputAutoLockMouse(s32 wantlock);
// show/hide mouse cursor; if mouse lock is on the cursor is always hidden
void inputMouseShowCursor(s32 show);

void inputStartTextInput(void);
void inputStopTextInput(void);
s32 inputIsTextInputActive(void);

void inputClearLastTextChar(void);
char inputGetLastTextChar(void);

s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol, s32 oskCharsOnly);

void inputClearClipboard(void);
const char *inputGetClipboard(void);

// returns keymod values
u32 inputGetKeyModState(void);

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

static s32 lastKey = 0;
static char lastChar = 0;
static s32 textInput = 0;

static char *clipboardText = NULL;

static const char *ckNames[CK_TOTAL_COUNT] = {
"R_CBUTTONS",
Expand Down Expand Up @@ -523,6 +527,12 @@ static int inputEventFilter(void *data, SDL_Event *event)
}
break;

case SDL_TEXTINPUT:
if (!lastChar && event->text.text[0] && (u8)event->text.text[0] < 0x80) {
lastChar = event->text.text[0];
}
break;

default:
break;
}
Expand Down Expand Up @@ -734,6 +744,14 @@ s32 inputReadController(s32 idx, OSContPad *npad)

npad->button = 0;

if (textInput) {
npad->stick_x = 0;
npad->stick_y = 0;
npad->rstick_x = 0;
npad->rstick_y = 0;
return 0;
}

for (u32 i = 0; i < CONT_NUM_BUTTONS; ++i) {
if (inputBindPressed(idx, i)) {
npad->button |= 1U << i;
Expand Down Expand Up @@ -1330,6 +1348,116 @@ s32 inputGetLastKey(void)
return lastKey;
}

void inputStartTextInput(void)
{
lastChar = 0;
lastKey = 0;
textInput = 1;
SDL_StartTextInput();
}

void inputClearLastTextChar(void)
{
lastChar = 0;
}

char inputGetLastTextChar(void)
{
return lastChar;
}

static inline s32 filterChar(const char ch)
{
return isalnum(ch) || ch == ' ' || ch == '?' || ch == '!' || ch == '.';
}

s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol, s32 oskCharsOnly)
{
const s32 ctrlHeld = inputGetKeyModState() & KM_CTRL;

if (!ctrlHeld) {
const char chr = inputGetLastTextChar();
inputClearLastTextChar();
const s32 valid = chr && (oskCharsOnly ? filterChar(chr) : isprint(chr));
if (valid) {
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) {
SDL_free(clipboardText);
clipboardText = NULL;
}
}

const char *inputGetClipboard(void)
{
if (!clipboardText) {
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;
}

void inputStopTextInput(void)
{
SDL_StopTextInput();
textInput = 0;
}

s32 inputIsTextInputActive(void)
{
return textInput;
}

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

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

void menuPlaySound(s32 menusound)
Expand Down Expand Up @@ -602,7 +603,11 @@ void menuCalculateItemSize(struct menuitem *item, s16 *width, s16 *height, struc
break;
case MENUITEMTYPE_KEYBOARD:
*width = 130;
#ifndef PLATFORM_N64
*height = 84;
#else
*height = 73;
#endif
break;
case MENUITEMTYPE_LIST:
if (item->param2 > 0) {
Expand Down Expand Up @@ -4713,6 +4718,14 @@ void menuProcessInput(void)
if (dialog && inputs.mousemoved) {
g_MenuUsingMouse = true;
}
if (inputs.back && g_MenuKeyboardPlayer != -1) {
// eat the back input so that other menus don't quit when we hit ESC
inputs.back = false;
if (g_MenuKeyboardPlayer == menu->playernum) {
g_MenuKeyboardPlayer = -1;
inputStopTextInput();
}
}
}
#endif

Expand Down
Loading

0 comments on commit 7cfead4

Please sign in to comment.