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>
|
|
|
|
|
|
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)
|
2020-09-12 10:55:46 -07:00
|
|
|
: NTStringChooserModel{nt::GetDefaultInstance(), path} {}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTStringChooserModel::NTStringChooserModel(NT_Inst inst, std::string_view path)
|
2020-09-12 10:55:46 -07:00
|
|
|
: m_nt{inst},
|
2021-06-06 16:13:58 -07:00
|
|
|
m_default{m_nt.GetEntry(fmt::format("{}/default", path))},
|
|
|
|
|
m_selected{m_nt.GetEntry(fmt::format("{}/selected", path))},
|
|
|
|
|
m_active{m_nt.GetEntry(fmt::format("{}/active", path))},
|
|
|
|
|
m_options{m_nt.GetEntry(fmt::format("{}/options", path))} {
|
2020-09-12 10:55:46 -07:00
|
|
|
m_nt.AddListener(m_default);
|
|
|
|
|
m_nt.AddListener(m_selected);
|
|
|
|
|
m_nt.AddListener(m_active);
|
|
|
|
|
m_nt.AddListener(m_options);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void NTStringChooserModel::SetDefault(std::string_view val) {
|
2020-09-12 10:55:46 -07:00
|
|
|
nt::SetEntryValue(m_default, nt::Value::MakeString(val));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void NTStringChooserModel::SetSelected(std::string_view val) {
|
2020-09-12 10:55:46 -07:00
|
|
|
nt::SetEntryValue(m_selected, nt::Value::MakeString(val));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void NTStringChooserModel::SetActive(std::string_view val) {
|
2020-09-12 10:55:46 -07:00
|
|
|
nt::SetEntryValue(m_active, nt::Value::MakeString(val));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTStringChooserModel::SetOptions(wpi::ArrayRef<std::string> val) {
|
|
|
|
|
nt::SetEntryValue(m_options, nt::Value::MakeStringArray(val));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTStringChooserModel::Update() {
|
|
|
|
|
for (auto&& event : m_nt.PollListener()) {
|
2021-02-14 13:28:45 -08:00
|
|
|
if (event.entry == m_default) {
|
|
|
|
|
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
|
|
|
|
|
m_defaultValue.clear();
|
|
|
|
|
} else if (event.value && event.value->IsString()) {
|
|
|
|
|
m_defaultValue = event.value->GetString();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.entry == m_selected) {
|
|
|
|
|
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
|
|
|
|
|
m_selectedValue.clear();
|
|
|
|
|
} else if (event.value && event.value->IsString()) {
|
|
|
|
|
m_selectedValue = event.value->GetString();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.entry == m_active) {
|
|
|
|
|
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
|
|
|
|
|
m_activeValue.clear();
|
|
|
|
|
} else if (event.value && event.value->IsString()) {
|
|
|
|
|
m_activeValue = event.value->GetString();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.entry == m_options) {
|
|
|
|
|
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
|
|
|
|
|
m_optionsValue.clear();
|
|
|
|
|
} else if (event.value && event.value->IsStringArray()) {
|
|
|
|
|
m_optionsValue = event.value->GetStringArray();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NTStringChooserModel::Exists() {
|
|
|
|
|
return m_nt.IsConnected() && nt::GetEntryType(m_options) != NT_UNASSIGNED;
|
|
|
|
|
}
|