[glass] Split DataSource into type-specific variants (#7588)

This commit is contained in:
Peter Johnson
2025-01-03 13:36:40 -08:00
committed by GitHub
parent 148fcdca85
commit 0f6693594c
62 changed files with 667 additions and 390 deletions

View File

@@ -4,7 +4,11 @@
#include "glass/DataSource.h"
#include <cstdio>
#include <string>
#include <fmt/format.h>
#include <imgui.h>
#include "glass/ContextInternal.h"
@@ -12,17 +16,13 @@ using namespace glass;
wpi::sig::Signal<const char*, DataSource*> DataSource::sourceCreated;
DataSource::DataSource(std::string_view id)
: m_id{id}, m_name{gContext->sourceNameStorage.GetString(m_id)} {
gContext->sources.try_emplace(m_id, this);
sourceCreated(m_id.c_str(), this);
std::string glass::MakeSourceId(std::string_view id, int index) {
return fmt::format("{}[{}]", id, index);
}
DataSource::DataSource(std::string_view id, int index)
: DataSource{fmt::format("{}[{}]", id, index)} {}
DataSource::DataSource(std::string_view id, int index, int index2)
: DataSource{fmt::format("{}[{},{}]", id, index, index2)} {}
std::string glass::MakeSourceId(std::string_view id, int index, int index2) {
return fmt::format("{}[{},{}]", id, index, index2);
}
DataSource::~DataSource() {
if (!gContext) {
@@ -99,10 +99,11 @@ bool DataSource::InputInt(const char* label, int* v, int step, int step_fast,
void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
if (ImGui::BeginDragDropSource(flags)) {
char buf[32];
std::snprintf(buf, sizeof(buf), "DataSource:%s", GetType());
auto self = this;
ImGui::SetDragDropPayload("DataSource", &self, sizeof(self)); // NOLINT
const char* name = GetName().c_str();
ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
ImGui::SetDragDropPayload(buf, &self, sizeof(self)); // NOLINT
DragDropTooltip();
ImGui::EndDragDropSource();
}
}
@@ -114,3 +115,38 @@ DataSource* DataSource::Find(std::string_view id) {
}
return it->second;
}
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;
}