diff --git a/glass/src/lib/native/cpp/other/Alerts.cpp b/glass/src/lib/native/cpp/other/Alerts.cpp new file mode 100644 index 00000000000..87cd5885e29 --- /dev/null +++ b/glass/src/lib/native/cpp/other/Alerts.cpp @@ -0,0 +1,32 @@ +// 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/other/Alerts.h" + +#include +#include + +using namespace glass; + +void glass::DisplayAlerts(AlertsModel* model) { + auto& infos = model->GetInfos(); + auto& warnings = model->GetWarnings(); + auto& errors = model->GetErrors(); + + const ImVec4 kWarningColor{0.85f, 0.56f, 0.0f, 1.0f}; + const ImVec4 kErrorColor{0.9f, 0.25f, 0.25f, 1.0f}; + + // show higher severity alerts on top + for (auto&& error : errors) { + ImGui::TextColored(kErrorColor, "%s %s", ICON_FA_CIRCLE_XMARK, + error.c_str()); + } + for (auto&& warning : warnings) { + ImGui::TextColored(kWarningColor, "%s %s", ICON_FA_TRIANGLE_EXCLAMATION, + warning.c_str()); + } + for (auto&& info : infos) { + ImGui::Text("%s %s", ICON_FA_CIRCLE_INFO, info.c_str()); + } +} diff --git a/glass/src/lib/native/include/glass/other/Alerts.h b/glass/src/lib/native/include/glass/other/Alerts.h new file mode 100644 index 00000000000..849f3e3b143 --- /dev/null +++ b/glass/src/lib/native/include/glass/other/Alerts.h @@ -0,0 +1,23 @@ +// 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. + +#pragma once + +#include +#include + +#include "glass/Model.h" + +namespace glass { + +class AlertsModel : public Model { + public: + virtual const std::vector& GetInfos() = 0; + virtual const std::vector& GetWarnings() = 0; + virtual const std::vector& GetErrors() = 0; +}; + +void DisplayAlerts(AlertsModel* model); + +} // namespace glass diff --git a/glass/src/libnt/native/cpp/NTAlerts.cpp b/glass/src/libnt/native/cpp/NTAlerts.cpp new file mode 100644 index 00000000000..1257ea2a8f7 --- /dev/null +++ b/glass/src/libnt/native/cpp/NTAlerts.cpp @@ -0,0 +1,51 @@ +// 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/networktables/NTAlerts.h" + +#include + +#include + +using namespace glass; + +NTAlertsModel::NTAlertsModel(std::string_view path) + : NTAlertsModel{nt::NetworkTableInstance::GetDefault(), path} {} + +NTAlertsModel::NTAlertsModel(nt::NetworkTableInstance inst, + std::string_view path) + : m_inst{inst}, + m_infos{m_inst.GetStringArrayTopic(fmt::format("{}/infos", path)) + .Subscribe({})}, + m_warnings{m_inst.GetStringArrayTopic(fmt::format("{}/warnings", path)) + .Subscribe({})}, + m_errors{m_inst.GetStringArrayTopic(fmt::format("{}/errors", path)) + .Subscribe({})} {} + +void NTAlertsModel::Update() { + if (!m_infos.Exists()) { + m_infosValue.clear(); + } + for (auto&& v : m_infos.ReadQueue()) { + m_infosValue = std::move(v.value); + } + + if (!m_warnings.Exists()) { + m_warningsValue.clear(); + } + for (auto&& v : m_warnings.ReadQueue()) { + m_warningsValue = std::move(v.value); + } + + if (!m_errors.Exists()) { + m_errorsValue.clear(); + } + for (auto&& v : m_errors.ReadQueue()) { + m_errorsValue = std::move(v.value); + } +} + +bool NTAlertsModel::Exists() { + return m_infos.Exists(); +} diff --git a/glass/src/libnt/native/cpp/StandardNetworkTables.cpp b/glass/src/libnt/native/cpp/StandardNetworkTables.cpp index 05796c39a76..6def5a931ce 100644 --- a/glass/src/libnt/native/cpp/StandardNetworkTables.cpp +++ b/glass/src/libnt/native/cpp/StandardNetworkTables.cpp @@ -4,6 +4,7 @@ #include +#include "glass/networktables/NTAlerts.h" #include "glass/networktables/NTCommandScheduler.h" #include "glass/networktables/NTCommandSelector.h" #include "glass/networktables/NTDifferentialDrive.h" @@ -24,6 +25,16 @@ using namespace glass; void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) { + provider.Register( + NTAlertsModel::kType, + [](nt::NetworkTableInstance inst, const char* path) { + return std::make_unique(inst, path); + }, + [](Window* win, Model* model, const char*) { + win->SetDefaultSize(300, 150); + return MakeFunctionView( + [=] { DisplayAlerts(static_cast(model)); }); + }); provider.Register( NTCommandSchedulerModel::kType, [](nt::NetworkTableInstance inst, const char* path) { diff --git a/glass/src/libnt/native/include/glass/networktables/NTAlerts.h b/glass/src/libnt/native/include/glass/networktables/NTAlerts.h new file mode 100644 index 00000000000..4148d4b3452 --- /dev/null +++ b/glass/src/libnt/native/include/glass/networktables/NTAlerts.h @@ -0,0 +1,49 @@ +// 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. + +#pragma once + +#include +#include +#include + +#include +#include + +#include "glass/other/Alerts.h" + +namespace glass { + +class NTAlertsModel : public AlertsModel { + public: + static constexpr const char* kType = "Alerts"; + + // path is to the table containing ".type", excluding the trailing / + explicit NTAlertsModel(std::string_view path); + NTAlertsModel(nt::NetworkTableInstance inst, std::string_view path); + + const std::vector& GetInfos() override { return m_infosValue; } + + const std::vector& GetWarnings() override { + return m_warningsValue; + } + + const std::vector& GetErrors() override { return m_errorsValue; } + + void Update() override; + bool Exists() override; + bool IsReadOnly() override { return false; } + + private: + nt::NetworkTableInstance m_inst; + nt::StringArraySubscriber m_infos; + nt::StringArraySubscriber m_warnings; + nt::StringArraySubscriber m_errors; + + std::vector m_infosValue; + std::vector m_warningsValue; + std::vector m_errorsValue; +}; + +} // namespace glass