|
8 | 8 | // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] |
9 | 9 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
10 | 10 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. |
11 | | -// Missing features: |
12 | | -// [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME. |
| 11 | +// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. |
13 | 12 |
|
14 | 13 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
15 | 14 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
|
18 | 17 |
|
19 | 18 | // CHANGELOG |
20 | 19 | // (minor and older changes stripped away, please see git history for details) |
| 20 | +// 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()). |
21 | 21 | // 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3. |
22 | 22 | // 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). |
23 | 23 | // 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096) |
|
84 | 84 | #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 |
85 | 85 | #endif |
86 | 86 | #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) |
87 | | -#define SDL_HAS_SET_TEXT_INPUT_RECT SDL_VERSION_ATLEAST(2,0,0) |
88 | 87 |
|
89 | 88 | // SDL Data |
90 | 89 | struct ImGui_ImplSDL2_Data |
@@ -127,6 +126,25 @@ static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text) |
127 | 126 | SDL_SetClipboardText(text); |
128 | 127 | } |
129 | 128 |
|
| 129 | +// Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow(). |
| 130 | +static void ImGui_ImplSDL2_SetPlatformImeData(ImGuiViewport*, ImGuiPlatformImeData* data) |
| 131 | +{ |
| 132 | + if (data->WantVisible) |
| 133 | + { |
| 134 | + SDL_Rect r; |
| 135 | + r.x = (int)data->InputPos.x; |
| 136 | + r.y = (int)data->InputPos.y; |
| 137 | + r.w = 1; |
| 138 | + r.h = (int)data->InputLineHeight; |
| 139 | + SDL_SetTextInputRect(&r); |
| 140 | + SDL_StartTextInput(); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + SDL_StopTextInput(); |
| 145 | + } |
| 146 | +} |
| 147 | + |
130 | 148 | static ImGuiKey ImGui_ImplSDL2_KeycodeToImGuiKey(int keycode) |
131 | 149 | { |
132 | 150 | switch (keycode) |
@@ -337,27 +355,6 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) |
337 | 355 | return false; |
338 | 356 | } |
339 | 357 |
|
340 | | -#if SDL_HAS_SET_TEXT_INPUT_RECT |
341 | | -static void ImGui_ImplSDL2_SetPlatformImeData(ImGuiViewport* viewport, ImGuiPlatformImeData* data) |
342 | | -{ |
343 | | - (void)viewport; |
344 | | - if (data->WantVisible) |
345 | | - { |
346 | | - SDL_Rect r; |
347 | | - r.x = (int)data->InputPos.x; |
348 | | - r.y = (int)data->InputPos.y; |
349 | | - r.w = 1; |
350 | | - r.h = (int)data->InputLineHeight; |
351 | | - SDL_SetTextInputRect(&r); |
352 | | - SDL_StartTextInput(); |
353 | | - } |
354 | | - else |
355 | | - { |
356 | | - SDL_StopTextInput(); |
357 | | - } |
358 | | -} |
359 | | -#endif |
360 | | - |
361 | 358 | static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer) |
362 | 359 | { |
363 | 360 | ImGuiIO& io = ImGui::GetIO(); |
@@ -388,10 +385,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer) |
388 | 385 | io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText; |
389 | 386 | io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText; |
390 | 387 | io.ClipboardUserData = nullptr; |
391 | | - |
392 | | -#if SDL_HAS_SET_TEXT_INPUT_RECT |
393 | 388 | io.SetPlatformImeDataFn = ImGui_ImplSDL2_SetPlatformImeData; |
394 | | -#endif |
395 | 389 |
|
396 | 390 | // Load mouse cursors |
397 | 391 | bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); |
@@ -428,6 +422,13 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer) |
428 | 422 | SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); |
429 | 423 | #endif |
430 | 424 |
|
| 425 | + // From 2.0.18: Enable native IME. |
| 426 | + // IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any. |
| 427 | + // For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow(). |
| 428 | +#ifdef SDL_HINT_IME_SHOW_UI |
| 429 | + SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 430 | +#endif |
| 431 | + |
431 | 432 | // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710) |
432 | 433 | #ifdef SDL_HINT_MOUSE_AUTO_CAPTURE |
433 | 434 | SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); |
|
0 commit comments