add nf7::Node::Meta and improve nf7::Node interface
This commit is contained in:
parent
173edff4a3
commit
df56eb3462
@ -28,6 +28,24 @@ class Node : public File::Interface {
|
||||
};
|
||||
using Flags = uint8_t;
|
||||
|
||||
struct Meta final {
|
||||
public:
|
||||
Meta() = default;
|
||||
Meta(std::vector<std::string>&& i, std::vector<std::string>&& o) noexcept :
|
||||
inputs(std::move(i)), outputs(std::move(o)) {
|
||||
}
|
||||
Meta(const std::vector<std::string>& i, const std::vector<std::string>& o) noexcept :
|
||||
inputs(i), outputs(o) {
|
||||
}
|
||||
|
||||
Meta(const Meta&) = default;
|
||||
Meta(Meta&&) = default;
|
||||
Meta& operator=(const Meta&) = default;
|
||||
Meta& operator=(Meta&&) = default;
|
||||
|
||||
std::vector<std::string> inputs, outputs;
|
||||
};
|
||||
|
||||
static void ValidateSockets(std::span<const std::string> v) {
|
||||
for (auto itr = v.begin(); itr < v.end(); ++itr) {
|
||||
if (v.end() != std::find(itr+1, v.end(), *itr)) {
|
||||
@ -50,13 +68,12 @@ class Node : public File::Interface {
|
||||
virtual void UpdateNode(Editor&) noexcept { }
|
||||
virtual void UpdateMenu(Editor&) noexcept { }
|
||||
|
||||
// The returned span is alive until next operation to the file.
|
||||
virtual std::span<const std::string> GetInputs() const noexcept = 0;
|
||||
virtual std::span<const std::string> GetOutputs() const noexcept = 0;
|
||||
// don't call too often because causes heap allocation
|
||||
virtual Meta GetMeta() const noexcept = 0;
|
||||
|
||||
Flags flags() const noexcept { return flags_; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
Flags flags_;
|
||||
};
|
||||
|
||||
|
@ -122,13 +122,12 @@ class Device final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"info", "mix", "peek"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"result"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {};
|
||||
return {
|
||||
{"info", "mix", "peek"},
|
||||
{"result"}
|
||||
};
|
||||
}
|
||||
|
||||
nf7::Future<std::shared_ptr<Instance>> Build() noexcept;
|
||||
|
@ -106,16 +106,13 @@ class FontFace final : public nf7::FileBase,
|
||||
return {std::current_exception()};
|
||||
}
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"command"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"result"};
|
||||
return kOutputs;
|
||||
}
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {
|
||||
{"command"}, {"result"},
|
||||
};
|
||||
}
|
||||
|
||||
void UpdateMenu() noexcept override;
|
||||
void UpdateTooltip() noexcept override;
|
||||
|
@ -140,11 +140,8 @@ class ObjBase : public nf7::FileBase,
|
||||
const std::shared_ptr<nf7::Node::Lambda>& parent) noexcept override {
|
||||
return std::make_shared<Lambda>(*this, parent);
|
||||
}
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
return T::kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return T::kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {T::kInputs, T::kOutputs};
|
||||
}
|
||||
|
||||
ResourceFuture Create() noexcept final {
|
||||
|
@ -90,11 +90,8 @@ class Node final : public nf7::FileBase,
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
return mem_->inputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return mem_->outputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return nf7::Node::Meta {mem_->inputs, mem_->outputs};
|
||||
}
|
||||
|
||||
nf7::Future<std::shared_ptr<nf7::luajit::Ref>> Build() noexcept;
|
||||
|
@ -65,13 +65,8 @@ class Imm final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"in"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"out"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"in"}, {"out"}};
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor&) noexcept override;
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
@ -163,12 +164,8 @@ class Network final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
return mem_->inputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return mem_->outputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {mem_->inputs, mem_->outputs};
|
||||
}
|
||||
|
||||
File::Interface* interface(const std::type_info& t) noexcept override {
|
||||
@ -327,6 +324,7 @@ class Network::Item final {
|
||||
nf7::Env& env() const noexcept { return file_->env(); }
|
||||
nf7::File& file() const noexcept { return *file_; }
|
||||
nf7::Node& node() const noexcept { return *node_; }
|
||||
const nf7::Node::Meta& meta() const noexcept { return meta_; }
|
||||
|
||||
InternalNode* inode() const noexcept { return inode_; }
|
||||
InternalNode::Flags iflags() const noexcept { return inode_? inode_->flags(): 0; }
|
||||
@ -335,8 +333,9 @@ class Network::Item final {
|
||||
ItemId id_;
|
||||
|
||||
std::unique_ptr<nf7::File> file_;
|
||||
nf7::Node* node_;
|
||||
InternalNode* inode_;
|
||||
nf7::Node* node_;
|
||||
InternalNode* inode_;
|
||||
nf7::Node::Meta meta_;
|
||||
|
||||
std::optional<nf7::MementoRecorder> mem_;
|
||||
|
||||
@ -365,7 +364,9 @@ class Network::Item final {
|
||||
node_ = &file_->interfaceOrThrow<nf7::Node>();
|
||||
mem_.emplace(file_->interface<nf7::Memento>());
|
||||
|
||||
inode_ = file_->interface<Network::InternalNode>();
|
||||
inode_ = file_->interface<Network::InternalNode>();
|
||||
meta_ = node_->GetMeta();
|
||||
|
||||
prev_pos_ = pos_;
|
||||
}
|
||||
};
|
||||
@ -717,12 +718,8 @@ class Network::Initiator final : public nf7::File,
|
||||
};
|
||||
return std::make_shared<Emitter>(*this, parent);
|
||||
}
|
||||
std::span<const std::string> GetInputs() const noexcept {
|
||||
return {};
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept {
|
||||
static const std::vector<std::string> kOutputs = {"out"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept {
|
||||
return {{}, {"out"}};
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor& ed) noexcept override;
|
||||
@ -769,20 +766,14 @@ class Network::Terminal : public nf7::FileBase,
|
||||
const std::shared_ptr<nf7::Node::Lambda>& parent) noexcept override {
|
||||
return std::make_shared<Emitter>(*this, parent);
|
||||
}
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
if (data().type == kOutput) {
|
||||
static const std::vector<std::string> kInputs = {"in"};
|
||||
return kInputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
switch (data().type) {
|
||||
case kInput:
|
||||
return {{}, {"out"}};
|
||||
case kOutput:
|
||||
return {{"in"}, {}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
if (data().type == kInput) {
|
||||
static const std::vector<std::string> kInputs = {"out"};
|
||||
return kInputs;
|
||||
}
|
||||
return {};
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor&) noexcept override;
|
||||
@ -867,7 +858,7 @@ void Network::Sanitize() {
|
||||
// remove expired links
|
||||
for (const auto& item : items_) {
|
||||
auto cmd = links_.CreateCommandToRemoveExpired(
|
||||
item->id(), item->node().GetInputs(), item->node().GetOutputs());
|
||||
item->id(), item->meta().inputs, item->meta().outputs);
|
||||
if (cmd) {
|
||||
cmd->Apply();
|
||||
}
|
||||
@ -937,7 +928,6 @@ void Network::Item::Detach() noexcept {
|
||||
}
|
||||
void Network::Item::Watcher::Handle(const File::Event& ev) noexcept {
|
||||
auto& item = *owner_;
|
||||
auto& node = item.node();
|
||||
|
||||
switch (ev.type) {
|
||||
case File::Event::kUpdate:
|
||||
@ -945,8 +935,10 @@ void Network::Item::Watcher::Handle(const File::Event& ev) noexcept {
|
||||
auto& net = *item.owner_;
|
||||
net.Touch();
|
||||
|
||||
const auto inputs = node.GetInputs();
|
||||
const auto outputs = node.GetOutputs();
|
||||
// update metadata
|
||||
item.meta_ = item.node().GetMeta();
|
||||
const auto& inputs = item.meta().inputs;
|
||||
const auto& outputs = item.meta().outputs;
|
||||
|
||||
// check expired sockets
|
||||
if (auto cmd = net.links_.CreateCommandToRemoveExpired(item.id(), inputs, outputs)) {
|
||||
@ -1195,9 +1187,9 @@ void Network::Item::UpdateNode(Node::Editor& ed) noexcept {
|
||||
node_->UpdateNode(ed);
|
||||
} else {
|
||||
ImGui::TextUnformatted(file_->type().name().c_str());
|
||||
nf7::gui::NodeInputSockets(node_->GetInputs());
|
||||
nf7::gui::NodeInputSockets(meta_.inputs);
|
||||
ImGui::SameLine();
|
||||
nf7::gui::NodeOutputSockets(node_->GetOutputs());
|
||||
nf7::gui::NodeOutputSockets(meta_.outputs);
|
||||
}
|
||||
}
|
||||
ImNodes::EndNode();
|
||||
|
@ -77,11 +77,8 @@ class Ref final : public nf7::FileBase, public nf7::Node {
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
return mem_->inputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return mem_->outputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {mem_->inputs, mem_->outputs};
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor&) noexcept override;
|
||||
@ -116,12 +113,13 @@ class Ref final : public nf7::FileBase, public nf7::Node {
|
||||
bool mod = false;
|
||||
try {
|
||||
auto& n = target().interfaceOrThrow<nf7::Node>();
|
||||
const auto meta = n.GetMeta();
|
||||
|
||||
const auto srci = n.GetInputs();
|
||||
const auto& srci = meta.inputs;
|
||||
mod |= std::equal(dsti.begin(), dsti.end(), srci.begin(), srci.end());
|
||||
dsti = std::vector<std::string>{srci.begin(), srci.end()};
|
||||
|
||||
const auto srco = n.GetOutputs();
|
||||
const auto& srco = meta.outputs;
|
||||
mod |= std::equal(dsto.begin(), dsto.end(), srco.begin(), srco.end());
|
||||
dsto = std::vector<std::string>{srco.begin(), srco.end()};
|
||||
} catch (nf7::Exception& e) {
|
||||
|
@ -180,7 +180,8 @@ try {
|
||||
}
|
||||
|
||||
ssla_->Listen(*file_, ss);
|
||||
for (const auto& name : node.GetInputs()) {
|
||||
const auto inputs = node.GetMeta().inputs;
|
||||
for (const auto& name : inputs) {
|
||||
if (auto v = ss->Receive(name)) {
|
||||
la_->Handle(name, *v, ssla_);
|
||||
}
|
||||
|
@ -90,13 +90,8 @@ class TL final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"exec"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"result"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"exec"}, {"result"}};
|
||||
}
|
||||
|
||||
void PostHandle(const nf7::File::Event& ev) noexcept;
|
||||
|
@ -51,12 +51,8 @@ class Call final : public nf7::File, public nf7::Node {
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"save", "exit", "abort", "panic"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return {};
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"save", "exit", "abort", "panic"}, {}};
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor&) noexcept override;
|
||||
|
@ -92,12 +92,8 @@ class Event final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"value"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return {};
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"value"}, {}};
|
||||
}
|
||||
|
||||
void PostUpdate() noexcept override;
|
||||
@ -121,12 +117,6 @@ class Event final : public nf7::FileBase,
|
||||
nf7::Node& GetHandler() const {
|
||||
return ResolveOrThrow(mem_->handler).interfaceOrThrow<nf7::Node>();
|
||||
}
|
||||
std::span<const std::string> GetHandlerInputs() const noexcept
|
||||
try {
|
||||
return GetHandler().GetInputs();
|
||||
} catch (nf7::Exception&) {
|
||||
return {};
|
||||
}
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambdaIf() noexcept {
|
||||
try {
|
||||
if (!la_) {
|
||||
@ -180,21 +170,18 @@ std::shared_ptr<nf7::Node::Lambda> Event::CreateLambda(
|
||||
|
||||
void Event::PostUpdate() noexcept {
|
||||
const auto& io = ImGui::GetIO();
|
||||
const auto in = GetHandlerInputs();
|
||||
|
||||
if (in.end() != std::find(in.begin(), in.end(), "key")) {
|
||||
for (size_t i = 0; i < ImGuiKey_KeysData_SIZE; ++i) {
|
||||
const auto& key = io.KeysData[i];
|
||||
const char* event = nullptr;
|
||||
if (key.DownDuration == 0) {
|
||||
event = "down";
|
||||
} else if (key.DownDurationPrev >= 0 && !key.Down) {
|
||||
event = "up";
|
||||
}
|
||||
if (event) {
|
||||
const auto k = static_cast<ImGuiKey>(i);
|
||||
TriggerKeyEvent(ImGui::GetKeyName(k), event);
|
||||
}
|
||||
for (size_t i = 0; i < ImGuiKey_KeysData_SIZE; ++i) {
|
||||
const auto& key = io.KeysData[i];
|
||||
const char* event = nullptr;
|
||||
if (key.DownDuration == 0) {
|
||||
event = "down";
|
||||
} else if (key.DownDurationPrev >= 0 && !key.Down) {
|
||||
event = "up";
|
||||
}
|
||||
if (event) {
|
||||
const auto k = static_cast<ImGuiKey>(i);
|
||||
TriggerKeyEvent(ImGui::GetKeyName(k), event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -143,14 +143,8 @@ class NFile final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"command"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"result"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"command"}, {"result"}};
|
||||
}
|
||||
|
||||
void UpdateTooltip() noexcept override;
|
||||
|
@ -97,13 +97,8 @@ class Curve final : public nf7::FileBase,
|
||||
std::shared_ptr<nf7::Sequencer::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Context>&) noexcept override;
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
static const std::vector<std::string> kInputs = {"x"};
|
||||
return kInputs;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
static const std::vector<std::string> kOutputs = {"y"};
|
||||
return kOutputs;
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {{"x"}, {"y"}};
|
||||
}
|
||||
|
||||
void UpdateItem(nf7::Sequencer::Editor&) noexcept override;
|
||||
|
@ -134,12 +134,8 @@ class Plot final : public nf7::FileBase,
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
|
||||
std::span<const std::string> GetInputs() const noexcept override {
|
||||
return inputs_;
|
||||
}
|
||||
std::span<const std::string> GetOutputs() const noexcept override {
|
||||
return {};
|
||||
nf7::Node::Meta GetMeta() const noexcept override {
|
||||
return {inputs_, {}};
|
||||
}
|
||||
|
||||
void UpdateMenu() noexcept override;
|
||||
|
Loading…
x
Reference in New Issue
Block a user