Skip to content

Commit

Permalink
Remove enum, use dynamic_cast and string type instead
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Dec 25, 2024
1 parent 3872386 commit 2ad82ee
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 157 deletions.
79 changes: 23 additions & 56 deletions glass/src/lib/native/cpp/DataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ std::string glass::MakeSourceId(std::string_view id, int index, int index2) {
return fmt::format("{}[{},{}]", id, index, index2);
}

DataSource::DataSource(Kind kind, std::string_view id)
: m_id{id},
m_name{gContext->sourceNameStorage.GetString(m_id)},
m_kind{kind} {}

DataSource::~DataSource() {
if (!gContext) {
return;
Expand Down Expand Up @@ -103,9 +98,11 @@ bool DataSource::InputInt(const char* label, int* v, int step, int step_fast,

void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
if (ImGui::BeginDragDropSource(flags)) {
SetDragDropPayload();
const char* name = GetName().c_str();
ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
char buf[32];
std::snprintf(buf, sizeof(buf), "DataSource:%s", GetType());
auto self = this;
ImGui::SetDragDropPayload(buf, &self, sizeof(self)); // NOLINT
DragDropTooltip();
ImGui::EndDragDropSource();
}
}
Expand All @@ -118,67 +115,37 @@ 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);
}

BooleanSource* BooleanSource::AcceptDragDropPayload(ImGuiDragDropFlags flags) {
if (auto payload = ImGui::AcceptDragDropPayload("BooleanSource", flags)) {
return *static_cast<BooleanSource**>(payload->Data);
}
return nullptr;
}

void BooleanSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("BooleanSource", &self, sizeof(self)); // NOLINT
}

DoubleSource* DoubleSource::AcceptDragDropPayload(ImGuiDragDropFlags flags) {
if (auto payload = ImGui::AcceptDragDropPayload("DoubleSource", flags)) {
return *static_cast<DoubleSource**>(payload->Data);
}
return nullptr;
}

void DoubleSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("DoubleSource", &self, sizeof(self)); // NOLINT
}

FloatSource* FloatSource::AcceptDragDropPayload(ImGuiDragDropFlags flags) {
if (auto payload = ImGui::AcceptDragDropPayload("FloatSource", flags)) {
return *static_cast<FloatSource**>(payload->Data);
}
return nullptr;
void DataSource::DragDropTooltip() const {
const char* name = GetName().c_str();
ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
ImGui::Text("(%s)", GetType());
}

void FloatSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("FloatSource", &self, sizeof(self)); // NOLINT
const char* BooleanSource::GetType() const {
return kType;
}

IntegerSource* IntegerSource::AcceptDragDropPayload(ImGuiDragDropFlags flags) {
if (auto payload = ImGui::AcceptDragDropPayload("IntegerSource", flags)) {
return *static_cast<IntegerSource**>(payload->Data);
}
return nullptr;
const char* DoubleSource::GetType() const {
return kType;
}

void IntegerSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("IntegerSource", &self, sizeof(self)); // NOLINT
const char* FloatSource::GetType() const {
return kType;
}

StringSource* StringSource::AcceptDragDropPayload(ImGuiDragDropFlags flags) {
if (auto payload = ImGui::AcceptDragDropPayload("StringSource", flags)) {
return *static_cast<StringSource**>(payload->Data);
}
return nullptr;
const char* IntegerSource::GetType() const {
return kType;
}

void StringSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("StringSource", &self, sizeof(self)); // NOLINT
const char* StringSource::GetType() const {
return kType;
}
81 changes: 37 additions & 44 deletions glass/src/lib/native/cpp/other/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ class PlotSeries {
private:
bool IsDigital() const {
return m_digital.GetValue() == kDigital ||
(m_digital.GetValue() == kAuto && m_source &&
m_source->GetKind() == DataSource::kBoolean);
(m_digital.GetValue() == kAuto && m_source && m_digitalSource);
}
void AppendValue(double value, int64_t time);

// source linkage
DataSource* m_source = nullptr;
bool m_digitalSource = false;
wpi::sig::ScopedConnection m_sourceCreatedConn;
wpi::sig::ScopedConnection m_newValueConn;
std::string& m_id;
Expand Down Expand Up @@ -256,45 +256,38 @@ void PlotSeries::CheckSource() {
void PlotSeries::SetSource(DataSource* source) {
m_source = source;

switch (source->GetKind()) {
case DataSource::kBoolean: {
auto s = static_cast<BooleanSource*>(source);
// add initial value
AppendValue(s->GetValue(), 0);
if (auto s = dynamic_cast<BooleanSource*>(source)) {
m_digitalSource = true;

m_newValueConn = s->valueChanged.connect_connection(
[this](bool value, int64_t time) { AppendValue(value, time); });
break;
}
case DataSource::kDouble: {
auto s = static_cast<DoubleSource*>(source);
// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](double value, int64_t time) { AppendValue(value, time); });
break;
}
case DataSource::kFloat: {
auto s = static_cast<FloatSource*>(source);
// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](double value, int64_t time) { AppendValue(value, time); });
break;
}
case DataSource::kInteger: {
auto s = static_cast<IntegerSource*>(source);
// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](int64_t value, int64_t time) { AppendValue(value, time); });
break;
}
default:
break;
// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](bool value, int64_t time) { AppendValue(value, time); });
} else if (auto s = dynamic_cast<DoubleSource*>(source)) {
m_digitalSource = false;

// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](double value, int64_t time) { AppendValue(value, time); });
} else if (auto s = dynamic_cast<FloatSource*>(source)) {
m_digitalSource = false;

// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](double value, int64_t time) { AppendValue(value, time); });
} else if (auto s = dynamic_cast<IntegerSource*>(source)) {
m_digitalSource = false;

// add initial value
AppendValue(s->GetValue(), 0);

m_newValueConn = s->valueChanged.connect_connection(
[this](int64_t value, int64_t time) { AppendValue(value, time); });
}
}

Expand Down Expand Up @@ -559,15 +552,15 @@ Plot::Plot(Storage& storage)

void Plot::DragDropAccept(PlotView& view, size_t i, int yAxis) {
// accept any of double/float/boolean/integer sources
DataSource* source = DoubleSource::AcceptDragDropPayload();
DataSource* source = AcceptDragDropPayload<DoubleSource>();
if (!source) {
source = FloatSource::AcceptDragDropPayload();
source = AcceptDragDropPayload<FloatSource>();
}
if (!source) {
source = BooleanSource::AcceptDragDropPayload();
source = AcceptDragDropPayload<BooleanSource>();
}
if (!source) {
source = IntegerSource::AcceptDragDropPayload();
source = AcceptDragDropPayload<IntegerSource>();
}
if (source) {
// don't add duplicates unless it's onto a different Y axis
Expand Down
Loading

0 comments on commit 2ad82ee

Please sign in to comment.