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. |
| 4 | |
| 5 | #include "glass/Context.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <cinttypes> |
| 9 | #include <cstdio> |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 10 | #include <filesystem> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 11 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 12 | #include <fmt/format.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 13 | #include <imgui.h> |
| 14 | #include <imgui_internal.h> |
| 15 | #include <imgui_stdlib.h> |
| 16 | #include <wpi/StringExtras.h> |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 17 | #include <wpi/fs.h> |
| 18 | #include <wpi/json.h> |
| 19 | #include <wpi/json_serializer.h> |
| 20 | #include <wpi/raw_istream.h> |
| 21 | #include <wpi/raw_ostream.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 22 | #include <wpi/timestamp.h> |
| 23 | #include <wpigui.h> |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 24 | #include <wpigui_internal.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | |
| 26 | #include "glass/ContextInternal.h" |
| 27 | |
| 28 | using namespace glass; |
| 29 | |
| 30 | Context* glass::gContext; |
| 31 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 32 | static void WorkspaceResetImpl() { |
| 33 | // call reset functions |
| 34 | for (auto&& reset : gContext->workspaceReset) { |
| 35 | if (reset) { |
| 36 | reset(); |
| 37 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 39 | |
| 40 | // clear storage |
| 41 | for (auto&& root : gContext->storageRoots) { |
| 42 | root.second->Clear(); |
| 43 | } |
| 44 | |
| 45 | // ImGui reset |
| 46 | ImGui::ClearIniSettings(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 49 | static void WorkspaceInit() { |
| 50 | for (auto&& init : gContext->workspaceInit) { |
| 51 | if (init) { |
| 52 | init(); |
| 53 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 54 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 55 | |
| 56 | for (auto&& root : gContext->storageRoots) { |
| 57 | root.getValue()->Apply(); |
| 58 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 61 | static bool JsonToWindow(const wpi::json& jfile, const char* filename) { |
| 62 | if (!jfile.is_object()) { |
| 63 | ImGui::LogText("%s top level is not object", filename); |
| 64 | return false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 66 | |
| 67 | // loop over JSON and generate ini format |
| 68 | std::string iniStr; |
| 69 | wpi::raw_string_ostream ini{iniStr}; |
| 70 | |
| 71 | for (auto&& jsection : jfile.items()) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 72 | if (jsection.key() == "Docking") { |
| 73 | continue; |
| 74 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 75 | if (!jsection.value().is_object()) { |
| 76 | ImGui::LogText("%s section %s is not object", filename, |
| 77 | jsection.key().c_str()); |
| 78 | return false; |
| 79 | } |
| 80 | for (auto&& jsubsection : jsection.value().items()) { |
| 81 | if (!jsubsection.value().is_object()) { |
| 82 | ImGui::LogText("%s section %s subsection %s is not object", filename, |
| 83 | jsection.key().c_str(), jsubsection.key().c_str()); |
| 84 | return false; |
| 85 | } |
| 86 | ini << '[' << jsection.key() << "][" << jsubsection.key() << "]\n"; |
| 87 | for (auto&& jkv : jsubsection.value().items()) { |
| 88 | try { |
| 89 | auto& value = jkv.value().get_ref<const std::string&>(); |
| 90 | ini << jkv.key() << '=' << value << "\n"; |
| 91 | } catch (wpi::json::exception&) { |
| 92 | ImGui::LogText("%s section %s subsection %s value %s is not string", |
| 93 | filename, jsection.key().c_str(), |
| 94 | jsubsection.key().c_str(), jkv.key().c_str()); |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | ini << '\n'; |
| 99 | } |
| 100 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 101 | |
| 102 | // emit Docking section last |
| 103 | auto docking = jfile.find("Docking"); |
| 104 | if (docking != jfile.end()) { |
| 105 | for (auto&& jsubsection : docking->items()) { |
| 106 | if (!jsubsection.value().is_array()) { |
| 107 | ImGui::LogText("%s section %s subsection %s is not array", filename, |
| 108 | "Docking", jsubsection.key().c_str()); |
| 109 | return false; |
| 110 | } |
| 111 | ini << "[Docking][" << jsubsection.key() << "]\n"; |
| 112 | for (auto&& jv : jsubsection.value()) { |
| 113 | try { |
| 114 | auto& value = jv.get_ref<const std::string&>(); |
| 115 | ini << value << "\n"; |
| 116 | } catch (wpi::json::exception&) { |
| 117 | ImGui::LogText("%s section %s subsection %s value is not string", |
| 118 | filename, "Docking", jsubsection.key().c_str()); |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | ini << '\n'; |
| 123 | } |
| 124 | } |
| 125 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 126 | ini.flush(); |
| 127 | |
| 128 | ImGui::LoadIniSettingsFromMemory(iniStr.data(), iniStr.size()); |
| 129 | return true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 132 | static bool LoadWindowStorageImpl(const std::string& filename) { |
| 133 | std::error_code ec; |
| 134 | wpi::raw_fd_istream is{filename, ec}; |
| 135 | if (ec) { |
| 136 | ImGui::LogText("error opening %s: %s", filename.c_str(), |
| 137 | ec.message().c_str()); |
| 138 | return false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | } else { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 140 | try { |
| 141 | return JsonToWindow(wpi::json::parse(is), filename.c_str()); |
| 142 | } catch (wpi::json::parse_error& e) { |
| 143 | ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what()); |
| 144 | return false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 149 | static bool LoadStorageRootImpl(Context* ctx, const std::string& filename, |
| 150 | std::string_view rootName) { |
| 151 | std::error_code ec; |
| 152 | wpi::raw_fd_istream is{filename, ec}; |
| 153 | if (ec) { |
| 154 | ImGui::LogText("error opening %s: %s", filename.c_str(), |
| 155 | ec.message().c_str()); |
| 156 | return false; |
| 157 | } else { |
| 158 | auto& storage = ctx->storageRoots[rootName]; |
| 159 | bool createdStorage = false; |
| 160 | if (!storage) { |
| 161 | storage = std::make_unique<Storage>(); |
| 162 | createdStorage = true; |
| 163 | } |
| 164 | try { |
| 165 | storage->FromJson(wpi::json::parse(is), filename.c_str()); |
| 166 | } catch (wpi::json::parse_error& e) { |
| 167 | ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what()); |
| 168 | if (createdStorage) { |
| 169 | ctx->storageRoots.erase(rootName); |
| 170 | } |
| 171 | return false; |
| 172 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 173 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 174 | return true; |
| 175 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 176 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 177 | static bool LoadStorageImpl(Context* ctx, std::string_view dir, |
| 178 | std::string_view name) { |
| 179 | WorkspaceResetImpl(); |
| 180 | |
| 181 | bool rv = true; |
| 182 | for (auto&& root : ctx->storageRoots) { |
| 183 | std::string filename; |
| 184 | auto rootName = root.getKey(); |
| 185 | if (rootName.empty()) { |
| 186 | filename = (fs::path{dir} / fmt::format("{}.json", name)).string(); |
| 187 | } else { |
| 188 | filename = |
| 189 | (fs::path{dir} / fmt::format("{}-{}.json", name, rootName)).string(); |
| 190 | } |
| 191 | if (!LoadStorageRootImpl(ctx, filename, rootName)) { |
| 192 | rv = false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | WorkspaceInit(); |
| 197 | return rv; |
| 198 | } |
| 199 | |
| 200 | static wpi::json WindowToJson() { |
| 201 | size_t iniLen; |
| 202 | const char* iniData = ImGui::SaveIniSettingsToMemory(&iniLen); |
| 203 | std::string_view ini{iniData, iniLen}; |
| 204 | |
| 205 | // parse the ini data and build JSON |
| 206 | // JSON format: |
| 207 | // { |
| 208 | // "Section": { |
| 209 | // "Subsection": { |
| 210 | // "Key": "Value" // all values are saved as strings |
| 211 | // } |
| 212 | // } |
| 213 | // } |
| 214 | |
| 215 | wpi::json out = wpi::json::object(); |
| 216 | wpi::json* curSection = nullptr; |
| 217 | while (!ini.empty()) { |
| 218 | std::string_view line; |
| 219 | std::tie(line, ini) = wpi::split(ini, '\n'); |
| 220 | line = wpi::trim(line); |
| 221 | if (line.empty()) { |
| 222 | continue; |
| 223 | } |
| 224 | if (line[0] == '[') { |
| 225 | // new section |
| 226 | auto [section, subsection] = wpi::split(line, ']'); |
| 227 | section = wpi::drop_front(section); // drop '['; ']' was dropped by split |
| 228 | subsection = wpi::drop_back(wpi::drop_front(subsection)); // drop [] |
| 229 | auto& jsection = out[section]; |
| 230 | if (jsection.is_null()) { |
| 231 | jsection = wpi::json::object(); |
| 232 | } |
| 233 | curSection = &jsection[subsection]; |
| 234 | if (curSection->is_null()) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 235 | if (section == "Docking") { |
| 236 | *curSection = wpi::json::array(); |
| 237 | } else { |
| 238 | *curSection = wpi::json::object(); |
| 239 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 240 | } |
| 241 | } else { |
| 242 | // value |
| 243 | if (!curSection) { |
| 244 | continue; // shouldn't happen, but just in case |
| 245 | } |
| 246 | auto [name, value] = wpi::split(line, '='); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 247 | if (curSection->is_object()) { |
| 248 | (*curSection)[name] = value; |
| 249 | } else if (curSection->is_array()) { |
| 250 | curSection->emplace_back(line); |
| 251 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | return out; |
| 256 | } |
| 257 | |
| 258 | bool SaveWindowStorageImpl(const std::string& filename) { |
| 259 | std::error_code ec; |
| 260 | wpi::raw_fd_ostream os{filename, ec}; |
| 261 | if (ec) { |
| 262 | ImGui::LogText("error opening %s: %s", filename.c_str(), |
| 263 | ec.message().c_str()); |
| 264 | return false; |
| 265 | } |
| 266 | WindowToJson().dump(os, 2); |
| 267 | os << '\n'; |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | static bool SaveStorageRootImpl(Context* ctx, const std::string& filename, |
| 272 | const Storage& storage) { |
| 273 | std::error_code ec; |
| 274 | wpi::raw_fd_ostream os{filename, ec}; |
| 275 | if (ec) { |
| 276 | ImGui::LogText("error opening %s: %s", filename.c_str(), |
| 277 | ec.message().c_str()); |
| 278 | return false; |
| 279 | } |
| 280 | storage.ToJson().dump(os, 2); |
| 281 | os << '\n'; |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | static bool SaveStorageImpl(Context* ctx, std::string_view dir, |
| 286 | std::string_view name, bool exiting) { |
| 287 | fs::path dirPath{dir}; |
| 288 | |
| 289 | std::error_code ec; |
| 290 | fs::create_directories(dirPath, ec); |
| 291 | if (ec) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | // handle erasing save files on exit if requested |
| 296 | if (exiting && wpi::gui::gContext->resetOnExit) { |
| 297 | fs::remove(dirPath / fmt::format("{}-window.json", name), ec); |
| 298 | for (auto&& root : ctx->storageRoots) { |
| 299 | auto rootName = root.getKey(); |
| 300 | if (rootName.empty()) { |
| 301 | fs::remove(dirPath / fmt::format("{}.json", name), ec); |
| 302 | } else { |
| 303 | fs::remove(dirPath / fmt::format("{}-{}.json", name, rootName), ec); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 306 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 307 | |
| 308 | bool rv = SaveWindowStorageImpl( |
| 309 | (dirPath / fmt::format("{}-window.json", name)).string()); |
| 310 | |
| 311 | for (auto&& root : ctx->storageRoots) { |
| 312 | auto rootName = root.getKey(); |
| 313 | std::string filename; |
| 314 | if (rootName.empty()) { |
| 315 | filename = (dirPath / fmt::format("{}.json", name)).string(); |
| 316 | } else { |
| 317 | filename = (dirPath / fmt::format("{}-{}.json", name, rootName)).string(); |
| 318 | } |
| 319 | if (!SaveStorageRootImpl(ctx, filename, *root.getValue())) { |
| 320 | rv = false; |
| 321 | } |
| 322 | } |
| 323 | return rv; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 326 | Context::Context() |
| 327 | : sourceNameStorage{storageRoots.insert({"", std::make_unique<Storage>()}) |
| 328 | .first->getValue() |
| 329 | ->GetChild("sourceNames")} { |
| 330 | storageStack.emplace_back(storageRoots[""].get()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 331 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 332 | // override ImGui ini saving |
| 333 | wpi::gui::ConfigureCustomSaveSettings( |
| 334 | [this] { LoadStorageImpl(this, storageLoadDir, storageName); }, |
| 335 | [this] { |
| 336 | LoadWindowStorageImpl((fs::path{storageLoadDir} / |
| 337 | fmt::format("{}-window.json", storageName)) |
| 338 | .string()); |
| 339 | }, |
| 340 | [this](bool exiting) { |
| 341 | SaveStorageImpl(this, storageAutoSaveDir, storageName, exiting); |
| 342 | }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 345 | Context::~Context() { |
| 346 | wpi::gui::ConfigureCustomSaveSettings(nullptr, nullptr, nullptr); |
| 347 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 348 | |
| 349 | Context* glass::CreateContext() { |
| 350 | Context* ctx = new Context; |
| 351 | if (!gContext) { |
| 352 | SetCurrentContext(ctx); |
| 353 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 354 | return ctx; |
| 355 | } |
| 356 | |
| 357 | void glass::DestroyContext(Context* ctx) { |
| 358 | if (!ctx) { |
| 359 | ctx = gContext; |
| 360 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 361 | if (gContext == ctx) { |
| 362 | SetCurrentContext(nullptr); |
| 363 | } |
| 364 | delete ctx; |
| 365 | } |
| 366 | |
| 367 | Context* glass::GetCurrentContext() { |
| 368 | return gContext; |
| 369 | } |
| 370 | |
| 371 | void glass::SetCurrentContext(Context* ctx) { |
| 372 | gContext = ctx; |
| 373 | } |
| 374 | |
| 375 | void glass::ResetTime() { |
| 376 | gContext->zeroTime = wpi::Now(); |
| 377 | } |
| 378 | |
| 379 | uint64_t glass::GetZeroTime() { |
| 380 | return gContext->zeroTime; |
| 381 | } |
| 382 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 383 | void glass::WorkspaceReset() { |
| 384 | WorkspaceResetImpl(); |
| 385 | WorkspaceInit(); |
| 386 | } |
| 387 | |
| 388 | void glass::AddWorkspaceInit(std::function<void()> init) { |
| 389 | if (init) { |
| 390 | gContext->workspaceInit.emplace_back(std::move(init)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 394 | void glass::AddWorkspaceReset(std::function<void()> reset) { |
| 395 | if (reset) { |
| 396 | gContext->workspaceReset.emplace_back(std::move(reset)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 400 | void glass::SetStorageName(std::string_view name) { |
| 401 | gContext->storageName = name; |
| 402 | } |
| 403 | |
| 404 | void glass::SetStorageDir(std::string_view dir) { |
| 405 | if (dir.empty()) { |
| 406 | gContext->storageLoadDir = "."; |
| 407 | gContext->storageAutoSaveDir = "."; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 408 | } else { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 409 | gContext->storageLoadDir = dir; |
| 410 | gContext->storageAutoSaveDir = dir; |
| 411 | gContext->isPlatformSaveDir = (dir == wpi::gui::GetPlatformSaveFileDir()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 415 | std::string glass::GetStorageDir() { |
| 416 | return gContext->storageAutoSaveDir; |
| 417 | } |
| 418 | |
| 419 | bool glass::LoadStorage(std::string_view dir) { |
| 420 | SaveStorage(); |
| 421 | SetStorageDir(dir); |
| 422 | LoadWindowStorageImpl((fs::path{gContext->storageLoadDir} / |
| 423 | fmt::format("{}-window.json", gContext->storageName)) |
| 424 | .string()); |
| 425 | return LoadStorageImpl(gContext, dir, gContext->storageName); |
| 426 | } |
| 427 | |
| 428 | bool glass::SaveStorage() { |
| 429 | return SaveStorageImpl(gContext, gContext->storageAutoSaveDir, |
| 430 | gContext->storageName, false); |
| 431 | } |
| 432 | |
| 433 | bool glass::SaveStorage(std::string_view dir) { |
| 434 | return SaveStorageImpl(gContext, dir, gContext->storageName, false); |
| 435 | } |
| 436 | |
| 437 | Storage& glass::GetCurStorageRoot() { |
| 438 | return *gContext->storageStack.front(); |
| 439 | } |
| 440 | |
| 441 | Storage& glass::GetStorageRoot(std::string_view rootName) { |
| 442 | auto& storage = gContext->storageRoots[rootName]; |
| 443 | if (!storage) { |
| 444 | storage = std::make_unique<Storage>(); |
| 445 | } |
| 446 | return *storage; |
| 447 | } |
| 448 | |
| 449 | void glass::ResetStorageStack(std::string_view rootName) { |
| 450 | if (gContext->storageStack.size() != 1) { |
| 451 | ImGui::LogText("resetting non-empty storage stack"); |
| 452 | } |
| 453 | gContext->storageStack.clear(); |
| 454 | gContext->storageStack.emplace_back(&GetStorageRoot(rootName)); |
| 455 | } |
| 456 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 457 | Storage& glass::GetStorage() { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 458 | return *gContext->storageStack.back(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 461 | void glass::PushStorageStack(std::string_view label_id) { |
| 462 | gContext->storageStack.emplace_back( |
| 463 | &gContext->storageStack.back()->GetChild(label_id)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 466 | void glass::PushStorageStack(Storage& storage) { |
| 467 | gContext->storageStack.emplace_back(&storage); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 470 | void glass::PopStorageStack() { |
| 471 | if (gContext->storageStack.size() <= 1) { |
| 472 | ImGui::LogText("attempted to pop empty storage stack, mismatch push/pop?"); |
| 473 | return; // ignore |
| 474 | } |
| 475 | gContext->storageStack.pop_back(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | bool glass::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 479 | PushStorageStack(name); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 480 | return ImGui::Begin(name, p_open, flags); |
| 481 | } |
| 482 | |
| 483 | void glass::End() { |
| 484 | ImGui::End(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 485 | PopStorageStack(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | bool glass::BeginChild(const char* str_id, const ImVec2& size, bool border, |
| 489 | ImGuiWindowFlags flags) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 490 | PushStorageStack(str_id); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 491 | return ImGui::BeginChild(str_id, size, border, flags); |
| 492 | } |
| 493 | |
| 494 | void glass::EndChild() { |
| 495 | ImGui::EndChild(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 496 | PopStorageStack(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | bool glass::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 500 | bool& open = GetStorage().GetChild(label).GetBool( |
| 501 | "open", (flags & ImGuiTreeNodeFlags_DefaultOpen) != 0); |
| 502 | ImGui::SetNextItemOpen(open); |
| 503 | open = ImGui::CollapsingHeader(label, flags); |
| 504 | return open; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | bool glass::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 508 | PushStorageStack(label); |
| 509 | bool& open = GetStorage().GetBool( |
| 510 | "open", (flags & ImGuiTreeNodeFlags_DefaultOpen) != 0); |
| 511 | ImGui::SetNextItemOpen(open); |
| 512 | open = ImGui::TreeNodeEx(label, flags); |
| 513 | if (!open) { |
| 514 | PopStorageStack(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 515 | } |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 516 | return open; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void glass::TreePop() { |
| 520 | ImGui::TreePop(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 521 | PopStorageStack(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void glass::PushID(const char* str_id) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 525 | PushStorageStack(str_id); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 526 | ImGui::PushID(str_id); |
| 527 | } |
| 528 | |
| 529 | void glass::PushID(const char* str_id_begin, const char* str_id_end) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 530 | PushStorageStack(std::string_view(str_id_begin, str_id_end - str_id_begin)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 531 | ImGui::PushID(str_id_begin, str_id_end); |
| 532 | } |
| 533 | |
| 534 | void glass::PushID(int int_id) { |
| 535 | char buf[16]; |
| 536 | std::snprintf(buf, sizeof(buf), "%d", int_id); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 537 | PushStorageStack(buf); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 538 | ImGui::PushID(int_id); |
| 539 | } |
| 540 | |
| 541 | void glass::PopID() { |
| 542 | ImGui::PopID(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 543 | PopStorageStack(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | bool glass::PopupEditName(const char* label, std::string* name) { |
| 547 | bool rv = false; |
| 548 | if (ImGui::BeginPopupContextItem(label)) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 549 | rv = ItemEditName(name); |
| 550 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 551 | ImGui::EndPopup(); |
| 552 | } |
| 553 | return rv; |
| 554 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 555 | |
| 556 | bool glass::ItemEditName(std::string* name) { |
| 557 | bool rv = false; |
| 558 | |
| 559 | ImGui::Text("Edit name:"); |
| 560 | if (ImGui::InputText("##editname", name)) { |
| 561 | rv = true; |
| 562 | } |
| 563 | if (ImGui::Button("Close") || ImGui::IsKeyPressedMap(ImGuiKey_Enter) || |
| 564 | ImGui::IsKeyPressedMap(ImGuiKey_KeyPadEnter)) { |
| 565 | ImGui::CloseCurrentPopup(); |
| 566 | } |
| 567 | |
| 568 | return rv; |
| 569 | } |