forked from wpilibsuite/allwpilib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[glass] Add Alerts widget (wpilibsuite#7219)
- Loading branch information
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
glass/src/libnt/native/include/glass/networktables/NTAlerts.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |