-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathNodeFactory.hpp
More file actions
99 lines (76 loc) · 4.64 KB
/
NodeFactory.hpp
File metadata and controls
99 lines (76 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of CosmoScout VR //
////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: German Aerospace Center (DLR) <cosmoscout@dlr.de>
// SPDX-License-Identifier: MIT
#ifndef CSL_NODE_EDITOR_NODE_FACTORY_HPP
#define CSL_NODE_EDITOR_NODE_FACTORY_HPP
#include "csl_node_editor_export.hpp"
#include "Node.hpp"
namespace csl::nodeeditor {
/// This class is used to register all socket and node types which should be available in a node
/// editor instance. The node editor will use an instance of this class to instantiate nodes which
/// have been registered before. You can have a look at the csp-demo-node-editor plugin for an usage
/// example.
class CSL_NODE_EDITOR_EXPORT NodeFactory {
public:
// public API ------------------------------------------------------------------------------------
// As a user of this library, you will usually only have to call the three methods below.
/// Registers a new node socket which can be used by nodes of the node editor.
/// @param name The unique name of the socket type. This is used to retrieve references to
/// this socket type in the JavaScript part of your nodes. You can access the
/// socket in JavaScript using this: CosmoScout.socketTypes['Socket Name'].
/// @param color A string defining the color of the socket. This can be anything which is
/// accepted by CSS. For example 'red', '#f00', or 'rgb(255, 0, 0)'.
void registerSocketType(std::string name, std::string color);
/// Registers a new node type which can then be used in the node editor.
/// @tparam T The node type to be registered. This should be derived from nodeeditor::Node.
/// See the documentation of this class for more information.
/// @param ...args The given arguments will be passed to the static sCreate() method whenever a
/// new node of this type is constructed.
template <typename T, typename... Args>
void registerNodeType(Args... args) {
mNodeSourceFuncs.push_back([=]() { return T::sSource(); });
mNodeCreateFuncs[T::sName] = [=]() { return T::sCreate(args...); };
}
/// Register a new control type. The string should contain a JavaScript class that inherits from
/// Rete.Control.
/// @param controlSource The source code of a JavaScript class derived from Rete.Control.
void registerControlType(std::string controlSource);
// Node Editor API -------------------------------------------------------------------------------
// The methods below are primarily meant to be used by the NodeEditor class. You may want use them
// for debugging purposes, though.
/// This creates the JavaScript source snippet which is injected into the node editor web page to
/// setup all registered socket types.
/// @return The JavaScript source code.
std::string getSocketSource() const;
/// This creates the JavaScript source snippet which is injected into the node editor web page to
/// setup all registered node types.
/// @return The JavaScript source code.
std::string getNodeSource() const;
/// This creates the JavaScript source snippet which is injected into the node editor web page to
/// setup all registered control types.
/// @return The JavaScript source code.
std::string getControlSource() const;
/// This creates the JavaScript source snippet which is injected into the node editor web page to
/// register all node types.
/// @return The JavaScript source code.
std::string getRegisterSource() const;
/// Create a new Node given the name of the node type.
/// @param type The name of the node type (the static NAME of the registered node type).
/// @return A newly created node of the given type name.
/// @throws This may throw a std::runtime_error if the given type has not been registered
/// before.
std::unique_ptr<Node> createNode(std::string const& type) const;
private:
// Stores socket color for each unique socket name.
std::unordered_map<std::string, std::string> mSockets;
// Functions to retrieve the JavaScript source of each registered node.
std::vector<std::function<std::string(void)>> mNodeSourceFuncs;
// Source code for controls.
std::vector<std::string> mControls;
// Functions to create new nodes for each type name.
std::unordered_map<std::string, std::function<std::unique_ptr<Node>(void)>> mNodeCreateFuncs;
};
} // namespace csl::nodeeditor
#endif // CSL_NODE_EDITOR_NODE_FACTORY_HPP