-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
executable file
·658 lines (567 loc) · 19.4 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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include "innpch.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSurfaceFormat>
#include <QDesktopWidget>
#include <QTreeView>
#include <QTreeWidgetItem>
#include "world.h"
#include "renderer.h"
#include "Widgets/transformwidget.h"
#include "Widgets/meshwidget.h"
#include "Widgets/physicswidget.h"
#include "Widgets/inputwidget.h"
#include "Widgets/soundwidget.h"
#include "Widgets/directionallightwidget.h"
#include "Widgets/pointlightwidget.h"
#include "Widgets/spotlightwidget.h"
#include "Widgets/scriptwidget.h"
#include "Widgets/colliderwidget.h"
#include "componentdata.h"
#include <QSplitter>
#include "constants.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QJsonDocument>
#include "postprocesseswindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
mRenderer = new Renderer();
mRenderWindowContainer = QWidget::createWindowContainer(mRenderer);
QVBoxLayout* layout = new QVBoxLayout(ui->OpenGLLayout);
ui->OpenGLLayout->setLayout(layout);
ui->OpenGLLayout->layout()->addWidget(mRenderWindowContainer);
mRenderWindowContainer->setFocus();
resize(QDesktopWidget().availableGeometry(this).size() * 0.7);
ui->treeWidget_ObjectList->setEditTriggers(QAbstractItemView::DoubleClicked);
ui->treeWidget_ObjectList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->treeWidget_ObjectList->setDragEnabled(true);
ui->treeWidget_ObjectList->viewport()->setAcceptDrops(true);
ui->treeWidget_ObjectList->setDropIndicatorShown(true);
ui->treeWidget_ObjectList->setDragDropMode(QAbstractItemView::InternalMove);
ui->treeWidget_ObjectList->setMainWindow(this);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->scrollArea->setWidgetResizable(true);
ui->button_AddComponent->setEnabled(false);
ui->comboBox_Components->setEnabled(false);
mPostProcessesWindow = new PostProcessesWindow(this);
connect(mPostProcessesWindow, &PostProcessesWindow::onSaveClicked, this, &MainWindow::onPostprocessorSaved);
show();
}
MainWindow::~MainWindow()
{
delete mPostProcessesWindow;
delete ui;
}
void MainWindow::updateStatusBar(int vertices, float deltaTime, float frameCounter)
{
statusBar()->showMessage("Vertices drawn: " +
QString::number(vertices) +
" | Time pr FrameDraw: "
+ QString::number(static_cast<double>(deltaTime), 'g', 4)
+ " ms | " + "FPS: "
+ QString::number(static_cast<double>(frameCounter), 'g', 4));
}
EntityInfo* MainWindow::getEntityAt(QTreeWidgetItem* item)
{
if(mTreeDataCache.find(item) != mTreeDataCache.end())
{
return &mTreeDataCache[item];
}
return nullptr;
}
void MainWindow::addGlobalPostProcessing(const std::vector<std::pair<std::string, Postprocessor*>> &postprocessors)
{
mPostProcessesWindow->addPostProcessors(postprocessors);
}
// If a widget is removed we need to recreate the components
void MainWindow::onWidgetRemoved(ComponentWidget* widget)
{
mCurrentComponentWidgets.erase(std::remove(mCurrentComponentWidgets.begin(), mCurrentComponentWidgets.end(), widget), mCurrentComponentWidgets.end());
updateComponentArea(currentEntitySelected->entityId);
}
void MainWindow::refreshWidgets()
{
if(currentEntitySelected)
{
updateComponentArea(currentEntitySelected->entityId);
}
}
void MainWindow::setSceneName(const std::string& name)
{
auto n = QString::fromStdString(name);
n.replace(0, 1, n[0].toUpper());
ui->treeWidget_ObjectList->topLevelItem(0)->setText(0, n);
}
void MainWindow::on_actionEmpty_Object_triggered()
{
World::getWorld().getEntityManager()->createObject(0);
}
void MainWindow::on_actionCube_triggered()
{
World::getWorld().getEntityManager()->createObject(1);
}
void MainWindow::on_actionMonkey_triggered()
{
World::getWorld().getEntityManager()->createObject(2);
}
void MainWindow::on_actionPlay_triggered(bool value)
{
if(value)
{
ui->dockWidget_WorldOutliner->hide();
ui->dockWidget_ComponentWidgets->hide();
updateComponentArea(0);
play();
ui->actionPlay->setText("Stop");
}
else
{
ui->dockWidget_WorldOutliner->show();
ui->dockWidget_ComponentWidgets->show();
updateComponentArea(0);
stop();
ui->lineEdit_SelectedObject->setText("");
ui->actionPlay->setText("Play");
ui->OpenGLLayout->setFocus(Qt::OtherFocusReason);
}
}
// Called from entity manager when a new entity is added to the scene
void MainWindow::updateUI(const std::vector<EntityInfo> &entityData)
{
// Clear the map and widget
mTreeDataCache.clear();
ui->treeWidget_ObjectList->clear();
// Create the new tree root
QTreeWidgetItem* root = new QTreeWidgetItem(ui->treeWidget_ObjectList);
root->setText(0, "SceneName");
ui->treeWidget_ObjectList->addTopLevelItem(root);
ui->treeWidget_ObjectList->expandAll();
std::vector<unsigned> childrenAdded;
// Add all the entities
for(unsigned i = 0; i < entityData.size(); ++i)
{
bool found = false;
for(auto& child : childrenAdded)
{
if(entityData[i].entityId == child)
{
found = true;
}
}
if(found)
continue;
if(entityData[i].shouldShowInEditor)
{
QTreeWidgetItem* item = new QTreeWidgetItem(root);
item->setText(0, QString::fromStdString(entityData[i].name));
item->setFlags(item->flags() | Qt::ItemIsEditable);
mTreeDataCache[item] = entityData[i];
if(auto comp = World::getWorld().getEntityManager()->getComponent<TransformComponent>(entityData[i].entityId))
{
for(auto& child : comp->children)
{
if(auto info = World::getWorld().getEntityManager()->getComponent<EntityInfo>(child))
{
QTreeWidgetItem* childItem = new QTreeWidgetItem(item);
childItem->setText(0, QString::fromStdString(info->name));
item->setFlags(item->flags() | Qt::ItemIsEditable);
mTreeDataCache[childItem] = *info;
childrenAdded.push_back(child);
}
}
}
}
}
}
// Add the given entity components to the component area
void MainWindow::updateComponentArea(unsigned int entityID)
{
// Delete any previous component widgets
if(ui->scrollArea->widget())
delete ui->scrollArea->widget();
// Delete cached widgets
mCurrentComponentWidgets.clear();
if(!entityID)
return;
// Get all available components
mAvailableComponentsToAddCache = ComponentTypes;
// Get the components for this entity
auto components = World::getWorld().getEntityManager()->getAllComponents(entityID);
if(!components.empty())
{
// Components were found, add them
QWidget* widget = new QWidget();
ui->scrollArea->setWidget(widget);
QVBoxLayout* layout = new QVBoxLayout(widget);
widget->setLayout(layout);
layout->setMargin(0);
ComponentWidget* componentWidget = nullptr;
for(auto& component: components)
{
mAvailableComponentsToAddCache.erase(std::remove(mAvailableComponentsToAddCache.begin(), mAvailableComponentsToAddCache.end(), component->type), mAvailableComponentsToAddCache.end());
switch (component->type)
{
case ComponentType::Mesh:
{
componentWidget = new MeshWidget(this);
break;
}
case ComponentType::Transform:
{
componentWidget = new TransformWidget(this);
break;
}
case ComponentType::Physics:
{
componentWidget = new PhysicsWidget(this);
break;
}
case ComponentType::Input:
{
componentWidget = new InputWidget(this);
break;
}
case ComponentType::Sound:
{
componentWidget = new SoundWidget(this);
break;
}
case ComponentType::LightSpot:
{
componentWidget = new SpotLightWidget(this);
break;
}
case ComponentType::LightPoint:
{
componentWidget = new PointLightWidget(this);
break;
}
case ComponentType::LightDirectional:
{
componentWidget = new DirectionalLightWidget(this);
break;
}
case ComponentType::Script:
{
componentWidget = new ScriptWidget(this);
break;
}
case ComponentType::Collider:
{
componentWidget = new ColliderWidget(this);
break;
}
default:
break;
}
if(componentWidget)
{
connect(componentWidget, &ComponentWidget::widgetRemoved, this, &MainWindow::onWidgetRemoved);
layout->addWidget(componentWidget);
mCurrentComponentWidgets.push_back(componentWidget);
componentWidget = nullptr;
}
}
}
updateAvailableComponents(mAvailableComponentsToAddCache);
}
// Update the combo box at the bottom of the component area with the given types)
void MainWindow::updateAvailableComponents(std::vector<ComponentType> types)
{
// Clear the currnet types
ui->comboBox_Components->clear();
// If none are given (aka the entity has all available components), disable
// the combo box and button
if(!types.size())
{
ui->button_AddComponent->setEnabled(false);
ui->comboBox_Components->setEnabled(false);
return;
}
ui->comboBox_Components->setEnabled(true);
ui->button_AddComponent->setEnabled(true);
for(auto& type : types)
{
switch (type)
{
case ComponentType::Mesh:
{
ui->comboBox_Components->addItem("Render");
break;
}
case ComponentType::Transform:
{
ui->comboBox_Components->addItem("Transform");
break;
}
case ComponentType::Physics:
{
ui->comboBox_Components->addItem("Physics");
break;
}
case ComponentType::Input:
{
ui->comboBox_Components->addItem("Input");
break;
}
case ComponentType::Sound:
{
ui->comboBox_Components->addItem("Sound");
break;
}
case ComponentType::LightSpot:
{
ui->comboBox_Components->addItem("Spot light");
break;
}
case ComponentType::LightPoint:
{
ui->comboBox_Components->addItem("Point light");
break;
}
case ComponentType::LightDirectional:
{
ui->comboBox_Components->addItem("Directional light");
break;
}
case ComponentType::Script:
{
ui->comboBox_Components->addItem("Script");
break;
}
case ComponentType::Collider:
{
ui->comboBox_Components->addItem("Collider");
break;
}
default:
break;
}
}
}
// Add the component in the combo box to the selected entity
void MainWindow::on_button_AddComponent_clicked()
{
int index = ui->comboBox_Components->currentIndex();
if(static_cast<size_t>(index) < mAvailableComponentsToAddCache.size() && currentEntitySelected)
{
// Get the entity id and the component type to add
auto id = currentEntitySelected->entityId;
auto type = mAvailableComponentsToAddCache[static_cast<size_t>(index)];
// Add the component
World::getWorld().getEntityManager()->addComponent(id, type);
// Refresh the UI with the components that the entity noew has
updateComponentArea(id);
// Remove the just added component from the cache of available components to add
mAvailableComponentsToAddCache.erase(std::remove(mAvailableComponentsToAddCache.begin(), mAvailableComponentsToAddCache.end(), type), mAvailableComponentsToAddCache.end());
// Refresh the combo box
updateAvailableComponents(mAvailableComponentsToAddCache);
}
}
// When editing the name in the line edit under Selected Object
void MainWindow::on_lineEdit_SelectedObject_editingFinished()
{
auto text = ui->lineEdit_SelectedObject->text();
if(currentEntitySelected)
{
if(!text.length())
{
ui->lineEdit_SelectedObject->setText(QString::fromStdString(currentEntitySelected->name));
return;
}
currentEntitySelected->name = text.toStdString();
// Loop through all QTreeWidgetItems and find the correct one
for(auto data : mTreeDataCache)
{
// If found, set the text in the QTreeWidgetItem to the new name
if(data.second.entityId == currentEntitySelected->entityId)
{
data.first->setText(0, text);
}
}
updateEntityName(currentEntitySelected->entityId, text.toStdString());
}
}
// When editing the name directly in the tree widget
void MainWindow::on_treeWidget_ObjectList_itemChanged(QTreeWidgetItem *item, int /*column*/)
{
if(mTreeDataCache.find(item) != mTreeDataCache.end())
{
// Get the entity data
auto entity = mTreeDataCache[item];
auto text = item->text(0);
if(!text.size())
{
item->setText(0, QString::fromStdString(entity.name));
return;
}
// If this is the selected object get the name and then set it in line edit under Selected Object text
entity.name = text.toStdString();
if(currentEntitySelected && entity.entityId == currentEntitySelected->entityId)
{
ui->lineEdit_SelectedObject->setText(text);
currentEntitySelected->name = text.toStdString();
}
updateEntityName(entity.entityId, text.toStdString());
}
}
void MainWindow::updateEntityName(unsigned entity, const std::string& name)
{
for(auto& info : World::getWorld().getEntityManager()->getEntityInfos())
{
if(info.entityId == entity)
{
info.name = name;
return;
}
}
}
// When left clicking on an item in the tree (selecting an entity)
void MainWindow::setSelected(EntityInfo* entityInfo)
{
if (entityInfo == nullptr)
{
currentEntitySelected = nullptr;
if (auto renderer = getRenderer())
renderer->EditorCurrentEntitySelected = nullptr;
updateComponentArea(0);
updateSelectedInTreeWidget(nullptr);
ui->lineEdit_SelectedObject->setText("");
return;
}
// Cache currently selected entity
currentEntitySelected = entityInfo;
// Update the selected object name
ui->lineEdit_SelectedObject->setText(QString::fromStdString(currentEntitySelected->name));
updateSelectedInTreeWidget(entityInfo);
// Update widgets
updateComponentArea(currentEntitySelected->entityId);
if (auto renderer = getRenderer())
renderer->EditorCurrentEntitySelected = currentEntitySelected;
}
void MainWindow::updateSelectedInTreeWidget(EntityInfo *entityInfo)
{
for(auto& item : mTreeDataCache)
{
if(item.first->isSelected())
{
item.first->setSelected(false);
}
if(entityInfo && item.second.entityId == entityInfo->entityId)
{
if(!item.first->isSelected())
item.first->setSelected(true);
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
quitting();
}
void MainWindow::on_treeWidget_ObjectList_itemClicked(QTreeWidgetItem *item, int /*column*/)
{
if(mTreeDataCache.find(item) != mTreeDataCache.end())
{
setSelected(&mTreeDataCache[item]);
}
}
void MainWindow::on_actionToggle_wireframe_triggered(bool checked)
{
toggleWireframe(checked);
}
void MainWindow::on_actionExit_triggered()
{
close();
}
void MainWindow::on_actionToggle_shutup_triggered(bool checked)
{
shutUp(checked);
}
void MainWindow::on_actionSave_triggered()
{
if(!QDir(QString::fromStdString(gsl::assetFilePath + "/Scenes")).exists())
{
QDir().mkdir(QString::fromStdString(gsl::assetFilePath + "/Scenes"));
}
auto filepath = QFileDialog::getSaveFileName(this, "Save scene to disk", QString::fromStdString(gsl::scenesFilePath),
"Scene files (*.json)",
new QString("Scene files (*.json)"));
if(!filepath.size())
{
return;
}
saveScene(filepath.toStdString());
}
void MainWindow::on_actionLoad_triggered()
{
if(!QDir(QString::fromStdString(gsl::assetFilePath + "/Scenes")).exists())
{
QDir().mkdir(QString::fromStdString(gsl::assetFilePath + "/Scenes"));
}
auto fileName = QFileDialog::getOpenFileName(this, tr("Choose scene"), QString::fromStdString(gsl::assetFilePath + "/Scenes"), tr("JSON files (*.json)"));
if(fileName.size())
{
loadScene(fileName.toStdString());
}
}
void MainWindow::on_actionNew_triggered()
{
QMessageBox messageBox;
messageBox.setText("Creating a new scene will overwrite the current one");
messageBox.setInformativeText("Save current scene to disk?");
messageBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
messageBox.setDefaultButton(QMessageBox::Save);
int ret = messageBox.exec();
switch (ret)
{
case QMessageBox::Save:
on_actionSave_triggered();
break;
case QMessageBox::Discard:
newScene();
break;
case QMessageBox::Cancel:
return;
}
}
void MainWindow::on_actionPost_Processes_triggered()
{
mPostProcessesWindow->show();
}
void MainWindow::onPostprocessorSaved(const std::map<Postprocessor*, std::vector<Postprocessor::Setting>>& steps)
{
QFile file("../Inngine2019/Settings/postprocessorsettings.json");
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "ERROR MainWindow PP save: Failed to open '../Inngine2019/Settings/postprocessorsettings.json'!";
return;
}
QJsonArray mainArray;
for(auto& setting : steps)
{
for(auto& step : setting.second)
{
QJsonObject object;
object.insert("postprocessor", setting.first->mName.c_str());
object.insert("shader", step.material->mShader->mName.c_str());
auto materialJSON = step.material->toJSON();
QJsonObject paramObject;
for(auto ref : materialJSON["Parameters"].toArray())
{
auto obj = ref.toObject();
foreach(const QString& key, obj.keys())
{
paramObject.insert(key, obj.value(key));
}
}
object.insert("parameters", paramObject);
mainArray.push_back(object);
}
}
QJsonDocument document(mainArray);
file.write(document.toJson());
file.close();
}