Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpigui.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <cstdio> |
| 9 | #include <cstring> |
| 10 | |
| 11 | #include <GLFW/glfw3.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 12 | #include <IconsFontAwesome6.h> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 13 | #include <imgui.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 14 | #include <imgui_FontAwesomeSolid.h> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 15 | #include <imgui_ProggyDotted.h> |
| 16 | #include <imgui_impl_glfw.h> |
| 17 | #include <imgui_internal.h> |
| 18 | #include <implot.h> |
| 19 | #include <stb_image.h> |
| 20 | |
| 21 | #include "wpigui_internal.h" |
| 22 | |
| 23 | using namespace wpi::gui; |
| 24 | |
| 25 | namespace wpi { |
| 26 | |
| 27 | Context* gui::gContext; |
| 28 | |
| 29 | static void ErrorCallback(int error, const char* description) { |
| 30 | std::fprintf(stderr, "GLFW Error %d: %s\n", error, description); |
| 31 | } |
| 32 | |
| 33 | static void WindowSizeCallback(GLFWwindow* window, int width, int height) { |
| 34 | if (!gContext->maximized) { |
| 35 | gContext->width = width; |
| 36 | gContext->height = height; |
| 37 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 38 | if (!gContext->isPlatformRendering) { |
| 39 | PlatformRenderFrame(); |
| 40 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void FramebufferSizeCallback(GLFWwindow* window, int width, int height) { |
| 44 | PlatformFramebufferSizeChanged(width, height); |
| 45 | } |
| 46 | |
| 47 | static void WindowMaximizeCallback(GLFWwindow* window, int maximized) { |
| 48 | gContext->maximized = maximized; |
| 49 | } |
| 50 | |
| 51 | static void WindowPosCallback(GLFWwindow* window, int xpos, int ypos) { |
| 52 | if (!gContext->maximized) { |
| 53 | gContext->xPos = xpos; |
| 54 | gContext->yPos = ypos; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void* IniReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler, |
| 59 | const char* name) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | if (std::strcmp(name, "GLOBAL") != 0) { |
| 61 | return nullptr; |
| 62 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 63 | return static_cast<SavedSettings*>(gContext); |
| 64 | } |
| 65 | |
| 66 | static void IniReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler, |
| 67 | void* entry, const char* lineStr) { |
| 68 | auto impl = static_cast<SavedSettings*>(entry); |
| 69 | const char* value = std::strchr(lineStr, '='); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 70 | if (!value) { |
| 71 | return; |
| 72 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 73 | ++value; |
| 74 | int num = std::atoi(value); |
| 75 | if (std::strncmp(lineStr, "width=", 6) == 0) { |
| 76 | impl->width = num; |
| 77 | impl->loadedWidthHeight = true; |
| 78 | } else if (std::strncmp(lineStr, "height=", 7) == 0) { |
| 79 | impl->height = num; |
| 80 | impl->loadedWidthHeight = true; |
| 81 | } else if (std::strncmp(lineStr, "maximized=", 10) == 0) { |
| 82 | impl->maximized = num; |
| 83 | } else if (std::strncmp(lineStr, "xpos=", 5) == 0) { |
| 84 | impl->xPos = num; |
| 85 | } else if (std::strncmp(lineStr, "ypos=", 5) == 0) { |
| 86 | impl->yPos = num; |
| 87 | } else if (std::strncmp(lineStr, "userScale=", 10) == 0) { |
| 88 | impl->userScale = num; |
| 89 | } else if (std::strncmp(lineStr, "style=", 6) == 0) { |
| 90 | impl->style = num; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void IniWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, |
| 95 | ImGuiTextBuffer* out_buf) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 96 | if (!gContext) { |
| 97 | return; |
| 98 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 99 | out_buf->appendf( |
| 100 | "[MainWindow][GLOBAL]\nwidth=%d\nheight=%d\nmaximized=%d\n" |
| 101 | "xpos=%d\nypos=%d\nuserScale=%d\nstyle=%d\n\n", |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 102 | gContext->width, gContext->height, gContext->maximized ? 1 : 0, |
| 103 | gContext->xPos, gContext->yPos, gContext->userScale, gContext->style); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void gui::CreateContext() { |
| 107 | gContext = new Context; |
| 108 | AddFont("ProggyDotted", [](ImGuiIO& io, float size, const ImFontConfig* cfg) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 109 | auto font = ImGui::AddFontProggyDotted(io, size, cfg); |
| 110 | static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_16_FA, 0}; |
| 111 | ImFontConfig icons_cfg; |
| 112 | icons_cfg.MergeMode = true; |
| 113 | icons_cfg.PixelSnapH = true; |
| 114 | ImGui::AddFontFontAwesomeSolid(io, size, &icons_cfg, icons_ranges); |
| 115 | return font; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 116 | }); |
| 117 | PlatformCreateContext(); |
| 118 | } |
| 119 | |
| 120 | void gui::DestroyContext() { |
| 121 | PlatformDestroyContext(); |
| 122 | delete gContext; |
| 123 | gContext = nullptr; |
| 124 | } |
| 125 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 126 | static void UpdateFontScale() { |
| 127 | // Scale based on OS window content scaling |
| 128 | float windowScale = 1.0; |
| 129 | #ifndef __APPLE__ |
| 130 | glfwGetWindowContentScale(gContext->window, &windowScale, nullptr); |
| 131 | #endif |
| 132 | // map to closest font size: 0 = 0.5x, 1 = 0.75x, 2 = 1.0x, 3 = 1.25x, |
| 133 | // 4 = 1.5x, 5 = 1.75x, 6 = 2x |
| 134 | int fontScale = |
| 135 | gContext->userScale + static_cast<int>((windowScale - 1.0) * 4); |
| 136 | if (fontScale < 0) { |
| 137 | fontScale = 0; |
| 138 | } |
| 139 | if (gContext->fontScale != fontScale) { |
| 140 | gContext->reloadFonts = true; |
| 141 | gContext->fontScale = fontScale; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // the range is based on 13px being the "nominal" 100% size and going from |
| 146 | // ~0.5x (7px) to ~2.0x (25px) |
| 147 | static void ReloadFonts() { |
| 148 | auto& io = ImGui::GetIO(); |
| 149 | io.Fonts->Clear(); |
| 150 | gContext->fonts.clear(); |
| 151 | float size = 7.0f + gContext->fontScale * 3.0f; |
| 152 | bool first = true; |
| 153 | for (auto&& makeFont : gContext->makeFonts) { |
| 154 | if (makeFont.second) { |
| 155 | ImFontConfig cfg; |
| 156 | std::snprintf(cfg.Name, sizeof(cfg.Name), "%s", makeFont.first); |
| 157 | ImFont* font = makeFont.second(io, size, &cfg); |
| 158 | if (first) { |
| 159 | ImGui::GetIO().FontDefault = font; |
| 160 | first = false; |
| 161 | } |
| 162 | gContext->fonts.emplace_back(font); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool gui::Initialize(const char* title, int width, int height, |
| 168 | ImGuiConfigFlags configFlags) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 169 | gContext->title = title; |
| 170 | gContext->width = width; |
| 171 | gContext->height = height; |
| 172 | gContext->defaultWidth = width; |
| 173 | gContext->defaultHeight = height; |
| 174 | |
| 175 | // Setup window |
| 176 | glfwSetErrorCallback(ErrorCallback); |
| 177 | glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 178 | glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 179 | PlatformGlfwInitHints(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 180 | if (!glfwInit()) { |
| 181 | return false; |
| 182 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 183 | |
| 184 | PlatformGlfwWindowHints(); |
| 185 | |
| 186 | // Setup Dear ImGui context |
| 187 | IMGUI_CHECKVERSION(); |
| 188 | ImGui::CreateContext(); |
| 189 | ImPlot::CreateContext(); |
| 190 | ImGuiIO& io = ImGui::GetIO(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 191 | io.ConfigFlags |= configFlags; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 192 | |
| 193 | // Hook ini handler to save settings |
| 194 | ImGuiSettingsHandler iniHandler; |
| 195 | iniHandler.TypeName = "MainWindow"; |
| 196 | iniHandler.TypeHash = ImHashStr(iniHandler.TypeName); |
| 197 | iniHandler.ReadOpenFn = IniReadOpen; |
| 198 | iniHandler.ReadLineFn = IniReadLine; |
| 199 | iniHandler.WriteAllFn = IniWriteAll; |
| 200 | ImGui::GetCurrentContext()->SettingsHandlers.push_back(iniHandler); |
| 201 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 202 | if (gContext->loadSettings) { |
| 203 | gContext->loadSettings(); |
| 204 | io.IniFilename = nullptr; |
| 205 | } else { |
| 206 | io.IniFilename = gContext->iniPath.c_str(); |
| 207 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 208 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 209 | for (auto&& initialize : gContext->initializers) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 210 | if (initialize) { |
| 211 | initialize(); |
| 212 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Load INI file |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 216 | if (gContext->loadIniSettings) { |
| 217 | gContext->loadIniSettings(); |
| 218 | } else if (io.IniFilename) { |
| 219 | ImGui::LoadIniSettingsFromDisk(io.IniFilename); |
| 220 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 221 | |
| 222 | // Set initial window settings |
| 223 | glfwWindowHint(GLFW_MAXIMIZED, gContext->maximized ? GLFW_TRUE : GLFW_FALSE); |
| 224 | |
| 225 | if (gContext->width == 0 || gContext->height == 0) { |
| 226 | gContext->width = gContext->defaultWidth; |
| 227 | gContext->height = gContext->defaultHeight; |
| 228 | gContext->loadedWidthHeight = false; |
| 229 | } |
| 230 | |
| 231 | float windowScale = 1.0; |
| 232 | if (!gContext->loadedWidthHeight) { |
| 233 | glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); |
| 234 | // get the primary monitor work area to see if we have a reasonable initial |
| 235 | // window size; if not, maximize, and default scaling to smaller |
| 236 | if (GLFWmonitor* primary = glfwGetPrimaryMonitor()) { |
| 237 | int monWidth, monHeight; |
| 238 | glfwGetMonitorWorkarea(primary, nullptr, nullptr, &monWidth, &monHeight); |
| 239 | if (monWidth < gContext->width || monHeight < gContext->height) { |
| 240 | glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); |
| 241 | windowScale = (std::min)(monWidth * 1.0 / gContext->width, |
| 242 | monHeight * 1.0 / gContext->height); |
| 243 | } |
| 244 | } |
| 245 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 246 | if (gContext->xPos != -1 && gContext->yPos != -1) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 247 | glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 248 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 249 | |
| 250 | // Create window with graphics context |
| 251 | gContext->window = |
| 252 | glfwCreateWindow(gContext->width, gContext->height, |
| 253 | gContext->title.c_str(), nullptr, nullptr); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 254 | if (!gContext->window) { |
| 255 | return false; |
| 256 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 257 | |
| 258 | if (!gContext->loadedWidthHeight) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 259 | #ifndef __APPLE__ |
| 260 | if (windowScale == 1.0) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 261 | glfwGetWindowContentScale(gContext->window, &windowScale, nullptr); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 262 | } |
| 263 | #endif |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 264 | // force user scale if window scale is smaller |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 265 | if (windowScale <= 0.5) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 266 | gContext->userScale = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 267 | } else if (windowScale <= 0.75) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 268 | gContext->userScale = 1; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 269 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 270 | if (windowScale != 1.0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 271 | for (auto&& func : gContext->windowScalers) { |
| 272 | func(windowScale); |
| 273 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | // Update window settings |
| 278 | if (gContext->xPos != -1 && gContext->yPos != -1) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 279 | // check to make sure the position isn't off-screen |
| 280 | bool found = false; |
| 281 | int monCount; |
| 282 | GLFWmonitor** monitors = glfwGetMonitors(&monCount); |
| 283 | for (int i = 0; i < monCount; ++i) { |
| 284 | int monXPos, monYPos, monWidth, monHeight; |
| 285 | glfwGetMonitorWorkarea(monitors[i], &monXPos, &monYPos, &monWidth, |
| 286 | &monHeight); |
| 287 | if (gContext->xPos >= monXPos && gContext->xPos < (monXPos + monWidth) && |
| 288 | gContext->yPos >= monYPos && gContext->yPos < (monYPos + monHeight)) { |
| 289 | found = true; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | if (found) { |
| 294 | glfwSetWindowPos(gContext->window, gContext->xPos, gContext->yPos); |
| 295 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 296 | glfwShowWindow(gContext->window); |
| 297 | } |
| 298 | |
| 299 | // Set window callbacks |
| 300 | glfwGetWindowSize(gContext->window, &gContext->width, &gContext->height); |
| 301 | glfwSetWindowSizeCallback(gContext->window, WindowSizeCallback); |
| 302 | glfwSetFramebufferSizeCallback(gContext->window, FramebufferSizeCallback); |
| 303 | glfwSetWindowMaximizeCallback(gContext->window, WindowMaximizeCallback); |
| 304 | glfwSetWindowPosCallback(gContext->window, WindowPosCallback); |
| 305 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 306 | // Set icons |
| 307 | if (!gContext->icons.empty()) { |
| 308 | glfwSetWindowIcon(gContext->window, gContext->icons.size(), |
| 309 | gContext->icons.data()); |
| 310 | for (auto&& icon : gContext->icons) { |
| 311 | stbi_image_free(icon.pixels); |
| 312 | } |
| 313 | gContext->icons.clear(); |
| 314 | } |
| 315 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 316 | // Setup Dear ImGui style |
| 317 | SetStyle(static_cast<Style>(gContext->style)); |
| 318 | |
| 319 | // Load Fonts |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 320 | UpdateFontScale(); |
| 321 | ReloadFonts(); |
| 322 | gContext->reloadFonts = false; // init renderer will do this |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 323 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 324 | if (!PlatformInitRenderer()) { |
| 325 | return false; |
| 326 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 327 | |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | void gui::Main() { |
| 332 | // Main loop |
| 333 | while (!glfwWindowShouldClose(gContext->window) && !gContext->exit) { |
| 334 | // Poll and handle events (inputs, window resize, etc.) |
| 335 | glfwPollEvents(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 336 | gContext->isPlatformRendering = true; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 337 | UpdateFontScale(); |
| 338 | if (gContext->reloadFonts) { |
| 339 | ReloadFonts(); |
| 340 | // PlatformRenderFrame() will clear reloadFonts flag |
| 341 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 342 | PlatformRenderFrame(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 343 | gContext->isPlatformRendering = false; |
| 344 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 345 | auto& io = ImGui::GetIO(); |
| 346 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 347 | // custom saving |
| 348 | if (gContext->saveSettings) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 349 | if (io.WantSaveIniSettings) { |
| 350 | gContext->saveSettings(false); |
| 351 | io.WantSaveIniSettings = false; // reset flag |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Save (if custom save) |
| 357 | if (gContext->saveSettings) { |
| 358 | gContext->saveSettings(true); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Cleanup |
| 362 | PlatformShutdown(); |
| 363 | ImGui_ImplGlfw_Shutdown(); |
| 364 | ImPlot::DestroyContext(); |
| 365 | ImGui::DestroyContext(); |
| 366 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 367 | // Delete the save file if requested. |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 368 | if (!gContext->saveSettings && gContext->resetOnExit) { |
| 369 | std::remove(gContext->iniPath.c_str()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 372 | glfwDestroyWindow(gContext->window); |
| 373 | glfwTerminate(); |
| 374 | } |
| 375 | |
| 376 | void gui::CommonRenderFrame() { |
| 377 | ImGui_ImplGlfw_NewFrame(); |
| 378 | |
| 379 | // Start the Dear ImGui frame |
| 380 | ImGui::NewFrame(); |
| 381 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 382 | for (size_t i = 0; i < gContext->earlyExecutors.size(); ++i) { |
| 383 | auto& execute = gContext->earlyExecutors[i]; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 384 | if (execute) { |
| 385 | execute(); |
| 386 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | for (size_t i = 0; i < gContext->lateExecutors.size(); ++i) { |
| 390 | auto& execute = gContext->lateExecutors[i]; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 391 | if (execute) { |
| 392 | execute(); |
| 393 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Rendering |
| 397 | ImGui::Render(); |
| 398 | } |
| 399 | |
| 400 | void gui::Exit() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 401 | if (!gContext) { |
| 402 | return; |
| 403 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 404 | gContext->exit = true; |
| 405 | } |
| 406 | |
| 407 | void gui::AddInit(std::function<void()> initialize) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 408 | if (initialize) { |
| 409 | gContext->initializers.emplace_back(std::move(initialize)); |
| 410 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void gui::AddWindowScaler(std::function<void(float scale)> windowScaler) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 414 | if (windowScaler) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 415 | gContext->windowScalers.emplace_back(std::move(windowScaler)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 416 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void gui::AddEarlyExecute(std::function<void()> execute) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 420 | if (execute) { |
| 421 | gContext->earlyExecutors.emplace_back(std::move(execute)); |
| 422 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void gui::AddLateExecute(std::function<void()> execute) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 426 | if (execute) { |
| 427 | gContext->lateExecutors.emplace_back(std::move(execute)); |
| 428 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 431 | void gui::ConfigureCustomSaveSettings(std::function<void()> load, |
| 432 | std::function<void()> loadIni, |
| 433 | std::function<void(bool)> save) { |
| 434 | gContext->loadSettings = load; |
| 435 | gContext->loadIniSettings = loadIni; |
| 436 | gContext->saveSettings = save; |
| 437 | } |
| 438 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 439 | GLFWwindow* gui::GetSystemWindow() { |
| 440 | return gContext->window; |
| 441 | } |
| 442 | |
| 443 | bool gui::AddIcon(const unsigned char* data, int len) { |
| 444 | // Load from memory |
| 445 | GLFWimage image; |
| 446 | image.pixels = |
| 447 | stbi_load_from_memory(data, len, &image.width, &image.height, nullptr, 4); |
| 448 | if (!data) { |
| 449 | return false; |
| 450 | } |
| 451 | gContext->icons.emplace_back(std::move(image)); |
| 452 | return true; |
| 453 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 454 | |
| 455 | int gui::AddFont( |
| 456 | const char* name, |
| 457 | std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)> |
| 458 | makeFont) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 459 | if (makeFont) { |
| 460 | gContext->makeFonts.emplace_back(name, std::move(makeFont)); |
| 461 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 462 | return gContext->makeFonts.size() - 1; |
| 463 | } |
| 464 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 465 | void gui::SetStyle(Style style) { |
| 466 | gContext->style = static_cast<int>(style); |
| 467 | switch (style) { |
| 468 | case kStyleClassic: |
| 469 | ImGui::StyleColorsClassic(); |
| 470 | break; |
| 471 | case kStyleDark: |
| 472 | ImGui::StyleColorsDark(); |
| 473 | break; |
| 474 | case kStyleLight: |
| 475 | ImGui::StyleColorsLight(); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 480 | void gui::SetClearColor(ImVec4 color) { |
| 481 | gContext->clearColor = color; |
| 482 | } |
| 483 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 484 | std::string gui::GetPlatformSaveFileDir() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 485 | #if defined(_MSC_VER) |
| 486 | const char* env = std::getenv("APPDATA"); |
| 487 | if (env) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 488 | return env + std::string("/"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 489 | } |
| 490 | #elif defined(__APPLE__) |
| 491 | const char* env = std::getenv("HOME"); |
| 492 | if (env) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 493 | return env + std::string("/Library/Preferences/"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 494 | } |
| 495 | #else |
| 496 | const char* xdg = std::getenv("XDG_CONFIG_HOME"); |
| 497 | const char* env = std::getenv("HOME"); |
| 498 | if (xdg) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 499 | return xdg + std::string("/"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 500 | } else if (env) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 501 | return env + std::string("/.config/"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 502 | } |
| 503 | #endif |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 504 | return ""; |
| 505 | } |
| 506 | |
| 507 | void gui::ConfigurePlatformSaveFile(const std::string& name) { |
| 508 | gContext->iniPath = GetPlatformSaveFileDir() + name; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 509 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 510 | |
| 511 | void gui::EmitViewMenu() { |
| 512 | if (ImGui::BeginMenu("View")) { |
| 513 | if (ImGui::BeginMenu("Style")) { |
| 514 | bool selected; |
| 515 | selected = gContext->style == kStyleClassic; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 516 | if (ImGui::MenuItem("Classic", nullptr, &selected, true)) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 517 | SetStyle(kStyleClassic); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 518 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 519 | selected = gContext->style == kStyleDark; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 520 | if (ImGui::MenuItem("Dark", nullptr, &selected, true)) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 521 | SetStyle(kStyleDark); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 522 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 523 | selected = gContext->style == kStyleLight; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 524 | if (ImGui::MenuItem("Light", nullptr, &selected, true)) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 525 | SetStyle(kStyleLight); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 526 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 527 | ImGui::EndMenu(); |
| 528 | } |
| 529 | |
| 530 | if (ImGui::BeginMenu("Zoom")) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 531 | for (int i = 0; i < kFontScaledLevels && (25 * (i + 2)) <= 200; ++i) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 532 | char label[20]; |
| 533 | std::snprintf(label, sizeof(label), "%d%%", 25 * (i + 2)); |
| 534 | bool selected = gContext->userScale == i; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 535 | if (ImGui::MenuItem(label, nullptr, &selected)) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 536 | gContext->userScale = i; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 537 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 538 | } |
| 539 | ImGui::EndMenu(); |
| 540 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 541 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 542 | if (!gContext->saveSettings) { |
| 543 | ImGui::MenuItem("Reset UI on Exit?", nullptr, &gContext->resetOnExit); |
| 544 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 545 | ImGui::EndMenu(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | bool gui::UpdateTextureFromImage(ImTextureID* texture, int width, int height, |
| 550 | const unsigned char* data, int len) { |
| 551 | // Load from memory |
| 552 | int width2 = 0; |
| 553 | int height2 = 0; |
| 554 | unsigned char* imgData = |
| 555 | stbi_load_from_memory(data, len, &width2, &height2, nullptr, 4); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 556 | if (!data) { |
| 557 | return false; |
| 558 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 559 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 560 | if (width2 == width && height2 == height) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 561 | UpdateTexture(texture, kPixelRGBA, width2, height2, imgData); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 562 | } else { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 563 | *texture = CreateTexture(kPixelRGBA, width2, height2, imgData); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 564 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 565 | |
| 566 | stbi_image_free(imgData); |
| 567 | |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | bool gui::CreateTextureFromFile(const char* filename, ImTextureID* out_texture, |
| 572 | int* out_width, int* out_height) { |
| 573 | // Load from file |
| 574 | int width = 0; |
| 575 | int height = 0; |
| 576 | unsigned char* data = stbi_load(filename, &width, &height, nullptr, 4); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 577 | if (!data) { |
| 578 | return false; |
| 579 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 580 | |
| 581 | *out_texture = CreateTexture(kPixelRGBA, width, height, data); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 582 | if (out_width) { |
| 583 | *out_width = width; |
| 584 | } |
| 585 | if (out_height) { |
| 586 | *out_height = height; |
| 587 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 588 | |
| 589 | stbi_image_free(data); |
| 590 | |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | bool gui::CreateTextureFromImage(const unsigned char* data, int len, |
| 595 | ImTextureID* out_texture, int* out_width, |
| 596 | int* out_height) { |
| 597 | // Load from memory |
| 598 | int width = 0; |
| 599 | int height = 0; |
| 600 | unsigned char* imgData = |
| 601 | stbi_load_from_memory(data, len, &width, &height, nullptr, 4); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 602 | if (!imgData) { |
| 603 | return false; |
| 604 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 605 | |
| 606 | *out_texture = CreateTexture(kPixelRGBA, width, height, imgData); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 607 | if (out_width) { |
| 608 | *out_width = width; |
| 609 | } |
| 610 | if (out_height) { |
| 611 | *out_height = height; |
| 612 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 613 | |
| 614 | stbi_image_free(imgData); |
| 615 | |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | void gui::MaxFit(ImVec2* min, ImVec2* max, float width, float height) { |
| 620 | float destWidth = max->x - min->x; |
| 621 | float destHeight = max->y - min->y; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 622 | if (width == 0 || height == 0) { |
| 623 | return; |
| 624 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 625 | if (destWidth * height > destHeight * width) { |
| 626 | float outputWidth = width * destHeight / height; |
| 627 | min->x += (destWidth - outputWidth) / 2; |
| 628 | max->x -= (destWidth - outputWidth) / 2; |
| 629 | } else { |
| 630 | float outputHeight = height * destWidth / width; |
| 631 | min->y += (destHeight - outputHeight) / 2; |
| 632 | max->y -= (destHeight - outputHeight) / 2; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | } // namespace wpi |