2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2018-09-28 04:18:18 -04:00
|
|
|
|
|
|
|
|
#include "frc/shuffleboard/ShuffleboardInstance.h"
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2018-09-28 04:18:18 -04:00
|
|
|
#include <networktables/NetworkTable.h>
|
|
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2020-08-27 20:45:14 -07:00
|
|
|
#include <wpi/SmallVector.h>
|
2018-09-28 04:18:18 -04:00
|
|
|
#include <wpi/StringMap.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/shuffleboard/Shuffleboard.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc::detail;
|
|
|
|
|
|
|
|
|
|
struct ShuffleboardInstance::Impl {
|
2021-06-11 20:16:35 -07:00
|
|
|
wpi::StringMap<std::unique_ptr<ShuffleboardTab>> tabs;
|
2018-09-28 04:18:18 -04:00
|
|
|
|
|
|
|
|
bool tabsChanged = false;
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> rootTable;
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> rootMetaTable;
|
2023-03-27 06:12:55 +03:00
|
|
|
nt::StringPublisher selectedTabPub;
|
2018-09-28 04:18:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ShuffleboardInstance::ShuffleboardInstance(nt::NetworkTableInstance ntInstance)
|
|
|
|
|
: m_impl(new Impl) {
|
|
|
|
|
m_impl->rootTable = ntInstance.GetTable(Shuffleboard::kBaseTableName);
|
|
|
|
|
m_impl->rootMetaTable = m_impl->rootTable->GetSubTable(".metadata");
|
2023-03-27 06:12:55 +03:00
|
|
|
m_impl->selectedTabPub =
|
|
|
|
|
m_impl->rootMetaTable->GetStringTopic("Selected")
|
|
|
|
|
.Publish(nt::PubSubOptions{.keepDuplicates = true});
|
2019-05-30 12:36:49 -04:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Shuffleboard, 0);
|
2018-09-28 04:18:18 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 00:37:33 -08:00
|
|
|
ShuffleboardInstance::~ShuffleboardInstance() = default;
|
2018-09-28 04:18:18 -04:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
frc::ShuffleboardTab& ShuffleboardInstance::GetTab(std::string_view title) {
|
2018-09-28 04:18:18 -04:00
|
|
|
if (m_impl->tabs.find(title) == m_impl->tabs.end()) {
|
2021-06-11 20:16:35 -07:00
|
|
|
m_impl->tabs.try_emplace(title,
|
|
|
|
|
std::make_unique<ShuffleboardTab>(*this, title));
|
2018-09-28 04:18:18 -04:00
|
|
|
m_impl->tabsChanged = true;
|
|
|
|
|
}
|
2021-06-11 20:16:35 -07:00
|
|
|
return *m_impl->tabs.find(title)->second;
|
2018-09-28 04:18:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShuffleboardInstance::Update() {
|
|
|
|
|
if (m_impl->tabsChanged) {
|
2020-08-27 20:45:14 -07:00
|
|
|
wpi::SmallVector<std::string, 16> tabTitles;
|
2018-09-28 04:18:18 -04:00
|
|
|
for (auto& entry : m_impl->tabs) {
|
2021-06-11 20:16:35 -07:00
|
|
|
tabTitles.emplace_back(entry.second->GetTitle());
|
2018-09-28 04:18:18 -04:00
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_impl->rootMetaTable->GetEntry("Tabs").SetStringArray(tabTitles);
|
2018-09-28 04:18:18 -04:00
|
|
|
m_impl->tabsChanged = false;
|
|
|
|
|
}
|
|
|
|
|
for (auto& entry : m_impl->tabs) {
|
2021-06-11 20:16:35 -07:00
|
|
|
auto& tab = *entry.second;
|
2018-09-28 04:18:18 -04:00
|
|
|
tab.BuildInto(m_impl->rootTable,
|
|
|
|
|
m_impl->rootMetaTable->GetSubTable(tab.GetTitle()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShuffleboardInstance::EnableActuatorWidgets() {
|
|
|
|
|
for (auto& entry : m_impl->tabs) {
|
2021-06-11 20:16:35 -07:00
|
|
|
auto& tab = *entry.second;
|
2018-09-28 04:18:18 -04:00
|
|
|
for (auto& component : tab.GetComponents()) {
|
|
|
|
|
component->EnableIfActuator();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShuffleboardInstance::DisableActuatorWidgets() {
|
|
|
|
|
for (auto& entry : m_impl->tabs) {
|
2021-06-11 20:16:35 -07:00
|
|
|
auto& tab = *entry.second;
|
2018-09-28 04:18:18 -04:00
|
|
|
for (auto& component : tab.GetComponents()) {
|
|
|
|
|
component->DisableIfActuator();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-28 01:12:50 -05:00
|
|
|
|
|
|
|
|
void ShuffleboardInstance::SelectTab(int index) {
|
2023-03-27 06:12:55 +03:00
|
|
|
m_impl->selectedTabPub.Set(std::to_string(index));
|
2018-11-28 01:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void ShuffleboardInstance::SelectTab(std::string_view title) {
|
2023-03-27 06:12:55 +03:00
|
|
|
m_impl->selectedTabPub.Set(title);
|
2018-11-28 01:12:50 -05:00
|
|
|
}
|