2020-12-26 14:31:24 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/Window.h"
|
|
|
|
|
|
2023-01-01 21:05:09 -07:00
|
|
|
#include <imgui.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <imgui_internal.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <wpi/StringExtras.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/Context.h"
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
#include "glass/Storage.h"
|
2023-01-01 21:05:09 -07:00
|
|
|
#include "glass/support/ExtraGuiWidgets.h"
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
Window::Window(Storage& storage, std::string_view id,
|
|
|
|
|
Visibility defaultVisibility)
|
|
|
|
|
: m_id{id},
|
|
|
|
|
m_name{storage.GetString("name")},
|
|
|
|
|
m_defaultName{id},
|
|
|
|
|
m_visible{storage.GetBool("visible", defaultVisibility != kHide)},
|
|
|
|
|
m_enabled{storage.GetBool("enabled", defaultVisibility != kDisabled)},
|
|
|
|
|
m_defaultVisible{storage.GetValue("visible").boolDefault},
|
|
|
|
|
m_defaultEnabled{storage.GetValue("enabled").boolDefault} {}
|
|
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
void Window::SetVisibility(Visibility visibility) {
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
m_visible = visibility != kHide;
|
|
|
|
|
m_enabled = visibility != kDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Window::SetDefaultVisibility(Visibility visibility) {
|
|
|
|
|
m_defaultVisible = visibility != kHide;
|
|
|
|
|
m_defaultEnabled = visibility != kDisabled;
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Window::Display() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_view) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
if (!m_visible || !m_enabled) {
|
|
|
|
|
PushID(m_id);
|
|
|
|
|
m_view->Hidden();
|
|
|
|
|
PopID();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_posCond != 0) {
|
|
|
|
|
ImGui::SetNextWindowPos(m_pos, m_posCond);
|
|
|
|
|
}
|
|
|
|
|
if (m_sizeCond != 0) {
|
|
|
|
|
ImGui::SetNextWindowSize(m_size, m_sizeCond);
|
|
|
|
|
}
|
|
|
|
|
if (m_setPadding) {
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
char label[128];
|
2023-09-17 20:00:16 -07:00
|
|
|
if (m_name.empty()) {
|
|
|
|
|
wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_defaultName,
|
|
|
|
|
m_id);
|
|
|
|
|
} else {
|
|
|
|
|
wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_name, m_id);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
if (Begin(label, &m_visible, m_flags)) {
|
2023-01-01 21:05:09 -07:00
|
|
|
if (m_renamePopupEnabled || m_view->HasSettings()) {
|
|
|
|
|
bool isClicked = (ImGui::IsMouseReleased(ImGuiMouseButton_Right) &&
|
|
|
|
|
ImGui::IsItemHovered());
|
|
|
|
|
ImGuiWindow* window = ImGui::GetCurrentWindow();
|
|
|
|
|
|
|
|
|
|
bool settingsButtonClicked = false;
|
|
|
|
|
// Not docked, and window has just enough for the circles not to be
|
|
|
|
|
// touching
|
|
|
|
|
if (!ImGui::IsWindowDocked() &&
|
|
|
|
|
ImGui::GetWindowWidth() > (ImGui::GetFontSize() + 2) * 3 +
|
|
|
|
|
ImGui::GetStyle().FramePadding.x * 2) {
|
|
|
|
|
const ImGuiItemFlags itemFlagsRestore =
|
|
|
|
|
ImGui::GetCurrentContext()->CurrentItemFlags;
|
|
|
|
|
|
|
|
|
|
ImGui::GetCurrentContext()->CurrentItemFlags |=
|
|
|
|
|
ImGuiItemFlags_NoNavDefaultFocus;
|
|
|
|
|
window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;
|
|
|
|
|
|
|
|
|
|
// Allow to draw outside of normal window
|
|
|
|
|
ImGui::PushClipRect(window->OuterRectClipped.Min,
|
|
|
|
|
window->OuterRectClipped.Max, false);
|
|
|
|
|
|
|
|
|
|
const ImRect titleBarRect = ImGui::GetCurrentWindow()->TitleBarRect();
|
|
|
|
|
const ImVec2 position = {titleBarRect.Max.x -
|
|
|
|
|
(ImGui::GetStyle().FramePadding.x * 3) -
|
|
|
|
|
(ImGui::GetFontSize() * 2),
|
|
|
|
|
titleBarRect.Min.y};
|
|
|
|
|
settingsButtonClicked =
|
|
|
|
|
HamburgerButton(ImGui::GetID("#SETTINGS"), position);
|
|
|
|
|
|
|
|
|
|
ImGui::PopClipRect();
|
|
|
|
|
|
|
|
|
|
ImGui::GetCurrentContext()->CurrentItemFlags = itemFlagsRestore;
|
|
|
|
|
}
|
|
|
|
|
if (settingsButtonClicked || isClicked) {
|
|
|
|
|
ImGui::OpenPopup(window->ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImGui::BeginPopupEx(window->ID,
|
|
|
|
|
ImGuiWindowFlags_AlwaysAutoResize |
|
|
|
|
|
ImGuiWindowFlags_NoTitleBar |
|
|
|
|
|
ImGuiWindowFlags_NoSavedSettings)) {
|
|
|
|
|
if (m_renamePopupEnabled) {
|
|
|
|
|
ItemEditName(&m_name);
|
|
|
|
|
}
|
|
|
|
|
m_view->Settings();
|
|
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2023-01-01 21:05:09 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
m_view->Display();
|
|
|
|
|
} else {
|
|
|
|
|
m_view->Hidden();
|
|
|
|
|
}
|
|
|
|
|
End();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_setPadding) {
|
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Window::DisplayMenuItem(const char* label) {
|
|
|
|
|
bool wasVisible = m_visible;
|
|
|
|
|
ImGui::MenuItem(
|
|
|
|
|
label ? label : (m_name.empty() ? m_id.c_str() : m_name.c_str()), nullptr,
|
|
|
|
|
&m_visible, m_enabled);
|
|
|
|
|
return !wasVisible && m_visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Window::ScaleDefault(float scale) {
|
|
|
|
|
if ((m_posCond & ImGuiCond_FirstUseEver) != 0) {
|
|
|
|
|
m_pos.x *= scale;
|
|
|
|
|
m_pos.y *= scale;
|
|
|
|
|
}
|
|
|
|
|
if ((m_sizeCond & ImGuiCond_FirstUseEver) != 0) {
|
|
|
|
|
m_size.x *= scale;
|
|
|
|
|
m_size.y *= scale;
|
|
|
|
|
}
|
|
|
|
|
}
|