wilhelm_renderer_imgui 0.11.0

Dear ImGui integration for wilhelm_renderer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "imgui_wrapper.h"
#include "imgui/imgui.h"
#include "imgui/backends/imgui_impl_glfw.h"
#include "imgui/backends/imgui_impl_opengl3.h"
#include <GLFW/glfw3.h>
#include <math.h>

extern "C" {

// Context management
void* imgui_create_context(void) {
    return ImGui::CreateContext();
}

void imgui_destroy_context(void* ctx) {
    ImGui::DestroyContext(static_cast<ImGuiContext*>(ctx));
}

// Backend initialization/shutdown
int imgui_init_for_glfw(GLFWwindow* window, int install_callbacks) {
    return ImGui_ImplGlfw_InitForOpenGL(window, install_callbacks != 0) ? 1 : 0;
}

int imgui_init_for_opengl3(const char* glsl_version) {
    return ImGui_ImplOpenGL3_Init(glsl_version) ? 1 : 0;
}

void imgui_shutdown_opengl3(void) {
    ImGui_ImplOpenGL3_Shutdown();
}

void imgui_shutdown_glfw(void) {
    ImGui_ImplGlfw_Shutdown();
}

// Frame management
void imgui_new_frame(void) {
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();
}

void imgui_render(void) {
    ImGui::Render();
}

void imgui_end_frame(void) {
    ImGui::EndFrame();
}

// OpenGL3 backend rendering
void imgui_opengl3_render_draw_data(void) {
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

// IO access
void imgui_io_set_display_size(float width, float height) {
    ImGuiIO& io = ImGui::GetIO();
    io.DisplaySize = ImVec2(width, height);
}

int imgui_io_want_capture_mouse(void) {
    return ImGui::GetIO().WantCaptureMouse ? 1 : 0;
}

int imgui_io_want_capture_keyboard(void) {
    return ImGui::GetIO().WantCaptureKeyboard ? 1 : 0;
}

void imgui_io_set_config_flags(int flags) {
    ImGui::GetIO().ConfigFlags |= flags;
}

// Basic widgets
int imgui_begin(const char* name, int* p_open, int flags) {
    bool* open_ptr = nullptr;
    bool open_val;
    if (p_open) {
        open_val = (*p_open != 0);
        open_ptr = &open_val;
    }
    bool result = ImGui::Begin(name, open_ptr, static_cast<ImGuiWindowFlags>(flags));
    if (p_open && open_ptr) {
        *p_open = open_val ? 1 : 0;
    }
    return result ? 1 : 0;
}

void imgui_end(void) {
    ImGui::End();
}

void imgui_text(const char* text) {
    ImGui::TextUnformatted(text);
}

int imgui_button(const char* label) {
    return ImGui::Button(label) ? 1 : 0;
}

int imgui_checkbox(const char* label, int* v) {
    bool val = (*v != 0);
    bool result = ImGui::Checkbox(label, &val);
    *v = val ? 1 : 0;
    return result ? 1 : 0;
}

int imgui_slider_float(const char* label, float* v, float v_min, float v_max) {
    return ImGui::SliderFloat(label, v, v_min, v_max) ? 1 : 0;
}

int imgui_slider_int(const char* label, int* v, int v_min, int v_max) {
    return ImGui::SliderInt(label, v, v_min, v_max) ? 1 : 0;
}

int imgui_input_float(const char* label, float* v) {
    return ImGui::InputFloat(label, v) ? 1 : 0;
}

int imgui_input_int(const char* label, int* v) {
    return ImGui::InputInt(label, v) ? 1 : 0;
}

int imgui_input_text(const char* label, char* buf, unsigned long buf_size, int flags) {
    return ImGui::InputText(label, buf, static_cast<size_t>(buf_size),
                            static_cast<ImGuiInputTextFlags>(flags)) ? 1 : 0;
}

int imgui_color_edit3(const char* label, float col[3]) {
    return ImGui::ColorEdit3(label, col) ? 1 : 0;
}

int imgui_color_edit4(const char* label, float col[4]) {
    return ImGui::ColorEdit4(label, col) ? 1 : 0;
}

void imgui_same_line(void) {
    ImGui::SameLine();
}

void imgui_separator(void) {
    ImGui::Separator();
}

void imgui_separator_text(const char* label) {
    ImGui::SeparatorText(label);
}

void imgui_spacing(void) {
    ImGui::Spacing();
}

void imgui_dummy(float width, float height) {
    ImGui::Dummy(ImVec2(width, height));
}

void imgui_indent(float indent_w) {
    ImGui::Indent(indent_w);
}

void imgui_unindent(float indent_w) {
    ImGui::Unindent(indent_w);
}

// Tree nodes
int imgui_tree_node(const char* label) {
    return ImGui::TreeNode(label) ? 1 : 0;
}

void imgui_tree_pop(void) {
    ImGui::TreePop();
}

// Combo box
int imgui_begin_combo(const char* label, const char* preview_value, int flags) {
    return ImGui::BeginCombo(label, preview_value, static_cast<ImGuiComboFlags>(flags)) ? 1 : 0;
}

void imgui_end_combo(void) {
    ImGui::EndCombo();
}

int imgui_selectable(const char* label, int selected, int flags) {
    return ImGui::Selectable(label, selected != 0, static_cast<ImGuiSelectableFlags>(flags)) ? 1 : 0;
}

// Menu
int imgui_begin_main_menu_bar(void) {
    return ImGui::BeginMainMenuBar() ? 1 : 0;
}

void imgui_end_main_menu_bar(void) {
    ImGui::EndMainMenuBar();
}

int imgui_begin_menu(const char* label, int enabled) {
    return ImGui::BeginMenu(label, enabled != 0) ? 1 : 0;
}

void imgui_end_menu(void) {
    ImGui::EndMenu();
}

int imgui_menu_item(const char* label, const char* shortcut, int selected, int enabled) {
    return ImGui::MenuItem(label, shortcut, selected != 0, enabled != 0) ? 1 : 0;
}

// Tooltips
void imgui_set_tooltip(const char* text) {
    ImGui::SetTooltip("%s", text);
}

int imgui_begin_tooltip(void) {
    return ImGui::BeginTooltip() ? 1 : 0;
}

void imgui_end_tooltip(void) {
    ImGui::EndTooltip();
}

// Popups
int imgui_begin_popup(const char* str_id, int flags) {
    return ImGui::BeginPopup(str_id, static_cast<ImGuiWindowFlags>(flags)) ? 1 : 0;
}

int imgui_begin_popup_modal(const char* name, int* p_open, int flags) {
    bool* open_ptr = nullptr;
    bool open_val;
    if (p_open) {
        open_val = (*p_open != 0);
        open_ptr = &open_val;
    }
    bool result = ImGui::BeginPopupModal(name, open_ptr, static_cast<ImGuiWindowFlags>(flags));
    if (p_open && open_ptr) {
        *p_open = open_val ? 1 : 0;
    }
    return result ? 1 : 0;
}

void imgui_end_popup(void) {
    ImGui::EndPopup();
}

void imgui_open_popup(const char* str_id) {
    ImGui::OpenPopup(str_id);
}

void imgui_close_current_popup(void) {
    ImGui::CloseCurrentPopup();
}

// Tables
int imgui_begin_table(const char* str_id, int column, int flags) {
    return ImGui::BeginTable(str_id, column, static_cast<ImGuiTableFlags>(flags)) ? 1 : 0;
}

void imgui_end_table(void) {
    ImGui::EndTable();
}

void imgui_table_next_row(void) {
    ImGui::TableNextRow();
}

int imgui_table_next_column(void) {
    return ImGui::TableNextColumn() ? 1 : 0;
}

int imgui_table_set_column_index(int column_n) {
    return ImGui::TableSetColumnIndex(column_n) ? 1 : 0;
}

void imgui_table_setup_column(const char* label, int flags, float init_width_or_weight) {
    ImGui::TableSetupColumn(label, static_cast<ImGuiTableColumnFlags>(flags), init_width_or_weight);
}

void imgui_table_headers_row(void) {
    ImGui::TableHeadersRow();
}

// Columns (legacy)
void imgui_columns(int count, const char* id, int border) {
    ImGui::Columns(count, id, border != 0);
}

void imgui_next_column(void) {
    ImGui::NextColumn();
}

// Style
void imgui_push_style_color(int idx, float r, float g, float b, float a) {
    ImGui::PushStyleColor(static_cast<ImGuiCol>(idx), ImVec4(r, g, b, a));
}

void imgui_pop_style_color(int count) {
    ImGui::PopStyleColor(count);
}

void imgui_push_style_var_float(int idx, float val) {
    ImGui::PushStyleVar(static_cast<ImGuiStyleVar>(idx), val);
}

void imgui_push_style_var_vec2(int idx, float x, float y) {
    ImGui::PushStyleVar(static_cast<ImGuiStyleVar>(idx), ImVec2(x, y));
}

void imgui_pop_style_var(int count) {
    ImGui::PopStyleVar(count);
}

// ID stack
void imgui_push_id_int(int int_id) {
    ImGui::PushID(int_id);
}

void imgui_push_id_str(const char* str_id) {
    ImGui::PushID(str_id);
}

void imgui_pop_id(void) {
    ImGui::PopID();
}

// Utilities
int imgui_is_item_hovered(void) {
    return ImGui::IsItemHovered() ? 1 : 0;
}

int imgui_is_item_clicked(int mouse_button) {
    return ImGui::IsItemClicked(static_cast<ImGuiMouseButton>(mouse_button)) ? 1 : 0;
}

int imgui_is_item_active(void) {
    return ImGui::IsItemActive() ? 1 : 0;
}

void imgui_set_next_window_pos(float x, float y, int cond) {
    ImGui::SetNextWindowPos(ImVec2(x, y), static_cast<ImGuiCond>(cond));
}

void imgui_set_next_window_size(float width, float height, int cond) {
    ImGui::SetNextWindowSize(ImVec2(width, height), static_cast<ImGuiCond>(cond));
}

// Demo window
void imgui_show_demo_window(int* p_open) {
    bool* open_ptr = nullptr;
    bool open_val;
    if (p_open) {
        open_val = (*p_open != 0);
        open_ptr = &open_val;
    }
    ImGui::ShowDemoWindow(open_ptr);
    if (p_open && open_ptr) {
        *p_open = open_val ? 1 : 0;
    }
}

// DPI scaling for Windows high-DPI displays
float imgui_get_dpi_scale(GLFWwindow* window) {
    float x_scale, y_scale;
    glfwGetWindowContentScale(window, &x_scale, &y_scale);
    return x_scale; // x and y are typically the same on Windows
}

void imgui_apply_dpi_scale(GLFWwindow* window) {
    float scale = imgui_get_dpi_scale(window);
    if (scale <= 0.0f) scale = 1.0f;

    // Scale all style sizes
    ImGuiStyle& style = ImGui::GetStyle();
    style.ScaleAllSizes(scale);

    // Rebuild font atlas with scaled font size
    ImGuiIO& io = ImGui::GetIO();
    io.Fonts->Clear();

    // Load default font at scaled size (13.0f is ImGui's default font size)
    float font_size = 13.0f * scale;
    io.Fonts->AddFontDefault(nullptr);

    // Scale the default font
    ImFontConfig config;
    config.SizePixels = font_size;
    config.OversampleH = 1;
    config.OversampleV = 1;
    config.PixelSnapH = true;
    io.Fonts->Clear();
    io.Fonts->AddFontDefault(&config);
    io.Fonts->Build();

    // Set global font scale to 1.0 since we've already scaled the font
    io.FontGlobalScale = 1.0f;
}

// Explicit UI scale. Unlike imgui_apply_dpi_scale, takes the factor from the
// caller instead of GLFW's content scale. Idempotent: resets the style to
// defaults before scaling (ScaleAllSizes is cumulative), so repeated calls
// don't compound. Safe before the first NewFrame and between frames.
void imgui_set_ui_scale(float scale) {
    if (ImGui::GetCurrentContext() == nullptr) return;
    if (!(scale > 0.0f)) scale = 1.0f;  // also rejects NaN

    ImGuiStyle& style = ImGui::GetStyle();
    style = ImGuiStyle();  // reset: ScaleAllSizes compounds otherwise
    style.ScaleAllSizes(scale);

    ImGuiIO& io = ImGui::GetIO();
    io.Fonts->Clear();
    ImFontConfig config;
    config.SizePixels = roundf(13.0f * scale);  // 13 px is ImGui's default
    config.OversampleH = 1;
    config.OversampleV = 1;
    config.PixelSnapH = true;
    io.Fonts->AddFontDefault(&config);
    io.Fonts->Build();
    io.FontGlobalScale = 1.0f;

    // Invalidate the GL backend's copy of the atlas; the next
    // ImGui_ImplOpenGL3_NewFrame() recreates it (no-op before the first
    // frame). Deliberately NOT calling CreateFontsTexture here: before the
    // first frame, CreateDeviceObjects() would create a second texture and
    // leak the first.
    if (io.BackendRendererUserData != nullptr)
        ImGui_ImplOpenGL3_DestroyFontsTexture();
}

} // extern "C"