diff --git a/CMakeLists.txt b/CMakeLists.txt index 5210f1e..37d543e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ target_sources(nf7 file/gl_obj.cc file/luajit_context.cc file/luajit_node.cc + file/node_comment.cc file/node_exprtk.cc file/node_mutex.cc file/node_network.cc diff --git a/file/node_comment.cc b/file/node_comment.cc new file mode 100644 index 0000000..3cd0d38 --- /dev/null +++ b/file/node_comment.cc @@ -0,0 +1,112 @@ +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "nf7.hh" + +#include "common/file_base.hh" +#include "common/generic_memento.hh" +#include "common/generic_type_info.hh" +#include "common/memento.hh" +#include "common/node.hh" +#include "common/ptr_selector.hh" + + +namespace nf7 { +namespace { + +class Comment final : public nf7::FileBase, public nf7::Node { + public: + static inline const nf7::GenericTypeInfo kType = { + "Node/Comment", {"nf7::Node",}, + "adds comments for your future", + }; + + struct Data { + std::string text; + + void serialize(auto& ar) { + ar(text); + } + }; + + Comment(nf7::Env& env, Data&& d = {}) noexcept : + nf7::FileBase(kType, env), + nf7::Node(nf7::Node::kCustomNode), + mem_(*this, std::move(d)) { + } + + Comment(nf7::Deserializer& ar) : Comment(ar.env()) { + ar(mem_.data()); + } + void Serialize(nf7::Serializer& ar) const noexcept override { + ar(mem_.data()); + } + std::unique_ptr Clone(nf7::Env& env) const noexcept override { + return std::make_unique(env, Data {mem_.data()}); + } + + std::shared_ptr CreateLambda( + const std::shared_ptr& parent) noexcept override { + return std::make_shared(*this, parent); + } + nf7::Node::Meta GetMeta() const noexcept override { + return {{}, {}}; + } + + void UpdateNode(nf7::Node::Editor&) noexcept override; + void UpdateMenu(nf7::Node::Editor&) noexcept override; + + nf7::File::Interface* interface(const std::type_info& t) noexcept override { + return nf7::InterfaceSelector(t).Select(this, &mem_); + } + + private: + nf7::GenericMemento mem_; + + + void Editor() noexcept; +}; + +void Comment::UpdateNode(nf7::Node::Editor&) noexcept { + ImGui::TextUnformatted("Node/Comment"); + ImGui::SameLine(); + if (ImGui::SmallButton("edit")) { + ImGui::OpenPopup("Editor"); + } + ImGui::Spacing(); + ImGui::Indent(); + ImGui::TextUnformatted(mem_->text.c_str()); + ImGui::Unindent(); + + if (ImGui::BeginPopup("Editor")) { + Editor(); + ImGui::EndPopup(); + } +} +void Comment::UpdateMenu(nf7::Node::Editor&) noexcept { + if (ImGui::BeginMenu("edit")) { + Editor(); + ImGui::EndMenu(); + } +} + +void Comment::Editor() noexcept { + const auto em = ImGui::GetFontSize(); + + ImGui::InputTextMultiline("##text", &mem_->text, ImVec2 {16*em, 4*em}); + if (ImGui::IsItemDeactivatedAfterEdit()) { + mem_.Commit(); + } +} + +} +} // namespace nf7