#include "simulationdialog.h" #include "ui_simulationdialog.h" #include "simulation.h" #include "mapscene.h" #include #include #include #include #ifdef Q_OS_WIN #define LIB_SUFFIX "dll" #else #define LIB_SUFFIX "so" #endif SimulationDialog::SimulationDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SimulationDialog), m_map(NULL) { ui->setupUi(this); connect(ui->behaviorsRefreshButton, SIGNAL(pressed()), this, SLOT(refreshBehaviors())); connect(ui->generatorsRefreshButton, SIGNAL(pressed()), this, SLOT(refreshGenerators())); connect(ui->generateButton, SIGNAL(pressed()), this, SLOT(generateMap())); connect(ui->behaviorsList, SIGNAL(clicked(QModelIndex)), this, SLOT(selectBehavior(QModelIndex))); refreshBehaviors(); refreshGenerators(); ui->generatorsList->setCurrentRow(0); } SimulationDialog::~SimulationDialog() { delete ui; } void SimulationDialog::resizeEvent(QResizeEvent *e) { int h = ui->mapPreview->height(); ui->mapPreview->resize(h*2, h); } void SimulationDialog::refreshBehaviors() { while(ui->behaviorsList->count() > 0) ui->behaviorsList->takeItem(0); m_behaviorList.clear(); m_selectedBehaviors.clear(); ui->selectedBehaviors->clear(); QDir teamDir(QCoreApplication::applicationDirPath()); if(teamDir.cd("../teams")) { for(const QFileInfo &info : teamDir.entryInfoList()) { if(info.suffix().compare(LIB_SUFFIX) == 0) { QLibrary lib(info.absoluteFilePath()); if(lib.load()) { BehaviorFunction func = (BehaviorFunction)lib.resolve("think"); if(func) { ui->behaviorsList->addItem(info.baseName()); m_behaviorList.push_back(info.absoluteFilePath()); } lib.unload(); } } } } else emit sendError(QString("ERROR : can't open the teams folder.\n"), 5000); } void SimulationDialog::refreshGenerators() { while(ui->generatorsList->count() > 0) ui->generatorsList->takeItem(0); m_genList.clear(); QDir genDir(QCoreApplication::applicationDirPath()); if(genDir.cd("../generators")) { for(const QFileInfo &info : genDir.entryInfoList()) { if(info.suffix().compare(LIB_SUFFIX) == 0) { QLibrary lib(info.absoluteFilePath()); if(lib.load()) { GenerateFunction func = (GenerateFunction)lib.resolve("generate"); if(func) { ui->generatorsList->addItem(info.baseName()); m_genList.push_back(info.absoluteFilePath()); } lib.unload(); } } } } else emit sendError(QString("ERROR : can't open the generators folder.\n"), 5000); if(!m_genList.empty()) ui->generatorsList->item(0)->setSelected(true); } void SimulationDialog::generateMap() { if(m_map != NULL) delete m_map; m_map = NULL; QString path = m_genList[ui->generatorsList->currentRow()]; QLibrary lib(path); if(lib.load()) { GenerateFunction func = (GenerateFunction)lib.resolve("generate"); if(func) { m_map = new MapScene(m_selectedBehaviors.size(), ui->mapSizeSpinBox->value()); func((Map*)m_map); // QPixmap preview(); // TODO : display map in preview } lib.unload(); } } void SimulationDialog::selectBehavior(const QModelIndex &index) { QListWidgetItem* item = ui->behaviorsList->item(index.row()); QString path = m_behaviorList[ui->behaviorsList->row(item)]; QLibrary lib(path); if(lib.load()) { BehaviorFunction func = (BehaviorFunction)lib.resolve("think"); if(func) { m_selectedBehaviors.push_back(func); ui->selectedBehaviors->addItem(item->text()); } else lib.unload(); } // TODO : unload later } Simulation* SimulationDialog::getSimulation() { if(m_map == NULL || m_map->getNbTeams() != m_selectedBehaviors.size()) generateMap(); return new Simulation(m_map, m_selectedBehaviors); }