1 | // dear imgui: Platform Binding for SDL2
|
---|
2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
---|
3 | // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
---|
4 | // (Requires: SDL 2.0. Prefer SDL 2.0.4+ for full feature support.)
|
---|
5 |
|
---|
6 | // Implemented features:
|
---|
7 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
---|
8 | // [X] Platform: Clipboard support.
|
---|
9 | // [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE).
|
---|
10 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
|
---|
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.
|
---|
13 |
|
---|
14 | // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
---|
15 | // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
---|
16 | // https://github.com/ocornut/imgui
|
---|
17 |
|
---|
18 | // CHANGELOG
|
---|
19 | // (minor and older changes stripped away, please see git history for details)
|
---|
20 | // 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
|
---|
21 | // 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
|
---|
22 | // 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
|
---|
23 | // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
---|
24 | // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
|
---|
25 | // 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
|
---|
26 | // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
|
---|
27 | // 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
|
---|
28 | // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
---|
29 | // 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
|
---|
30 | // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
|
---|
31 | // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
|
---|
32 | // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
|
---|
33 | // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
|
---|
34 | // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
|
---|
35 | // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
---|
36 | // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
|
---|
37 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
---|
38 | // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
---|
39 | // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
|
---|
40 | // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
|
---|
41 | // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
|
---|
42 | // 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
|
---|
43 | // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
|
---|
44 | // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
|
---|
45 | // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
|
---|
46 |
|
---|
47 | #include "imgui.h"
|
---|
48 | #include "imgui_impl_sdl.h"
|
---|
49 |
|
---|
50 | // SDL
|
---|
51 | #include <SDL2/SDL.h>
|
---|
52 | #include <SDL2/SDL_syswm.h>
|
---|
53 | #if defined(__APPLE__)
|
---|
54 | #include "TargetConditionals.h"
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE SDL_VERSION_ATLEAST(2,0,4)
|
---|
58 | #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
|
---|
59 |
|
---|
60 | // Data
|
---|
61 | static SDL_Window* g_Window = NULL;
|
---|
62 | static Uint64 g_Time = 0;
|
---|
63 | static bool g_MousePressed[3] = { false, false, false };
|
---|
64 | static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
|
---|
65 | static char* g_ClipboardTextData = NULL;
|
---|
66 | static bool g_MouseCanUseGlobalState = true;
|
---|
67 |
|
---|
68 | static const char* ImGui_ImplSDL2_GetClipboardText(void*)
|
---|
69 | {
|
---|
70 | if (g_ClipboardTextData)
|
---|
71 | SDL_free(g_ClipboardTextData);
|
---|
72 | g_ClipboardTextData = SDL_GetClipboardText();
|
---|
73 | return g_ClipboardTextData;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
|
---|
77 | {
|
---|
78 | SDL_SetClipboardText(text);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
---|
82 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
|
---|
83 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
|
---|
84 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
---|
85 | // If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
|
---|
86 | bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
|
---|
87 | {
|
---|
88 | ImGuiIO& io = ImGui::GetIO();
|
---|
89 | switch (event->type)
|
---|
90 | {
|
---|
91 | case SDL_MOUSEWHEEL:
|
---|
92 | {
|
---|
93 | if (event->wheel.x > 0) io.MouseWheelH += 1;
|
---|
94 | if (event->wheel.x < 0) io.MouseWheelH -= 1;
|
---|
95 | if (event->wheel.y > 0) io.MouseWheel += 1;
|
---|
96 | if (event->wheel.y < 0) io.MouseWheel -= 1;
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 | case SDL_MOUSEBUTTONDOWN:
|
---|
100 | {
|
---|
101 | if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
|
---|
102 | if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
|
---|
103 | if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | case SDL_TEXTINPUT:
|
---|
107 | {
|
---|
108 | io.AddInputCharactersUTF8(event->text.text);
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 | case SDL_KEYDOWN:
|
---|
112 | case SDL_KEYUP:
|
---|
113 | {
|
---|
114 | int key = event->key.keysym.scancode;
|
---|
115 | IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
|
---|
116 | io.KeysDown[key] = (event->type == SDL_KEYDOWN);
|
---|
117 | io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
|
---|
118 | io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
|
---|
119 | io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
|
---|
120 | #ifdef _WIN32
|
---|
121 | io.KeySuper = false;
|
---|
122 | #else
|
---|
123 | io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
|
---|
124 | #endif
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static bool ImGui_ImplSDL2_Init(SDL_Window* window)
|
---|
132 | {
|
---|
133 | g_Window = window;
|
---|
134 |
|
---|
135 | // Setup back-end capabilities flags
|
---|
136 | ImGuiIO& io = ImGui::GetIO();
|
---|
137 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
---|
138 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
---|
139 | io.BackendPlatformName = "imgui_impl_sdl";
|
---|
140 |
|
---|
141 | // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
---|
142 | io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
|
---|
143 | io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
|
---|
144 | io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
|
---|
145 | io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
|
---|
146 | io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
|
---|
147 | io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
|
---|
148 | io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
|
---|
149 | io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
|
---|
150 | io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
|
---|
151 | io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
|
---|
152 | io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
|
---|
153 | io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
|
---|
154 | io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
|
---|
155 | io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
|
---|
156 | io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
|
---|
157 | io.KeyMap[ImGuiKey_KeyPadEnter] = SDL_SCANCODE_KP_ENTER;
|
---|
158 | io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
|
---|
159 | io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
|
---|
160 | io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
|
---|
161 | io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
|
---|
162 | io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
|
---|
163 | io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
|
---|
164 |
|
---|
165 | io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
---|
166 | io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
---|
167 | io.ClipboardUserData = NULL;
|
---|
168 |
|
---|
169 | // Load mouse cursors
|
---|
170 | g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
---|
171 | g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
|
---|
172 | g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
|
---|
173 | g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
|
---|
174 | g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
|
---|
175 | g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
|
---|
176 | g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
|
---|
177 | g_MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
---|
178 | g_MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
|
---|
179 |
|
---|
180 | // Check and store if we are on Wayland
|
---|
181 | g_MouseCanUseGlobalState = strncmp(SDL_GetCurrentVideoDriver(), "wayland", 7) != 0;
|
---|
182 |
|
---|
183 | #ifdef _WIN32
|
---|
184 | SDL_SysWMinfo wmInfo;
|
---|
185 | SDL_VERSION(&wmInfo.version);
|
---|
186 | SDL_GetWindowWMInfo(window, &wmInfo);
|
---|
187 | io.ImeWindowHandle = wmInfo.info.win.window;
|
---|
188 | #else
|
---|
189 | (void)window;
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
|
---|
196 | {
|
---|
197 | (void)sdl_gl_context; // Viewport branch will need this.
|
---|
198 | return ImGui_ImplSDL2_Init(window);
|
---|
199 | }
|
---|
200 |
|
---|
201 | bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
|
---|
202 | {
|
---|
203 | #if !SDL_HAS_VULKAN
|
---|
204 | IM_ASSERT(0 && "Unsupported");
|
---|
205 | #endif
|
---|
206 | return ImGui_ImplSDL2_Init(window);
|
---|
207 | }
|
---|
208 |
|
---|
209 | bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
|
---|
210 | {
|
---|
211 | #if !defined(_WIN32)
|
---|
212 | IM_ASSERT(0 && "Unsupported");
|
---|
213 | #endif
|
---|
214 | return ImGui_ImplSDL2_Init(window);
|
---|
215 | }
|
---|
216 |
|
---|
217 | bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
|
---|
218 | {
|
---|
219 | return ImGui_ImplSDL2_Init(window);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void ImGui_ImplSDL2_Shutdown()
|
---|
223 | {
|
---|
224 | g_Window = NULL;
|
---|
225 |
|
---|
226 | // Destroy last known clipboard data
|
---|
227 | if (g_ClipboardTextData)
|
---|
228 | SDL_free(g_ClipboardTextData);
|
---|
229 | g_ClipboardTextData = NULL;
|
---|
230 |
|
---|
231 | // Destroy SDL mouse cursors
|
---|
232 | for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
---|
233 | SDL_FreeCursor(g_MouseCursors[cursor_n]);
|
---|
234 | memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
|
---|
235 | }
|
---|
236 |
|
---|
237 | static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
|
---|
238 | {
|
---|
239 | ImGuiIO& io = ImGui::GetIO();
|
---|
240 |
|
---|
241 | // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
|
---|
242 | if (io.WantSetMousePos)
|
---|
243 | SDL_WarpMouseInWindow(g_Window, (int)io.MousePos.x, (int)io.MousePos.y);
|
---|
244 | else
|
---|
245 | io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
|
---|
246 |
|
---|
247 | int mx, my;
|
---|
248 | Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
|
---|
249 | io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
|
---|
250 | io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
|
---|
251 | io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
|
---|
252 | g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
|
---|
253 |
|
---|
254 | #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS)
|
---|
255 | SDL_Window* focused_window = SDL_GetKeyboardFocus();
|
---|
256 | if (g_Window == focused_window)
|
---|
257 | {
|
---|
258 | if (g_MouseCanUseGlobalState)
|
---|
259 | {
|
---|
260 | // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?)
|
---|
261 | // The creation of a new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
|
---|
262 | // Won't use this workaround when on Wayland, as there is no global mouse position.
|
---|
263 | int wx, wy;
|
---|
264 | SDL_GetWindowPosition(focused_window, &wx, &wy);
|
---|
265 | SDL_GetGlobalMouseState(&mx, &my);
|
---|
266 | mx -= wx;
|
---|
267 | my -= wy;
|
---|
268 | }
|
---|
269 | io.MousePos = ImVec2((float)mx, (float)my);
|
---|
270 | }
|
---|
271 |
|
---|
272 | // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger the OS window resize cursor.
|
---|
273 | // The function is only supported from SDL 2.0.4 (released Jan 2016)
|
---|
274 | bool any_mouse_button_down = ImGui::IsAnyMouseDown();
|
---|
275 | SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
|
---|
276 | #else
|
---|
277 | if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
|
---|
278 | io.MousePos = ImVec2((float)mx, (float)my);
|
---|
279 | #endif
|
---|
280 | }
|
---|
281 |
|
---|
282 | static void ImGui_ImplSDL2_UpdateMouseCursor()
|
---|
283 | {
|
---|
284 | ImGuiIO& io = ImGui::GetIO();
|
---|
285 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
---|
286 | return;
|
---|
287 |
|
---|
288 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
---|
289 | if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
|
---|
290 | {
|
---|
291 | // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
---|
292 | SDL_ShowCursor(SDL_FALSE);
|
---|
293 | }
|
---|
294 | else
|
---|
295 | {
|
---|
296 | // Show OS mouse cursor
|
---|
297 | SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
|
---|
298 | SDL_ShowCursor(SDL_TRUE);
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | static void ImGui_ImplSDL2_UpdateGamepads()
|
---|
303 | {
|
---|
304 | ImGuiIO& io = ImGui::GetIO();
|
---|
305 | memset(io.NavInputs, 0, sizeof(io.NavInputs));
|
---|
306 | if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
|
---|
307 | return;
|
---|
308 |
|
---|
309 | // Get gamepad
|
---|
310 | SDL_GameController* game_controller = SDL_GameControllerOpen(0);
|
---|
311 | if (!game_controller)
|
---|
312 | {
|
---|
313 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | // Update gamepad inputs
|
---|
318 | #define MAP_BUTTON(NAV_NO, BUTTON_NO) { io.NavInputs[NAV_NO] = (SDL_GameControllerGetButton(game_controller, BUTTON_NO) != 0) ? 1.0f : 0.0f; }
|
---|
319 | #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GameControllerGetAxis(game_controller, AXIS_NO) - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
|
---|
320 | const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
|
---|
321 | MAP_BUTTON(ImGuiNavInput_Activate, SDL_CONTROLLER_BUTTON_A); // Cross / A
|
---|
322 | MAP_BUTTON(ImGuiNavInput_Cancel, SDL_CONTROLLER_BUTTON_B); // Circle / B
|
---|
323 | MAP_BUTTON(ImGuiNavInput_Menu, SDL_CONTROLLER_BUTTON_X); // Square / X
|
---|
324 | MAP_BUTTON(ImGuiNavInput_Input, SDL_CONTROLLER_BUTTON_Y); // Triangle / Y
|
---|
325 | MAP_BUTTON(ImGuiNavInput_DpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); // D-Pad Left
|
---|
326 | MAP_BUTTON(ImGuiNavInput_DpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); // D-Pad Right
|
---|
327 | MAP_BUTTON(ImGuiNavInput_DpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); // D-Pad Up
|
---|
328 | MAP_BUTTON(ImGuiNavInput_DpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); // D-Pad Down
|
---|
329 | MAP_BUTTON(ImGuiNavInput_FocusPrev, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
|
---|
330 | MAP_BUTTON(ImGuiNavInput_FocusNext, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
|
---|
331 | MAP_BUTTON(ImGuiNavInput_TweakSlow, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
|
---|
332 | MAP_BUTTON(ImGuiNavInput_TweakFast, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
|
---|
333 | MAP_ANALOG(ImGuiNavInput_LStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
---|
334 | MAP_ANALOG(ImGuiNavInput_LStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
---|
335 | MAP_ANALOG(ImGuiNavInput_LStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32767);
|
---|
336 | MAP_ANALOG(ImGuiNavInput_LStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
---|
337 |
|
---|
338 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
---|
339 | #undef MAP_BUTTON
|
---|
340 | #undef MAP_ANALOG
|
---|
341 | }
|
---|
342 |
|
---|
343 | void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
|
---|
344 | {
|
---|
345 | ImGuiIO& io = ImGui::GetIO();
|
---|
346 | IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
|
---|
347 |
|
---|
348 | // Setup display size (every frame to accommodate for window resizing)
|
---|
349 | int w, h;
|
---|
350 | int display_w, display_h;
|
---|
351 | SDL_GetWindowSize(window, &w, &h);
|
---|
352 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
|
---|
353 | w = h = 0;
|
---|
354 | SDL_GL_GetDrawableSize(window, &display_w, &display_h);
|
---|
355 | io.DisplaySize = ImVec2((float)w, (float)h);
|
---|
356 | if (w > 0 && h > 0)
|
---|
357 | io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
|
---|
358 |
|
---|
359 | // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
|
---|
360 | static Uint64 frequency = SDL_GetPerformanceFrequency();
|
---|
361 | Uint64 current_time = SDL_GetPerformanceCounter();
|
---|
362 | io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
|
---|
363 | g_Time = current_time;
|
---|
364 |
|
---|
365 | ImGui_ImplSDL2_UpdateMousePosAndButtons();
|
---|
366 | ImGui_ImplSDL2_UpdateMouseCursor();
|
---|
367 |
|
---|
368 | // Update game controllers (if enabled and available)
|
---|
369 | ImGui_ImplSDL2_UpdateGamepads();
|
---|
370 | }
|
---|