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/NetworkTablesProvider.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <ntcore_cpp.h>
|
|
|
|
|
#include <wpi/SmallString.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <wpi/StringExtras.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <wpigui.h>
|
|
|
|
|
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
#include "glass/Storage.h"
|
|
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
using namespace glass;
|
|
|
|
|
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
NetworkTablesProvider::NetworkTablesProvider(Storage& storage)
|
2022-10-08 10:01:31 -07:00
|
|
|
: NetworkTablesProvider{storage, nt::NetworkTableInstance::GetDefault()} {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
NetworkTablesProvider::NetworkTablesProvider(Storage& storage,
|
|
|
|
|
nt::NetworkTableInstance inst)
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
: Provider{storage.GetChild("windows")},
|
2022-10-08 10:01:31 -07:00
|
|
|
m_inst{inst},
|
2022-10-31 21:52:14 -07:00
|
|
|
m_poller{inst},
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
m_typeCache{storage.GetChild("types")} {
|
|
|
|
|
storage.SetCustomApply([this] {
|
2024-09-01 00:44:52 +08:00
|
|
|
m_listener = m_poller.AddListener(
|
|
|
|
|
{{""}}, nt::EventFlags::kImmediate | nt::EventFlags::kTopic);
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
for (auto&& childIt : m_storage.GetChildren()) {
|
|
|
|
|
auto id = childIt.key();
|
|
|
|
|
auto typePtr = m_typeCache.FindValue(id);
|
|
|
|
|
if (!typePtr || typePtr->type != Storage::Value::kString) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
// only handle ones where we have a builder
|
|
|
|
|
auto builderIt = m_typeMap.find(typePtr->stringVal);
|
|
|
|
|
if (builderIt == m_typeMap.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto entry = GetOrCreateView(
|
2022-10-08 10:01:31 -07:00
|
|
|
builderIt->second, m_inst.GetTopic(fmt::format("{}/.type", id)), id);
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
if (entry) {
|
|
|
|
|
Show(entry, nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
storage.SetCustomClear([this, &storage] {
|
2022-10-31 21:52:14 -07:00
|
|
|
m_poller.RemoveListener(m_listener);
|
|
|
|
|
m_listener = 0;
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
for (auto&& modelEntry : m_modelEntries) {
|
|
|
|
|
modelEntry->model.reset();
|
|
|
|
|
}
|
|
|
|
|
m_viewEntries.clear();
|
|
|
|
|
m_windows.clear();
|
|
|
|
|
m_typeCache.EraseAll();
|
|
|
|
|
storage.ClearValues();
|
|
|
|
|
});
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkTablesProvider::DisplayMenu() {
|
2021-06-06 16:13:58 -07:00
|
|
|
wpi::SmallVector<std::string_view, 6> path;
|
2020-09-12 10:55:46 -07:00
|
|
|
wpi::SmallString<64> name;
|
|
|
|
|
for (auto&& entry : m_viewEntries) {
|
|
|
|
|
path.clear();
|
2021-06-06 16:13:58 -07:00
|
|
|
wpi::split(entry->name, path, '/', -1, false);
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
bool fullDepth = true;
|
|
|
|
|
int depth = 0;
|
|
|
|
|
for (; depth < (static_cast<int>(path.size()) - 1); ++depth) {
|
|
|
|
|
name = path[depth];
|
|
|
|
|
if (!ImGui::BeginMenu(name.c_str())) {
|
|
|
|
|
fullDepth = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fullDepth) {
|
|
|
|
|
bool visible = entry->window && entry->window->IsVisible();
|
|
|
|
|
bool wasVisible = visible;
|
|
|
|
|
// FIXME: enabled?
|
|
|
|
|
// data is the last item, so is guaranteed to be null-terminated
|
|
|
|
|
ImGui::MenuItem(path.back().data(), nullptr, &visible, true);
|
2024-01-01 14:58:13 -05:00
|
|
|
// Add type label to smartdashboard sendables
|
|
|
|
|
if (wpi::starts_with(entry->name, "/SmartDashboard/")) {
|
|
|
|
|
auto typeEntry = m_typeCache.FindValue(entry->name);
|
|
|
|
|
if (typeEntry) {
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::Text("%s", typeEntry->stringVal.c_str());
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
ImGui::Dummy(ImVec2(10.0f, 0.0f));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
if (!wasVisible && visible) {
|
|
|
|
|
Show(entry.get(), entry->window);
|
|
|
|
|
} else if (wasVisible && !visible && entry->window) {
|
|
|
|
|
entry->window->SetVisible(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
for (; depth > 0; --depth) {
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkTablesProvider::Update() {
|
|
|
|
|
Provider::Update();
|
|
|
|
|
|
2022-10-31 21:52:14 -07:00
|
|
|
for (auto&& event : m_poller.ReadQueue()) {
|
|
|
|
|
if (auto info = event.GetTopicInfo()) {
|
|
|
|
|
// add/remove entries from NT changes
|
|
|
|
|
// look for .type fields
|
|
|
|
|
if (!wpi::ends_with(info->name, "/.type") || info->type != NT_STRING ||
|
|
|
|
|
info->type_str != "string") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-31 21:52:14 -07:00
|
|
|
if (event.flags & nt::EventFlags::kUnpublish) {
|
|
|
|
|
auto it = m_topicMap.find(info->topic);
|
|
|
|
|
if (it != m_topicMap.end()) {
|
|
|
|
|
m_poller.RemoveListener(it->second.listener);
|
|
|
|
|
m_topicMap.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto it2 = std::find_if(
|
|
|
|
|
m_viewEntries.begin(), m_viewEntries.end(), [&](const auto& elem) {
|
|
|
|
|
return static_cast<Entry*>(elem->modelEntry)
|
|
|
|
|
->typeTopic.GetHandle() == info->topic;
|
|
|
|
|
});
|
|
|
|
|
if (it2 != m_viewEntries.end()) {
|
|
|
|
|
m_viewEntries.erase(it2);
|
|
|
|
|
}
|
|
|
|
|
} else if (event.flags & nt::EventFlags::kPublish) {
|
|
|
|
|
// subscribe to it; use a subscriber so we only get string values
|
|
|
|
|
SubListener sublistener;
|
|
|
|
|
sublistener.subscriber = nt::StringTopic{info->topic}.Subscribe("");
|
|
|
|
|
sublistener.listener = m_poller.AddListener(
|
|
|
|
|
sublistener.subscriber,
|
|
|
|
|
nt::EventFlags::kValueAll | nt::EventFlags::kImmediate);
|
|
|
|
|
m_topicMap.try_emplace(info->topic, std::move(sublistener));
|
|
|
|
|
}
|
|
|
|
|
} else if (auto valueData = event.GetValueEventData()) {
|
|
|
|
|
// handle actual .type strings
|
|
|
|
|
if (!valueData->value.IsString()) {
|
|
|
|
|
continue;
|
2022-10-08 10:01:31 -07:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2022-10-31 21:52:14 -07:00
|
|
|
// only handle ones where we have a builder
|
|
|
|
|
auto builderIt = m_typeMap.find(valueData->value.GetString());
|
|
|
|
|
if (builderIt == m_typeMap.end()) {
|
|
|
|
|
continue;
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
|
2022-10-31 21:52:14 -07:00
|
|
|
auto topicName = nt::GetTopicName(valueData->topic);
|
2024-06-04 21:01:52 -07:00
|
|
|
auto tableName =
|
|
|
|
|
wpi::remove_suffix(topicName, "/.type").value_or(topicName);
|
2022-10-08 10:01:31 -07:00
|
|
|
|
2022-10-31 21:52:14 -07:00
|
|
|
GetOrCreateView(builderIt->second, nt::Topic{valueData->topic},
|
|
|
|
|
tableName);
|
|
|
|
|
// cache the type
|
|
|
|
|
m_typeCache.SetString(tableName, valueData->value.GetString());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void NetworkTablesProvider::Register(std::string_view typeName,
|
2020-09-12 10:55:46 -07:00
|
|
|
CreateModelFunc createModel,
|
|
|
|
|
CreateViewFunc createView) {
|
|
|
|
|
m_typeMap[typeName] = Builder{std::move(createModel), std::move(createView)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NetworkTablesProvider::Show(ViewEntry* entry, Window* window) {
|
|
|
|
|
// if there's already a window, just show it
|
|
|
|
|
if (entry->window) {
|
|
|
|
|
entry->window->SetVisible(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get or create model
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!entry->modelEntry->model) {
|
2020-09-12 10:55:46 -07:00
|
|
|
entry->modelEntry->model =
|
2022-10-08 10:01:31 -07:00
|
|
|
entry->modelEntry->createModel(m_inst, entry->name.c_str());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (!entry->modelEntry->model) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
// the window might exist and we're just not associated to it yet
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!window) {
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
window = GetOrAddWindow(entry->name, true, Window::kHide);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (!window) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-04 21:01:52 -07:00
|
|
|
if (auto name = wpi::remove_prefix(entry->name, "/SmartDashboard/")) {
|
|
|
|
|
window->SetDefaultName(fmt::format("{} (SmartDashboard)", *name));
|
2021-01-17 20:33:42 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
entry->window = window;
|
|
|
|
|
|
|
|
|
|
// create view
|
|
|
|
|
auto view = entry->createView(window, entry->modelEntry->model.get(),
|
|
|
|
|
entry->name.c_str());
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!view) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
window->SetView(std::move(view));
|
|
|
|
|
|
|
|
|
|
entry->window->SetVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkTablesProvider::ViewEntry* NetworkTablesProvider::GetOrCreateView(
|
2022-10-08 10:01:31 -07:00
|
|
|
const Builder& builder, nt::Topic typeTopic, std::string_view name) {
|
2020-09-12 10:55:46 -07:00
|
|
|
// get view entry if it already exists
|
|
|
|
|
auto viewIt = FindViewEntry(name);
|
|
|
|
|
if (viewIt != m_viewEntries.end() && (*viewIt)->name == name) {
|
|
|
|
|
// make sure typeEntry is set in model
|
2022-10-08 10:01:31 -07:00
|
|
|
static_cast<Entry*>((*viewIt)->modelEntry)->typeTopic = typeTopic;
|
2020-09-12 10:55:46 -07:00
|
|
|
return viewIt->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get or create model entry
|
|
|
|
|
auto modelIt = FindModelEntry(name);
|
|
|
|
|
if (modelIt != m_modelEntries.end() && (*modelIt)->name == name) {
|
2022-10-08 10:01:31 -07:00
|
|
|
static_cast<Entry*>(modelIt->get())->typeTopic = typeTopic;
|
2020-09-12 10:55:46 -07:00
|
|
|
} else {
|
|
|
|
|
modelIt = m_modelEntries.emplace(
|
2022-10-08 10:01:31 -07:00
|
|
|
modelIt, std::make_unique<Entry>(typeTopic, name, builder));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create new view entry
|
|
|
|
|
viewIt = m_viewEntries.emplace(
|
|
|
|
|
viewIt,
|
|
|
|
|
std::make_unique<ViewEntry>(
|
|
|
|
|
name, modelIt->get(), [](Model*, const char*) { return true; },
|
|
|
|
|
builder.createView));
|
|
|
|
|
|
|
|
|
|
return viewIt->get();
|
|
|
|
|
}
|