galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
ImGuiHelpers.cpp
Go to the documentation of this file.
1
7
8#include <gl2d.h>
9#include <glad/glad.h>
10
11#include <entt/locator/locator.hpp>
12#include <imgui/backends/imgui_impl_opengl3.h>
13#include <imgui/backends/imgui_impl_sdl3.h>
14#include <imgui/misc/freetype/imgui_freetype.h>
15#include <imgui/notify/ImGuiNotify.hpp>
16#include <mimalloc.h>
17
24
25#include "ImGuiHelpers.hpp"
26
27// #ifdef GALAXY_WIN_PLATFORM
28// #pragma warning(push)
29// #pragma warning(disable : 4312)
30// #endif
31
32namespace galaxy
33{
34 ImGuiStyle g_Style;
35
36 void* mem_alloc(size_t sz, void* user_data)
37 {
38 return mi_malloc(sz);
39 }
40
41 void mem_free(void* ptr, void* user_data)
42 {
43 mi_free(ptr);
44 }
45
46 namespace ui
47 {
48 void imgui_init(const char* ini)
49 {
50 auto& window = entt::locator<Window>::value();
51
52 // clang-format off
53 IMGUI_CHECKVERSION();
54 ImGui::SetAllocatorFunctions(mem_alloc, mem_free, nullptr);
55 ImGuiFreeType::SetAllocatorFunctions(mem_alloc, mem_free, nullptr);
56 ImGui::CreateContext();
57 ImGuiIO& io = ImGui::GetIO(); (void)io;
58 // clang-format on
59
60 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
61 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
62 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
63
64 io.ConfigInputTextCursorBlink = true;
65 io.ConfigDockingWithShift = true;
66 // io.ConfigDockingAlwaysTabBar = true;
67 io.ConfigWindowsMoveFromTitleBarOnly = true;
68 io.ConfigWindowsResizeFromEdges = true;
69 io.MouseDrawCursor = false;
70 io.ConfigDpiScaleFonts = true;
71 io.ConfigDpiScaleViewports = true;
72 io.FontAllowUserScaling = true;
73 io.IniFilename = ini;
74 io.ConfigColorEditFlags =
75 ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_PickerHueBar | ImGuiColorEditFlags_AlphaBar;
76
77 ImGui_ImplSDL3_InitForOpenGL(window.handle(), window.context());
78 ImGui_ImplOpenGL3_Init("#version 460 core");
79
80 ImFontConfig font_cfg = {};
81 font_cfg.FontDataOwnedByAtlas = false;
82 font_cfg.RasterizerMultiply = 1.5f;
83 font_cfg.OversampleH = 1;
84 font_cfg.OversampleV = 1;
85 font_cfg.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_LoadColor;
86 io.FontDefault = io.Fonts->AddFontFromMemoryTTF(&resource::roboto_light, resource::roboto_light_len, 20.0f, &font_cfg);
87
88 static const ImWchar icons_ranges[] = {ICON_MIN_MDI, ICON_MAX_MDI, 0};
89 ImFontConfig md_icons_cfg;
90 md_icons_cfg.FontDataOwnedByAtlas = false;
91 md_icons_cfg.MergeMode = true;
92 md_icons_cfg.PixelSnapH = true;
93 io.Fonts->AddFontFromMemoryTTF(&materialdesignicons_ttf, sizeof(materialdesignicons_ttf), 20.0f, &md_icons_cfg, icons_ranges);
94
95 // Preserve initial copy.
96 g_Style = ImGui::GetStyle();
97
98 // Then set theme.
99 imgui_set_theme(window.get_display_scale());
100 }
101
102 void imgui_set_theme(const float scale) noexcept
103 {
104 if (ImGui::GetCurrentContext())
105 {
106 // Copy back over.
107 ImGui::GetCurrentContext()->Style = g_Style;
108
109 ImGuiStyle& style = ImGui::GetStyle();
110 ImVec4* colors = style.Colors;
111
112 style.WindowPadding = ImVec2(10.0f, 10.0f);
113 style.FramePadding = ImVec2(6.0f, 4.0f);
114 style.ItemSpacing = ImVec2(8.0f, 6.0f);
115 style.ScrollbarSize = 14.0f;
116 style.GrabMinSize = 12.0f;
117 style.WindowRounding = 6.0f;
118 style.FrameRounding = 4.0f;
119 style.PopupRounding = 4.0f;
120 style.ScrollbarRounding = 12.0f;
121 style.GrabRounding = 4.0f;
122 style.TabRounding = 4.0f;
123 style.WindowBorderSize = 1.0f;
124 style.FrameBorderSize = 1.0f;
125
126 colors[ImGuiCol_Text] = ImVec4(0.97f, 0.97f, 0.95f, 1.00f);
127 colors[ImGuiCol_TextDisabled] = ImVec4(0.38f, 0.45f, 0.64f, 1.00f);
128 colors[ImGuiCol_WindowBg] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
129 colors[ImGuiCol_ChildBg] = ImVec4(0.16f, 0.16f, 0.21f, 0.00f);
130 colors[ImGuiCol_PopupBg] = ImVec4(0.16f, 0.16f, 0.21f, 0.96f);
131 colors[ImGuiCol_Border] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
132 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
133 colors[ImGuiCol_FrameBg] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
134 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.38f, 0.45f, 0.64f, 1.00f);
135 colors[ImGuiCol_FrameBgActive] = ImVec4(0.48f, 0.55f, 0.74f, 1.00f);
136 colors[ImGuiCol_TitleBg] = ImVec4(0.13f, 0.14f, 0.18f, 1.00f);
137 colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
138 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.13f, 0.14f, 0.18f, 1.00f);
139 colors[ImGuiCol_MenuBarBg] = ImVec4(0.13f, 0.14f, 0.18f, 1.00f);
140 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
141 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
142 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.38f, 0.45f, 0.64f, 1.00f);
143 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.48f, 0.55f, 0.74f, 1.00f);
144 colors[ImGuiCol_CheckMark] = ImVec4(0.31f, 0.98f, 0.48f, 1.00f);
145 colors[ImGuiCol_SliderGrab] = ImVec4(0.74f, 0.58f, 0.98f, 1.00f);
146 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.84f, 0.68f, 1.00f, 1.00f);
147 colors[ImGuiCol_Button] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
148 colors[ImGuiCol_ButtonHovered] = ImVec4(1.00f, 0.47f, 0.78f, 1.00f);
149 colors[ImGuiCol_ButtonActive] = ImVec4(0.80f, 0.37f, 0.62f, 1.00f);
150 colors[ImGuiCol_Header] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
151 colors[ImGuiCol_HeaderHovered] = ImVec4(0.38f, 0.45f, 0.64f, 1.00f);
152 colors[ImGuiCol_HeaderActive] = ImVec4(0.48f, 0.55f, 0.74f, 1.00f);
153 colors[ImGuiCol_Tab] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
154 colors[ImGuiCol_TabHovered] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
155 colors[ImGuiCol_TabSelected] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
156 colors[ImGuiCol_TabDimmed] = ImVec4(0.13f, 0.14f, 0.18f, 1.00f);
157 colors[ImGuiCol_TabDimmedSelected] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
158 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
159 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.38f, 0.45f, 0.64f, 1.00f);
160 colors[ImGuiCol_TableBorderLight] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
161 colors[ImGuiCol_PlotLines] = ImVec4(0.55f, 0.91f, 0.99f, 1.00f);
162 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.27f, 0.28f, 0.35f, 1.00f);
163 colors[ImGuiCol_NavCursor] = ImVec4(0.74f, 0.58f, 0.98f, 1.00f);
164 colors[ImGuiCol_DockingPreview] = ImVec4(0.74f, 0.58f, 0.98f, 0.50f);
165 colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.16f, 0.16f, 0.21f, 1.00f);
166
167 style.FontSizeBase = 17.0f;
168 style.FontScaleDpi = scale;
169 style.ScaleAllSizes(scale);
170 }
171 }
172
173 void imgui_handle_events(const SDL_Event& events)
174 {
175 if (ImGui::GetCurrentContext())
176 {
177 ImGui_ImplSDL3_ProcessEvent(&events);
178 }
179 }
180
182 {
183 if (ImGui::GetCurrentContext())
184 {
185 ImGui_ImplOpenGL3_NewFrame();
186 ImGui_ImplSDL3_NewFrame();
187 ImGui::NewFrame();
188 }
189 }
190
192 {
193 if (ImGui::GetCurrentContext())
194 {
195 sl::detail::gl2d_flush();
196 ImGui::Render();
197 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
198 }
199 }
200
202 {
203 if (ImGui::GetCurrentContext())
204 {
205 ImGui_ImplOpenGL3_Shutdown();
206 ImGui_ImplSDL3_Shutdown();
207 ImGui::DestroyContext();
208 }
209 }
210
211 void imgui_notify_success(const char* msg)
212 {
213 ImGui::InsertNotification({ImGuiToastType::Warning, 2000, msg});
214 }
215
216 void imgui_notify_info(const char* msg)
217 {
218 ImGui::InsertNotification({ImGuiToastType::Info, 2000, msg});
219 }
220
221 void imgui_notify_warning(const char* msg)
222 {
223 ImGui::InsertNotification({ImGuiToastType::Warning, 2000, msg});
224 }
225
226 void imgui_notify_error(const char* msg)
227 {
228 ImGui::InsertNotification({ImGuiToastType::Error, 2000, msg});
229 }
230
232 {
233 const auto center = ImGui::GetMainViewport()->GetCenter();
234 ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, {0.5f, 0.5f});
235 }
236
237 void imgui_confirm(const char* msg, std::move_only_function<void(void)>&& yes, std::move_only_function<void(void)>&& no)
238 {
239 ImGui::Text(msg);
240
241 ImGui::Separator();
242 ImGui::Spacing();
243
244 if (ImGui::Button("Yes"))
245 {
246 if (yes)
247 {
248 yes();
249 }
250
251 ImGui::CloseCurrentPopup();
252 }
253
254 ImGui::SameLine();
255
256 if (ImGui::Button("No"))
257 {
258 if (no)
259 {
260 no();
261 }
262
263 ImGui::CloseCurrentPopup();
264 }
265 }
266
267 ImGuiInputTextFlags imgui_default_flags() noexcept
268 {
269 return ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_EscapeClearsAll;
270 }
271 } // namespace ui
272} // namespace galaxy
273
274// #ifdef GALAXY_WIN_PLATFORM
275// #pragma warning(pop)
276// #endif
277
278/*
279 bool imgui_imagebutton(const graphics::Texture2D& texture, const ImVec2& size, const ImVec4& bg_col, const ImVec4& tint_col)
280 {
281 const auto id = std::to_string(texture.id()) + std::to_string(texture.width()) + std::to_string(texture.height());
282 const auto upcast = static_cast<std::uint64_t>(texture.id());
283
284 return ImGui::ImageButton(id.c_str(), reinterpret_cast<void*>(upcast), size, {0, 1}, {1, 0}, bg_col, tint_col);
285 }
286
287 void imgui_image(const graphics::Texture2D& texture, const ImVec2& size)
288 {
289 const auto upcast = static_cast<std::uint64_t>(texture.id());
290 ImGui::Image(reinterpret_cast<void*>(upcast), size, {0, 1}, {1, 0});
291 }
292
293 void imgui_image(const graphics::RenderTexture& texture, const ImVec2& size)
294 {
295 const auto upcast = static_cast<std::uint64_t>(texture.texture());
296 ImGui::Image(reinterpret_cast<void*>(upcast), size, {0, 1}, {1, 0});
297 }
298
299
300
301 bool imgui_glm_vec2(const char* label, glm::vec2& vec)
302 {
303 auto clicked = false;
304
305 ImGui::PushID(label);
306
307 ImGui::TextUnformatted(label);
308
309 ImGui::SameLine();
310
311 ImGui::SetNextItemWidth(150);
312 clicked |= ImGui::InputFloat("X", &vec.x, 1.0f, 10.0f, "%.1f");
313
314 ImGui::SameLine();
315
316 ImGui::SetNextItemWidth(150);
317 clicked |= ImGui::InputFloat("Y", &vec.y, 1.0f, 10.0f, "%.1f");
318
319 ImGui::PopID();
320 return clicked;
321 }
322
323 bool imgui_glm_vec3(const char* label, glm::vec3& vec)
324 {
325 auto clicked = false;
326
327 ImGui::PushID(label);
328
329 ImGui::TextUnformatted(label);
330
331 ImGui::SameLine();
332
333 ImGui::SetNextItemWidth(100);
334 clicked |= ImGui::InputFloat("X", &vec.x, 1.0f, 10.0f, "%.1f");
335
336 ImGui::SameLine();
337
338 ImGui::SetNextItemWidth(100);
339 clicked |= ImGui::InputFloat("Y", &vec.y, 1.0f, 10.0f, "%.1f");
340
341 ImGui::SameLine();
342
343 ImGui::SetNextItemWidth(100);
344 clicked |= ImGui::InputFloat("Z", &vec.z, 1.0f, 10.0f, "%.1f");
345
346 ImGui::PopID();
347 return clicked;
348 }
349
350 bool imgui_glm_vec4(const char* label, glm::vec4& vec)
351 {
352 auto clicked = false;
353
354 ImGui::PushID(label);
355
356 ImGui::TextUnformatted(label);
357
358 ImGui::SameLine();
359
360 ImGui::SetNextItemWidth(75);
361 clicked |= ImGui::InputFloat("X", &vec.x, 1.0f, 10.0f, "%.1f");
362
363 ImGui::SameLine();
364
365 ImGui::SetNextItemWidth(75);
366 clicked |= ImGui::InputFloat("Y", &vec.y, 1.0f, 10.0f, "%.1f");
367
368 ImGui::SameLine();
369
370 ImGui::SetNextItemWidth(75);
371 clicked |= ImGui::InputFloat("Z", &vec.z, 1.0f, 10.0f, "%.1f");
372
373 ImGui::SameLine();
374
375 ImGui::SetNextItemWidth(75);
376 clicked |= ImGui::InputFloat("W", &vec.w, 1.0f, 10.0f, "%.1f");
377
378 ImGui::PopID();
379 return clicked;
380 }
381
382 bool imgui_frect(const char* label, math::fRect& rect)
383 {
384 auto clicked = false;
385
386 ImGui::PushID(label);
387
388 ImGui::TextUnformatted(label);
389
390 ImGui::SameLine();
391
392 ImGui::SetNextItemWidth(75);
393 clicked |= ImGui::InputFloat("X", &rect.x, 1.0f, 10.0f, "%.1f");
394
395 ImGui::SameLine();
396
397 ImGui::SetNextItemWidth(75);
398 clicked |= ImGui::InputFloat("Y", &rect.y, 1.0f, 10.0f, "%.1f");
399
400 ImGui::SameLine();
401
402 ImGui::SetNextItemWidth(75);
403 clicked |= ImGui::InputFloat("W", &rect.width, 1.0f, 10.0f, "%.1f");
404
405 ImGui::SameLine();
406
407 ImGui::SetNextItemWidth(75);
408 clicked |= ImGui::InputFloat("H", &rect.height, 1.0f, 10.0f, "%.1f");
409
410 ImGui::PopID();
411 return clicked;
412 }
413*/
#define ICON_MIN_MDI
#define ICON_MAX_MDI
void imgui_notify_error(const char *msg)
ImGui error notification.
ImGuiInputTextFlags imgui_default_flags() noexcept
Get default text input flags.
void imgui_set_theme(const float scale) noexcept
Set imgui theme.
void imgui_handle_events(const SDL_Event &events)
Handle events in ImGui.
void imgui_new_frame()
Begin setting up ImGui pass.
void imgui_exit()
Destroy imgui context.
void imgui_init(const char *ini)
Initialize ImGui context with galaxy.
void imgui_center_next_window()
Make sure the next window opened is centered on the monitor.
void imgui_notify_success(const char *msg)
ImGui success notification.
void imgui_confirm(const char *msg, std::move_only_function< void(void)> &&yes, std::move_only_function< void(void)> &&no)
Handles a popup for a yes/no situation.
void imgui_notify_info(const char *msg)
ImGui info notification.
void imgui_notify_warning(const char *msg)
ImGui warning notification.
void imgui_render()
Render imgui.
Tag.cpp galaxy.
Definition Tag.cpp:13
void mem_free(void *ptr, void *user_data)
void * mem_alloc(size_t sz, void *user_data)
ImGuiStyle g_Style