From 7af80a922356959f7bcc9193bed0116a25408592 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 2 May 2024 21:03:39 +1000 Subject: [PATCH] Virtual keyboard: X for backspace, Y for space --- src/Input.c | 9 +++++++++ src/Input.h | 1 + src/LScreens.c | 11 +++-------- src/VirtualKeyboard.h | 30 +++++++++++++++++------------- src/Widgets.c | 17 ++++++----------- src/Window_Win.c | 2 +- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Input.c b/src/Input.c index 3934c57c1..73516cd4e 100644 --- a/src/Input.c +++ b/src/Input.c @@ -291,6 +291,15 @@ void Input_Clear(void) { ClearTouches(); } +int Input_CalcDelta(int key, int horDelta, int verDelta) { + if (Input_IsLeftButton(key) || key == CCKEY_KP4) return -horDelta; + if (Input_IsRightButton(key) || key == CCKEY_KP6) return +horDelta; + if (Input_IsUpButton(key) || key == CCKEY_KP8) return -verDelta; + if (Input_IsDownButton(key) || key == CCKEY_KP2) return +verDelta; + + return 0; +} + /*########################################################################################################################* *----------------------------------------------------------Mouse----------------------------------------------------------* diff --git a/src/Input.h b/src/Input.h index 642090569..35fff1e2b 100644 --- a/src/Input.h +++ b/src/Input.h @@ -106,6 +106,7 @@ void Input_Clear(void); #else #define Input_IsActionPressed() Input_IsCtrlPressed() #endif +int Input_CalcDelta(int btn, int horDelta, int verDelta); #ifdef CC_BUILD_TOUCH diff --git a/src/LScreens.c b/src/LScreens.c index 44c6b1904..4d8df2bd6 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -380,14 +380,9 @@ static void ColoursScreen_MouseWheel(struct LScreen* s_, float delta) { } static void ColoursScreen_KeyDown(struct LScreen* s, int key, cc_bool was) { - if (Input_IsLeftButton(key)) { - ColoursScreen_AdjustSelected(s, -1); - } else if (Input_IsRightButton(key)) { - ColoursScreen_AdjustSelected(s, +1); - } else if (Input_IsUpButton(key)) { - ColoursScreen_AdjustSelected(s, +10); - } else if (Input_IsDownButton(key)) { - ColoursScreen_AdjustSelected(s, -10); + int delta = Input_CalcDelta(key, 1, 10); + if (delta) { + ColoursScreen_AdjustSelected(s, delta); } else { LScreen_KeyDown(s, key, was); } diff --git a/src/VirtualKeyboard.h b/src/VirtualKeyboard.h index aacebd5e0..99c5d847c 100644 --- a/src/VirtualKeyboard.h +++ b/src/VirtualKeyboard.h @@ -154,6 +154,12 @@ static void VirtualKeyboard_AppendChar(char c) { if (kb_shift) { VirtualKeyboard_ToggleTable(); kb_shift = false; } } +static void VirtualKeyboard_Backspace(void) { + if (kb_str.length) kb_str.length--; + Event_RaiseString(&InputEvents.TextChanged, &kb_str); + KB_MarkDirty(); +} + static void VirtualKeyboard_ClickSelected(void) { int selected = VirtualKeyboard_GetSelected(); if (selected < 0) return; @@ -161,9 +167,7 @@ static void VirtualKeyboard_ClickSelected(void) { /* TODO kinda hacky, redo this */ switch (selected) { case KB_INDEX(KB_LAST_CELL, 0): - if (kb_str.length) kb_str.length--; - Event_RaiseString(&InputEvents.TextChanged, &kb_str); - KB_MarkDirty(); + VirtualKeyboard_Backspace(); break; case KB_INDEX(KB_LAST_CELL, 2): OnscreenKeyboard_Close(); @@ -192,20 +196,20 @@ static void VirtualKeyboard_ClickSelected(void) { } static void VirtualKeyboard_ProcessDown(void* obj, int key, cc_bool was) { - if (Input_IsLeftButton(key)) { - VirtualKeyboard_Scroll(-1); - } else if (Input_IsRightButton(key)) { - VirtualKeyboard_Scroll(+1); - } else if (Input_IsUpButton(key)) { - VirtualKeyboard_Scroll(-KB_CELLS_PER_ROW); - } else if (Input_IsDownButton(key)) { - VirtualKeyboard_Scroll(+KB_CELLS_PER_ROW); - } else if (Input_IsEnterButton(key) || key == CCPAD_A) { + int delta = Input_CalcDelta(key, 1, KB_CELLS_PER_ROW); + if (delta) { + VirtualKeyboard_Scroll(delta); + } else if (key == CCPAD_START || key == CCPAD_A) { VirtualKeyboard_ClickSelected(); - } else if (Input_IsEscapeButton(key) || key == CCPAD_B) { + } else if (key == CCPAD_SELECT || key == CCPAD_B) { VirtualKeyboard_Close(); + } else if (key == CCPAD_X) { + VirtualKeyboard_Backspace(); + } else if (key == CCPAD_Y) { + VirtualKeyboard_AppendChar(' '); } } +} static void VirtualKeyboard_PadAxis(void* obj, int port, int axis, float x, float y) { int xSteps, ySteps; diff --git a/src/Widgets.c b/src/Widgets.c index 47eb3a5be..29984b36e 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -975,20 +975,15 @@ static int TableWidget_PointerMove(void* widget, int id, int x, int y) { static int TableWidget_KeyDown(void* widget, int key) { struct TableWidget* w = (struct TableWidget*)widget; + int delta; if (w->selectedIndex == -1) return false; - if (Input_IsLeftButton(key) || key == CCKEY_KP4) { - TableWidget_ScrollRelative(w, -1); - } else if (Input_IsRightButton(key) || key == CCKEY_KP6) { - TableWidget_ScrollRelative(w, 1); - } else if (Input_IsUpButton(key) || key == CCKEY_KP8) { - TableWidget_ScrollRelative(w, -w->blocksPerRow); - } else if (Input_IsDownButton(key) || key == CCKEY_KP2) { - TableWidget_ScrollRelative(w, w->blocksPerRow); - } else { - return false; + delta = Input_CalcDelta(key, 1, w->blocksPerRow); + if (delta) { + TableWidget_ScrollRelative(w, delta); + return true; } - return true; + return false; } static int TableWidget_PadAxis(void* widget, int axis, float x, float y) { diff --git a/src/Window_Win.c b/src/Window_Win.c index bd8efb382..3e8ac81ef 100644 --- a/src/Window_Win.c +++ b/src/Window_Win.c @@ -53,7 +53,7 @@ static cc_bool is_ansiWindow, grabCursor; static int windowX, windowY; static const cc_uint8 key_map[14 * 16] = { - 0, 0, 0, 0, 0, 0, 0, 0, CCKEY_BACKSPACE, CCKEY_TAB, 0, 0, 0, CCKEY_ENTER, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, CCKEY_BACKSPACE, CCKEY_TAB, 0, 0, CCKEY_F5, CCKEY_ENTER, 0, 0, 0, 0, 0, CCKEY_PAUSE, CCKEY_CAPSLOCK, 0, 0, 0, 0, 0, 0, CCKEY_ESCAPE, 0, 0, 0, 0, CCKEY_SPACE, CCKEY_PAGEUP, CCKEY_PAGEDOWN, CCKEY_END, CCKEY_HOME, CCKEY_LEFT, CCKEY_UP, CCKEY_RIGHT, CCKEY_DOWN, 0, CCKEY_PRINTSCREEN, 0, CCKEY_PRINTSCREEN, CCKEY_INSERT, CCKEY_DELETE, 0, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0,