[glass] Add Alerts widget (#7219)

This commit is contained in:
Ryan Heuer
2024-10-16 14:45:56 -05:00
committed by GitHub
parent 68715aa484
commit f7dddb8014
5 changed files with 166 additions and 0 deletions

View File

@@ -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 <IconsFontAwesome6.h>
#include <imgui.h>
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());
}
}

View File

@@ -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 <string>
#include <vector>
#include "glass/Model.h"
namespace glass {
class AlertsModel : public Model {
public:
virtual const std::vector<std::string>& GetInfos() = 0;
virtual const std::vector<std::string>& GetWarnings() = 0;
virtual const std::vector<std::string>& GetErrors() = 0;
};
void DisplayAlerts(AlertsModel* model);
} // namespace glass

View File

@@ -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 <utility>
#include <fmt/format.h>
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();
}

View File

@@ -4,6 +4,7 @@
#include <memory>
#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<NTAlertsModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetDefaultSize(300, 150);
return MakeFunctionView(
[=] { DisplayAlerts(static_cast<NTAlertsModel*>(model)); });
});
provider.Register(
NTCommandSchedulerModel::kType,
[](nt::NetworkTableInstance inst, const char* path) {

View File

@@ -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 <string>
#include <string_view>
#include <vector>
#include <networktables/NetworkTableInstance.h>
#include <networktables/StringArrayTopic.h>
#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<std::string>& GetInfos() override { return m_infosValue; }
const std::vector<std::string>& GetWarnings() override {
return m_warningsValue;
}
const std::vector<std::string>& 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<std::string> m_infosValue;
std::vector<std::string> m_warningsValue;
std::vector<std::string> m_errorsValue;
};
} // namespace glass