Skip to content

Commit

Permalink
[glass] Split DataSource into type-specific variants
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Dec 24, 2024
1 parent 19d385d commit 0cbee59
Show file tree
Hide file tree
Showing 62 changed files with 667 additions and 386 deletions.
86 changes: 76 additions & 10 deletions glass/src/lib/native/cpp/DataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
#include <fmt/format.h>

#include "glass/ContextInternal.h"
#include "imgui.h"

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)} {}
std::string glass::MakeSourceId(std::string_view id, int index, int index2) {
return fmt::format("{}[{},{}]", id, index, index2);
}

DataSource::DataSource(std::string_view id, int index, int index2)
: DataSource{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) {
Expand Down Expand Up @@ -99,8 +101,7 @@ bool DataSource::InputInt(const char* label, int* v, int step, int step_fast,

void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
if (ImGui::BeginDragDropSource(flags)) {
auto self = this;
ImGui::SetDragDropPayload("DataSource", &self, sizeof(self)); // NOLINT
SetDragDropPayload();
const char* name = GetName().c_str();
ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
ImGui::EndDragDropSource();
Expand All @@ -114,3 +115,68 @@ DataSource* DataSource::Find(std::string_view id) {
}
return it->second;
}

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 FloatSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("FloatSource", &self, sizeof(self)); // NOLINT
}

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

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

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

void StringSource::SetDragDropPayload() const {
auto self = this;
ImGui::SetDragDropPayload("StringSource", &self, sizeof(self)); // NOLINT
}
10 changes: 5 additions & 5 deletions glass/src/lib/native/cpp/hardware/DIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ void DisplayDIOImpl(DIOModel* model, int index, bool outputsEnabled) {
dioData->LabelText(label, "unknown");
ImGui::PopStyleColor();
} else if (model->IsReadOnly()) {
dioData->LabelText(
label, "%s",
outputsEnabled ? (dioData->GetValue() != 0 ? "1 (high)" : "0 (low)")
: "1 (disabled)");
dioData->LabelText(label, "%s",
outputsEnabled
? (dioData->GetValue() ? "1 (high)" : "0 (low)")
: "1 (disabled)");

} else {
static const char* options[] = {"0 (low)", "1 (high)"};
int val = dioData->GetValue() != 0 ? 1 : 0;
int val = dioData->GetValue() ? 1 : 0;
if (dioData->Combo(label, &val, options, 2)) {
model->SetValue(val);
}
Expand Down
1 change: 0 additions & 1 deletion glass/src/lib/native/cpp/hardware/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <imgui.h>
#include <imgui_internal.h>

#include "glass/Context.h"
#include "glass/DataSource.h"

using namespace glass;
Expand Down
2 changes: 1 addition & 1 deletion glass/src/lib/native/cpp/hardware/Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void glass::DisplayRelay(RelayModel* model, int index, bool outputsEnabled) {
IM_COL32(128, 128, 128, 255)};
int values[2] = {reverseData ? (reverse ? 2 : -2) : -3,
forwardData ? (forward ? 1 : -1) : -3};
DataSource* sources[2] = {reverseData, forwardData};
BooleanSource* sources[2] = {reverseData, forwardData};
DrawLEDSources(values, sources, 2, 2, colors);
}

Expand Down
1 change: 0 additions & 1 deletion glass/src/lib/native/cpp/other/CommandSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <imgui.h>

#include "glass/Context.h"
#include "glass/DataSource.h"

using namespace glass;
Expand Down
1 change: 0 additions & 1 deletion glass/src/lib/native/cpp/other/Drive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <imgui_internal.h>
#include <numbers>

#include "glass/Context.h"
#include "glass/DataSource.h"

using namespace glass;
Expand Down
22 changes: 11 additions & 11 deletions glass/src/lib/native/cpp/other/FMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ void glass::DisplayFMS(FMSModel* model, bool editableDsAttached) {
}

// Game Specific Message
wpi::SmallString<64> gameSpecificMessageBuf;
std::string gameSpecificMessage{
model->GetGameSpecificMessage(gameSpecificMessageBuf)};
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (ImGui::InputText("Game Specific", &gameSpecificMessage)) {
model->SetGameSpecificMessage(gameSpecificMessage);
if (auto data = model->GetGameSpecificMessageData()) {
std::string gameSpecificMessage = data->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (ImGui::InputText("Game Specific", &gameSpecificMessage)) {
model->SetGameSpecificMessage(gameSpecificMessage);
}
}
}

Expand Down Expand Up @@ -148,11 +148,11 @@ void glass::DisplayFMSReadOnly(FMSModel* model) {
ImGui::TextUnformatted("?");
}
}

