openblocks/editor/mainwindow.cpp

108 lines
3.6 KiB
C++
Raw Normal View History

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QProcess>
#include <QThread>
2024-12-01 12:08:45 +00:00
#include <QTimerEvent>
#include <QMouseEvent>
#include <QWidget>
2025-01-18 15:27:43 +00:00
#include <QTreeView>
#include <QAbstractItemView>
2025-01-28 20:27:33 +00:00
#include <optional>
2024-12-01 12:08:45 +00:00
#include "common.h"
2025-02-02 23:23:25 +00:00
#include "editorcommon.h"
2024-12-01 12:08:45 +00:00
#include "physics/simulation.h"
2025-01-17 08:24:18 +00:00
#include "objects/part.h"
2025-01-28 20:27:33 +00:00
#include "qitemselectionmodel.h"
2025-01-20 10:25:04 +00:00
#include "qobject.h"
2025-02-04 20:32:54 +00:00
#include "qsysinfo.h"
2024-12-01 23:14:42 +00:00
2025-02-02 23:23:25 +00:00
SelectedTool selectedTool;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
2025-02-04 20:32:54 +00:00
dataModel->Init();
ui->setupUi(this);
2024-12-01 12:08:45 +00:00
timer.start(33, this);
setMouseTracking(true);
2024-12-02 10:01:51 +00:00
2025-01-28 20:27:33 +00:00
connect(ui->explorerView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [&](const QItemSelection &selected, const QItemSelection &deselected) {
if (selected.count() == 0) return;
std::optional<InstanceRef> inst = selected.count() == 0 ? std::nullopt
: std::make_optional(((Instance*)selected.indexes()[0].internalPointer())->shared_from_this());
ui->propertiesView->setSelected(inst);
});
2025-02-02 23:23:25 +00:00
connect(ui->actionToolSelect, &QAction::triggered, this, [&]() { selectedTool = SelectedTool::SELECT; updateSelectedTool(); });
connect(ui->actionToolMove, &QAction::triggered, this, [&](bool state) { selectedTool = state ? SelectedTool::MOVE : SelectedTool::SELECT; updateSelectedTool(); });
connect(ui->actionToolScale, &QAction::triggered, this, [&](bool state) { selectedTool = state ? SelectedTool::SCALE : SelectedTool::SELECT; updateSelectedTool(); });
connect(ui->actionToolRotate, &QAction::triggered, this, [&](bool state) { selectedTool = state ? SelectedTool::ROTATE : SelectedTool::SELECT; updateSelectedTool(); });
ui->actionToolSelect->setChecked(true);
// ui->explorerView->Init(ui);
2025-01-17 23:13:42 +00:00
2024-12-02 10:01:51 +00:00
simulationInit();
// Baseplate
workspace()->AddChild(ui->mainWidget->lastPart = Part::New({
2024-12-02 10:01:51 +00:00
.position = glm::vec3(0, -5, 0),
.rotation = glm::vec3(0),
.scale = glm::vec3(512, 1.2, 512),
.material = Material {
.diffuse = glm::vec3(0.388235, 0.372549, 0.384314),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
},
.anchored = true,
2025-01-17 08:24:18 +00:00
}));
2025-01-20 10:25:04 +00:00
ui->mainWidget->lastPart->name = "Baseplate";
2025-01-17 08:24:18 +00:00
syncPartPhysics(ui->mainWidget->lastPart);
2024-12-02 10:01:51 +00:00
workspace()->AddChild(ui->mainWidget->lastPart = Part::New({
2024-12-02 10:01:51 +00:00
.position = glm::vec3(0),
.rotation = glm::vec3(0),
.scale = glm::vec3(4, 1.2, 2),
.material = Material {
.diffuse = glm::vec3(0.639216f, 0.635294f, 0.647059f),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
}
2025-01-17 08:24:18 +00:00
}));
syncPartPhysics(ui->mainWidget->lastPart);
2024-12-01 12:08:45 +00:00
}
2024-12-02 10:01:51 +00:00
static std::chrono::time_point lastTime = std::chrono::steady_clock::now();
2024-12-01 12:08:45 +00:00
void MainWindow::timerEvent(QTimerEvent* evt) {
if (evt->timerId() != timer.timerId()) {
QWidget::timerEvent(evt);
return;
}
float deltaTime = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - lastTime).count();
lastTime = std::chrono::steady_clock::now();
physicsStep(deltaTime);
ui->mainWidget->update();
2024-12-02 10:01:51 +00:00
ui->mainWidget->updateCycle();
}
2025-02-02 23:23:25 +00:00
void MainWindow::updateSelectedTool() {
ui->actionToolSelect->setChecked(selectedTool == SelectedTool::SELECT);
ui->actionToolMove->setChecked(selectedTool == SelectedTool::MOVE);
ui->actionToolScale->setChecked(selectedTool == SelectedTool::SCALE);
ui->actionToolRotate->setChecked(selectedTool == SelectedTool::ROTATE);
}
MainWindow::~MainWindow()
{
delete ui;
2024-12-01 11:26:14 +00:00
}