-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigData.pde
315 lines (283 loc) · 9.1 KB
/
configData.pde
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
class ConfigBase{
HashMap<String,String> _fieldMap;
ConfigBase(JSONObject json){
_getFieldMap();
setValuesFromJSON(json);
}
ConfigBase(){
_getFieldMap();
}
void setValuesFromJSON(JSONObject json){
for (Map.Entry<String,String> entry : _fieldMap.entrySet()){
String className = entry.getValue();
boolean isarr = false;
if (className.endsWith("[]")){
isarr = true;
className = className.substring(0, className.length());
}
//Class<?> c;
//if (className == "int"){
// c = Class.forName("int");
//}
//try {
// c = Class.forName(entry.getKey());
//} catch(ClassNotFoundException e){
// e.printStackTrace();
// continue;
//}
Object value = getValueFromJSON(className, isarr, entry.getKey(), json);
try {
Field f = this.getClass().getDeclaredField(entry.getKey());
try {
f.set(this, value);
} catch(IllegalAccessException e){
e.printStackTrace();
throw(new Error(entry.getKey()));
}
} catch(NoSuchFieldException e){
e.printStackTrace();
throw(new Error(String.format("key=%s, cls=%s, value=%s", entry.getKey(), this.getClass(), value)));
}
}
}
Object getValueFromJSON(String className, boolean isarr, String fieldName, JSONObject json){
//try {
Object value;
if (className == "int"){
//f.setInt(this, json.getInt(fieldName));
value = json.getInt(fieldName);
} else if (className == "boolean"){
//f.setBoolean(this, json.getBoolean(fieldName));
value = json.getBoolean(fieldName);
} else if (className == "String"){
value = json.getString(fieldName);
} else if (className == "Point"){
value = new Point(json.getJSONObject(fieldName));
//f.set(this, p);
} else if (className == "Box"){
value = new Box(json.getJSONObject(fieldName));
} else {
throw(new Error(String.format("could not serialize field '%s', className='%s', cls=%s", fieldName, className, this.getClass())));
}
return value;
}
JSONObject serialize(){
JSONObject json = new JSONObject();
for (Map.Entry<String,String> entry : _fieldMap.entrySet()){
String className = entry.getValue();
boolean isarr = false;
if (className.endsWith("[]")){
isarr = true;
className = className.substring(0, className.length());
}
Object value;
try {
Field f = this.getClass().getDeclaredField(entry.getKey());
try {
value = f.get(this);
} catch(IllegalAccessException e){
e.printStackTrace();
continue;
}
} catch(NoSuchFieldException e){
e.printStackTrace();
throw(new Error(String.format("key=%s, cls=%s", entry.getKey(), this.getClass())));
}
setJSONValue(className, isarr, entry.getKey(), value, json);
}
return json;
}
void setJSONValue(String className, boolean isarr, String fieldName, Object value, JSONObject json){
if (value instanceof ConfigBase){
try {
Class c = value.getClass();
Method m = c.getMethod("serialize");
Object _json = m.invoke(value);
json.setJSONObject(fieldName, (JSONObject)_json);
//if (_json instanceof JSONArray){
// json.setJSONArray(fieldName, (JSONArray)_json);
//} else {
// json.setJSONObject(fieldName, (JSONObject)_json);
//}
} catch(NoSuchMethodException e){
e.printStackTrace();
throw(new Error(String.format("key=%s, cls=%s", fieldName, this.getClass())));
} catch(IllegalAccessException e){
e.printStackTrace();
throw(new Error(String.format("key=%s, cls=%s", fieldName, this.getClass())));
} catch(InvocationTargetException e){
e.printStackTrace();
throw(new Error(String.format("key=%s, cls=%s", fieldName, this.getClass())));
}
} else if (className == "int"){
json.setInt(fieldName, (int)value);
} else if (className == "boolean"){
json.setBoolean(fieldName, (boolean)value);
} else if (className == "String"){
json.setString(fieldName, (String)value);
} else if (className == "Point"){
Point p = (Point)value;
json.setJSONObject(fieldName, p.serialize());
} else if (className == "Box"){
Box b = (Box)value;
json.setJSONObject(fieldName, b.serialize());
} else {
throw(new Error(String.format("could not set value '%s' for field '%s' (className='%s'), cls=%s", value, fieldName, className, this.getClass())));
}
}
void _getFieldMap(){
_fieldMap = new HashMap<String,String>();
}
}
class Config extends ConfigBase {
AppConfig app;
GridConfig windowGrid;
Config(JSONObject json){ super(json); }
Config(){
super();
app = new AppConfig();
windowGrid = new GridConfig();
}
void update(MultiviewApplet applet){
app.update(applet);
windowGrid.update(applet);
}
Object getValueFromJSON(String className, boolean isarr, String fieldName, JSONObject json){
if (className == "AppConfig"){
return new AppConfig(json.getJSONObject(fieldName));
} else if (className == "GridConfig"){
return new GridConfig(json.getJSONObject(fieldName));
} else {
return super.getValueFromJSON(className, isarr, fieldName, json);
}
}
//void setJSONValue(String className, boolean isarr, String fieldName, Object value, JSONObject json){
// if (className == "AppConfig"){
// AppConfig c = (AppConfig)value;
// json.setJSONObject(fieldName, c.
void _getFieldMap(){
_fieldMap = new HashMap<String,String>();
_fieldMap.put("app", "AppConfig");
_fieldMap.put("windowGrid", "GridConfig");
}
}
class AppConfig extends ConfigBase {
boolean fullScreen;
int displayNumber;
Point canvasSize;
Box windowBounds;
AppConfig(JSONObject json){ super(json); }
AppConfig(){
super();
fullScreen = false;
displayNumber = -1;
canvasSize = new Point(640, 360);
windowBounds = new Box(0, 0, 640, 360);
}
void update(MultiviewApplet applet){
fullScreen = applet.isFullScreen;
canvasSize = new Point(applet.width, applet.height);
//windowBounds = applet.getWindowDims();
}
void _getFieldMap(){
_fieldMap = new HashMap<String,String>();
_fieldMap.put("fullScreen", "boolean");
_fieldMap.put("displayNumber", "int");
_fieldMap.put("canvasSize", "Point");
_fieldMap.put("windowBounds", "Box");
}
}
class GridConfig extends ConfigBase {
int cols, rows;
Point padding;
Point outputSize;
WindowConfig[] windows;
GridConfig(JSONObject json){ super(json); }
GridConfig(){
super();
cols = 2;
rows = 2;
padding = new Point(2, 2);
outputSize = new Point(640, 360);
windows = new WindowConfig[4];
String windowNames[] = {"A", "B", "C", "D"};
int i = 0;
for (int x=0; x<cols; x++){
for (int y=0; y<rows; y++){
WindowConfig w = new WindowConfig();
w.col = x;
w.row = y;
w.name = windowNames[i];
windows[i] = w;
i += 1;
}
}
//update(mvApp.windowGrid);
}
//GridConfig(WindowGrid grid){
// super();
// setValuesFromJSON(grid.serialize());
//}
void update(MultiviewApplet applet){
update(applet.windowGrid);
}
void update(WindowGrid grid){
setValuesFromJSON(grid.serialize());
}
Object getValueFromJSON(String className, boolean isarr, String fieldName, JSONObject json){
if (className.startsWith("WindowConfig")){
JSONArray jsonArr = json.getJSONArray(fieldName);
WindowConfig[] w = new WindowConfig[jsonArr.size()];
for (int i=0; i<jsonArr.size(); i++){
w[i] = new WindowConfig(jsonArr.getJSONObject(i));
}
return w;
} else {
return super.getValueFromJSON(className, isarr, fieldName, json);
}
}
void setJSONValue(String className, boolean isarr, String fieldName, Object value, JSONObject json){
if (className.startsWith("WindowConfig")){
JSONArray _json = new JSONArray();
for (int i=0; i<windows.length; i++){
_json.append(windows[i].serialize());
}
json.setJSONArray(fieldName, _json);
} else {
super.setJSONValue(className, isarr, fieldName, value, json);
}
}
void _getFieldMap(){
_fieldMap = new HashMap<String,String>();
_fieldMap.put("cols", "int");
_fieldMap.put("rows", "int");
_fieldMap.put("padding", "Point");
_fieldMap.put("outputSize", "Point");
_fieldMap.put("windows", "WindowConfig[]");
}
}
//class WindowConfigs extends ConfigBase {
// WindowConfig[] windows;
//}
class WindowConfig extends ConfigBase {
String name, ndiSourceName;
int col, row;
WindowConfig(JSONObject json){ super(json); }
WindowConfig(){
super();
name = "";
ndiSourceName = "";
col = 0;
row = 0;
}
void _getFieldMap(){
_fieldMap = new HashMap<String,String>();
_fieldMap.put("name", "String");
_fieldMap.put("ndiSourceName", "String");
_fieldMap.put("col", "int");
_fieldMap.put("row", "int");
}
}