Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Nov 2, 2024
1 parent 1382d8a commit 6dd1977
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 63 deletions.
4 changes: 2 additions & 2 deletions glass/src/lib/native/cpp/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void Window::Display() {

if (BeginWindow()) {
if (m_renamePopupEnabled || m_view->HasSettings()) {
if (BeginWindowSettingsPopup()) {
if (imm::BeginWindowSettingsPopup()) {
if (m_renamePopupEnabled) {
EditName();
}
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions glass/src/lib/native/include/glass/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ class Window {
[[nodiscard]]
bool BeginWindow();
void EndWindow();
[[nodiscard]]
bool BeginWindowSettingsPopup();

bool EditName() { return ItemEditName(&m_name); }

Expand Down Expand Up @@ -201,9 +199,7 @@ inline void EndWindow() {
}

[[nodiscard]]
inline bool BeginWindowSettingsPopup() {
return GetWindow()->BeginWindowSettingsPopup();
}
bool BeginWindowSettingsPopup();

} // namespace imm

Expand Down
41 changes: 21 additions & 20 deletions glass/src/libnt/native/cpp/NetworkTablesProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <wpigui.h>

#include "glass/Storage.h"
#include "glass/Window.h"

using namespace glass;

Expand All @@ -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) {
Expand All @@ -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();
});
Expand All @@ -66,9 +63,9 @@ NetworkTablesProvider::NetworkTablesProvider(Storage& storage,
void NetworkTablesProvider::DisplayMenu() {
wpi::SmallVector<std::string_view, 6> 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;
Expand All @@ -81,14 +78,16 @@ void NetworkTablesProvider::DisplayMenu() {
}

if (fullDepth) {
bool visible = entry->window && entry->window->IsVisible();
auto* data = entry.value().GetData<Entry>();
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));
Expand All @@ -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);

Check failure on line 101 in glass/src/libnt/native/cpp/NetworkTablesProvider.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'Show': identifier not found
} else if (wasVisible && !visible && data->window) {
data->window.reset();
data->model.reset();
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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<>();

Check failure on line 165 in glass/src/libnt/native/cpp/NetworkTablesProvider.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'glass::Storage::GetOrNewData': no matching overloaded function found
GetOrCreateView(builderIt->second, nt::Topic{valueData->topic},
tableName);
// cache the type
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,21 @@
#include <wpi/StringMap.h>

#include "glass/Model.h"
#include "glass/Provider.h"
#include "glass/Storage.h"

namespace glass {

class Window;

namespace detail {
struct NTProviderFunctions {
using Exists =
std::function<bool(nt::NetworkTableInstance inst, const char* path)>;
using CreateModel = std::function<std::unique_ptr<Model>(
nt::NetworkTableInstance inst, const char* path)>;
using ViewExists = std::function<bool(Model*, const char* path)>;
using CreateView =
std::function<std::unique_ptr<View>(Window*, Model*, const char* path)>;
};
} // namespace detail

/**
* A provider for NetworkTables (SmartDashboard style) models and views.
* NetworkTables (SmartDashboard style) windows.
*/
class NetworkTablesProvider : private Provider<detail::NTProviderFunctions> {
class NetworkTablesProvider {
public:
using Provider::CreateModelFunc;
using Provider::CreateViewFunc;
using CreateModelFunc = std::function<std::unique_ptr<Model>(
nt::NetworkTableInstance inst, const char* path)>;
using SetupWindowFunc = std::function<void(Window*)>;
using DisplayFunc = std::function<void(Model*)>;

explicit NetworkTablesProvider(Storage& storage);
NetworkTablesProvider(Storage& storage, nt::NetworkTableInstance inst);
Expand All @@ -54,28 +44,30 @@ class NetworkTablesProvider : private Provider<detail::NTProviderFunctions> {
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;
Expand All @@ -86,10 +78,11 @@ class NetworkTablesProvider : private Provider<detail::NTProviderFunctions> {

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<Builder> m_typeMap;

struct SubListener {
Expand All @@ -100,18 +93,18 @@ class NetworkTablesProvider : private Provider<detail::NTProviderFunctions> {
// mapping from .type topic to subscriber/listener
wpi::DenseMap<NT_Topic, SubListener> 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> model;
std::unique_ptr<Window> window;
};
// active windows
#if 0
ViewEntry* GetOrCreateView(const Builder& builder, nt::Topic typeTopic,
std::string_view name);
#endif
};

/**
Expand Down

0 comments on commit 6dd1977

Please sign in to comment.