|
|
@@ -1,26 +1,53 @@
|
|
|
#include "mainwindow.h"
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
-#include <QPainter>
|
|
|
-#include <QTimer>
|
|
|
+#include "playfield.h"
|
|
|
+
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
|
: QMainWindow(parent)
|
|
|
, ui(new Ui::MainWindow)
|
|
|
{
|
|
|
ui->setupUi(this);
|
|
|
+ lblCycles = new QLabel(ui->statusBar);
|
|
|
+ ui->statusBar->addWidget(lblCycles);
|
|
|
|
|
|
// setMinimumSize(simulator.size * 40, simulator.size * 40);
|
|
|
|
|
|
simulator.loadProgram(Qt::red, 10, 10);
|
|
|
simulator.loadProgram(Qt::blue, 15, 15);
|
|
|
|
|
|
- QTimer* timer = new QTimer(this);
|
|
|
- connect(timer, &QTimer::timeout, this, [this](){
|
|
|
- simulator.simulate();
|
|
|
+ ui->centralWidget->layout()->addWidget(new Playfield(simulator, ui->centralWidget));
|
|
|
+
|
|
|
+ auto timerRepaint = new QTimer(this);
|
|
|
+ connect(timerRepaint, &QTimer::timeout, this, [this](){
|
|
|
+ mutex.lock();
|
|
|
repaint();
|
|
|
+ lblCycles->setText(QString(tr("Cycles: %1")).arg(simulator.cycle));
|
|
|
+ mutex.unlock();
|
|
|
+ });
|
|
|
+ timerRepaint->start(200);
|
|
|
+
|
|
|
+ connect(timer, &QTimer::timeout, this, [this](){
|
|
|
+ simulate();
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ connect(ui->actionStart, &QAction::triggered, this, [this](){
|
|
|
+ timer->start(1);
|
|
|
});
|
|
|
- timer->start(50);
|
|
|
+
|
|
|
+ connect(ui->actionPause, &QAction::triggered, this, [this](){
|
|
|
+ timer->stop();
|
|
|
+ });
|
|
|
+
|
|
|
+ connect(ui->actionSingleStep, &QAction::triggered, this, [this](){
|
|
|
+ timer->stop();
|
|
|
+ simulate();
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
@@ -28,51 +55,9 @@ MainWindow::~MainWindow()
|
|
|
delete ui;
|
|
|
}
|
|
|
|
|
|
-void MainWindow::paintEvent(QPaintEvent* event) {
|
|
|
- QPainter painter(this);
|
|
|
-
|
|
|
- int w = qMin(width(), height()) / simulator.size;
|
|
|
- int h = qMin(width(), height()) / simulator.size;
|
|
|
-
|
|
|
- for(int y = 0; y < simulator.size; ++y) {
|
|
|
- for(int x = 0; x < simulator.size; ++x) {
|
|
|
- int xx = x * w;
|
|
|
- int yy = y * w;
|
|
|
- painter.setPen(Qt::black);
|
|
|
- painter.setBrush(Qt::gray);
|
|
|
- painter.drawRect(QRect(xx, yy,w, h));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- for (auto& program : simulator.programs) {
|
|
|
- int xx = program.position.x * w;
|
|
|
- int yy = program.position.y * w;
|
|
|
- painter.setPen(Qt::black);
|
|
|
- painter.setBrush(program.color);
|
|
|
- QRect rect(xx, yy,w, h);
|
|
|
- painter.drawRect(rect);
|
|
|
-
|
|
|
- auto& taskIndex = program.taskIndex;
|
|
|
-
|
|
|
- if(program.tasks.empty()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- taskIndex += 1;
|
|
|
- taskIndex %= program.tasks.size();
|
|
|
-
|
|
|
- if (taskIndex < program.tasks.size()) {
|
|
|
- auto& task = program.tasks[taskIndex];
|
|
|
-
|
|
|
- switch(task.direction) {
|
|
|
- case Right: painter.drawLine(rect.center(), QPoint(rect.right(), rect.center().y())); break;
|
|
|
- case Down: painter.drawLine(rect.center(), QPoint(rect.center().x(), rect.bottom())); break;
|
|
|
- case Left: painter.drawLine(rect.center(), QPoint(rect.left(), rect.center().y())); break;
|
|
|
- case Up: painter.drawLine(rect.center(), QPoint(rect.center().x(), rect.top())); break;
|
|
|
- }
|
|
|
- painter.drawText(rect, QString("%1:%2:%3").arg(program.banks.size()).arg(task.bankIndex).arg(task.instIndex));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+void MainWindow::simulate()
|
|
|
+{
|
|
|
+ mutex.lock();
|
|
|
+ simulator.simulate();
|
|
|
+ mutex.unlock();
|
|
|
}
|