-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
581 lines (516 loc) · 15.9 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Box2D/Box2D.h"
#include "globaltimer.h"
#include "dialogvec2input.h"
#include "obj/nbdatamnger.h"
#include "formshapechainedit.h"
#include "formshapeedgeedit.h"
#include "dialogshapecircleedit.h"
#include "dialogshapepolygonedit.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
B2Physx::the()->init();
B2Physx::the()->setDraw(&draw);
B2Physx::the()->setGravity();
draw.setDrawPtr(ui->openGLWidget);
QObject::connect(&simTimer,&QTimer::timeout,this,&MainWindow::step);
currentRID.clear();
curWidget = nullptr;
nullWidget = nullptr;
nullWidget = new QWidget(this);
nullWidget->setMaximumWidth(0);
curWidget = nullWidget;
ui->horizontalLayout->addWidget(curWidget);
QObject::connect(ui->openGLWidget,&NQOpenGL2DWidget::finishEdit,this,&MainWindow::refreshAtt);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::step()
{
//GlobalTimer::update();
B2Physx::the()->step(0.04);
this->update();
}
void MainWindow::setWorldBox(float w, float h)
{
B2Physx::the()->createWorldBaseObj();
float whalf = w/2;
float hhalf = h/2;
B2Physx::the()->createBlockWall(-whalf,-hhalf,-whalf,hhalf);
B2Physx::the()->createBlockWall(-whalf,hhalf,whalf,hhalf);
B2Physx::the()->createBlockWall(whalf,hhalf,whalf,-hhalf);
B2Physx::the()->createBlockWall(whalf,-hhalf,-whalf,-hhalf);
}
void MainWindow::setWorldGround(float w, float h)
{
B2Physx::the()->createWorldBaseObj();
float whalf = w/2;
B2Physx::the()->createBlockWall(-whalf,h,whalf,h);
}
void MainWindow::changeCurrentWidget(QWidget *widget)
{
curWidget->hide();
ui->horizontalLayout->removeWidget(curWidget);
ui->horizontalLayout->addWidget(widget);
curWidget = widget;
curWidget->show();
}
void MainWindow::editShape(b2Shape *s)
{
int type = s->GetType();
if(type == b2Shape::e_circle){
DialogShapeCircleEdit edit;
edit.set(s);
edit.exec();
}
if(type == b2Shape::e_polygon){
DialogShapePolygonEdit edit;
edit.set(s);
edit.exec();
}
// if(type == b2Shape::e_edge){
// FormShapeEdgeEdit* edit = new FormShapeEdgeEdit();
// edit->show();
// }
// if(type == b2Shape::e_chain){
// FormShapeChainEdit* edit = new FormShapeChainEdit();
// edit->show();
// }
}
void MainWindow::updateShapeList()
{
ui->listWidget_Shape->clear();
ui->listWidget_Shape->addItems(NBDataMnger::the()->getShapeList());
}
void MainWindow::updateBodyList()
{
ui->listWidget_RigidBodys->clear();
ui->listWidget_RigidBodys->addItems(NBDataMnger::the()->layer.getBodyList());
}
void MainWindow::updateFixtureList(QString bodyID)
{
ui->listWidget_Fixtures->clear();
ui->listWidget_Fixtures->addItems(NBDataMnger::the()->layer.getFixtureList(bodyID));
}
void MainWindow::updateJointList()
{
ui->listWidget_Joint->clear();
ui->listWidget_Joint->addItems(NBDataMnger::the()->layer.getJointList());
}
void MainWindow::updateFilterList()
{
ui->listWidget_Filter->clear();
ui->listWidget_Filter->addItems(NBDataMnger::the()->getFilterList());
}
void MainWindow::updateAllList()
{
updateShapeList();
updateFilterList();
updateJointList();
updateBodyList();
updateFixtureList("");
}
void MainWindow::updateAtt(NBObject *obj)
{
QString type = obj->getType();
if(obj->getType() == NBObject::type_body){
formBody.setBody(obj->toBody());
changeCurrentWidget(&formBody);
}
if(obj->getType() == NBObject::type_joint){
NBJoint* joint = obj->toJoint();
if(joint->getJointType() == e_distanceJoint){
formJointDistance.setJoint(joint->toJointDistance());
changeCurrentWidget(&formJointDistance);
}
if(joint->getJointType() == e_revoluteJoint){
formJointRevolute.setJoint(joint->toJointRevolute());
changeCurrentWidget(&formJointRevolute);
}
if(joint->getJointType() == e_prismaticJoint){
uiJointPrismatic.attchJoint(joint);
changeCurrentWidget(&uiJointPrismatic);
}
}
if(obj->getType() == NBObject::type_fixture){
formFixture.setFixture(obj->toFixture());
changeCurrentWidget(&formFixture);
}
update();//!!!可以删除
}
void MainWindow::refreshAtt()
{
qDebug()<<__FUNCTION__<<__LINE__;
if(curWidget){
qDebug()<<__FUNCTION__<<__LINE__;
// formBody.upload();
// formFixture.upload();
// formJointDistance.upload();
// formJointRevolute.upload();
// uiJointPrismatic.upload();
}
}
NBBody* MainWindow::addNewBody(QString id)
{
NBBody* body = new NBBody(id);
NBDataMnger::the()->layer.addBody(body);
updateBodyList();
currentRID = id;
ui->openGLWidget->setEditNBObject(body);
return body;
}
NBFixture *MainWindow::addNewFixture(QString id, QString shapeID)
{
NBBody* body = getCurrentBody();
if(body == nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return nullptr;
}
b2Shape* shapePtr = NBDataMnger::the()->getShapeClone(shapeID);
if(shapePtr==nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return nullptr;
}
NBFixture* fixture = new NBFixture(body,id);
fixture->def.shape = shapePtr;
NBDataMnger::the()->layer.addFixture(fixture);
updateFixtureList(currentRID);
updateAtt(fixture);
}
void MainWindow::addNewJoint(QString id, QString jointType)
{
NBJoint* obj = NBDataMnger::the()->layer.newDefJoint(id.toStdString(),jointType);
updateJointList();
updateAtt(obj);
ui->openGLWidget->setEditNBObject(obj);
}
void MainWindow::inputIDtoAddNewJoint(QString jointType)
{
QString id = QInputDialog::getText(this,"Joint","ID");
addNewJoint(id,jointType);
}
NBBody *MainWindow::getCurrentBody()
{
NBObject* obj = NBDataMnger::the()->layer.getNBBody(currentRID);
if(obj==nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return nullptr;
}
return obj->toBody();
}
QString MainWindow::genAutoName(QString baseName, QStringList existNameList)
{
QString genAutoName = baseName;
int autoNum = 0;
while(existNameList.contains(genAutoName)){
autoNum++;
genAutoName = baseName + QString::number(autoNum);
}
return genAutoName;
}
void MainWindow::on_actionAdd_triggered()
{
QString id = QInputDialog::getText(this,"RigidBody","ID");
addNewBody(id);
}
void MainWindow::on_actionRun_triggered()
{
const int boxWidth = 10;
const int boxHeight = 10;
const int groundWidth = 1000;
const int groundLevel = -10;
if(ui->actionGenerate_Box->isChecked()){
setWorldBox(boxWidth,boxHeight);
}
if(ui->actionGenerate_Ground->isChecked()){
setWorldGround(groundWidth,groundLevel);
}
ui->openGLWidget->setDrawLayer(false);
simTimer.start(40);
}
void MainWindow::on_listWidget_RigidBodys_clicked(const QModelIndex &index)
{
currentRID = index.data().toString();
NBBody* body = getCurrentBody();
if(body == nullptr){
qDebug()<<__FUNCTION__<<__LINE__<<"!!!";
return;
}
updateAtt(body);
updateFixtureList(currentRID);
ui->openGLWidget->setEditNBObject(body);
}
void MainWindow::on_actionGravity_triggered()
{
DialogVec2Input *dlg = new DialogVec2Input;
dlg->exec();
b2Vec2 g = b2Vec2(dlg->getX(),dlg->getY());
B2Physx::the()->setGravity(g);
delete dlg;
}
void MainWindow::on_actionStop_triggered()
{
ui->openGLWidget->setDrawLayer(true);
simTimer.stop();
B2Physx::the()->destroyAll();
}
void MainWindow::on_actionCircle_triggered()
{
DialogShapeCircleEdit edit;
edit.exec();
}
void MainWindow::on_actionPolygon_triggered()
{
DialogShapePolygonEdit edit;
b2PolygonShape *shape = new b2PolygonShape;
shape->SetAsBox(1,1);
edit.set(shape);
if(edit.exec()){
QString newShapeID = QInputDialog::getText(this,"Polygon","ID");
NBDataMnger::the()->addShape(newShapeID,shape);
return;
}
delete shape;
}
void MainWindow::on_actionChain_triggered()
{
FormShapeChainEdit* edit = new FormShapeChainEdit();
edit->show();
}
void MainWindow::on_actionEdge_triggered()
{
FormShapeEdgeEdit* edit = new FormShapeEdgeEdit();
edit->show();
}
void MainWindow::on_listWidget_Shape_clicked(const QModelIndex &index)
{
qDebug()<<NBDataMnger::the()->getShapeList();
ui->listWidget_Shape->clear();
ui->listWidget_Shape->addItems(NBDataMnger::the()->getShapeList());
}
void MainWindow::on_listWidget_Shape_viewportEntered()
{
qDebug()<<__FUNCTION__<<NBDataMnger::the()->getShapeList();
ui->listWidget_Shape->clear();
ui->listWidget_Shape->addItems(NBDataMnger::the()->getShapeList());
}
void MainWindow::on_actionNew_triggered()
{
QStringList shapeList = NBDataMnger::the()->getShapeList();
QString shapeID = QInputDialog::getItem(this,"Shape List","Shape",shapeList,0,false);
QStringList currentBodyFixtureList = getCurrentBody()->getFixtureList();
QString autoName = genAutoName("shape",currentBodyFixtureList);
QString id = QInputDialog::getText(this,"Fixture","ID",QLineEdit::Normal,autoName);
if(currentBodyFixtureList.contains(id)){
qDebug()<<__FUNCTION__<<__LINE__;
QMessageBox::warning(this,"Waring!!!","exist shape name!!!");
return;
}
addNewFixture(id,shapeID);
}
void MainWindow::on_actionSave_triggered()
{
NBDataMnger::the()->saveAllPrefab();
}
void MainWindow::on_actionDistance_triggered()
{
inputIDtoAddNewJoint("distance");
}
void MainWindow::on_actionRevolute_triggered()
{
inputIDtoAddNewJoint("revolute");
}
void MainWindow::on_actionPrismatic_triggered()
{
inputIDtoAddNewJoint("prismatic");
}
void MainWindow::on_actionPulley_triggered()
{
b2PulleyJointDef def;
b2Vec2 groundAnchorA;
/// The second ground anchor in world coordinates. This point never moves.
b2Vec2 groundAnchorB;
/// The local anchor point relative to bodyA's origin.
b2Vec2 localAnchorA;
/// The local anchor point relative to bodyB's origin.
b2Vec2 localAnchorB;
/// The a reference length for the segment attached to bodyA.
float32 lengthA;
/// The a reference length for the segment attached to bodyB.
float32 lengthB;
/// The pulley ratio, used to simulate a block-and-tackle.
float32 ratio;
}
void MainWindow::on_actionGear_triggered()
{
//!!!!!!!!!!!!!!!!!
b2GearJointDef def;
/// The first revolute/prismatic joint attached to the gear joint.
b2Joint* joint1;
/// The second revolute/prismatic joint attached to the gear joint.
b2Joint* joint2;
/// The gear ratio.
/// @see b2GearJoint for explanation.
float32 ratio;
}
void MainWindow::on_listWidget_Joint_clicked(const QModelIndex &index)
{
QString jointID = index.data().toString();
NBJoint* obj = (NBDataMnger::the()->layer.getNBJoint(jointID.toStdString()))->toJoint();
if(obj==nullptr){
return;
}
ui->openGLWidget->setEditNBObject(obj);
updateJointList();
updateAtt(obj);
}
void MainWindow::on_actionCreate_Unit_triggered()
{
NBDataMnger::the()->layer.create(B2Physx::the()->world);
}
void MainWindow::on_actionExport_Unit_triggered()
{
QString fileName = QFileDialog::getSaveFileName();
NBDataMnger::the()->layer.write(fileName);
}
void MainWindow::on_actionImport_Unit_triggered()
{
QString fileName = QFileDialog::getOpenFileName();
NBDataMnger::the()->layer.read(fileName);
updateAllList();
}
void MainWindow::on_listWidget_Fixtures_clicked(const QModelIndex &index)
{
QString fixtureID = ui->listWidget_Fixtures->currentItem()->text();
NBFixture* obj = NBDataMnger::the()->layer.getNBFixture(currentRID,fixtureID)->toFixture();
if(!obj){
return;
}
updateFixtureList(currentRID);
updateAtt(obj);
ui->openGLWidget->setEditNBObject(obj);
}
void MainWindow::on_listWidget_RigidBodys_customContextMenuRequested(const QPoint &pos)
{
QListWidgetItem* item = ui->listWidget_RigidBodys->itemAt(pos);
if(item->isSelected()){
QMenu* popMenu = new QMenu(this);
popMenu->addAction(tr("Add Fixture"));
popMenu->addAction(tr("delete"));
QAction* action = popMenu->exec(QCursor::pos());
if(action == nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return;
}
qDebug()<<__FUNCTION__<<__LINE__<< action->text();
if(action->text() == "Add Fixture"){
on_actionNew_triggered();
updateBodyList();
}
if(action->text() == "delete"){
NBDataMnger::the()->layer.delBody(item->text());
updateBodyList();
}
return;
}
}
void MainWindow::on_listWidget_Joint_customContextMenuRequested(const QPoint &pos)
{
QListWidgetItem* item = ui->listWidget_Joint->itemAt(pos);
if(item){
QMenu* popMenu = new QMenu(this);
popMenu->addAction(tr("delete"));
QAction* action = popMenu->exec(QCursor::pos());
if(action == nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return;
}
qDebug()<<__FUNCTION__<<__LINE__<< action->text();
if(action->text() == "delete"){
NBDataMnger::the()->layer.delJoint(item->text().toStdString());
}
updateJointList();
}
}
void MainWindow::on_listWidget_Shape_customContextMenuRequested(const QPoint &pos)
{
QListWidgetItem* item = ui->listWidget_Shape->itemAt(pos);
if(item){
QMenu* popMenu = new QMenu(this);
popMenu->addAction("edit");
popMenu->addAction("delete");
QAction* action = popMenu->exec(QCursor::pos());
if(action == nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return;
}
if(action->text() == "edit"){
b2Shape* shape = NBDataMnger::the()->getShape(item->text());
editShape(shape);
}
if(action->text() == "delete"){
NBDataMnger::the()->delShape(item->text());
updateFixtureList(currentRID);
}
}
}
void MainWindow::on_listWidget_Fixtures_customContextMenuRequested(const QPoint &pos)
{
QListWidgetItem* item = ui->listWidget_Fixtures->itemAt(pos);
if(item){
QMenu* popMenu = new QMenu(this);
popMenu->addAction("delete");
QAction* action = popMenu->exec(QCursor::pos());
if(action == nullptr){
qDebug()<<__FUNCTION__<<__LINE__;
return;
}
if(action->text() == "delete"){
qDebug()<<__FUNCTION__<<__LINE__<<item->text();
getCurrentBody()->delFixture(item->text());
//currentFID = "";
updateFixtureList(currentRID);
}
}
}
void MainWindow::on_actionCircle_Object_triggered()
{
QString bodyID = genAutoName("body",NBDataMnger::the()->layer.getBodyList());
NBBody* body = addNewBody(bodyID);
QString fixtureID = genAutoName("shape",body->getFixtureList());
addNewFixture(fixtureID,"Circle");
}
void MainWindow::on_actionSquare_Object_triggered()
{
QString bodyID = genAutoName("body",NBDataMnger::the()->layer.getBodyList());
NBBody* body = addNewBody(bodyID);
QString fixtureID = genAutoName("shape",body->getFixtureList());
addNewFixture(fixtureID,"Quad");
}
void MainWindow::on_actionClear_triggered()
{
NBDataMnger::the()->layer.clear();
}
void MainWindow::on_actionFilter_triggered()
{
QStringList filterList = NBDataMnger::the()->getFilterList();
QString autoName = genAutoName("filter",filterList);
QString filterID = QInputDialog::getText(this,"Filter","ID",QLineEdit::Normal,autoName);
if(filterList.contains(filterID)){
qDebug()<<__FUNCTION__<<__LINE__;
QMessageBox::warning(this,"Waring!!!","exist filter name!!!");
return;
}
DialogFilter dlg;
if(dlg.exec()==QDialog::Accepted){
b2Filter filter = dlg.get();
NBDataMnger::the()->addFilter(filterID,filter);
updateFilterList();
}
}