From 8b8f827b70533cf70fab196e5f9a22bcb204738f Mon Sep 17 00:00:00 2001 From: Dav999 Date: Thu, 1 Aug 2024 23:38:55 +0200 Subject: [PATCH] Add IME text rendering This shows the uncommitted text in a box in the bottom left corner. This doesn't show the selection (defined by the start and length fields in the event) yet, but this is already much better than it was on its own, and I don't know how urgent the selection is since it's broken on Windows anyway. --- desktop_version/CMakeLists.txt | 1 + desktop_version/src/Graphics.cpp | 10 ++++-- desktop_version/src/IMERender.cpp | 57 +++++++++++++++++++++++++++++++ desktop_version/src/IMERender.h | 7 ++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 desktop_version/src/IMERender.cpp create mode 100644 desktop_version/src/IMERender.h diff --git a/desktop_version/CMakeLists.txt b/desktop_version/CMakeLists.txt index 0aec895e..28b4b4b8 100644 --- a/desktop_version/CMakeLists.txt +++ b/desktop_version/CMakeLists.txt @@ -82,6 +82,7 @@ set(VVV_CXX_SRC src/Graphics.cpp src/GraphicsResources.cpp src/GraphicsUtil.cpp + src/IMERender.cpp src/Input.cpp src/KeyPoll.cpp src/Labclass.cpp diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 7d7489e5..0c2dba35 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -12,6 +12,7 @@ #include "FileSystemUtils.h" #include "Font.h" #include "GraphicsUtil.h" +#include "IMERender.h" #include "Localization.h" #include "Map.h" #include "Maths.h" @@ -3529,6 +3530,7 @@ void Graphics::get_stretch_info(SDL_Rect* rect) void Graphics::render(void) { + ime_render(); draw_screenshot_border(); if (gameScreen.badSignalEffect) @@ -3541,10 +3543,12 @@ void Graphics::render(void) draw_window_background(); - SDL_Rect rect; - get_stretch_info(&rect); + SDL_Rect stretch_info; + get_stretch_info(&stretch_info); - copy_texture(gameTexture, NULL, &rect, 0, NULL, flipmode ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE); + ime_set_rect(&stretch_info); + + copy_texture(gameTexture, NULL, &stretch_info, 0, NULL, flipmode ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE); } void Graphics::renderwithscreeneffects(void) diff --git a/desktop_version/src/IMERender.cpp b/desktop_version/src/IMERender.cpp new file mode 100644 index 00000000..dde7e37d --- /dev/null +++ b/desktop_version/src/IMERender.cpp @@ -0,0 +1,57 @@ +#include + +#include "Constants.h" +#include "Font.h" +#include "Graphics.h" +#include "KeyPoll.h" + +static bool render_done = false; +static SDL_Rect imebox; + +void ime_render(void) +{ + render_done = false; + + if (!SDL_IsTextInputActive() || key.imebuffer == "") + { + return; + } + + int fontheight = font::height(PR_FONT_LEVEL); + imebox.x = 8; + imebox.y = SCREEN_HEIGHT_PIXELS - 32 - fontheight; + imebox.w = font::len(PR_FONT_LEVEL, key.imebuffer.c_str()) + 1; + imebox.h = fontheight + 1; + + SDL_Rect imebox_border = imebox; + imebox_border.x -= 1; + imebox_border.y -= 1; + imebox_border.w += 2; + imebox_border.h += 2; + + graphics.fill_rect(&imebox_border, 128, 128, 128); + graphics.fill_rect(&imebox, 0, 0, 0); + font::print(PR_FONT_LEVEL | PR_CJK_LOW, imebox.x + 1, imebox.y + 1, key.imebuffer, 255, 255, 255); + + render_done = true; +} + +void ime_set_rect(SDL_Rect* stretch_info) +{ + if (!render_done) + { + return; + } + + SDL_Rect imebox_scaled = imebox; + float x_scale = (float) stretch_info->w / SCREEN_WIDTH_PIXELS; + float y_scale = (float) stretch_info->h / SCREEN_HEIGHT_PIXELS; + imebox_scaled.x *= x_scale; + imebox_scaled.y *= y_scale; + imebox_scaled.w *= x_scale; + imebox_scaled.h *= y_scale; + imebox_scaled.x += stretch_info->x; + imebox_scaled.y += stretch_info->y; + + SDL_SetTextInputRect(&imebox_scaled); +} diff --git a/desktop_version/src/IMERender.h b/desktop_version/src/IMERender.h new file mode 100644 index 00000000..9e48ebd1 --- /dev/null +++ b/desktop_version/src/IMERender.h @@ -0,0 +1,7 @@ +#ifndef IMERENDER_H +#define IMERENDER_H + +void ime_render(void); +void ime_set_rect(SDL_Rect* stretch_info); + +#endif /* IMERENDER_H */