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/DataSource.h"
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2025-01-03 13:36:40 -08:00
|
|
|
#include <imgui.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
#include "glass/ContextInternal.h"
|
|
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
|
|
|
|
wpi::sig::Signal<const char*, DataSource*> DataSource::sourceCreated;
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
std::string glass::MakeSourceId(std::string_view id, int index) {
|
|
|
|
|
return fmt::format("{}[{}]", id, index);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
std::string glass::MakeSourceId(std::string_view id, int index, int index2) {
|
|
|
|
|
return fmt::format("{}[{},{}]", id, index, index2);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
DataSource::~DataSource() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!gContext) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
[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
|
|
|
gContext->sources.erase(m_id);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataSource::LabelText(const char* label, const char* fmt, ...) const {
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
LabelTextV(label, fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a label+text combo aligned to other label+value widgets
|
|
|
|
|
void DataSource::LabelTextV(const char* label, const char* fmt,
|
|
|
|
|
va_list args) const {
|
|
|
|
|
ImGui::PushID(label);
|
[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
|
|
|
ImGui::LabelTextV("##input", fmt, args); // NOLINT
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(label);
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
EmitDrag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DataSource::Combo(const char* label, int* current_item,
|
|
|
|
|
const char* const items[], int items_count,
|
|
|
|
|
int popup_max_height_in_items) const {
|
|
|
|
|
ImGui::PushID(label);
|
|
|
|
|
bool rv = ImGui::Combo("##input", current_item, items, items_count,
|
|
|
|
|
popup_max_height_in_items);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(label);
|
|
|
|
|
EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DataSource::SliderFloat(const char* label, float* v, float v_min,
|
|
|
|
|
float v_max, const char* format,
|
|
|
|
|
float power) const {
|
|
|
|
|
ImGui::PushID(label);
|
|
|
|
|
bool rv = ImGui::SliderFloat("##input", v, v_min, v_max, format, power);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(label);
|
|
|
|
|
EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DataSource::InputDouble(const char* label, double* v, double step,
|
|
|
|
|
double step_fast, const char* format,
|
|
|
|
|
ImGuiInputTextFlags flags) const {
|
|
|
|
|
ImGui::PushID(label);
|
|
|
|
|
bool rv = ImGui::InputDouble("##input", v, step, step_fast, format, flags);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(label);
|
|
|
|
|
EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DataSource::InputInt(const char* label, int* v, int step, int step_fast,
|
|
|
|
|
ImGuiInputTextFlags flags) const {
|
|
|
|
|
ImGui::PushID(label);
|
|
|
|
|
bool rv = ImGui::InputInt("##input", v, step, step_fast, flags);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(label);
|
|
|
|
|
EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
|
|
|
|
|
if (ImGui::BeginDragDropSource(flags)) {
|
2025-01-03 13:36:40 -08:00
|
|
|
char buf[32];
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "DataSource:%s", GetType());
|
2020-09-12 10:55:46 -07:00
|
|
|
auto self = this;
|
2025-01-03 13:36:40 -08:00
|
|
|
ImGui::SetDragDropPayload(buf, &self, sizeof(self)); // NOLINT
|
|
|
|
|
DragDropTooltip();
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::EndDragDropSource();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
DataSource* DataSource::Find(std::string_view id) {
|
2020-09-12 10:55:46 -07:00
|
|
|
auto it = gContext->sources.find(id);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (it == gContext->sources.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2024-10-23 21:33:12 -07:00
|
|
|
return it->second;
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
2025-01-03 13:36:40 -08:00
|
|
|
|
|
|
|
|
std::string& DataSource::GetNameStorage(std::string_view id) {
|
|
|
|
|
return gContext->sourceNameStorage.GetString(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataSource::Register() {
|
|
|
|
|
gContext->sources.insert_or_assign(m_id, this);
|
|
|
|
|
sourceCreated(m_id.c_str(), this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataSource::DragDropTooltip() const {
|
|
|
|
|
const char* name = GetName().c_str();
|
|
|
|
|
ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
|
|
|
|
|
ImGui::Text("(%s)", GetType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* BooleanSource::GetType() const {
|
|
|
|
|
return kType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* DoubleSource::GetType() const {
|
|
|
|
|
return kType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* FloatSource::GetType() const {
|
|
|
|
|
return kType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* IntegerSource::GetType() const {
|
|
|
|
|
return kType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* StringSource::GetType() const {
|
|
|
|
|
return kType;
|
|
|
|
|
}
|