-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.cpp
609 lines (552 loc) · 17.1 KB
/
node.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include "node.h"
#include <QMessageBox>
namespace meshlang{
void line::remove(bool erst,bool ered){
if(erst){
if(startId==-1){
start->trueThen = NULL;
}else
if(startId==-2){
start->falseThen = NULL;
}else{
try{
start->output.at(startId).erase(this);
}catch(...){
QMessageBox::information(NULL,"Error",QStringLiteral("找不到端口"),QMessageBox::Ok);
}
}
}
if(ered){
if(endId<0){
end->last.erase(this);
}else{
try{
end->input.at(endId).erase(this);
}catch(...){
QMessageBox::information(NULL,"Error",QStringLiteral("找不到端口"),QMessageBox::Ok);
}
}
}
delete this;
}
void program::addModule(const std::string & name,const std::vector<variable> & input,const std::vector<variable> & output,const HBB::vec & posi){
auto p = addFunc(name,input,output);
if(p){
std::vector<variable> var;
for(auto it:p->types){
var.push_back(variable(it.first,it.second));
}
var.push_back(variable("result","int"));
auto pname = name+"_self";
addFunc(pname,var,var);
auto npt = addNode(pname,posi);
npt->name = name;
npt->initval["result"] = "0";
}
}
function * functions::addFunc(const std::string & name,const std::vector<variable> & input,const std::vector<variable> & output){
if(funcs.find(name)!=funcs.end())
return NULL;
auto p = new function;
p->input.clear();
p->output.clear();
p->types.clear();
for(auto it:input){
p->types[it.name] = it.type;
}
for(auto it:output){
p->types[it.name] = it.type;
}
for(auto it:input){
p->input.push_back(variable(it.name,p->types[it.name]));
}
for(auto it:output){
p->output.push_back(variable(it.name,p->types[it.name]));
}
p->name = name;
p->size.X = 4;
p->size.Y = std::max(input.size(),output.size())*2 + 4;
p->isPrivate = false;
if(p->name.size()>5){
int len = p->name.size();
auto ns = p->name.c_str();
if(ns[len-1]=='f' && ns[len-2]=='l' && ns[len-3]=='e' && ns[len-4]=='s' && ns[len-5]=='_')
p->isPrivate = true;
}
funcs[name]=p;
return p;
}
functions::~functions(){
for(auto it:funcs){
delete it.second;
}
}
node * program::addNode(const std::string & name,const HBB::vec & tposition , int id){
if(id>0 && nodeMap.find(id)!=nodeMap.end()){
QMessageBox::information(NULL,"Error",QStringLiteral("id重名"),QMessageBox::Ok);
return NULL;
}
auto it = funcs.find(name);//找到函数
if(it==funcs.end()){
QMessageBox::information(NULL,"Error",QStringLiteral("找不到函数"),QMessageBox::Ok);
return NULL;
}
auto p = new node;
p->type = it->second;
p->input.resize(p->type->input.size());//设置输出
p->output.resize(p->type->output.size());
int len,i;
len=p->input.size();
for(i=0;i<len;++i){
p->input[i].clear();
p->initval[p->type->input[i].name] = "";
}
len=p->output.size();
for(i=0;i<len;++i){
p->output[i].clear();
p->initval[p->type->output[i].name] = "";
}
p->trueThen=NULL;
p->falseThen=NULL;
p->last.clear();
p->name.clear();
HBB::vec from,to;
//p->position = tposition - (p->type->size/2);
p->position = tposition;
from = p->position;
to = from + p->type->size;
p->em=elements.add(from,to,p);
if(id>0){
p->id = id;
if(id > nodeId){
nodeId = id;
}
}else{
p->id = ++nodeId;
}
nodeMap[p->id]=p;
nodes.insert(p);
return p;
}
void program::removeNode(node * n){
for(auto itn:n->input){
for(auto it:itn){
if(it){
removeLine(it,true,false);
}
}
}
for(auto itn:n->output){
for(auto it:itn){
if(it){
removeLine(it,false,true);
}
}
}
if(n->trueThen){
removeLine(n->trueThen,false,true);
}
if(n->falseThen){
removeLine(n->falseThen,false,true);
}
for(auto it:n->last){
removeLine(it,true,false);
}
n->last.clear();
if(n->em){
n->em->autodrop();
}
nodes.erase(n);
nodeMap.erase(n->id);
delete n;
}
line * program::link(int a, int ida, int b, int idb){
auto ita = nodeMap.find(a);
if(ita == nodeMap.end())
return NULL;
auto itb = nodeMap.find(b);
if(itb == nodeMap.end())
return NULL;
return link(ita->second,ida,itb->second,idb);
}
line * program::link(node * a,int ida,node * b,int idb){
try{
if((ida>=0 && idb<0) || (ida<0 && idb>=0)){
QMessageBox::information(NULL,"Error",QStringLiteral("流程端口与普通端口间无法连接"),QMessageBox::Ok);
return NULL;
}
bool isProcess = false;
if(ida>=0 && idb>=0){
for(auto it:a->output.at(ida)){
if(it->end == b && it->endId == idb)
return NULL;
}
for(auto it:b->input.at(idb)){
if(it->start == a && it->startId == ida)
return NULL;
}
if(a->type->output.at(ida).type!=b->type->input.at(idb).type)//类型不符
return NULL;
}else{
if(ida==-1 && a->trueThen!=NULL)
return NULL;
if(ida==-2 && a->falseThen!=NULL)
return NULL;
for(auto it:b->last){
if(it->start==a && it->startId==ida)
return NULL;
}
if(b->type->isPrivate)
return NULL;
if(ida==-2 && a->type->isPrivate)
return NULL;
isProcess = true;
}
auto p = new line;
if(!isProcess)
p->var = a->type->output.at(ida);
else
p->var.type.clear();
char buf[64];
snprintf(buf,64,"line%d",lineNum);
p->var.name = buf;
++lineNum;
p->startId = ida>=0 ? ida : (ida==-1 ? -1 : -2);
p->endId = idb>=0 ? idb : -2;
a->getPortPosition(p->startPosi,2,p->startId);
b->getPortPosition(p->endPosi ,1,p->endId);
if(ida<0 || idb<0){
if(ida==-1)
a->trueThen = p;
else
a->falseThen = p;
b->last.insert(p);
}else{
try{
a->output.at(p->startId).insert(p);
b->input.at(p->endId) .insert(p);
}catch(...){
}
}
p->start = a;
p->end = b;
printf("link %d %d %d %d\n" , a->id , ida , b->id , idb);
HBB::vec from,to;
from.X = std::min(a->position.X - a->type->size.X , b->position.X - b->type->size.X);
to.X = std::max(a->position.X + a->type->size.X , b->position.X + b->type->size.X);
from.Y = std::min(a->position.Y - a->type->size.Y , b->position.Y - b->type->size.Y);
to.Y = std::max(a->position.Y + a->type->size.Y , b->position.Y + b->type->size.Y);
from.Y-=4;
to.Y +=4;
p->em=elementlines.add(from,to,p);
lines.insert(p);
return p;
}catch(...){
QMessageBox::information(NULL,"Error",QStringLiteral("节点无法被连接"),QMessageBox::Ok);
return NULL;
}
}
void program::removeLine(line * l,bool erst,bool ered){
if(l->em){
l->em->autodrop();
}
lines.erase(l);
l->remove(erst,ered);
}
program::program(){
lineNum =0;
nodeId =0;
}
program::~program(){
for(auto it:lines)
delete it;
for(auto it:nodes)
delete it;
}
void node::getClickStatus(const HBB::vec & p , int & mode /* 1 input , 2 output , 3 center */ , int & port){
auto realp = p-position;
if(realp.X < 0 || realp.Y<0 || realp.X > 4){
mode=0;
return;
}
port = (int)(realp.Y/2) - 2;
if(realp.X<1){//left
mode = 1;
}else
if(realp.X>3){//right
mode = 2;
}else{//center
mode = 3;
}
}
void node::getPortPosition(HBB::vec & p , int mode /* 1 input , 2 output , 3 center */ , int port){
p.Y = (port+2) * 2 + 1;
if(mode==1){
p.X = 0;
}else
if(mode==3){
p.X = 2;
}else{
p.X = 4;
}
p = p+position;
}
void program::clickToRemove(const HBB::vec & a){
node * A=NULL;
elements.fetchByPoint(a,[](HBB::AABB * bx,void * arg){
*((node**)arg) = (node*)bx->data;
},&A);
if(A)
removeNode(A);
}
void program::clickToEdit(const HBB::vec & a){
node * A=NULL;
elements.fetchByPoint(a,[](HBB::AABB * bx,void * arg){
*((node**)arg) = (node*)bx->data;
},&A);
if(A){
int Amode,Aport;
A->getClickStatus(a,Amode,Aport);
try{
if(Amode==1){
if(Aport==-2){
if(!A->last.empty()){
for(auto p:A->last){
if(p)
removeLine(p);
}
}
}else{
for(auto p:A->input.at(Aport)){
if(p)
removeLine(p);
}
}
}else
if(Amode==2){
if(Aport==-1){
if(A->trueThen){
removeLine(A->trueThen);
}
}else
if(Aport==-2){
if(A->falseThen){
removeLine(A->falseThen);
}
}else{
auto p = A->output.at(Aport);
if(!p.empty()){
for(auto pt:p){
if(pt)
removeLine(pt);
}
}
}
}else
if(Amode==3){
editNode(A);
}
}catch(...){
}
}else{
if(!editNote(a))
showMenu();
}
}
void program::clickTwoPoint(const HBB::vec & a , const HBB::vec & b){
node * A=NULL;
int Amode,Aport;
node * B=NULL;
int Bmode,Bport;
elements.fetchByPoint(a,[](HBB::AABB * bx,void * arg){
*((node**)arg) = (node*)bx->data;
},&A);
elements.fetchByPoint(b,[](HBB::AABB * bx,void * arg){
*((node**)arg) = (node*)bx->data;
},&B);
if(A==NULL){
if(B==NULL){//添加
std::string inm;
getInsertingName(inm);
if(!inm.empty()){
B = addNode(inm,b);
}
}
}else{
A->getClickStatus(a,Amode,Aport);
if((Aport==-2 || Aport==-1) && Amode==2 && B==NULL){//添加并连线
if(Aport==-1){
if(A->trueThen)
return;
}else
if(Aport==-2){
if(A->falseThen)
return;
}
std::string inm;
getInsertingName(inm);
if(!inm.empty()){
B = addNode(inm,b);
if(B){
link(A,Aport,B,-1);
}
}
}else{
if(A!=B && B!=NULL){
B->getClickStatus(b,Bmode,Bport);
if(Amode==1 && Bmode==2){
link(B,Bport,A,Aport);
}else
if(Amode==2 && Bmode==1){
link(A,Aport,B,Bport);
}
}
}
}
}
void program::import(const std::string & path){
FILE * fp = fopen(path.c_str(),"r");
if(fp){
char buf[2048];
while(!feof(fp)){
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),fp);
std::istringstream iss(buf);
std::string active;
iss>>active;
if(active=="line"){
int a,aid,b,bid;
iss>>a;
iss>>aid;
iss>>b;
iss>>bid;
link(a,aid,b,bid);
}else
if(active=="node"){
std::string type;
int id=0;
float x,y;
iss>>type;
iss>>id;
iss>>x;
iss>>y;
addNode(type,HBB::vec(x,y),id);
}else
if(active=="function"){
std::string name;
iss>>name;
QString str = buf;
QStringList inlist = str.split("input:");
std::vector<variable> input,output;
input.clear();
output.clear();
if(inlist.size()>1){
QStringList flist = inlist[1].split("output:");
if(flist.size()>0){
QStringList inputs = flist[0].split(",");
for(auto it:inputs){
QString real = it.trimmed();
if(!real.isEmpty()){
QStringList var = real.split("/");
if(var.size()>1)
input.push_back(variable(var[1].toStdString(),var[0].toStdString()));
}
}
}
if(flist.size()>1){
QStringList outputs = flist[1].split(",");
for(auto it:outputs){
QString real = it.trimmed();
if(!real.isEmpty()){
QStringList var = real.split("/");
if(var.size()>1)
output.push_back(variable(var[1].toStdString(),var[0].toStdString()));
}
}
}
}
addFunc(name,input,output);
}else
if(active=="initval"){
int id;
std::string key;
std::string val;
iss>>id;
iss>>key;
iss>>val;
if(!key.empty() && !val.empty()){
auto it = nodeMap.find(id);
if(it!=nodeMap.end()){
it->second->initval[key] = QByteArray::fromPercentEncoding(val.c_str()).toStdString().c_str();
}
}
}else
if(active=="setName"){
int id;
std::string n;
iss>>id;
iss>>n;
if(!n.empty()){
auto it = nodeMap.find(id);
if(it!=nodeMap.end()){
it->second->name = n;
}
}
}else
if(active=="note"){
float x,y;
std::string val;
iss>>x;
iss>>y;
iss>>val;
addNote(QByteArray::fromPercentEncoding(val.c_str()).toStdString().c_str() , HBB::vec(x,y));
}
}
fclose(fp);
}else{
QMessageBox::information(NULL,"Error",QString("载入文件\"")+path.c_str()+"\"失败",QMessageBox::Ok);
}
}
void program::save(const std::string & path){
FILE * fp = fopen(path.c_str(),"w");
if(fp){
for(auto it:funcs){
fprintf(fp,"function %s input:",it.first.c_str());
bool first=true;
for(auto it2:it.second->input){
if(first)
fprintf(fp,"%s/%s",it2.type.c_str(),it2.name.c_str());
else
fprintf(fp,",%s/%s",it2.type.c_str(),it2.name.c_str());
first = false;
}
first = true;
fprintf(fp," output:");
for(auto it2:it.second->output){
if(first)
fprintf(fp,"%s/%s",it2.type.c_str(),it2.name.c_str());
else
fprintf(fp,",%s/%s",it2.type.c_str(),it2.name.c_str());
first = false;
}
fprintf(fp," \n");
}
for(auto it:nodes){
fprintf(fp,"node %s %d %f %f\n" , it->type->name.c_str() , it->id , it->position.X , it->position.Y);
if(!it->name.empty())
fprintf(fp,"setName %d %s\n" , it->id , it->name.c_str());
for(auto it2:it->initval){
if(!it2.second.empty()){
QByteArray barr = it2.second.c_str();
fprintf(fp,"initval %d %s %s\n" , it->id , it2.first.c_str() , barr.toPercentEncoding().toStdString().c_str());
}
}
}
for(auto it:lines){
fprintf(fp,"line %d %d %d %d\n" , it->start->id , it->startId , it->end->id , it->endId);
}
saveNotes(fp);
fclose(fp);
}
}
}