1 // dear imgui: Platform Binding for GLFW
2 // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..)
3 // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
4
5 // Implemented features:
6 // [X] Platform: Clipboard support.
7 // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
8 // [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW.
9 // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE).
10
11 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
12 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
13 // https://github.com/ocornut/imgui
14
15 // CHANGELOG
16 // (minor and older changes stripped away, please see git history for details)
17 // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
18 // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
19 // 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples.
20 // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
21 // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()).
22 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
23 // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
24 // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
25 // 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
26 // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
27 // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
28 // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
29 // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
30
31 #include "imgui.h"
32 #include "imgui_impl_glfw.h"
33
34 // GLFW
35 #include <GLFW/glfw3.h>
36 #ifdef _WIN32
37 #undef APIENTRY
38 #define GLFW_EXPOSE_NATIVE_WIN32
39 #include <GLFW/glfw3native.h> // for glfwGetWin32Window
40 #endif
41 #define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
42 #define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
43 #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
44 #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
45 #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
46
47 // Data
48 enum GlfwClientApi
49 {
50 GlfwClientApi_Unknown,
51 GlfwClientApi_OpenGL,
52 GlfwClientApi_Vulkan
53 };
54 static GLFWwindow* g_Window = NULL;
55 static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown;
56 static double g_Time = 0.0;
57 static bool g_MouseJustPressed[5] = { false, false, false, false, false };
58 static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 };
59
ImGui_ImplGlfw_GetClipboardText(void * user_data)60 static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
61 {
62 return glfwGetClipboardString((GLFWwindow*)user_data);
63 }
64
ImGui_ImplGlfw_SetClipboardText(void * user_data,const char * text)65 static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
66 {
67 glfwSetClipboardString((GLFWwindow*)user_data, text);
68 }
69
ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow *,int button,int action,int)70 void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
71 {
72 if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
73 g_MouseJustPressed[button] = true;
74 }
75
ImGui_ImplGlfw_ScrollCallback(GLFWwindow *,double xoffset,double yoffset)76 void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
77 {
78 ImGuiIO& io = ImGui::GetIO();
79 io.MouseWheelH += (float)xoffset;
80 io.MouseWheel += (float)yoffset;
81 }
82
ImGui_ImplGlfw_KeyCallback(GLFWwindow *,int key,int,int action,int mods)83 void ImGui_ImplGlfw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
84 {
85 ImGuiIO& io = ImGui::GetIO();
86 if (action == GLFW_PRESS)
87 io.KeysDown[key] = true;
88 if (action == GLFW_RELEASE)
89 io.KeysDown[key] = false;
90
91 (void)mods; // Modifiers are not reliable across systems
92 io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
93 io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
94 io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
95 io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
96 }
97
ImGui_ImplGlfw_CharCallback(GLFWwindow *,unsigned int c)98 void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
99 {
100 ImGuiIO& io = ImGui::GetIO();
101 if (c > 0 && c < 0x10000)
102 io.AddInputCharacter((unsigned short)c);
103 }
104
ImGui_ImplGlfw_InstallCallbacks(GLFWwindow * window)105 void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
106 {
107 glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
108 glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
109 glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
110 glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
111 }
112
ImGui_ImplGlfw_Init(GLFWwindow * window,bool install_callbacks,GlfwClientApi client_api)113 static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
114 {
115 g_Window = window;
116 g_Time = 0.0;
117
118 // Setup back-end capabilities flags
119 ImGuiIO& io = ImGui::GetIO();
120 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
121 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
122
123 // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
124 io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
125 io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
126 io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
127 io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
128 io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
129 io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
130 io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
131 io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
132 io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
133 io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
134 io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
135 io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
136 io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
137 io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
138 io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
139 io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
140 io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
141 io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
142 io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
143 io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
144 io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
145
146 io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
147 io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
148 io.ClipboardUserData = g_Window;
149 #if defined(_WIN32)
150 io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window);
151 #endif
152
153 g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
154 g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
155 g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
156 g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
157 g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
158 g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
159 g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
160 g_MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
161
162 if (install_callbacks)
163 ImGui_ImplGlfw_InstallCallbacks(window);
164
165 g_ClientApi = client_api;
166 return true;
167 }
168
ImGui_ImplGlfw_InitForOpenGL(GLFWwindow * window,bool install_callbacks)169 bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks)
170 {
171 return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL);
172 }
173
ImGui_ImplGlfw_InitForVulkan(GLFWwindow * window,bool install_callbacks)174 bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks)
175 {
176 return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan);
177 }
178
ImGui_ImplGlfw_Shutdown()179 void ImGui_ImplGlfw_Shutdown()
180 {
181 for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
182 {
183 glfwDestroyCursor(g_MouseCursors[cursor_n]);
184 g_MouseCursors[cursor_n] = NULL;
185 }
186 g_ClientApi = GlfwClientApi_Unknown;
187 }
188
ImGui_ImplGlfw_UpdateMousePosAndButtons()189 static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
190 {
191 // Update buttons
192 ImGuiIO& io = ImGui::GetIO();
193 for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
194 {
195 // 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.
196 io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
197 g_MouseJustPressed[i] = false;
198 }
199
200 // Update mouse position
201 const ImVec2 mouse_pos_backup = io.MousePos;
202 io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
203 #ifdef __EMSCRIPTEN__
204 const bool focused = true; // Emscripten
205 #else
206 const bool focused = glfwGetWindowAttrib(g_Window, GLFW_FOCUSED) != 0;
207 #endif
208 if (focused)
209 {
210 if (io.WantSetMousePos)
211 {
212 glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y);
213 }
214 else
215 {
216 double mouse_x, mouse_y;
217 glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
218 io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
219 }
220 }
221 }
222
ImGui_ImplGlfw_UpdateMouseCursor()223 static void ImGui_ImplGlfw_UpdateMouseCursor()
224 {
225 ImGuiIO& io = ImGui::GetIO();
226 if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
227 return;
228
229 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
230 if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
231 {
232 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
233 glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
234 }
235 else
236 {
237 // Show OS mouse cursor
238 // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
239 glfwSetCursor(g_Window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
240 glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
241 }
242 }
243
ImGui_ImplGlfw_NewFrame()244 void ImGui_ImplGlfw_NewFrame()
245 {
246 ImGuiIO& io = ImGui::GetIO();
247 IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
248
249 // Setup display size
250 int w, h;
251 int display_w, display_h;
252 glfwGetWindowSize(g_Window, &w, &h);
253 glfwGetFramebufferSize(g_Window, &display_w, &display_h);
254 io.DisplaySize = ImVec2((float)w, (float)h);
255 io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
256
257 // Setup time step
258 double current_time = glfwGetTime();
259 io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
260 g_Time = current_time;
261
262 ImGui_ImplGlfw_UpdateMousePosAndButtons();
263 ImGui_ImplGlfw_UpdateMouseCursor();
264
265 // Gamepad navigation mapping [BETA]
266 memset(io.NavInputs, 0, sizeof(io.NavInputs));
267 if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad)
268 {
269 // Update gamepad inputs
270 #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
271 #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
272 int axes_count = 0, buttons_count = 0;
273 const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
274 const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
275 MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
276 MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
277 MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
278 MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
279 MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
280 MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
281 MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
282 MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
283 MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
284 MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
285 MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
286 MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
287 MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
288 MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
289 MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
290 MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
291 #undef MAP_BUTTON
292 #undef MAP_ANALOG
293 if (axes_count > 0 && buttons_count > 0)
294 io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
295 else
296 io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
297 }
298 }
299