9 | 9 |
var g_Window: ?*c.GLFWwindow = null;
|
10 | 10 |
var g_ClientApi: ClientApi = .Unknown;
|
11 | 11 |
var g_Time: f32 = 0.0;
|
12 | |
var g_MouseJustPressed = [5]bool;
|
13 | |
var g_MouseCursors = [_]?*c.GLFWcursor{null} ** @enumToInt(c.ImGuiMouseCursor_COUNT);
|
|
12 |
var g_MouseJustPressed = [_]bool{ false } ** 5;
|
|
13 |
var g_MouseCursors = [_]?*c.GLFWcursor{ null } ** @enumToInt(c.ImGuiMouseCursor_COUNT);
|
14 | 14 |
var g_WantUpdateMonitors = true;
|
15 | 15 |
|
16 | 16 |
// Chain GLFW callbacks for main viewport:
|
17 | 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;
|
|
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 | 22 |
|
23 | 23 |
pub fn Init(window: *c.GLFWwindow, install_callbacks: bool, client_api: ClientApi) void {
|
24 | 24 |
g_Window = window;
|
|
29 | 29 |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasMouseCursors); // We can honor GetMouseCursor() values (optional)
|
30 | 30 |
io.*.BackendFlags |= @enumToInt(c.ImGuiBackendFlags_HasSetMousePos); // We can honor io.WantSetMousePos requests (optional, rarely used)
|
31 | 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) {
|
|
32 |
if (@hasField(c, "GLFW_HAS_GLFW_HOVERED") and builtin.os == builtin.Os.windows) {
|
33 | 33 |
io.*.BackendFlags |= @enumToInt(ImGuiBackendFlags_HasMouseHoveredViewport); // We can set io.MouseHoveredViewport correctly (optional, not easy)
|
34 | 34 |
}
|
35 | |
io.*.BackendPlatformName = c"imgui_impl_glfw";
|
|
35 |
io.*.BackendPlatformName = c"imgui_impl_glfw.zig";
|
36 | 36 |
|
37 | 37 |
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
38 | 38 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Tab)] = c.GLFW_KEY_TAB;
|
|
58 | 58 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Y)] = c.GLFW_KEY_Y;
|
59 | 59 |
io.*.KeyMap[@enumToInt(c.ImGuiKey_Z)] = c.GLFW_KEY_Z;
|
60 | 60 |
|
|
61 |
// @TODO: Clipboard
|
61 | 62 |
// io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
|
62 | 63 |
// io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
|
63 | 64 |
// io.ClipboardUserData = g_Window;
|
|
76 | 77 |
g_PrevUserCallbackScroll = null;
|
77 | 78 |
g_PrevUserCallbackKey = null;
|
78 | 79 |
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);
|
|
80 |
if (install_callbacks) {
|
|
81 |
g_PrevUserCallbackMousebutton = c.glfwSetMouseButtonCallback(window, Callback_MouseButton);
|
|
82 |
g_PrevUserCallbackScroll = c.glfwSetScrollCallback(window, Callback_Scroll);
|
|
83 |
g_PrevUserCallbackKey = c.glfwSetKeyCallback(window, Callback_Key);
|
|
84 |
g_PrevUserCallbackChar = c.glfwSetCharCallback(window, Callback_Char);
|
84 | 85 |
}
|
85 | 86 |
|
86 | 87 |
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
|
90 | 91 |
main_viewport.*.PlatformHandleRaw = c.glfwGetWin32Window(g_Window);
|
91 | 92 |
}
|
92 | 93 |
|
|
94 |
// @TODO: Platform Interface (Viewport)
|
93 | 95 |
// if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable != 0)
|
94 | 96 |
// ImGui_ImplGlfw_InitPlatformInterface();
|
95 | 97 |
|
|
97 | 99 |
}
|
98 | 100 |
|
99 | 101 |
pub fn Shutdown() void {
|
|
102 |
// @TODO: Platform Interface (Viewport)
|
100 | 103 |
// ImGui_ImplGlfw_ShutdownPlatformInterface();
|
|
104 |
|
101 | 105 |
for (g_MouseCursors) |*cursor| {
|
102 | 106 |
c.glfwDestroyCursor(cursor.*);
|
103 | 107 |
cursor.* = null;
|
104 | 108 |
}
|
105 | 109 |
g_ClientApi = .Unknown;
|
106 | 110 |
}
|
|
111 |
|
|
112 |
pub fn NewFrame() void {
|
|
113 |
const io = c.igGetIO();
|
|
114 |
// @TODO: assert font atlas
|
|
115 |
|
|
116 |
var w : c_int = undefined;
|
|
117 |
var h : c_int = undefined;
|
|
118 |
var display_w : c_int = undefined;
|
|
119 |
var display_h : c_int = undefined;
|
|
120 |
c.glfwGetWindowSize(g_Window, &w, &h);
|
|
121 |
c.glfwGetFramebufferSize(g_Window, &display_w, &display_h);
|
|
122 |
io.*.DisplaySize = c.ImVec2{ .x = @intToFloat(f32, w), .y = @intToFloat(f32, h) };
|
|
123 |
}
|
|
124 |
|
|
125 |
// GLFW Callbacks
|
|
126 |
extern fn Callback_MouseButton(window: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) void {
|
|
127 |
// @TODO: delegate up
|
|
128 |
|
|
129 |
if (button < 0)
|
|
130 |
return;
|
|
131 |
|
|
132 |
const button_u = @intCast(usize, button);
|
|
133 |
if (action == c.GLFW_PRESS and button_u < g_MouseJustPressed.len)
|
|
134 |
g_MouseJustPressed[button_u] = true;
|
|
135 |
}
|
|
136 |
|
|
137 |
extern fn Callback_Scroll(window: ?*c.GLFWwindow, dx: f64, dy: f64) void {
|
|
138 |
// @TODO: delegate up
|
|
139 |
|
|
140 |
const io = c.igGetIO();
|
|
141 |
io.*.MouseWheelH += @floatCast(f32, dx);
|
|
142 |
io.*.MouseWheel += @floatCast(f32, dy);
|
|
143 |
}
|
|
144 |
|
|
145 |
extern fn Callback_Key(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, modifiers: c_int) void {
|
|
146 |
// @TODO: delegate up
|
|
147 |
|
|
148 |
if (key < 0)
|
|
149 |
unreachable;
|
|
150 |
|
|
151 |
const key_u = @intCast(usize, key);
|
|
152 |
|
|
153 |
const io = c.igGetIO();
|
|
154 |
if (action == c.GLFW_PRESS)
|
|
155 |
io.*.KeysDown[key_u] = true;
|
|
156 |
if (action == c.GLFW_RELEASE)
|
|
157 |
io.*.KeysDown[key_u] = false;
|
|
158 |
|
|
159 |
// Modifiers are not reliable across systems
|
|
160 |
io.*.KeyCtrl = io.*.KeysDown[c.GLFW_KEY_LEFT_CONTROL] or io.*.KeysDown[c.GLFW_KEY_RIGHT_CONTROL];
|
|
161 |
io.*.KeyShift = io.*.KeysDown[c.GLFW_KEY_LEFT_SHIFT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SHIFT];
|
|
162 |
io.*.KeyAlt = io.*.KeysDown[c.GLFW_KEY_LEFT_ALT] or io.*.KeysDown[c.GLFW_KEY_RIGHT_ALT];
|
|
163 |
io.*.KeySuper = io.*.KeysDown[c.GLFW_KEY_LEFT_SUPER] or io.*.KeysDown[c.GLFW_KEY_RIGHT_SUPER];
|
|
164 |
}
|
|
165 |
|
|
166 |
extern fn Callback_Char(window: ?*c.GLFWwindow, char: c_uint) void {
|
|
167 |
// @TODO: delegate up
|
|
168 |
|
|
169 |
const io = c.igGetIO();
|
|
170 |
c.ImGuiIO_AddInputCharacter(io, char);
|
|
171 |
}
|