14 | 14 |
var g_ClientApi: ClientApi = .Unknown;
|
15 | 15 |
var g_Time: f64 = 0.0;
|
16 | 16 |
var g_MouseJustPressed = [_]bool{false} ** 5;
|
17 | |
var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
|
|
17 |
var g_MouseCursors = [_]?*c.GLFWcursor{null} ** c.ImGuiMouseCursor_COUNT;
|
18 | 18 |
var g_WantUpdateMonitors = true;
|
19 | 19 |
|
20 | 20 |
// Chain GLFW callbacks for main viewport:
|
|
30 | 30 |
|
31 | 31 |
// Setup back-end capabilities flags
|
32 | 32 |
const io = c.igGetIO();
|
33 | |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
|
34 | |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
|
35 | |
if (false) io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional)
|
|
33 |
io.*.BackendFlags |= c.ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
|
34 |
io.*.BackendFlags |= c.ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
|
35 |
if (false) io.*.BackendFlags |= c.ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
|
36 | 36 |
if (false and @hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) {
|
37 | |
io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
38 | |
}
|
39 | |
io.*.BackendPlatformName = c"imgui_impl_glfw.zig";
|
|
37 |
io.*.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
|
38 |
}
|
|
39 |
io.*.BackendPlatformName = "imgui_impl_glfw.zig";
|
40 | 40 |
|
41 | 41 |
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
42 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
|
43 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_LeftArrow)] = c.GLFW_KEY_LEFT;
|
44 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_RightArrow)] = c.GLFW_KEY_RIGHT;
|
45 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_UpArrow)] = c.GLFW_KEY_UP;
|
46 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_DownArrow)] = c.GLFW_KEY_DOWN;
|
47 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_PageUp)] = c.GLFW_KEY_PAGE_UP;
|
48 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_PageDown)] = c.GLFW_KEY_PAGE_DOWN;
|
49 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Home)] = c.GLFW_KEY_HOME;
|
50 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_End)] = c.GLFW_KEY_END;
|
51 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Insert)] = c.GLFW_KEY_INSERT;
|
52 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Delete)] = c.GLFW_KEY_DELETE;
|
53 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Backspace)] = c.GLFW_KEY_BACKSPACE;
|
54 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Space)] = c.GLFW_KEY_SPACE;
|
55 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Enter)] = c.GLFW_KEY_ENTER;
|
56 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Escape)] = c.GLFW_KEY_ESCAPE;
|
57 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_KeyPadEnter)] = c.GLFW_KEY_KP_ENTER;
|
58 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_A)] = c.GLFW_KEY_A;
|
59 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_C)] = c.GLFW_KEY_C;
|
60 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_V)] = c.GLFW_KEY_V;
|
61 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_X)] = c.GLFW_KEY_X;
|
62 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
|
63 | |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
|
|
42 |
io.*.KeyMap[c.ImGuiKey_Tab] = c.GLFW_KEY_TAB;
|
|
43 |
io.*.KeyMap[c.ImGuiKey_LeftArrow] = c.GLFW_KEY_LEFT;
|
|
44 |
io.*.KeyMap[c.ImGuiKey_RightArrow] = c.GLFW_KEY_RIGHT;
|
|
45 |
io.*.KeyMap[c.ImGuiKey_UpArrow] = c.GLFW_KEY_UP;
|
|
46 |
io.*.KeyMap[c.ImGuiKey_DownArrow] = c.GLFW_KEY_DOWN;
|
|
47 |
io.*.KeyMap[c.ImGuiKey_PageUp] = c.GLFW_KEY_PAGE_UP;
|
|
48 |
io.*.KeyMap[c.ImGuiKey_PageDown] = c.GLFW_KEY_PAGE_DOWN;
|
|
49 |
io.*.KeyMap[c.ImGuiKey_Home] = c.GLFW_KEY_HOME;
|
|
50 |
io.*.KeyMap[c.ImGuiKey_End] = c.GLFW_KEY_END;
|
|
51 |
io.*.KeyMap[c.ImGuiKey_Insert] = c.GLFW_KEY_INSERT;
|
|
52 |
io.*.KeyMap[c.ImGuiKey_Delete] = c.GLFW_KEY_DELETE;
|
|
53 |
io.*.KeyMap[c.ImGuiKey_Backspace] = c.GLFW_KEY_BACKSPACE;
|
|
54 |
io.*.KeyMap[c.ImGuiKey_Space] = c.GLFW_KEY_SPACE;
|
|
55 |
io.*.KeyMap[c.ImGuiKey_Enter] = c.GLFW_KEY_ENTER;
|
|
56 |
io.*.KeyMap[c.ImGuiKey_Escape] = c.GLFW_KEY_ESCAPE;
|
|
57 |
io.*.KeyMap[c.ImGuiKey_KeyPadEnter] = c.GLFW_KEY_KP_ENTER;
|
|
58 |
io.*.KeyMap[c.ImGuiKey_A] = c.GLFW_KEY_A;
|
|
59 |
io.*.KeyMap[c.ImGuiKey_C] = c.GLFW_KEY_C;
|
|
60 |
io.*.KeyMap[c.ImGuiKey_V] = c.GLFW_KEY_V;
|
|
61 |
io.*.KeyMap[c.ImGuiKey_X] = c.GLFW_KEY_X;
|
|
62 |
io.*.KeyMap[c.ImGuiKey_Y] = c.GLFW_KEY_Y;
|
|
63 |
io.*.KeyMap[c.ImGuiKey_Z] = c.GLFW_KEY_Z;
|
64 | 64 |
|
65 | 65 |
// @TODO: Clipboard
|
66 | 66 |
// io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
|
67 | 67 |
// io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
|
68 | 68 |
io.*.ClipboardUserData = g_Window;
|
69 | 69 |
|
70 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
|
71 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_TextInput)] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
|
72 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeAll)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
73 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNS)] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
|
74 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeEW)] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
|
75 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNESW)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
76 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNWSE)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
77 | |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Hand)] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
|
|
70 |
g_MouseCursors[c.ImGuiMouseCursor_Arrow] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
|
|
71 |
g_MouseCursors[c.ImGuiMouseCursor_TextInput] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
|
|
72 |
g_MouseCursors[c.ImGuiMouseCursor_ResizeAll] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
73 |
g_MouseCursors[c.ImGuiMouseCursor_ResizeNS] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
|
|
74 |
g_MouseCursors[c.ImGuiMouseCursor_ResizeEW] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
|
|
75 |
g_MouseCursors[c.ImGuiMouseCursor_ResizeNESW] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
76 |
g_MouseCursors[c.ImGuiMouseCursor_ResizeNWSE] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
77 |
g_MouseCursors[c.ImGuiMouseCursor_Hand] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
|
78 | 78 |
|
79 | 79 |
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
|
80 | 80 |
g_PrevUserCallbackMousebutton = null;
|
|
91 | 91 |
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
92 | 92 |
const main_viewport = c.igGetMainViewport();
|
93 | 93 |
main_viewport.*.PlatformHandle = g_Window;
|
94 | |
if (builtin.os == builtin.Os.windows)
|
95 | |
main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
|
|
94 |
// if (builtin.os == builtin.Os.windows)
|
|
95 |
// main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
|
96 | 96 |
|
97 | 97 |
// @TODO: Platform Interface (Viewport)
|
98 | |
if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0)
|
|
98 |
if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0)
|
99 | 99 |
unreachable;
|
100 | 100 |
// ImGui_ImplGlfw_InitPlatformInterface();
|
101 | 101 |
|
|
206 | 206 |
var mouse_x: f64 = undefined;
|
207 | 207 |
var mouse_y: f64 = undefined;
|
208 | 208 |
c.glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
209 | |
if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) {
|
|
209 |
if (io.*.ConfigFlags & c.ImGuiConfigFlags_ViewportsEnable != 0) {
|
210 | 210 |
// Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
|
211 | 211 |
var window_x: c_int = undefined;
|
212 | 212 |
var window_y: c_int = undefined;
|
|
229 | 229 |
|
230 | 230 |
fn UpdateMouseCursor() void {
|
231 | 231 |
const io = c.igGetIO();
|
232 | |
if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_NoMouseCursorChange) != 0 or c.glfwGetInputMode(g_Window, c.GLFW_CURSOR) == c.GLFW_CURSOR_DISABLED)
|
|
232 |
if (io.*.ConfigFlags & c.ImGuiConfigFlags_NoMouseCursorChange != 0 or c.glfwGetInputMode(g_Window, c.GLFW_CURSOR) == c.GLFW_CURSOR_DISABLED)
|
233 | 233 |
return;
|
234 | 234 |
|
235 | 235 |
const imgui_cursor = c.igGetMouseCursor();
|
|
237 | 237 |
var n: usize = 0;
|
238 | 238 |
while (n < @intCast(usize, platform_io.*.Viewports.Size)) : (n += 1) {
|
239 | 239 |
const window = @ptrCast(*c.GLFWwindow, platform_io.*.Viewports.Data[n].*.PlatformHandle);
|
240 | |
if (imgui_cursor == @enumToInt(c.ImGuiMouseCursor_None) or io.*.MouseDrawCursor) {
|
|
240 |
if (imgui_cursor == c.ImGuiMouseCursor_None or io.*.MouseDrawCursor) {
|
241 | 241 |
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
242 | 242 |
c.glfwSetInputMode(window, c.GLFW_CURSOR, c.GLFW_CURSOR_HIDDEN);
|
243 | 243 |
} else {
|
244 | 244 |
// Show OS mouse cursor
|
245 | 245 |
// FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
|
246 | |
c.glfwSetCursor(window, if (g_MouseCursors[@intCast(usize, imgui_cursor)]) |cursor| cursor else g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)]);
|
|
246 |
c.glfwSetCursor(window, if (g_MouseCursors[@intCast(usize, imgui_cursor)]) |cursor| cursor else g_MouseCursors[c.ImGuiMouseCursor_Arrow]);
|
247 | 247 |
c.glfwSetInputMode(window, c.GLFW_CURSOR, c.GLFW_CURSOR_NORMAL);
|
248 | 248 |
}
|
249 | 249 |
}
|
|
254 | 254 |
}
|
255 | 255 |
|
256 | 256 |
// GLFW Callbacks
|
257 | |
extern fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) void {
|
|
257 |
fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
|
258 | 258 |
if (g_PrevUserCallbackMousebutton) |prev| {
|
259 | 259 |
prev(window, button, action, mods);
|
260 | 260 |
}
|
|
267 | 267 |
g_MouseJustPressed[button_u] = true;
|
268 | 268 |
}
|
269 | 269 |
|
270 | |
extern fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) void {
|
|
270 |
fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) callconv(.C) void {
|
271 | 271 |
if (g_PrevUserCallbackScroll) |prev| {
|
272 | 272 |
prev(window, dx, dy);
|
273 | 273 |
}
|
|
277 | 277 |
io.*.MouseWheel += @floatCast(f32, dy);
|
278 | 278 |
}
|
279 | 279 |
|
280 | |
extern fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) void {
|
|
280 |
fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) callconv(.C) void {
|
281 | 281 |
if (g_PrevUserCallbackKey) |prev| {
|
282 | 282 |
prev(window, key, scancode, action, modifiers);
|
283 | 283 |
}
|
|
300 | 300 |
io.*.KeySuper = io.*.KeysDown[c.GLFW_KEY_LEFT_SUPER] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SUPER];
|
301 | 301 |
}
|
302 | 302 |
|
303 | |
extern fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) void {
|
|
303 |
fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) callconv(.C) void {
|
304 | 304 |
if (g_PrevUserCallbackChar) |prev| {
|
305 | 305 |
prev(window, char);
|
306 | 306 |
}
|