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