|
0 |
const c = @import("c.zig");
|
|
1 |
const builtin = @import("builtin");
|
|
2 |
pub const ClientApi = enum {
|
|
3 |
Unknown,
|
|
4 |
OpenGL,
|
|
5 |
Vulkan,
|
|
6 |
};
|
|
7 |
|
|
8 |
// Data
|
|
9 |
var g_Window: ?*c.GLFWwindow = null;
|
|
10 |
var g_ClientApi: ClientApi = .Unknown;
|
|
11 |
var g_Time: f32 = 0.0;
|
|
12 |
var g_MouseJustPressed = [5]bool;
|
|
13 |
var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
|
|
14 |
var g_WantUpdateMonitors = true;
|
|
15 |
|
|
16 |
// Chain GLFW callbacks for main viewport:
|
|
17 |
// our callbacks will call the user's previously installed callbacks, if any.
|
|
18 |
var g_PrevUserCallbackMousebutton: ?*c.GLFWmousebuttonfun = null;
|
|
19 |
var g_PrevUserCallbackScroll: ?*c.GLFWscrollfun = null;
|
|
20 |
var g_PrevUserCallbackKey: ?*c.GLFWkeyfun = null;
|
|
21 |
var g_PrevUserCallbackChar: ?*c.GLFWcharfun = null;
|
|
22 |
|
|
23 |
pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void {
|
|
24 |
g_Window = window;
|
|
25 |
g_Time = 0.0;
|
|
26 |
|
|
27 |
// Setup back-end capabilities flags
|
|
28 |
const io = c.igGetIO();
|
|
29 |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
|
|
30 |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
|
|
31 |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_PlatformHasViewports); // We can create multi-viewports on the Platform side (optional)
|
|
32 |
if (false and c.GLFW_HAS_GLFW_HOVERED and builtin.os == builtin.Os.windows) {
|
|
33 |
io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
|
34 |
}
|
|
35 |
io.*.BackendPlatformName = c"imgui_impl_glfw";
|
|
36 |
|
|
37 |
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
|
38 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
|
|
39 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_LeftArrow)] = c.GLFW_KEY_LEFT;
|
|
40 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_RightArrow)] = c.GLFW_KEY_RIGHT;
|
|
41 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_UpArrow)] = c.GLFW_KEY_UP;
|
|
42 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_DownArrow)] = c.GLFW_KEY_DOWN;
|
|
43 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_PageUp)] = c.GLFW_KEY_PAGE_UP;
|
|
44 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_PageDown)] = c.GLFW_KEY_PAGE_DOWN;
|
|
45 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Home)] = c.GLFW_KEY_HOME;
|
|
46 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_End)] = c.GLFW_KEY_END;
|
|
47 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Insert)] = c.GLFW_KEY_INSERT;
|
|
48 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Delete)] = c.GLFW_KEY_DELETE;
|
|
49 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Backspace)] = c.GLFW_KEY_BACKSPACE;
|
|
50 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Space)] = c.GLFW_KEY_SPACE;
|
|
51 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Enter)] = c.GLFW_KEY_ENTER;
|
|
52 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Escape)] = c.GLFW_KEY_ESCAPE;
|
|
53 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_KeyPadEnter)] = c.GLFW_KEY_KP_ENTER;
|
|
54 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_A)] = c.GLFW_KEY_A;
|
|
55 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_C)] = c.GLFW_KEY_C;
|
|
56 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_V)] = c.GLFW_KEY_V;
|
|
57 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_X)] = c.GLFW_KEY_X;
|
|
58 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
|
|
59 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
|
|
60 |
|
|
61 |
// io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
|
|
62 |
// io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
|
|
63 |
// io.ClipboardUserData = g_Window;
|
|
64 |
|
|
65 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Arrow)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR);
|
|
66 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_TextInput)] = c.glfwCreateStandardCursor(c.GLFW_IBEAM_CURSOR);
|
|
67 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeAll)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
68 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNS)] = c.glfwCreateStandardCursor(c.GLFW_VRESIZE_CURSOR);
|
|
69 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeEW)] = c.glfwCreateStandardCursor(c.GLFW_HRESIZE_CURSOR);
|
|
70 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNESW)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
71 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_ResizeNWSE)] = c.glfwCreateStandardCursor(c.GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
|
|
72 |
g_MouseCursors[@enumToInt(c.ImGuiMouseCursor_Hand)] = c.glfwCreateStandardCursor(c.GLFW_HAND_CURSOR);
|
|
73 |
|
|
74 |
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
|
|
75 |
g_PrevUserCallbackMousebutton = null;
|
|
76 |
g_PrevUserCallbackScroll = null;
|
|
77 |
g_PrevUserCallbackKey = null;
|
|
78 |
g_PrevUserCallbackChar = null;
|
|
79 |
if (install_callbacks and false) {
|
|
80 |
// g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
|
|
81 |
// g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
|
|
82 |
// g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
|
|
83 |
// g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
|
|
84 |
}
|
|
85 |
|
|
86 |
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
|
87 |
const main_viewport = c.igGetMainViewport();
|
|
88 |
main_viewport.*.PlatformHandle = g_Window;
|
|
89 |
if (builtin.os == builtin.Os.windows) {
|
|
90 |
main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
|
|
91 |
}
|
|
92 |
|
|
93 |
// if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0)
|
|
94 |
// ImGui_ImplGlfw_InitPlatformInterface();
|
|
95 |
|
|
96 |
g_ClientApi = client_api;
|
|
97 |
}
|
|
98 |
|
|
99 |
pub fn Shutdown() void {
|
|
100 |
// ImGui_ImplGlfw_ShutdownPlatformInterface();
|
|
101 |
for (g_MouseCursors) |*cursor| {
|
|
102 |
c.glfwDestroyCursor(cursor.*);
|
|
103 |
cursor.* = null;
|
|
104 |
}
|
|
105 |
g_ClientApi = .Unknown;
|
|
106 |
}
|