wpi::SmallString<64> gameSpecificMessageBuf;
std::string_view gameSpecificMessage =
model->GetGameSpecificMessage(gameSpecificMessageBuf);
ImGui::Text("Game Specific: %s", exists ? gameSpecificMessage.data() : "?");
if (auto data = model->GetGameSpecificMessageData()) {
wpi::SmallString<64> gsmBuf;
ImGui::Text("Game Specific: %s",
exists ? data->GetValue(gsmBuf).data() : "?");
}

if (!exists) {
ImGui::PopStyleColor();
Expand Down
3 changes: 0 additions & 3 deletions glass/src/lib/native/cpp/other/PIDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

#include "glass/other/PIDController.h"

#include <string>

#include <imgui.h>

#include "glass/Context.h"
#include "glass/DataSource.h"

using namespace glass;
Expand Down
61 changes: 53 additions & 8 deletions glass/src/lib/native/cpp/other/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class PlotSeries {
private:
bool IsDigital() const {
return m_digital.GetValue() == kDigital ||
(m_digital.GetValue() == kAuto && m_source && m_source->IsDigital());
(m_digital.GetValue() == kAuto && m_source &&
m_source->GetKind() == DataSource::kBoolean);
}
void AppendValue(double value, int64_t time);

Expand Down Expand Up @@ -255,11 +256,46 @@ void PlotSeries::CheckSource() {
void PlotSeries::SetSource(DataSource* source) {
m_source = source;

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

m_newValueConn = source->valueChanged.connect_connection(
[this](double value, int64_t time) { AppendValue(value, time); });
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;
}
}

void PlotSeries::AppendValue(double value, int64_t timeUs) {
Expand Down Expand Up @@ -522,9 +558,18 @@ Plot::Plot(Storage& storage)
}

void Plot::DragDropAccept(PlotView& view, size_t i, int yAxis) {
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload("DataSource")) {
auto source = *static_cast<DataSource**>(payload->Data);
// accept any of double/float/boolean/integer sources
DataSource* source = DoubleSource::AcceptDragDropPayload();
if (!source) {
source = FloatSource::AcceptDragDropPayload();
}
if (!source) {
source = BooleanSource::AcceptDragDropPayload();
}
if (!source) {
source = IntegerSource::AcceptDragDropPayload();
}
if (source) {
// don't add duplicates unless it's onto a different Y axis
auto it =
std::find_if(m_series.begin(), m_series.end(), [=](const auto& elem) {
Expand Down
3 changes: 0 additions & 3 deletions glass/src/lib/native/cpp/other/ProfiledPIDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

#include "glass/other/ProfiledPIDController.h"

#include <string>

#include <imgui.h>

#include "glass/Context.h"
#include "glass/DataSource.h"

using namespace glass;
Expand Down
2 changes: 1 addition & 1 deletion glass/src/lib/native/cpp/support/ExtraGuiWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace glass {

void DrawLEDSources(const int* values, DataSource** sources, int numValues,
void DrawLEDSources(const int* values, BooleanSource** sources, int numValues,
int cols, const ImU32* colors, float size, float spacing,
const LEDConfig& config) {
if (numValues == 0 || cols < 1) {
Expand Down
Loading

0 comments on commit 0cbee59

Please sign in to comment.