diff --git a/forms/interface.ui b/forms/interface.ui index 9617cd387d04c74b52525c645c2988b234c6dd75..7c54e79f3349d6ac43b200bda134e9f9d1a75221 100644 --- a/forms/interface.ui +++ b/forms/interface.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>1056</width> + <width>1068</width> <height>771</height> </rect> </property> @@ -212,7 +212,7 @@ pattern recorded :</string> </size> </property> <property name="text"> - <string>50</string> + <string>10</string> </property> <property name="maxLength"> <number>10</number> @@ -605,8 +605,8 @@ pattern recorded :</string> <rect> <x>0</x> <y>0</y> - <width>1056</width> - <height>25</height> + <width>1068</width> + <height>26</height> </rect> </property> <widget class="QMenu" name="menuFichier"> diff --git a/include/interface.hpp b/include/interface.hpp index f11049ca37b57ae0eb3667c416d727dc56c42a58..bde52bf56be4b5fe2789f6f50306f6b09ba432b4 100644 --- a/include/interface.hpp +++ b/include/interface.hpp @@ -73,6 +73,12 @@ private slots: void on_savePatternButton_clicked(); + void on_nextButton_clicked(); + + void on_prevButton_clicked(); + + void on_playPauseButton_clicked(); + private: //! \brief Initialiser la liste des transitions et voisinages disponibles void init_transition_neighborhood_list(); @@ -114,6 +120,7 @@ private: TransitionRule* m_transition_rule; NeighborhoodRule* m_neighborhood_rule; QJsonObject m_loaded_model; + QTimer* timer; bool m_customizing; }; #endif // MAINWINDOW_HPP diff --git a/include/simulation.hpp b/include/simulation.hpp index d7190d03e93e7cff659ba723575600276f138503..8627f507b29960217a8d8cc7236923c0e395ed9e 100644 --- a/include/simulation.hpp +++ b/include/simulation.hpp @@ -67,6 +67,12 @@ public: + //! \brief Indique si la simulation peut tourner + //! \return vrai si la simulation peut tourner + bool runnable() {return canRun;} + + + //! \brief Remet la simulation dans son état de départ void reset(); @@ -74,7 +80,8 @@ public: void step(); //! \brief Fais reculer la simulation d'un pas si possible - void back(); + //! \return vrai si la simulation a reculé + bool back(); }; #endif // SIMULATION_HPP diff --git a/src/interface.cpp b/src/interface.cpp index 3b4c10242666cc79b61bfc19685298b5d02bf742..960531506d7a35dcc3388de2d629144b441ffaed 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -16,12 +16,14 @@ #include <QDate> #include <QTextStream> #include <QInputDialog> +#include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) - , simulation(), - m_customizing(false) + , simulation() + , timer(new QTimer(this)) + , m_customizing(false) { ui->setupUi(this); @@ -55,6 +57,12 @@ MainWindow::MainWindow(QWidget *parent) ui->struct_library->update_cell_pixel_size(ui->grid_view->cell_screen_size()); load_model(default_model()); + + connect(timer, &QTimer::timeout, this, [this](){ + on_nextButton_clicked(); + std::cout << "a\n"; + timer->start(); + }); } MainWindow::~MainWindow() @@ -605,3 +613,37 @@ void MainWindow::on_savePatternButton_clicked() { save_grid_configuration(); } + +void MainWindow::on_nextButton_clicked() +{ + simulation.setGrid(ui->grid_view->get_grid()); + simulation.step(); + ui->grid_view->copy_grid(simulation.getGrid()); +} + +void MainWindow::on_prevButton_clicked() +{ + if(!simulation.back()) { + QMessageBox::critical(this, "Retour arrière impossible", "L'historique est vide : impossible de revenir en arrière."); + } + ui->grid_view->copy_grid(simulation.getGrid()); +} + +void MainWindow::on_playPauseButton_clicked() +{ + if(timer->isActive()) { + //Pause + timer->stop(); + } else { + //Play + int frequence = ui->simSpeedSpinbox->value(); + if(frequence == 0) { + QMessageBox::critical(this, "Impossible de démarrer", "Impossible de lancer la simulation à une vitesse nulle."); + } else if(!simulation.runnable()) { + QMessageBox::critical(this, "Impossible de démarrer", "Impossible de lancer la simulation : les règles ne sont pas définies ou incompatibles."); + } else { + timer->setInterval(1000/frequence); + timer->start(); + } + } +} diff --git a/src/neighborhood_rules/mooreNeighborhoodRule.cpp b/src/neighborhood_rules/mooreNeighborhoodRule.cpp index 4abc484ef4b5c5965251b2060f1c5561f52902c9..35fe0f16ee885e819656bbd7c2eef05d02848c59 100644 --- a/src/neighborhood_rules/mooreNeighborhoodRule.cpp +++ b/src/neighborhood_rules/mooreNeighborhoodRule.cpp @@ -15,7 +15,9 @@ mooreNeighborhoodRule::mooreNeighborhoodRule(int _radius) for(int j = -_radius; j <= _radius; j++){ newCord.x = i; newCord.y = j; - format.positions.push_back(newCord); + if(!(newCord.x == 0 && newCord.y == 0)) { + format.positions.push_back(newCord); + } } } } diff --git a/src/simulation.cpp b/src/simulation.cpp index 5c61e764832d70726299040fc50918533a441215..540d5455186f7d4070b32e4b15b6c21114b7549f 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -5,13 +5,13 @@ Simulation::Simulation() : canRun(false), time(0), automaton(), startGrid(0,0), Simulation::~Simulation() {} void Simulation::setNeighborhoodRule(NeighborhoodRule* NR) { - canRun = false; automaton.setNeighborhoodRule(NR); + canRun = automaton.getNeighborhoodRule()&&automaton.getTransitionRule()&&automaton.getTransitionRule()->acceptFormat(automaton.getNeighborhoodRule()->getFormats()); } void Simulation::setTransitionRule(TransitionRule* TR) { - canRun = false; automaton.setTransitionRule(TR); + canRun = automaton.getNeighborhoodRule()&&automaton.getTransitionRule()&&automaton.getTransitionRule()->acceptFormat(automaton.getNeighborhoodRule()->getFormats()); } void Simulation::setAlphabet(const Alphabet& A) { @@ -71,11 +71,14 @@ void Simulation::step() { ++time; } -void Simulation::back() { +bool Simulation::back() { + bool r=false; if(!hist.isEmpty()) { automaton.setGrid(hist.topGrid()); //automaton.getNeighborhoodRule()->back(); hist.popGrid(); --time; + r=true; } + return r; }