blob: b426df40fcb1c8673d45cb409d7b3a96f021c254 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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/MainMenuBar.h"
6
Austin Schuh75263e32022-02-22 18:05:32 -08007#include <imgui.h>
James Kuszmaulb13e13f2023-11-22 20:44:04 -08008#include <wpi/StringExtras.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07009#include <wpigui.h>
10
Austin Schuh75263e32022-02-22 18:05:32 -080011#include "glass/Context.h"
12#include "glass/ContextInternal.h"
13
Austin Schuh812d0d12021-11-04 20:16:48 -070014using namespace glass;
15
16void MainMenuBar::AddMainMenu(std::function<void()> menu) {
17 if (menu) {
18 m_menus.emplace_back(std::move(menu));
19 }
20}
21
22void MainMenuBar::AddOptionMenu(std::function<void()> menu) {
23 if (menu) {
24 m_optionMenus.emplace_back(std::move(menu));
25 }
26}
27
28void MainMenuBar::Display() {
29 ImGui::BeginMainMenuBar();
30
Austin Schuh75263e32022-02-22 18:05:32 -080031 WorkspaceMenu();
32
Austin Schuh812d0d12021-11-04 20:16:48 -070033 if (!m_optionMenus.empty()) {
34 if (ImGui::BeginMenu("Options")) {
35 for (auto&& menu : m_optionMenus) {
36 if (menu) {
37 menu();
38 }
39 }
40 ImGui::EndMenu();
41 }
42 }
43
44 wpi::gui::EmitViewMenu();
45
46 for (auto&& menu : m_menus) {
47 if (menu) {
48 menu();
49 }
50 }
51
52#if 0
53 char str[64];
James Kuszmaulb13e13f2023-11-22 20:44:04 -080054 wpi::format_to_n_c_str(str, sizeof(str), "{:.3f} ms/frame ({:.1f} FPS)",
55 1000.0f / ImGui::GetIO().Framerate,
56 ImGui::GetIO().Framerate);
57
58 ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::CalcTextSize(str).x - 10);
Austin Schuh812d0d12021-11-04 20:16:48 -070059 ImGui::Text("%s", str);
60#endif
61 ImGui::EndMainMenuBar();
62}
Austin Schuh75263e32022-02-22 18:05:32 -080063
64void MainMenuBar::WorkspaceMenu() {
65 if (ImGui::BeginMenu("Workspace")) {
66 if (ImGui::MenuItem("Open...")) {
67 m_openFolder =
68 std::make_unique<pfd::select_folder>("Choose folder to open");
69 }
70 if (ImGui::MenuItem("Save As...")) {
71 m_saveFolder = std::make_unique<pfd::select_folder>("Choose save folder");
72 }
73 if (ImGui::MenuItem("Save As Global", nullptr, false,
74 !gContext->isPlatformSaveDir)) {
75 SetStorageDir(wpi::gui::GetPlatformSaveFileDir());
76 SaveStorage();
77 }
78 ImGui::Separator();
79 if (ImGui::MenuItem("Reset")) {
80 WorkspaceReset();
81 }
82 ImGui::Separator();
83 if (ImGui::MenuItem("Exit")) {
84 wpi::gui::Exit();
85 }
86 ImGui::EndMenu();
87 }
88
89 if (m_openFolder && m_openFolder->ready(0)) {
90 auto result = m_openFolder->result();
91 if (!result.empty()) {
92 LoadStorage(result);
93 }
94 m_openFolder.reset();
95 }
96
97 if (m_saveFolder && m_saveFolder->ready(0)) {
98 auto result = m_saveFolder->result();
99 if (!result.empty()) {
100 SetStorageDir(result);
101 SaveStorage(result);
102 }
103 m_saveFolder.reset();
104 }
105}