diff --git a/src/Game.c b/src/Game.c index 617a7b66b..3570696fa 100644 --- a/src/Game.c +++ b/src/Game.c @@ -662,6 +662,7 @@ static void Game_RenderFrame(double delta) { Gfx_Begin2D(Game.Width, Game.Height); Gui_RenderGui(delta); + OnscreenKeyboard_Draw3D(); /* TODO find a better solution than this */ #ifdef CC_BUILD_3DS if (Game_Anaglyph3D) { diff --git a/src/VirtualKeyboard.h b/src/VirtualKeyboard.h index d9e104cb8..3c78ad849 100644 --- a/src/VirtualKeyboard.h +++ b/src/VirtualKeyboard.h @@ -8,6 +8,7 @@ #include "Utils.h" #include "LBackend.h" #include "Window.h" +#include "Graphics.h" static cc_bool kb_inited, kb_showing, kb_shift, kb_needsHook; static struct FontDesc kb_font; @@ -66,9 +67,9 @@ static void VirtualKeyboard_Close(void); static void VirtualKeyboard_Hook(void); -#define KB_NORMAL_COLOR BitmapCol_Make(0x7F, 0x7F, 0x7F, 0x90) -#define KB_SELECTED_COLOR BitmapCol_Make(0xAF, 0xAF, 0xAF, 0x90) -#define KB_BACKGROUND_COLOR BitmapCol_Make(0xFF, 0xFF, 0xFF, 0x90) +#define KB_NORMAL_COLOR BitmapCol_Make(0x7F, 0x7F, 0x7F, 0x40) +#define KB_SELECTED_COLOR BitmapCol_Make(0xAF, 0xAF, 0xAF, 0x40) +#define KB_BACKGROUND_COLOR BitmapCol_Make(0xFF, 0xFF, 0xFF, 0x40) static void VirtualKeyboard_Draw(struct Context2D* ctx) { struct DrawTextArgs args; @@ -104,7 +105,18 @@ static void VirtualKeyboard_Draw(struct Context2D* ctx) { Drawer2D.Colors['f'] = Drawer2D.Colors['F']; } +static void VirtualKeyboard_CalcPosition(int* x, int* y, int width, int height) { + /* Draw virtual keyboard at centre of window bottom */ + *y = height - 1 - VirtualKeyboard_Height(); + if (*y < 0) *y = 0; + *x = (width - VirtualKeyboard_Width()) / 2; + if (*x < 0) *x = 0; +} + +/*########################################################################################################################* +*-----------------------------------------------------Input handling------------------------------------------------------* +*#########################################################################################################################*/ static void VirtualKeyboard_Scroll(int delta) { if (kb_selected < 0) kb_selected = 0; @@ -187,15 +199,93 @@ static void VirtualKeyboard_PadAxis(void* obj, int axis, float x, float y) { } +/*########################################################################################################################* +*--------------------------------------------------------2D mode----------------------------------------------------------* +*#########################################################################################################################*/ extern Rect2D dirty_rect; + static void VirtualKeyboard_MarkDirty2D(void) { if (!dirty_rect.width) dirty_rect.width = 2; } -static void VirtualKeyboard_MarkDirty3D(void) { - /* TODO */ +static void VirtualKeyboard_Display2D(Rect2D* r, struct Bitmap* bmp) { + struct Context2D ctx; + struct Bitmap copy = *bmp; + int x, y; + if (!kb_showing) return; + + /* Mark entire framebuffer as needing to be redrawn */ + r->x = 0; r->width = bmp->width; + r->y = 0; r->height = bmp->height; + + VirtualKeyboard_CalcPosition(&x, &y, bmp->width, bmp->height); + copy.scan0 = Bitmap_GetRow(bmp, y); + copy.scan0 += x; + + Context2D_Wrap(&ctx, ©); + VirtualKeyboard_Draw(&ctx); } +static void VirtualKeyboard_Close2D(void) { + LBackend_Redraw(); +} + +/*########################################################################################################################* +*--------------------------------------------------------3D mode----------------------------------------------------------* +*#########################################################################################################################*/ +static struct Texture kb_texture; +static GfxResourceID kb_vb; + +static void VirtualKeyboard_MarkDirty3D(void) { + Gfx_DeleteTexture(&kb_texture.ID); +} + +static void VirtualKeyboard_Close3D(void) { + Gfx_DeleteTexture(&kb_texture.ID); +} +static void VirtualKeyboard_MakeTexture(void) { + struct Context2D ctx; + int width = VirtualKeyboard_Width(); + int height = VirtualKeyboard_Height(); + Context2D_Alloc(&ctx, width, height); + { + VirtualKeyboard_Draw(&ctx); + Context2D_MakeTexture(&kb_texture, &ctx); + } + Context2D_Free(&ctx); + + int x, y; + VirtualKeyboard_CalcPosition(&x, &y, Window_Main.Width, Window_Main.Height); + kb_texture.x = x; kb_texture.y = y; +} + +/* TODO hook into context lost etc */ +static void VirtualKeyboard_Display3D(void) { + if (!kb_showing) return; + + if (!kb_vb) { + kb_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_TEXTURED, 4); + if (!kb_vb) return; + } + + if (!kb_texture.ID) { + VirtualKeyboard_MakeTexture(); + if (!kb_texture.ID) return; + } + + Gfx_SetVertexFormat(VERTEX_FORMAT_TEXTURED); + Gfx_BindTexture(kb_texture.ID); + + struct VertexTextured* data = Gfx_LockDynamicVb(kb_vb, VERTEX_FORMAT_TEXTURED, 4); + struct VertexTextured** ptr = &data; + Gfx_Make2DQuad(&kb_texture, PACKEDCOL_WHITE, ptr); + Gfx_UnlockDynamicVb(kb_vb); + Gfx_DrawVb_IndexedTris(4); +} + +/*########################################################################################################################* +*--------------------------------------------------------General----------------------------------------------------------* +*#########################################################################################################################*/ static void VirtualKeyboard_Hook(void) { /* Don't hook immediately into events, otherwise the initial up/down press that opened */ /* the virtual keyboard in the first place gets mistakenly processed */ @@ -235,7 +325,12 @@ static void VirtualKeyboard_SetText(const cc_string* text) { } static void VirtualKeyboard_Close(void) { - if (KB_MarkDirty) KB_MarkDirty(); + /* TODO find a better way */ + if (KB_MarkDirty == VirtualKeyboard_MarkDirty2D) + VirtualKeyboard_Close2D(); + if (KB_MarkDirty == VirtualKeyboard_MarkDirty3D) + VirtualKeyboard_Close3D(); + Event_Unregister_(&InputEvents.Down, NULL, VirtualKeyboard_ProcessDown); Event_Unregister_(&ControllerEvents.AxisUpdate, NULL, VirtualKeyboard_PadAxis); Window_Main.SoftKeyboardFocus = false; @@ -243,26 +338,4 @@ static void VirtualKeyboard_Close(void) { KB_MarkDirty = NULL; kb_showing = false; kb_needsHook = false; -} - -static void VirtualKeyboard_Display2D(Rect2D* r, struct Bitmap* bmp) { - struct Context2D ctx; - struct Bitmap copy = *bmp; - int x, y; - - /* Mark entire framebuffer as needing to be redrawn */ - r->x = 0; r->width = bmp->width; - r->y = 0; r->height = bmp->height; - - /* Draw virtual keyboard at centre of window bottom */ - y = bmp->height - 1 - VirtualKeyboard_Height(); - y = max(y, 0); - copy.scan0 = Bitmap_GetRow(bmp, y); - - x = (bmp->width - VirtualKeyboard_Width()) / 2; - x = max(x, 0); - copy.scan0 += x; - - Context2D_Wrap(&ctx, ©); - VirtualKeyboard_Draw(&ctx); } \ No newline at end of file diff --git a/src/Window.h b/src/Window.h index 42f7e94e1..d82ed37ad 100644 --- a/src/Window.h +++ b/src/Window.h @@ -195,6 +195,7 @@ void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args); /* whatever text input widget is actually being displayed on screen. */ void OnscreenKeyboard_SetText(const cc_string* text); void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp); +void OnscreenKeyboard_Draw3D(void); /* Hides/Removes the previously displayed on-screen keyboard. */ void OnscreenKeyboard_Close(void); /* Locks/Unlocks the landscape orientation. */ diff --git a/src/Window_3DS.c b/src/Window_3DS.c index dae4e9464..3688dfe82 100644 --- a/src/Window_3DS.c +++ b/src/Window_3DS.c @@ -259,6 +259,7 @@ void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/Window_Android.c b/src/Window_Android.c index e072b44e8..3357c6ac5 100644 --- a/src/Window_Android.c +++ b/src/Window_Android.c @@ -520,6 +520,7 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { JNIEnv* env; diff --git a/src/Window_Dreamcast.c b/src/Window_Dreamcast.c index e6ee46f1b..e415dbe48 100644 --- a/src/Window_Dreamcast.c +++ b/src/Window_Dreamcast.c @@ -297,7 +297,11 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { - if (kb_showing) VirtualKeyboard_Display2D(&r, bmp); + VirtualKeyboard_Display2D(r, bmp); +} + +void OnscreenKeyboard_Draw3D(void) { + VirtualKeyboard_Display3D(); } void OnscreenKeyboard_Close(void) { diff --git a/src/Window_GCWii.c b/src/Window_GCWii.c index bef2163e5..832cc79b9 100644 --- a/src/Window_GCWii.c +++ b/src/Window_GCWii.c @@ -485,7 +485,11 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { - if (kb_showing) VirtualKeyboard_Display2D(&r, bmp); + VirtualKeyboard_Display2D(r, bmp); +} + +void OnscreenKeyboard_Draw3D(void) { + VirtualKeyboard_Display3D(); } void OnscreenKeyboard_Close(void) { diff --git a/src/Window_N64.c b/src/Window_N64.c index 473162573..e21b414d9 100644 --- a/src/Window_N64.c +++ b/src/Window_N64.c @@ -153,6 +153,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { /* TODO implement */ } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/Window_NDS.c b/src/Window_NDS.c index e0365d068..d8f1b42c3 100644 --- a/src/Window_NDS.c +++ b/src/Window_NDS.c @@ -219,6 +219,7 @@ void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { keyboardHide(); diff --git a/src/Window_PS1.c b/src/Window_PS1.c index f34884a75..0e6e1ec8a 100644 --- a/src/Window_PS1.c +++ b/src/Window_PS1.c @@ -189,6 +189,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { /* TODO implement */ } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/Window_PS3.c b/src/Window_PS3.c index 927df7164..4ada25a88 100644 --- a/src/Window_PS3.c +++ b/src/Window_PS3.c @@ -350,7 +350,11 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { - if (kb_showing) VirtualKeyboard_Display2D(&r, bmp); + VirtualKeyboard_Display2D(r, bmp); +} + +void OnscreenKeyboard_Draw3D(void) { + VirtualKeyboard_Display3D(); } void OnscreenKeyboard_Close(void) { diff --git a/src/Window_PSP.c b/src/Window_PSP.c index 9609f9e51..87b37568a 100644 --- a/src/Window_PSP.c +++ b/src/Window_PSP.c @@ -154,7 +154,11 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { - if (kb_showing) VirtualKeyboard_Display2D(&r, bmp); + VirtualKeyboard_Display2D(r, bmp); +} + +void OnscreenKeyboard_Draw3D(void) { + VirtualKeyboard_Display3D(); } void OnscreenKeyboard_Close(void) { diff --git a/src/Window_PSVita.c b/src/Window_PSVita.c index e77e49086..5565c29f5 100644 --- a/src/Window_PSVita.c +++ b/src/Window_PSVita.c @@ -330,6 +330,7 @@ void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/Window_SDL.c b/src/Window_SDL.c index d7efb941f..86848a4c8 100644 --- a/src/Window_SDL.c +++ b/src/Window_SDL.c @@ -408,6 +408,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { SDL_StartTextInput(); } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { SDL_StopTextInput(); } void Window_EnableRawMouse(void) { diff --git a/src/Window_SDL3.c b/src/Window_SDL3.c index ffa8b1901..bf60dfe7c 100644 --- a/src/Window_SDL3.c +++ b/src/Window_SDL3.c @@ -438,6 +438,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { SDL_StartTextInput(); } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { SDL_StopTextInput(); } void Window_EnableRawMouse(void) { diff --git a/src/Window_Switch.c b/src/Window_Switch.c index 4c561424d..529c7fab5 100644 --- a/src/Window_Switch.c +++ b/src/Window_Switch.c @@ -276,6 +276,7 @@ void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/Window_Web.c b/src/Window_Web.c index 6ae7c0141..4147b5eee 100644 --- a/src/Window_Web.c +++ b/src/Window_Web.c @@ -694,6 +694,7 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { keyboardOpen = false; diff --git a/src/Window_WiiU.c b/src/Window_WiiU.c index 2be9304e1..7fbb4fb5e 100644 --- a/src/Window_WiiU.c +++ b/src/Window_WiiU.c @@ -241,6 +241,7 @@ void Window_SetSize(int width, int height) { } void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { /* TODO implement */ } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } void Window_ShowDialog(const char* title, const char* msg) { diff --git a/src/Window_Win.c b/src/Window_Win.c index f0d0f98a9..bf2a123b9 100644 --- a/src/Window_Win.c +++ b/src/Window_Win.c @@ -714,6 +714,7 @@ static void InitRawMouse(void) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { } void Window_EnableRawMouse(void) { diff --git a/src/Window_X11.c b/src/Window_X11.c index 479b0b376..4d82b95e0 100644 --- a/src/Window_X11.c +++ b/src/Window_X11.c @@ -1137,6 +1137,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { } static cc_bool rawMouseInited, rawMouseSupported; diff --git a/src/Window_Xbox.c b/src/Window_Xbox.c index 32d2b74de..e416e39b0 100644 --- a/src/Window_Xbox.c +++ b/src/Window_Xbox.c @@ -207,7 +207,11 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { - if (kb_showing) VirtualKeyboard_Display2D(&r, bmp); + VirtualKeyboard_Display2D(r, bmp); +} + +void OnscreenKeyboard_Draw3D(void) { + VirtualKeyboard_Display3D(); } void OnscreenKeyboard_Close(void) { diff --git a/src/Window_Xbox360.c b/src/Window_Xbox360.c index bc6e0bb99..6e1a64a91 100644 --- a/src/Window_Xbox360.c +++ b/src/Window_Xbox360.c @@ -152,6 +152,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { /* TODO implement */ } diff --git a/src/interop_BeOS.cpp b/src/interop_BeOS.cpp index 86ca91cc9..3cdaaf307 100644 --- a/src/interop_BeOS.cpp +++ b/src/interop_BeOS.cpp @@ -757,6 +757,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { } void Window_EnableRawMouse(void) { diff --git a/src/interop_cocoa.m b/src/interop_cocoa.m index bfad5e408..0963c6e04 100644 --- a/src/interop_cocoa.m +++ b/src/interop_cocoa.m @@ -715,6 +715,7 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) { void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { } void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { } diff --git a/src/interop_ios.m b/src/interop_ios.m index a11fe37e5..baea91826 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -515,6 +515,7 @@ void OnscreenKeyboard_SetText(const cc_string* text) { } void OnscreenKeyboard_Draw2D(Rect2D* r, struct Bitmap* bmp) { } +void OnscreenKeyboard_Draw3D(void) { } void OnscreenKeyboard_Close(void) { [text_input resignFirstResponder];