2020-12-26 14:31:24 -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.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/networktables/NTStringChooser.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2024-01-03 09:38:10 -08:00
|
|
|
#include <wpi/json.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTStringChooserModel::NTStringChooserModel(std::string_view path)
|
2022-10-08 10:01:31 -07:00
|
|
|
: NTStringChooserModel{nt::NetworkTableInstance::GetDefault(), path} {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
NTStringChooserModel::NTStringChooserModel(nt::NetworkTableInstance inst,
|
|
|
|
|
std::string_view path)
|
|
|
|
|
: m_inst{inst},
|
|
|
|
|
m_default{
|
|
|
|
|
m_inst.GetStringTopic(fmt::format("{}/default", path)).Subscribe("")},
|
2024-01-03 09:38:10 -08:00
|
|
|
m_selected{m_inst.GetStringTopic(fmt::format("{}/selected", path))
|
|
|
|
|
.Subscribe("")},
|
|
|
|
|
m_selectedPub{m_inst.GetStringTopic(fmt::format("{}/selected", path))
|
|
|
|
|
.PublishEx("string", {{"retained", true}})},
|
2022-10-08 10:01:31 -07:00
|
|
|
m_active{
|
|
|
|
|
m_inst.GetStringTopic(fmt::format("{}/active", path)).Subscribe("")},
|
|
|
|
|
m_options{m_inst.GetStringArrayTopic(fmt::format("{}/options", path))
|
2024-01-03 09:38:10 -08:00
|
|
|
.Subscribe({})} {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void NTStringChooserModel::SetSelected(std::string_view val) {
|
2024-01-03 09:38:10 -08:00
|
|
|
m_selectedPub.Set(val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void NTStringChooserModel::Update() {
|
|
|
|
|
if (!m_default.Exists()) {
|
|
|
|
|
m_defaultValue.clear();
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_default.ReadQueue()) {
|
|
|
|
|
m_defaultValue = std::move(v.value);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
if (!m_selected.Exists()) {
|
|
|
|
|
m_selectedValue.clear();
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_selected.ReadQueue()) {
|
|
|
|
|
m_selectedValue = std::move(v.value);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
if (!m_active.Exists()) {
|
|
|
|
|
m_activeValue.clear();
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_active.ReadQueue()) {
|
|
|
|
|
m_activeValue = std::move(v.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_options.Exists()) {
|
|
|
|
|
m_optionsValue.clear();
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_options.ReadQueue()) {
|
|
|
|
|
m_optionsValue = std::move(v.value);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NTStringChooserModel::Exists() {
|
2022-11-20 17:27:28 -08:00
|
|
|
return m_options.Exists();
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|