diff --git a/glass/src/libnt/native/cpp/NTMechanism2D.cpp b/glass/src/libnt/native/cpp/NTMechanism2D.cpp index d98da48d47..a963c28895 100644 --- a/glass/src/libnt/native/cpp/NTMechanism2D.cpp +++ b/glass/src/libnt/native/cpp/NTMechanism2D.cpp @@ -30,80 +30,15 @@ static void ConvertColor(std::string_view in, ImU32* out) { } } -namespace { - -class NTMechanismObjectModel; - -class NTMechanismGroupImpl final { - public: - NTMechanismGroupImpl(nt::NetworkTableInstance inst, std::string_view path, - std::string_view name) - : m_inst{inst}, m_path{path}, m_name{name} {} - - const char* GetName() const { return m_name.c_str(); } - void ForEachObject(wpi::function_ref func); - - void NTUpdate(const nt::Event& event, std::string_view name); - - protected: - nt::NetworkTableInstance m_inst; - std::string m_path; - std::string m_name; - std::vector> m_objects; -}; - -class NTMechanismObjectModel final : public MechanismObjectModel { - public: - NTMechanismObjectModel(nt::NetworkTableInstance inst, std::string_view path, - std::string_view name) - : m_group{inst, path, name}, - m_typeTopic{inst.GetTopic(fmt::format("{}/.type", path))}, - m_colorTopic{inst.GetTopic(fmt::format("{}/color", path))}, - m_weightTopic{inst.GetTopic(fmt::format("{}/weight", path))}, - m_angleTopic{inst.GetTopic(fmt::format("{}/angle", path))}, - m_lengthTopic{inst.GetTopic(fmt::format("{}/length", path))} {} - - const char* GetName() const final { return m_group.GetName(); } - void ForEachObject( - wpi::function_ref func) final { - m_group.ForEachObject(func); - } - - const char* GetType() const final { return m_typeValue.c_str(); } - ImU32 GetColor() const final { return m_colorValue; } - double GetWeight() const final { return m_weightValue; } - frc::Rotation2d GetAngle() const final { return m_angleValue; } - units::meter_t GetLength() const final { return m_lengthValue; } - - bool NTUpdate(const nt::Event& event, std::string_view name); - - private: - NTMechanismGroupImpl m_group; - - nt::Topic m_typeTopic; - nt::Topic m_colorTopic; - nt::Topic m_weightTopic; - nt::Topic m_angleTopic; - nt::Topic m_lengthTopic; - - std::string m_typeValue; - ImU32 m_colorValue = IM_COL32_WHITE; - double m_weightValue = 1.0; - frc::Rotation2d m_angleValue; - units::meter_t m_lengthValue = 0.0_m; -}; - -} // namespace - -void NTMechanismGroupImpl::ForEachObject( +void NTMechanism2DModel::NTMechanismGroupImpl::ForEachObject( wpi::function_ref func) { for (auto&& obj : m_objects) { func(*obj); } } -void NTMechanismGroupImpl::NTUpdate(const nt::Event& event, - std::string_view name) { +void NTMechanism2DModel::NTMechanismGroupImpl::NTUpdate(const nt::Event& event, + std::string_view name) { if (name.empty()) { return; } @@ -140,8 +75,8 @@ void NTMechanismGroupImpl::NTUpdate(const nt::Event& event, } } -bool NTMechanismObjectModel::NTUpdate(const nt::Event& event, - std::string_view childName) { +bool NTMechanism2DModel::NTMechanismObjectModel::NTUpdate( + const nt::Event& event, std::string_view childName) { if (auto info = event.GetTopicInfo()) { if (info->topic == m_typeTopic.GetHandle()) { if (event.flags & nt::EventFlags::kUnpublish) { @@ -181,31 +116,6 @@ bool NTMechanismObjectModel::NTUpdate(const nt::Event& event, return false; } -class NTMechanism2DModel::RootModel final : public MechanismRootModel { - public: - RootModel(nt::NetworkTableInstance inst, std::string_view path, - std::string_view name) - : m_group{inst, path, name}, - m_xTopic{inst.GetTopic(fmt::format("{}/x", path))}, - m_yTopic{inst.GetTopic(fmt::format("{}/y", path))} {} - - const char* GetName() const final { return m_group.GetName(); } - void ForEachObject( - wpi::function_ref func) final { - m_group.ForEachObject(func); - } - - bool NTUpdate(const nt::Event& event, std::string_view childName); - - frc::Translation2d GetPosition() const override { return m_pos; }; - - private: - NTMechanismGroupImpl m_group; - nt::Topic m_xTopic; - nt::Topic m_yTopic; - frc::Translation2d m_pos; -}; - bool NTMechanism2DModel::RootModel::NTUpdate(const nt::Event& event, std::string_view childName) { if (auto info = event.GetTopicInfo()) { diff --git a/glass/src/libnt/native/include/glass/networktables/NTMechanism2D.h b/glass/src/libnt/native/include/glass/networktables/NTMechanism2D.h index e8ffa9d7ee..a2709c7938 100644 --- a/glass/src/libnt/native/include/glass/networktables/NTMechanism2D.h +++ b/glass/src/libnt/native/include/glass/networktables/NTMechanism2D.h @@ -55,7 +55,92 @@ class NTMechanism2DModel : public Mechanism2DModel { frc::Translation2d m_dimensionsValue; ImU32 m_bgColorValue = 0; - class RootModel; + class NTMechanismObjectModel; + class NTMechanismGroupImpl final { + public: + NTMechanismGroupImpl(nt::NetworkTableInstance inst, std::string_view path, + std::string_view name) + : m_inst{inst}, m_path{path}, m_name{name} {} + + const char* GetName() const { return m_name.c_str(); } + void ForEachObject( + wpi::function_ref func); + + void NTUpdate(const nt::Event& event, std::string_view name); + + protected: + nt::NetworkTableInstance m_inst; + std::string m_path; + std::string m_name; + std::vector> m_objects; + }; + + class NTMechanismObjectModel final : public MechanismObjectModel { + public: + NTMechanismObjectModel(nt::NetworkTableInstance inst, std::string_view path, + std::string_view name) + : m_group{inst, path, name}, + m_typeTopic{inst.GetTopic(fmt::format("{}/.type", path))}, + m_colorTopic{inst.GetTopic(fmt::format("{}/color", path))}, + m_weightTopic{inst.GetTopic(fmt::format("{}/weight", path))}, + m_angleTopic{inst.GetTopic(fmt::format("{}/angle", path))}, + m_lengthTopic{inst.GetTopic(fmt::format("{}/length", path))} {} + + const char* GetName() const final { return m_group.GetName(); } + void ForEachObject( + wpi::function_ref func) final { + m_group.ForEachObject(func); + } + + const char* GetType() const final { return m_typeValue.c_str(); } + ImU32 GetColor() const final { return m_colorValue; } + double GetWeight() const final { return m_weightValue; } + frc::Rotation2d GetAngle() const final { return m_angleValue; } + units::meter_t GetLength() const final { return m_lengthValue; } + + bool NTUpdate(const nt::Event& event, std::string_view name); + + private: + NTMechanismGroupImpl m_group; + + nt::Topic m_typeTopic; + nt::Topic m_colorTopic; + nt::Topic m_weightTopic; + nt::Topic m_angleTopic; + nt::Topic m_lengthTopic; + + std::string m_typeValue; + ImU32 m_colorValue = IM_COL32_WHITE; + double m_weightValue = 1.0; + frc::Rotation2d m_angleValue; + units::meter_t m_lengthValue = 0.0_m; + }; + + class RootModel final : public MechanismRootModel { + public: + RootModel(nt::NetworkTableInstance inst, std::string_view path, + std::string_view name) + : m_group{inst, path, name}, + m_xTopic{inst.GetTopic(fmt::format("{}/x", path))}, + m_yTopic{inst.GetTopic(fmt::format("{}/y", path))} {} + + const char* GetName() const final { return m_group.GetName(); } + void ForEachObject( + wpi::function_ref func) final { + m_group.ForEachObject(func); + } + + bool NTUpdate(const nt::Event& event, std::string_view childName); + + frc::Translation2d GetPosition() const override { return m_pos; }; + + private: + NTMechanismGroupImpl m_group; + nt::Topic m_xTopic; + nt::Topic m_yTopic; + frc::Translation2d m_pos; + }; + std::vector> m_roots; };