summaryrefslogtreecommitdiff
path: root/NT/src/rotor.cpp
blob: bf0c26aeca79fbcdce906871d8b12c86c6438752 (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
#include <stdio.h>

#include "rotor.h"
#include "factory.h"
#include "nodes.h"
#include "graph.h"

using namespace std;
using namespace Rotor;

string Variable::get_connection_id(){
	if (connection) return connection->get_id();
	return "";
}

template <class T>
Json::Value Variable_type<T>::to_json(){
	Json::Value json;
	json["type"]=get_type();
	json["name"]=name;
	json["connectable"]=connectable?"yes":"no";
	json["input"]=connection?connection->get_id():"";
	return json;
}

template <class T>
Json::Value Variable_array_type<T>::to_json(){
	Json::Value json;
	json["type"]=get_type();
	json["name"]=name;
	json["connectable"]=connectable?"yes":"no";
	json["input"]=Json::arrayValue;
	for (auto& input: values) {
		json["input"].append(input.is_connected()?input.get_connection_id():"");
	}
	return json;
}

//explicit template instantiation
template class Variable_type<double>;
template class Variable_type<string>;

template class Variable_array_type<double>;


Json::Value Node::to_json(){
	Json::Value node;
	node["type"]=type;
	node["type_id"]=type_id;
	node["title"]=title;
	node["output_type"]=get_output_type();
	node["description"]=description;
	node["id"]=id;
	node["ui_type"]=ui_type;
	if (vars.size()){
		node["vars"]=Json::arrayValue;
		for (auto& var: vars) {
			node["vars"].append(var.second->to_json());
		}
	}
	return node;
}

//factory generates linker errors if rotor.h implementation is seperated: why?

int main(){
	Node_factory f;
/*
	map<string,string> settings={{"node_type","time"}};
	Node *t=f.create(settings);
	settings={{"value","2"},{"node_type","multiply"}};
	Node *m=f.create(settings);
	if (!m->connect("inlet",t)) printf("not connected...\n");
	settings={{"node_type","print"}};
	Node *p=f.create(settings);
	if (!p->connect("inlet",m)) printf("not connected...\n");
	for (double t=0;t<10.0;t+=0.765){
		Frame_parameters f=Frame_parameters(t,25.0,10.0,640,360);
		printf("%04f %s\n",t,(dynamic_cast<Node_type<string>*>(p))->get_output(f).c_str());
	}
	delete t;
	delete m;
	delete p;
*/
	Rotor::Graph g;
	cerr<<(g.load_file("testgraph.json","./")?"loaded ":"could not load ")<<"testgraph.json"<<endl;
	/*
	Json::Value js;
	f.list_node("time",js);
	Json::StyledWriter writer;
	cerr<<writer.write(js)<<endl;
	f.list_node("multiply",js);
	cerr<<writer.write(js)<<endl;
	f.list_node("print",js);
	cerr<<writer.write(js)<<endl;
	*/

	//now need a way to grab output

	Node* p=g.find_node("print");

	if (p){
		for (double t=0;t<10.0;t+=0.765){
			Frame_parameters f=Frame_parameters(t,25.0,10.0,640,360);
			printf("%04f %s\n",t,(dynamic_cast<Node_type<string>*>(p))->get_output(f).c_str());
		}
	}
}