summaryrefslogtreecommitdiff
path: root/NT/src/factory.cpp
blob: 3567f8316395755bb2a96fcfea78c1f6a02d8c6c (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "factory.h"

using namespace Rotor;

Node_factory::Node_factory(){
	//for now, statically load prototype map in constructor

	add_type(new Time(),"nodes");
	add_type(new Multiply(),"nodes");
	add_type(new Print(),"nodes");
}
bool Node_factory::list_node(const string &t,xmlIO XML){
	for (auto& type: type_map) {
		if (type.first==t) {
			list_node(type.second,XML);
			return true;
		}
	}
	XML.addValue("error","Node /"+t+"/ not found");
	return false;
};
bool Node_factory::list_node(const string &t,Json::Value &JSON){
	for (auto& type: type_map) {
		if (type.first==t) {
			JSON["node"]=list_node(type.second);
			return true;
		}
	}
	JSON["error"]="Node /"+t+"/ not found";
	return false;
};
Json::Value Node_factory::list_node(Rotor::Node* _node){
	Json::Value node;
	node["node_type"]=_node->node_type;
	node["title"]=_node->title;
	node["output_type"]=_node->output_type;
	node["description"]=_node->description;
	node["node_id"]=_node->node_id;
	node["ui_type"]=_node->ui_type;

	if (_node->vars.size()){
		node["vars"]=Json::arrayValue;
		for (auto& var: _node->vars) {
			string type=var.second->get_type();
			Json::Value newvar;
			newvar["type"]=type;
			newvar["connectable"]=var.second->connectable?"yes":"no";
			if (dynamic_cast<Variable_array*>(var.second)){
				newvar["input"]=Json::arrayValue;
				for (auto& element: dynamic_cast<Variable_array*>(var.second)->values) {
					Json::Value newvalue;
					newvar["input"].append(newvalue);
				}
			}
			else {
				newvar["input"]=Json::Value();
			}
			node["vars"].append(newvar);
		}
	}
/*
	if (_node->inputs.size()){
		node["signal_inputs"]=Json::arrayValue;
		for (auto& input: _node->inputs) {
			Json::Value signal_input;
			signal_input["title"]=input->title;
			signal_input["description"]=input->description;
			node["signal_inputs"].append(signal_input);
		}
	}
	if (dynamic_cast<Image_node*> (_node)!=nullptr) {
		if ((dynamic_cast<Image_node*>(_node))->image_inputs.size()){
			node["image_inputs"]=Json::arrayValue;
			for (auto& input: (dynamic_cast<Image_node*>(_node))->image_inputs) {
				Json::Value image_input;
				image_input["title"]=input->title;
				image_input["description"]=input->description;
				node["image_inputs"].append(image_input);
			}
		}
	}
	if (_node->parameters.size()){
		node["parameters"]=Json::arrayValue;
		for (auto& param: _node->parameters) {
			Json::Value parameter;
			parameter["name"]=param.first;
			parameter["type"]=param.second->type;
			parameter["title"]=param.second->title;
			parameter["description"]=param.second->description;
			parameter["value"]=param.second->value;
			parameter["min"]=param.second->min;
			parameter["max"]=param.second->max;
			parameter["step"]=param.second->step;
			node["parameters"].append(parameter);
		}
	}
	if (_node->attributes.size()){
		node["attributes"]=Json::arrayValue;
		for (auto& attr: _node->attributes) {
			Json::Value attribute;
			attribute["name"]=attr.first;
			attribute["title"]=attr.second->title;
			attribute["description"]=attr.second->description;
			if (attr.second->vals.size()){ //document attribute enumeration
				attribute["value"]=attr.second->value;
				attribute["type"]="enum";
				attribute["options"]=Json::arrayValue;
				for (auto val: attr.second->vals){
					attribute["options"].append(val);
				}
			}
			else {
				attribute["type"]=attr.second->type;
				if (attr.second->type=="array"){
					attribute["value"]=Json::arrayValue;
				}
				else attribute["value"]=attr.second->value;
			}
			node["attributes"].append(attribute);
		}
	}
*/
	return node;
}

void Node_factory::list_node(Rotor::Node* type,xmlIO XML,int i){
		XML.addTag("node");
		XML.addAttribute("node","output_type",type->output_type,i);
		XML.addAttribute("node","node_type",type->node_type,i);
		XML.addAttribute("node","title",type->title,i);
		XML.addAttribute("node","description",type->description,i);
		XML.addAttribute("node","node_id",type->node_id,i);
		XML.addAttribute("node","ui_type",type->ui_type,i);
		XML.pushTag("node",i);
/*
		//if (type->description!="") {
		//	XML.addTag("description");
		//	XML.setValue("description",type->description,0);
		//}
		int j=0;
		for (auto& input: type->inputs) {
			XML.addTag("signal_input");
			XML.addAttribute("signal_input","title",input->title,j);
			XML.addAttribute("signal_input","description",input->description,j);
			j++;
		}
		j=0;
		if (dynamic_cast<Image_node*> (type)!=nullptr) {
			for (auto& input: (dynamic_cast<Image_node*>(type))->image_inputs) {
				XML.addTag("image_input");
				XML.addAttribute("image_input","title",input->title,j);
				XML.addAttribute("image_input","description",input->description,j);
				j++;
			}
		}
		j=0;
		for (auto& parameter: type->parameters) {
			XML.addTag("parameter");
			XML.addAttribute("parameter","name",parameter.first,j);
			XML.addAttribute("parameter","type",parameter.second->type,j);
			XML.addAttribute("parameter","title",parameter.second->title,j);
			XML.addAttribute("parameter","description",parameter.second->description,j);
			XML.addAttribute("parameter","value",parameter.second->value,j);
			XML.addAttribute("parameter","min",parameter.second->min,j);
			XML.addAttribute("parameter","max",parameter.second->max,j);
			XML.addAttribute("parameter","step",parameter.second->step,j);
			j++;
		}
		j=0;
		for (auto& attribute: type->attributes) {
			XML.addTag("attribute");
			XML.addAttribute("attribute","name",attribute.first,j);
			XML.addAttribute("attribute","title",attribute.second->title,j);
			XML.addAttribute("attribute","description",attribute.second->description,j);
			if (attribute.second->vals.size()){ //document attribute enumeration
				XML.addAttribute("attribute","value",attribute.second->value,j);
				XML.addAttribute("attribute","type","enum",j);
				XML.pushTag("attribute",j);
				int k=0;
				for (auto val: attribute.second->vals){
					XML.addTag("option");
					XML.addAttribute("option","value",val,k);
					k++;
				}
				XML.popTag();
			}
			else {
				XML.addAttribute("attribute","type",attribute.second->type,j);
				if (attribute.second->type=="array"){
					XML.pushTag("attribute",j);
					XML.addTag("value");
					XML.popTag();
				}
				else XML.addAttribute("attribute","value",attribute.second->value,j);
			}
			j++;
		}
*/
		XML.popTag();
}