Files
allwpilib/glass/src/lib/native/cpp/support/NameSetting.cpp
Peter Johnson 0587b7043a [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-27 00:12:13 -08:00

124 lines
3.6 KiB
C++

// 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.
#include "glass/support/NameSetting.h"
#include <cstdio>
#include <cstring>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include <wpi/StringExtras.h>
using namespace glass;
void NameSetting::GetName(char* buf, size_t size,
const char* defaultName) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s", m_name.c_str());
} else {
std::snprintf(buf, size, "%s", defaultName);
}
}
void NameSetting::GetName(char* buf, size_t size, const char* defaultName,
int index) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d]", m_name.c_str(), index);
} else {
std::snprintf(buf, size, "%s[%d]", defaultName, index);
}
}
void NameSetting::GetName(char* buf, size_t size, const char* defaultName,
int index, int index2) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d,%d]", m_name.c_str(), index, index2);
} else {
std::snprintf(buf, size, "%s[%d,%d]", defaultName, index, index2);
}
}
void NameSetting::GetLabel(char* buf, size_t size,
const char* defaultName) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s###Name%s", m_name.c_str(), defaultName);
} else {
std::snprintf(buf, size, "%s###Name%s", defaultName, defaultName);
}
}
void NameSetting::GetLabel(char* buf, size_t size, const char* defaultName,
int index) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d]###Name%d", m_name.c_str(), index, index);
} else {
std::snprintf(buf, size, "%s[%d]###Name%d", defaultName, index, index);
}
}
void NameSetting::GetLabel(char* buf, size_t size, const char* defaultName,
int index, int index2) const {
if (!m_name.empty()) {
std::snprintf(buf, size, "%s [%d,%d]###Name%d", m_name.c_str(), index,
index2, index);
} else {
std::snprintf(buf, size, "%s[%d,%d]###Name%d", defaultName, index, index2,
index);
}
}
void NameSetting::PushEditNameId(int index) {
char id[64];
std::snprintf(id, sizeof(id), "Name%d", index);
ImGui::PushID(id);
}
void NameSetting::PushEditNameId(const char* name) {
char id[128];
std::snprintf(id, sizeof(id), "Name%s", name);
ImGui::PushID(id);
}
bool NameSetting::PopupEditName(int index) {
bool rv = false;
char id[64];
std::snprintf(id, sizeof(id), "Name%d", index);
if (ImGui::BeginPopupContextItem(id)) {
ImGui::Text("Edit name:");
if (InputTextName("##edit")) {
rv = true;
}
if (ImGui::Button("Close") || ImGui::IsKeyPressedMap(ImGuiKey_Enter) ||
ImGui::IsKeyPressedMap(ImGuiKey_KeyPadEnter)) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
return rv;
}
bool NameSetting::PopupEditName(const char* name) {
bool rv = false;
char id[128];
std::snprintf(id, sizeof(id), "Name%s", name);
if (ImGui::BeginPopupContextItem(id)) {
ImGui::Text("Edit name:");
if (InputTextName("##edit")) {
rv = true;
}
if (ImGui::Button("Close") || ImGui::IsKeyPressedMap(ImGuiKey_Enter) ||
ImGui::IsKeyPressedMap(ImGuiKey_KeyPadEnter)) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
return rv;
}
bool NameSetting::InputTextName(const char* label_id,
ImGuiInputTextFlags flags) {
return ImGui::InputText(label_id, &m_name, flags);
}