blob: a4b466b57c3e0529d8e04ca444c8629f31bc17a6 [file] [log] [blame]
James Kuszmaulcf324122023-01-14 14:07:17 -08001// 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
7#include <libssh/libssh.h>
8
9#include <memory>
10#include <string_view>
11
12#define IMGUI_DEFINE_MATH_OPERATORS
13
14#include <glass/Context.h>
15#include <glass/MainMenuBar.h>
16#include <glass/Storage.h>
17#include <imgui.h>
18#include <imgui_internal.h>
19#include <wpigui.h>
20
21#include "Downloader.h"
22#include "Exporter.h"
23
24namespace gui = wpi::gui;
25
26const char* GetWPILibVersion();
27
28namespace dlt {
29std::string_view GetResource_dlt_16_png();
30std::string_view GetResource_dlt_32_png();
31std::string_view GetResource_dlt_48_png();
32std::string_view GetResource_dlt_64_png();
33std::string_view GetResource_dlt_128_png();
34std::string_view GetResource_dlt_256_png();
35std::string_view GetResource_dlt_512_png();
36} // namespace dlt
37
38bool gShutdown = false;
39
40static std::unique_ptr<Downloader> gDownloader;
41static bool* gDownloadVisible;
42static float gDefaultScale = 1.0;
43
44void 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
52void 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
60static 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
76static 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
96 ImGui::EndMainMenuBar();
97
98 if (about) {
99 ImGui::OpenPopup("About");
100 }
101 if (ImGui::BeginPopupModal("About")) {
102 ImGui::Text("Datalog Tool");
103 ImGui::Separator();
104 ImGui::Text("v%s", GetWPILibVersion());
105 ImGui::Separator();
106 ImGui::Text("Save location: %s", glass::GetStorageDir().c_str());
107 if (ImGui::Button("Close")) {
108 ImGui::CloseCurrentPopup();
109 }
110 ImGui::EndPopup();
111 }
112}
113
114static void DisplayGui() {
115 DisplayMainMenu();
116 DisplayInputFiles();
117 DisplayEntries();
118 DisplayOutput(glass::GetStorageRoot().GetChild("output"));
119 DisplayDownload();
120}
121
122void Application(std::string_view saveDir) {
123 ssh_init();
124
125 gui::CreateContext();
126 glass::CreateContext();
127
128 // Add icons
129 gui::AddIcon(dlt::GetResource_dlt_16_png());
130 gui::AddIcon(dlt::GetResource_dlt_32_png());
131 gui::AddIcon(dlt::GetResource_dlt_48_png());
132 gui::AddIcon(dlt::GetResource_dlt_64_png());
133 gui::AddIcon(dlt::GetResource_dlt_128_png());
134 gui::AddIcon(dlt::GetResource_dlt_256_png());
135 gui::AddIcon(dlt::GetResource_dlt_512_png());
136
137 glass::SetStorageName("datalogtool");
138 glass::SetStorageDir(saveDir.empty() ? gui::GetPlatformSaveFileDir()
139 : saveDir);
140
141 gui::AddWindowScaler([](float scale) { gDefaultScale = scale; });
142 gui::AddLateExecute(DisplayGui);
143 gui::Initialize("Datalog Tool", 925, 510);
144
145 gDownloadVisible =
146 &glass::GetStorageRoot().GetChild("download").GetBool("visible", true);
147
148 gui::Main();
149
150 gShutdown = true;
151 glass::DestroyContext();
152 gui::DestroyContext();
153
154 gDownloader.reset();
155 ssh_finalize();
156}