From 6dd197729dafb82ed7345262190e347e38ff7e6b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 1 Nov 2024 20:09:17 -0700 Subject: [PATCH] WIP --- glass/src/lib/native/cpp/Window.cpp | 4 +- glass/src/lib/native/include/glass/Window.h | 6 +- .../native/cpp/NetworkTablesProvider.cpp | 41 ++++++------ .../networktables/NetworkTablesProvider.h | 65 +++++++++---------- 4 files changed, 53 insertions(+), 63 deletions(-) diff --git a/glass/src/lib/native/cpp/Window.cpp b/glass/src/lib/native/cpp/Window.cpp index 97320d43562..3dc42cd4b0a 100644 --- a/glass/src/lib/native/cpp/Window.cpp +++ b/glass/src/lib/native/cpp/Window.cpp @@ -42,7 +42,7 @@ void Window::Display() { if (BeginWindow()) { if (m_renamePopupEnabled || m_view->HasSettings()) { - if (BeginWindowSettingsPopup()) { + if (imm::BeginWindowSettingsPopup()) { if (m_renamePopupEnabled) { EditName(); } @@ -118,7 +118,7 @@ void Window::EndWindow() { } } -bool Window::BeginWindowSettingsPopup() { +bool imm::BeginWindowSettingsPopup() { bool isClicked = (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && ImGui::IsItemHovered()); ImGuiWindow* window = ImGui::GetCurrentWindow(); diff --git a/glass/src/lib/native/include/glass/Window.h b/glass/src/lib/native/include/glass/Window.h index 44ca510fa06..f1aab6e17aa 100644 --- a/glass/src/lib/native/include/glass/Window.h +++ b/glass/src/lib/native/include/glass/Window.h @@ -130,8 +130,6 @@ class Window { [[nodiscard]] bool BeginWindow(); void EndWindow(); - [[nodiscard]] - bool BeginWindowSettingsPopup(); bool EditName() { return ItemEditName(&m_name); } @@ -201,9 +199,7 @@ inline void EndWindow() { } [[nodiscard]] -inline bool BeginWindowSettingsPopup() { - return GetWindow()->BeginWindowSettingsPopup(); -} +bool BeginWindowSettingsPopup(); } // namespace imm diff --git a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp index 36e892ecaa0..93af206e5e3 100644 --- a/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp +++ b/glass/src/libnt/native/cpp/NetworkTablesProvider.cpp @@ -15,6 +15,7 @@ #include #include "glass/Storage.h" +#include "glass/Window.h" using namespace glass; @@ -23,14 +24,14 @@ NetworkTablesProvider::NetworkTablesProvider(Storage& storage) NetworkTablesProvider::NetworkTablesProvider(Storage& storage, nt::NetworkTableInstance inst) - : Provider{storage.GetChild("windows")}, + : m_windows{storage.GetChild("windows")}, m_inst{inst}, m_poller{inst}, m_typeCache{storage.GetChild("types")} { - storage.SetCustomApply([this] { + m_windows.SetCustomApply([this] { m_listener = m_poller.AddListener( {{""}}, nt::EventFlags::kImmediate | nt::EventFlags::kTopic); - for (auto&& childIt : m_storage.GetChildren()) { + for (auto&& childIt : m_windows.GetChildren()) { auto id = childIt.key(); auto typePtr = m_typeCache.FindValue(id); if (!typePtr || typePtr->type != Storage::Value::kString) { @@ -53,11 +54,7 @@ NetworkTablesProvider::NetworkTablesProvider(Storage& storage, storage.SetCustomClear([this, &storage] { m_poller.RemoveListener(m_listener); m_listener = 0; - for (auto&& modelEntry : m_modelEntries) { - modelEntry->model.reset(); - } - m_viewEntries.clear(); - m_windows.clear(); + m_windows.EraseAll(); m_typeCache.EraseAll(); storage.ClearValues(); }); @@ -66,9 +63,9 @@ NetworkTablesProvider::NetworkTablesProvider(Storage& storage, void NetworkTablesProvider::DisplayMenu() { wpi::SmallVector path; wpi::SmallString<64> name; - for (auto&& entry : m_viewEntries) { + for (auto&& entry : m_windows.GetChildren()) { path.clear(); - wpi::split(entry->name, path, '/', -1, false); + wpi::split(entry.key(), path, '/', -1, false); bool fullDepth = true; int depth = 0; @@ -81,14 +78,16 @@ void NetworkTablesProvider::DisplayMenu() { } if (fullDepth) { - bool visible = entry->window && entry->window->IsVisible(); + auto* data = entry.value().GetData(); + assert(data); + bool visible = data->window && data->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); // Add type label to smartdashboard sendables - if (wpi::starts_with(entry->name, "/SmartDashboard/")) { - auto typeEntry = m_typeCache.FindValue(entry->name); + if (wpi::starts_with(entry.key(), "/SmartDashboard/")) { + auto typeEntry = m_typeCache.FindValue(entry.key()); if (typeEntry) { ImGui::SameLine(); ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255)); @@ -99,9 +98,10 @@ void NetworkTablesProvider::DisplayMenu() { } } if (!wasVisible && visible) { - Show(entry.get(), entry->window); - } else if (wasVisible && !visible && entry->window) { - entry->window->SetVisible(false); + Show(entry.key(), data); + } else if (wasVisible && !visible && data->window) { + data->window.reset(); + data->model.reset(); } } @@ -112,8 +112,6 @@ void NetworkTablesProvider::DisplayMenu() { } void NetworkTablesProvider::Update() { - Provider::Update(); - for (auto&& event : m_poller.ReadQueue()) { if (auto info = event.GetTopicInfo()) { // add/remove entries from NT changes @@ -163,6 +161,8 @@ void NetworkTablesProvider::Update() { auto tableName = wpi::remove_suffix(topicName, "/.type").value_or(topicName); + auto& storage = m_windows.GetChild(tableName); + storage.GetOrNewData<>(); GetOrCreateView(builderIt->second, nt::Topic{valueData->topic}, tableName); // cache the type @@ -173,8 +173,9 @@ void NetworkTablesProvider::Update() { void NetworkTablesProvider::Register(std::string_view typeName, CreateModelFunc createModel, - CreateViewFunc createView) { - m_typeMap[typeName] = Builder{std::move(createModel), std::move(createView)}; + SetupWindowFunc setupWindow, + DisplayFunc display) { + m_typeMap[typeName] = Builder{std::move(createModel), std::move(setupWindow), std::move(display)}; } void NetworkTablesProvider::Show(ViewEntry* entry, Window* window) { diff --git a/glass/src/libnt/native/include/glass/networktables/NetworkTablesProvider.h b/glass/src/libnt/native/include/glass/networktables/NetworkTablesProvider.h index cba34cc6693..3f3a35faad2 100644 --- a/glass/src/libnt/native/include/glass/networktables/NetworkTablesProvider.h +++ b/glass/src/libnt/native/include/glass/networktables/NetworkTablesProvider.h @@ -17,31 +17,21 @@ #include #include "glass/Model.h" -#include "glass/Provider.h" +#include "glass/Storage.h" namespace glass { class Window; -namespace detail { -struct NTProviderFunctions { - using Exists = - std::function; - using CreateModel = std::function( - nt::NetworkTableInstance inst, const char* path)>; - using ViewExists = std::function; - using CreateView = - std::function(Window*, Model*, const char* path)>; -}; -} // namespace detail - /** - * A provider for NetworkTables (SmartDashboard style) models and views. + * NetworkTables (SmartDashboard style) windows. */ -class NetworkTablesProvider : private Provider { +class NetworkTablesProvider { public: - using Provider::CreateModelFunc; - using Provider::CreateViewFunc; + using CreateModelFunc = std::function( + nt::NetworkTableInstance inst, const char* path)>; + using SetupWindowFunc = std::function; + using DisplayFunc = std::function; explicit NetworkTablesProvider(Storage& storage); NetworkTablesProvider(Storage& storage, nt::NetworkTableInstance inst); @@ -54,28 +44,30 @@ class NetworkTablesProvider : private Provider { nt::NetworkTableInstance GetInstance() const { return m_inst; } /** - * Perform global initialization. This should be called prior to - * wpi::gui::Initialize(). + * Displays all windows. */ - void GlobalInit() override { Provider::GlobalInit(); } + void Display(); /** * Displays menu contents as a tree of available NetworkTables views. */ - void DisplayMenu() override; + void DisplayMenu(); /** - * Registers a NetworkTables model and view. + * Registers a NetworkTables model and display. * * @param typeName SmartDashboard .type value to match * @param createModel functor to create model - * @param createView functor to create view + * @param setupWindow functor to set up window + * @param display functor to display window content */ void Register(std::string_view typeName, CreateModelFunc createModel, - CreateViewFunc createView); + SetupWindowFunc setupWindow, DisplayFunc display); + + void Update(); private: - void Update() override; + Storage& m_windows; nt::NetworkTableInstance m_inst; nt::NetworkTableListenerPoller m_poller; @@ -86,10 +78,11 @@ class NetworkTablesProvider : private Provider { struct Builder { CreateModelFunc createModel; - CreateViewFunc createView; + SetupWindowFunc setupWindow; + DisplayFunc display; }; - // mapping from .type string to model/view creators + // mapping from .type string to builders wpi::StringMap m_typeMap; struct SubListener { @@ -100,18 +93,18 @@ class NetworkTablesProvider : private Provider { // mapping from .type topic to subscriber/listener wpi::DenseMap m_topicMap; - struct Entry : public ModelEntry { - Entry(nt::Topic typeTopic, std::string_view name, const Builder& builder) - : ModelEntry{name, [](auto, const char*) { return true; }, - builder.createModel}, - typeTopic{typeTopic} {} - nt::Topic typeTopic; - }; - - void Show(ViewEntry* entry, Window* window) override; + struct Entry { + explicit Entry(Storage& storage) : typeString{storage.GetString("type")} {} + std::string& typeString; + std::unique_ptr model; + std::unique_ptr window; + }; + // active windows +#if 0 ViewEntry* GetOrCreateView(const Builder& builder, nt::Topic typeTopic, std::string_view name); +#endif }; /**