2014-09-21 16:58:09 -04:00
|
|
|
#include "Globals.h"
|
2014-09-22 12:26:35 -04:00
|
|
|
#include "GeneratorSetup.h"
|
2014-09-21 16:58:09 -04:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
2014-09-23 06:39:09 -04:00
|
|
|
#include "src/Generating/BioGen.h"
|
2014-09-21 16:58:09 -04:00
|
|
|
#include "inifile/iniFile.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const QString s_GeneratorNames[] =
|
|
|
|
{
|
|
|
|
QString("Checkerboard"),
|
|
|
|
QString("Constant"),
|
|
|
|
QString("DistortedVoronoi"),
|
|
|
|
QString("MultiStepMap"),
|
|
|
|
QString("TwoLevel"),
|
|
|
|
QString("Voronoi"),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-22 12:26:35 -04:00
|
|
|
GeneratorSetup::GeneratorSetup(const AString & a_IniFileName, QWidget * a_Parent) :
|
2014-09-21 16:58:09 -04:00
|
|
|
super(a_Parent),
|
|
|
|
m_IniFile(new cIniFile())
|
|
|
|
{
|
2014-09-22 15:51:59 -04:00
|
|
|
// The seed and generator name is in a separate form layout at the top, always present:
|
|
|
|
m_eSeed = new QLineEdit();
|
|
|
|
m_eSeed->setValidator(new QIntValidator());
|
|
|
|
m_eSeed->setText("0");
|
2014-09-29 15:20:58 -04:00
|
|
|
m_eSeed->setProperty("INI.SectionName", QVariant("Seed"));
|
2014-09-22 15:51:59 -04:00
|
|
|
m_eSeed->setProperty("INI.ItemName", QVariant("Seed"));
|
2014-09-21 16:58:09 -04:00
|
|
|
m_cbGenerator = new QComboBox();
|
2014-09-22 12:26:35 -04:00
|
|
|
m_cbGenerator->setMinimumWidth(120);
|
2014-09-21 16:58:09 -04:00
|
|
|
for (size_t i = 0; i < ARRAYCOUNT(s_GeneratorNames); i++)
|
|
|
|
{
|
|
|
|
m_cbGenerator->addItem(s_GeneratorNames[i]);
|
|
|
|
}
|
2014-09-22 15:51:59 -04:00
|
|
|
QFormLayout * baseLayout = new QFormLayout();
|
|
|
|
baseLayout->addRow(new QLabel(tr("Seed")), m_eSeed);
|
|
|
|
baseLayout->addRow(new QLabel(tr("Generator")), m_cbGenerator);
|
2014-09-21 16:58:09 -04:00
|
|
|
|
|
|
|
// The rest of the controls are in a dynamically created form layout:
|
|
|
|
m_FormLayout = new QFormLayout();
|
|
|
|
|
|
|
|
// The main layout joins these two vertically:
|
|
|
|
m_MainLayout = new QVBoxLayout();
|
2014-09-22 15:51:59 -04:00
|
|
|
m_MainLayout->addLayout(baseLayout);
|
2014-09-21 16:58:09 -04:00
|
|
|
m_MainLayout->addLayout(m_FormLayout);
|
2014-09-22 12:26:35 -04:00
|
|
|
m_MainLayout->addStretch();
|
2014-09-21 16:58:09 -04:00
|
|
|
setLayout(m_MainLayout);
|
|
|
|
|
|
|
|
// Load the INI file, if specified, otherwise set defaults:
|
2014-09-29 15:20:58 -04:00
|
|
|
if (a_IniFileName.empty() || !m_IniFile->ReadFile(a_IniFileName))
|
2014-09-21 16:58:09 -04:00
|
|
|
{
|
|
|
|
m_IniFile->SetValue("Generator", "Generator", "Composable");
|
|
|
|
m_IniFile->SetValue("Generator", "BiomeGen", m_cbGenerator->currentText().toStdString());
|
|
|
|
bool dummy;
|
2014-09-22 15:51:59 -04:00
|
|
|
delete cBiomeGen::CreateBiomeGen(*m_IniFile, 0, dummy);
|
2014-09-21 16:58:09 -04:00
|
|
|
}
|
|
|
|
updateFromIni();
|
|
|
|
|
2014-09-22 15:51:59 -04:00
|
|
|
// Connect the change events only after the data has been loaded:
|
|
|
|
connect(m_cbGenerator, SIGNAL(currentIndexChanged(QString)), this, SLOT(generatorChanged(QString)));
|
|
|
|
connect(m_eSeed, SIGNAL(textChanged(QString)), this, SLOT(editChanged(QString)));
|
2014-09-21 16:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-22 12:26:35 -04:00
|
|
|
void GeneratorSetup::generatorChanged(const QString & a_NewName)
|
2014-09-21 16:58:09 -04:00
|
|
|
{
|
|
|
|
// Clear the current contents of the form layout by assigning it to a stack temporary:
|
|
|
|
{
|
|
|
|
m_MainLayout->takeAt(1);
|
|
|
|
QWidget().setLayout(m_FormLayout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-create the layout:
|
|
|
|
m_FormLayout = new QFormLayout();
|
2014-09-22 12:26:35 -04:00
|
|
|
m_MainLayout->insertLayout(1, m_FormLayout);
|
2014-09-21 16:58:09 -04:00
|
|
|
|
|
|
|
// Recreate the INI file:
|
|
|
|
m_IniFile->Clear();
|
|
|
|
m_IniFile->SetValue("Generator", "Generator", "Composable");
|
|
|
|
m_IniFile->SetValue("Generator", "BiomeGen", a_NewName.toStdString());
|
|
|
|
|
|
|
|
// Create a dummy biome gen from the INI file, this will create the defaults in the INI file:
|
|
|
|
bool dummy;
|
|
|
|
delete cBiomeGen::CreateBiomeGen(*m_IniFile, m_Seed, dummy);
|
|
|
|
|
|
|
|
// Read all values from the INI file and put them into the form layout:
|
|
|
|
updateFromIni();
|
2014-09-22 15:51:59 -04:00
|
|
|
|
|
|
|
// Notify of the changes:
|
|
|
|
emit generatorUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GeneratorSetup::editChanged(const QString & a_NewValue)
|
|
|
|
{
|
2014-09-29 15:20:58 -04:00
|
|
|
QString sectionName = sender()->property("INI.SectionName").toString();
|
|
|
|
QString itemName = sender()->property("INI.ItemName").toString();
|
|
|
|
m_IniFile->SetValue(sectionName.toStdString(), itemName.toStdString(), a_NewValue.toStdString());
|
2014-09-22 15:51:59 -04:00
|
|
|
emit generatorUpdated();
|
2014-09-21 16:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-22 12:26:35 -04:00
|
|
|
void GeneratorSetup::updateFromIni()
|
2014-09-21 16:58:09 -04:00
|
|
|
{
|
2014-09-29 15:20:58 -04:00
|
|
|
m_eSeed->setText(QString::number(m_IniFile->GetValueI("Seed", "Seed", 0)));
|
2014-09-21 16:58:09 -04:00
|
|
|
int keyID = m_IniFile->FindKey("Generator");
|
|
|
|
if (keyID <= -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int numItems = m_IniFile->GetNumValues(keyID);
|
2014-09-22 12:26:35 -04:00
|
|
|
AString generatorName = m_IniFile->GetValue("Generator", "BiomeGen");
|
|
|
|
size_t generatorNameLen = generatorName.length();
|
2014-09-21 16:58:09 -04:00
|
|
|
for (int i = 0; i < numItems; i++)
|
|
|
|
{
|
|
|
|
AString itemName = m_IniFile->GetValueName(keyID, i);
|
|
|
|
if ((itemName == "Generator") || (itemName == "BiomeGen"))
|
|
|
|
{
|
|
|
|
// These special cases are not to be added
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-22 15:51:59 -04:00
|
|
|
AString itemValue = m_IniFile->GetValue(keyID, i);
|
|
|
|
|
|
|
|
QLineEdit * edit = new QLineEdit();
|
|
|
|
edit->setText(QString::fromStdString(itemValue));
|
2014-09-29 15:20:58 -04:00
|
|
|
edit->setProperty("INI.SectionName", QVariant("Generator"));
|
2014-09-22 15:51:59 -04:00
|
|
|
edit->setProperty("INI.ItemName", QVariant(QString::fromStdString(itemName)));
|
2014-09-22 12:26:35 -04:00
|
|
|
|
|
|
|
// Remove the generator name prefix from the item name, for clarity purposes:
|
|
|
|
if (NoCaseCompare(itemName.substr(0, generatorNameLen), generatorName) == 0)
|
|
|
|
{
|
|
|
|
itemName.erase(0, generatorNameLen);
|
|
|
|
}
|
|
|
|
|
2014-09-22 15:51:59 -04:00
|
|
|
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(editChanged(QString)));
|
2014-09-21 16:58:09 -04:00
|
|
|
m_FormLayout->addRow(new QLabel(QString::fromStdString(itemName)), edit);
|
|
|
|
} // for i - INI values[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|