-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfactory.cpp
144 lines (128 loc) · 3.58 KB
/
factory.cpp
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
#include <yaml-cpp/yaml.h>
#include <vector>
#include <string>
#include <stdexcept>
#include <fstream>
#include <map>
#include <iostream>
#include "widget.h"
using std::vector;
using std::map;
using std::string;
struct X
{
YAML::Node n;
operator string() const { return n.as<string>(); }
};
struct F
{
YAML::Node n;
operator float() const { return n.as<float>(); }
};
//A generator of widgets from symbol based names
map<string, Properties> names;//Things that we should be able to construct
template<class T>
YAML::Node &operator|=(YAML::Node &&n, const T &t)
{
if(!n.IsDefined())
n = t;
return n;
}
void loadViewData(void)
{
YAML::Node config = YAML::LoadFile("input.yml");
size_t elms = config.size();
for(unsigned i=0; i<elms; ++i) {
auto node = config[i]["Widget"];
if(node.IsDefined()) {
node["class"] |= node["name"];
node["scale"] |= 1.0;
node["aspect"] |= 1.0;
names[node["name"].as<string>()] = node;
}
}
}
template<class T>
T *Spawn(NVGcontext *vg, Properties &p)
{
p["label"] |= "";
return new T(vg, p);
}
template<>
DropDown *Spawn(NVGcontext *vg, Properties &p)
{
p["text"] |= "";
return new DropDown(vg, p);
}
string nameRemap(string name, Properties &p)
{
//Merge onto properties the metaproperties
if(names.find(name) == names.end())
throw std::invalid_argument("Uknown widget type '"+name+"'");
auto metaclass = names[name];
name = X{metaclass["class"]};
for(auto prop:metaclass)
p[prop.first] |= prop.second;
return name;
}
Widget *SpawnByName(string name, NVGcontext *vg, Properties &p)
{
name = nameRemap(name, p);
if(name == "Knob")
return Spawn<Knob>(vg, p);
else if(name == "KnobDetail")
return Spawn<KnobDetail>(vg, p);
else if(name == "DropDown")
return Spawn<DropDown>(vg, p);
else if(name == "Button")
return Spawn<Button>(vg, p);
throw std::invalid_argument("Unknown Widget "+name+"...");
}
YAML::Node FindModule(string mod)
{
YAML::Node config = YAML::LoadFile("input.yml");
size_t elms = config.size();
for(unsigned i=0; i<elms; ++i) {
auto node = config[i]["Def"];
if(node.IsDefined() && node["name"].as<string>() == mod)
return node;
}
throw std::invalid_argument("Unknown module '"+mod+"'");
}
string getKey(YAML::Node n)
{
for(auto x:n)
return x.first.as<string>();
throw std::invalid_argument("EMPTY MAP");
}
Module *Generate(const char *mod, NVGcontext *vg)
{
try {
loadViewData();
auto node = FindModule(mod);
Module *module = new Module(vg, node);
auto n = node["children"];
if(n) {
for(auto itr=n.begin(); itr != n.end(); ++itr) {
if(!itr->IsDefined() || itr->Type() != YAML::NodeType::Map)
continue;
string widgetType = getKey(*itr);
auto name = widgetType;
auto props = (*itr)[widgetType];
if(name == "Spacer")
layoutDummyBox(module->inner.layout);
else {
Widget *child = SpawnByName(name, vg, props);
module->inner.add(child, F{props["aspect"]}, F{props["scale"]},X{props["label"]});
}
}
}
return module;
} catch(std::invalid_argument e) {
std::cout << e.what() << std::endl;
return NULL;
} catch(std::exception e) {
std::cout << "Malformed YAML" << std::endl;
return NULL;
}
}