Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/example_sdl_opengl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int main(int, char**)
printf("Error: %s\n", SDL_GetError());
return -1;
}
ImGui_ImplSDL2_Init();

// Setup window
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Expand All @@ -32,6 +33,7 @@ int main(int, char**)
SDL_GetCurrentDisplayMode(0, &current);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
ImGui_ImplSDL2_HookIme(window);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1); // Enable vsync

Expand Down
2 changes: 2 additions & 0 deletions examples/example_sdl_opengl3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int main(int, char**)
printf("Error: %s\n", SDL_GetError());
return -1;
}
ImGui_ImplSDL2_Init();

// Decide GL+GLSL versions
#if __APPLE__
Expand All @@ -56,6 +57,7 @@ int main(int, char**)
SDL_GetCurrentDisplayMode(0, &current);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
ImGui_ImplSDL2_HookIme(window);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1); // Enable vsync

Expand Down
2 changes: 2 additions & 0 deletions examples/example_sdl_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,14 @@ int main(int, char**)
printf("Error: %s\n", SDL_GetError());
return 1;
}
ImGui_ImplSDL2_Init();

// Setup window
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, &current);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
ImGui_ImplSDL2_HookIme(window);

// Setup Vulkan
uint32_t extensions_count = 0;
Expand Down
96 changes: 92 additions & 4 deletions examples/imgui_impl_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ static Uint64 g_Time = 0;
static bool g_MousePressed[3] = { false, false, false };
static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 };
static char* g_ClipboardTextData = NULL;
static bool g_PrevWantInputText = true;
static int g_ImeInputScreenPos[2] = { 0 , 0 };
static int g_NextImeInputScreenPos[2] = { 0 , 0 };

static const char* ImGui_ImplSDL2_GetClipboardText(void*)
{
Expand All @@ -74,6 +77,58 @@ static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
SDL_SetClipboardText(text);
}

static void ImGui_ImplSDL2_ImeSetInputScreenPos(int x, int y)
{
g_NextImeInputScreenPos[0] = x;
g_NextImeInputScreenPos[1] = y;
}

#ifdef _WIN32
static HWND ImGui_ImplSDL2_GetHWND(SDL_Window* window)
{
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
return (HWND)wmInfo.info.win.window;
}

static LRESULT CALLBACK ImGui_ImplSDL2_HookIme_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WNDPROC wndProc = (WNDPROC)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (msg)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
if (wParam == VK_PROCESSKEY)
wndProc = DefWindowProc;
}
return CallWindowProc(wndProc, hwnd, msg, wParam, lParam);
}
#endif

void ImGui_ImplSDL2_Init()
{
#ifdef _WIN32
SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
#endif
}

void ImGui_ImplSDL2_HookIme(SDL_Window* window)
{
#ifdef _WIN32
HWND hwnd = ImGui_ImplSDL2_GetHWND(window);
if (!GetWindowLongPtr(hwnd, GWLP_USERDATA)) {
SetWindowLongPtr(hwnd, GWLP_USERDATA, GetWindowLongPtr(hwnd, GWLP_WNDPROC));
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplSDL2_HookIme_WndProc);
}
ImmAssociateContext(hwnd, 0);
SDL_StartTextInput();
ImmAssociateContextEx(hwnd, 0, IACE_DEFAULT);
#endif
}

// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
Expand Down Expand Up @@ -116,6 +171,15 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
return true;
}
#ifdef _WIN32
case SDL_WINDOWEVENT:
if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED && SDL_EventState(SDL_TEXTINPUT, SDL_QUERY))
{
SDL_Window* window = SDL_GetWindowFromID(event->window.windowID);
if (window)
ImmAssociateContextEx(ImGui_ImplSDL2_GetHWND(window), 0, IACE_DEFAULT);
}
#endif
}
return false;
}
Expand Down Expand Up @@ -156,6 +220,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window)
io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
io.ClipboardUserData = NULL;
io.ImeSetInputScreenPosFn = ImGui_ImplSDL2_ImeSetInputScreenPos;

g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
Expand All @@ -167,10 +232,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window)
g_MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);

#ifdef _WIN32
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
io.ImeWindowHandle = wmInfo.info.win.window;
io.ImeWindowHandle = ImGui_ImplSDL2_GetHWND(window);
#else
(void)window;
#endif
Expand Down Expand Up @@ -268,6 +330,31 @@ static void ImGui_ImplSDL2_UpdateMouseCursor()
}
}

static void ImGui_ImplSDL2_UpdateTextInput(SDL_Window* window)
{
ImGuiIO& io = ImGui::GetIO();
if (io.WantTextInput != g_PrevWantInputText)
{
g_PrevWantInputText = io.WantTextInput;
if (io.WantTextInput)
{
SDL_StartTextInput();
#ifdef _WIN32
ImmAssociateContextEx(ImGui_ImplSDL2_GetHWND(window), 0, IACE_DEFAULT);
#endif
}
else
SDL_StopTextInput();
}
if (io.WantTextInput && (g_NextImeInputScreenPos[0] != g_ImeInputScreenPos[0] || g_NextImeInputScreenPos[1] != g_ImeInputScreenPos[1]))
{
g_ImeInputScreenPos[0] = g_NextImeInputScreenPos[0];
g_ImeInputScreenPos[1] = g_NextImeInputScreenPos[1];
SDL_Rect rect = { g_ImeInputScreenPos[0], g_ImeInputScreenPos[1], 0, 0 };
SDL_SetTextInputRect(&rect);
}
}

void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
{
ImGuiIO& io = ImGui::GetIO();
Expand All @@ -289,4 +376,5 @@ void ImGui_ImplSDL2_NewFrame(SDL_Window* window)

ImGui_ImplSDL2_UpdateMousePosAndButtons();
ImGui_ImplSDL2_UpdateMouseCursor();
ImGui_ImplSDL2_UpdateTextInput(window);
}
2 changes: 2 additions & 0 deletions examples/imgui_impl_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
struct SDL_Window;
typedef union SDL_Event SDL_Event;

IMGUI_IMPL_API void ImGui_ImplSDL2_Init();
IMGUI_IMPL_API void ImGui_ImplSDL2_HookIme(SDL_Window* window);
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window);
IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown();
Expand Down