James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [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 "App.h" |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <string_view> |
| 9 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 10 | #include <glass/Context.h> |
| 11 | #include <glass/MainMenuBar.h> |
| 12 | #include <glass/Storage.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 13 | #include <libssh/libssh.h> |
| 14 | |
| 15 | #define IMGUI_DEFINE_MATH_OPERATORS |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 16 | #include <imgui.h> |
| 17 | #include <imgui_internal.h> |
| 18 | #include <wpigui.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 19 | #include <wpigui_openurl.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 20 | |
| 21 | #include "Downloader.h" |
| 22 | #include "Exporter.h" |
| 23 | |
| 24 | namespace gui = wpi::gui; |
| 25 | |
| 26 | const char* GetWPILibVersion(); |
| 27 | |
| 28 | namespace dlt { |
| 29 | std::string_view GetResource_dlt_16_png(); |
| 30 | std::string_view GetResource_dlt_32_png(); |
| 31 | std::string_view GetResource_dlt_48_png(); |
| 32 | std::string_view GetResource_dlt_64_png(); |
| 33 | std::string_view GetResource_dlt_128_png(); |
| 34 | std::string_view GetResource_dlt_256_png(); |
| 35 | std::string_view GetResource_dlt_512_png(); |
| 36 | } // namespace dlt |
| 37 | |
| 38 | bool gShutdown = false; |
| 39 | |
| 40 | static std::unique_ptr<Downloader> gDownloader; |
| 41 | static bool* gDownloadVisible; |
| 42 | static float gDefaultScale = 1.0; |
| 43 | |
| 44 | void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond, const ImVec2& pivot) { |
| 45 | if ((cond & ImGuiCond_FirstUseEver) != 0) { |
| 46 | ImGui::SetNextWindowPos(pos * gDefaultScale, cond, pivot); |
| 47 | } else { |
| 48 | ImGui::SetNextWindowPos(pos, cond, pivot); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void SetNextWindowSize(const ImVec2& size, ImGuiCond cond) { |
| 53 | if ((cond & ImGuiCond_FirstUseEver) != 0) { |
| 54 | ImGui::SetNextWindowSize(size * gDefaultScale, cond); |
| 55 | } else { |
| 56 | ImGui::SetNextWindowPos(size, cond); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void DisplayDownload() { |
| 61 | if (!*gDownloadVisible) { |
| 62 | return; |
| 63 | } |
| 64 | SetNextWindowPos(ImVec2{0, 250}, ImGuiCond_FirstUseEver); |
| 65 | SetNextWindowSize(ImVec2{375, 260}, ImGuiCond_FirstUseEver); |
| 66 | if (ImGui::Begin("Download", gDownloadVisible)) { |
| 67 | if (!gDownloader) { |
| 68 | gDownloader = std::make_unique<Downloader>( |
| 69 | glass::GetStorageRoot().GetChild("download")); |
| 70 | } |
| 71 | gDownloader->Display(); |
| 72 | } |
| 73 | ImGui::End(); |
| 74 | } |
| 75 | |
| 76 | static void DisplayMainMenu() { |
| 77 | ImGui::BeginMainMenuBar(); |
| 78 | |
| 79 | static glass::MainMenuBar mainMenu; |
| 80 | mainMenu.WorkspaceMenu(); |
| 81 | gui::EmitViewMenu(); |
| 82 | |
| 83 | if (ImGui::BeginMenu("Window")) { |
| 84 | ImGui::MenuItem("Download", nullptr, gDownloadVisible); |
| 85 | ImGui::EndMenu(); |
| 86 | } |
| 87 | |
| 88 | bool about = false; |
| 89 | if (ImGui::BeginMenu("Info")) { |
| 90 | if (ImGui::MenuItem("About")) { |
| 91 | about = true; |
| 92 | } |
| 93 | ImGui::EndMenu(); |
| 94 | } |
| 95 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 96 | if (ImGui::BeginMenu("Docs")) { |
| 97 | if (ImGui::MenuItem("Online documentation")) { |
| 98 | wpi::gui::OpenURL( |
| 99 | "https://docs.wpilib.org/en/stable/docs/software/telemetry/" |
| 100 | "datalog.html"); |
| 101 | } |
| 102 | ImGui::EndMenu(); |
| 103 | } |
| 104 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 105 | ImGui::EndMainMenuBar(); |
| 106 | |
| 107 | if (about) { |
| 108 | ImGui::OpenPopup("About"); |
| 109 | } |
| 110 | if (ImGui::BeginPopupModal("About")) { |
| 111 | ImGui::Text("Datalog Tool"); |
| 112 | ImGui::Separator(); |
| 113 | ImGui::Text("v%s", GetWPILibVersion()); |
| 114 | ImGui::Separator(); |
| 115 | ImGui::Text("Save location: %s", glass::GetStorageDir().c_str()); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 116 | ImGui::Text("%.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, |
| 117 | ImGui::GetIO().Framerate); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 118 | if (ImGui::Button("Close")) { |
| 119 | ImGui::CloseCurrentPopup(); |
| 120 | } |
| 121 | ImGui::EndPopup(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void DisplayGui() { |
| 126 | DisplayMainMenu(); |
| 127 | DisplayInputFiles(); |
| 128 | DisplayEntries(); |
| 129 | DisplayOutput(glass::GetStorageRoot().GetChild("output")); |
| 130 | DisplayDownload(); |
| 131 | } |
| 132 | |
| 133 | void Application(std::string_view saveDir) { |
| 134 | ssh_init(); |
| 135 | |
| 136 | gui::CreateContext(); |
| 137 | glass::CreateContext(); |
| 138 | |
| 139 | // Add icons |
| 140 | gui::AddIcon(dlt::GetResource_dlt_16_png()); |
| 141 | gui::AddIcon(dlt::GetResource_dlt_32_png()); |
| 142 | gui::AddIcon(dlt::GetResource_dlt_48_png()); |
| 143 | gui::AddIcon(dlt::GetResource_dlt_64_png()); |
| 144 | gui::AddIcon(dlt::GetResource_dlt_128_png()); |
| 145 | gui::AddIcon(dlt::GetResource_dlt_256_png()); |
| 146 | gui::AddIcon(dlt::GetResource_dlt_512_png()); |
| 147 | |
| 148 | glass::SetStorageName("datalogtool"); |
| 149 | glass::SetStorageDir(saveDir.empty() ? gui::GetPlatformSaveFileDir() |
| 150 | : saveDir); |
| 151 | |
| 152 | gui::AddWindowScaler([](float scale) { gDefaultScale = scale; }); |
| 153 | gui::AddLateExecute(DisplayGui); |
| 154 | gui::Initialize("Datalog Tool", 925, 510); |
| 155 | |
| 156 | gDownloadVisible = |
| 157 | &glass::GetStorageRoot().GetChild("download").GetBool("visible", true); |
| 158 | |
| 159 | gui::Main(); |
| 160 | |
| 161 | gShutdown = true; |
| 162 | glass::DestroyContext(); |
| 163 | gui::DestroyContext(); |
| 164 | |
| 165 | gDownloader.reset(); |
| 166 | ssh_finalize(); |
| 167 | } |