SDL example: moved event loop to main.cpp , adding page up/page down. (#226)

docking
ocornut 9 years ago
parent b7a2a6b23f
commit 4167528001

@ -101,24 +101,46 @@ static void ImGui_ImplSdl_SetClipboardText(const char* text)
SDL_SetClipboardText(text); SDL_SetClipboardText(text);
} }
void ImGui_ImplSdl_KeyCallback(int key, bool down) bool ImGui_ImplSdl_EventCallback(const SDL_Event& event)
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (down) switch (event.type)
io.KeysDown[key] = true; {
else case SDL_MOUSEWHEEL:
io.KeysDown[key] = false; {
if (event.wheel.y > 0)
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); g_MouseWheel = 1;
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); if (event.wheel.y < 0)
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); g_MouseWheel = -1;
} return true;
}
void ImGui_ImplSdl_CharCallback(unsigned int c) case SDL_MOUSEBUTTONDOWN:
{ {
ImGuiIO& io = ImGui::GetIO(); if (event.button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
if (c > 0 && c < 0x10000) if (event.button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
io.AddInputCharacter((unsigned short)c); if (event.button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
return true;
}
case SDL_TEXTINPUT:
{
ImGuiIO& io = ImGui::GetIO();
unsigned int c = event.text.text[0];
if (c > 0 && c < 0x10000)
io.AddInputCharacter((unsigned short)c);
return true;
}
case SDL_KEYDOWN:
case SDL_KEYUP:
{
int key = event.key.keysym.sym & ~SDLK_SCANCODE_MASK;
io.KeysDown[key] = (event.type == SDL_KEYDOWN);
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
return true;
}
}
return false;
} }
bool ImGui_ImplSdl_CreateDeviceObjects() bool ImGui_ImplSdl_CreateDeviceObjects()
@ -164,6 +186,8 @@ bool ImGui_ImplSdl_Init(SDL_Window *window)
io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT; io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP; io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN; io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME; io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END; io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
io.KeyMap[ImGuiKey_Delete] = SDLK_DELETE; io.KeyMap[ImGuiKey_Delete] = SDLK_DELETE;
@ -197,47 +221,13 @@ void ImGui_ImplSdl_Shutdown()
ImGui::Shutdown(); ImGui::Shutdown();
} }
bool ImGui_ImplSdl_NewFrame(SDL_Window *window) void ImGui_ImplSdl_NewFrame(SDL_Window *window)
{ {
if (!g_FontTexture) if (!g_FontTexture)
ImGui_ImplSdl_CreateDeviceObjects(); ImGui_ImplSdl_CreateDeviceObjects();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
bool done = false;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_MOUSEWHEEL:
if (event.wheel.y > 0)
g_MouseWheel = 1;
if (event.wheel.y < 0)
g_MouseWheel = -1;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
if (event.button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
if (event.button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
break;
case SDL_TEXTINPUT:
ImGui_ImplSdl_CharCallback(event.text.text[0]);
break;
case SDL_KEYUP:
ImGui_ImplSdl_KeyCallback(event.key.keysym.sym&~SDLK_SCANCODE_MASK, false);
break;
case SDL_KEYDOWN:
ImGui_ImplSdl_KeyCallback(event.key.keysym.sym&~SDLK_SCANCODE_MASK, true);
break;
default:
break;
}
}
// Setup display size (every frame to accommodate for window resizing) // Setup display size (every frame to accommodate for window resizing)
int w, h; int w, h;
SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSize(window, &w, &h);
@ -273,5 +263,4 @@ bool ImGui_ImplSdl_NewFrame(SDL_Window *window)
// Start the frame // Start the frame
ImGui::NewFrame(); ImGui::NewFrame();
return done;
} }

@ -1,11 +1,11 @@
struct SDL_Window; struct SDL_Window;
typedef union SDL_Event SDL_Event;
bool ImGui_ImplSdl_Init(SDL_Window *window); bool ImGui_ImplSdl_Init(SDL_Window *window);
void ImGui_ImplSdl_Shutdown(); void ImGui_ImplSdl_Shutdown();
bool ImGui_ImplSdl_NewFrame(SDL_Window *window); void ImGui_ImplSdl_NewFrame(SDL_Window *window);
bool ImGui_ImplSdl_EventCallback(const SDL_Event& event);
void ImGui_ImplSdl_InvalidateDeviceObjects(); void ImGui_ImplSdl_InvalidateDeviceObjects();
bool ImGui_ImplSdl_CreateDeviceObjects(); bool ImGui_ImplSdl_CreateDeviceObjects();
void ImGui_ImplSdl_KeyCallback(int key, int, bool down);
void ImGui_ImplSdl_CharCallback(unsigned int c);

@ -56,7 +56,14 @@ int SDL_main(int /*argc*/, char* /*argv*/[])
bool done = false; bool done = false;
while (!done) while (!done)
{ {
done = ImGui_ImplSdl_NewFrame(window); SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSdl_EventCallback(event);
if (event.type == SDL_QUIT)
done = true;
}
ImGui_ImplSdl_NewFrame(window);
// 1. Show a simple window // 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"

Loading…
Cancel
Save