1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-05 13:04:54 +02:00

Load template name from user settings

This commit is contained in:
Tomasz Zielinski 2022-09-11 07:47:32 +02:00
parent 3656b2ef67
commit 73cc606ee2
4 changed files with 49 additions and 23 deletions

View File

@ -32,6 +32,8 @@ void CRmgTemplateStorage::loadObject(std::string scope, std::string name, const
templates[fullKey].setId(name); templates[fullKey].setId(name);
templates[fullKey].serializeJson(handler); templates[fullKey].serializeJson(handler);
templates[fullKey].validate(); templates[fullKey].validate();
templatesByName[name] = templates[fullKey];
} }
catch(const std::exception & e) catch(const std::exception & e)
{ {
@ -51,14 +53,22 @@ std::vector<JsonNode> CRmgTemplateStorage::loadLegacyData(size_t dataSize)
//it would be cool to load old rmg.txt files //it would be cool to load old rmg.txt files
} }
const CRmgTemplate * CRmgTemplateStorage::getTemplate(const std::string & templateName) const const CRmgTemplate * CRmgTemplateStorage::getTemplate(const std::string & templateFullId) const
{ {
auto iter = templates.find(templateName); auto iter = templates.find(templateFullId);
if(iter==templates.end()) if(iter==templates.end())
return nullptr; return nullptr;
return &iter->second; return &iter->second;
} }
const CRmgTemplate * CRmgTemplateStorage::getTemplateByName(const std::string & templateName) const
{
auto iter = templatesByName.find(templateName);
if(iter==templatesByName.end())
return nullptr;
return &iter->second;
}
std::vector<const CRmgTemplate *> CRmgTemplateStorage::getTemplates() const std::vector<const CRmgTemplate *> CRmgTemplateStorage::getTemplates() const
{ {
std::vector<const CRmgTemplate *> result; std::vector<const CRmgTemplate *> result;

View File

@ -29,10 +29,12 @@ public:
virtual void loadObject(std::string scope, std::string name, const JsonNode & data) override; virtual void loadObject(std::string scope, std::string name, const JsonNode & data) override;
virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override; virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
const CRmgTemplate* getTemplate(const std::string & templateName) const; const CRmgTemplate* getTemplate(const std::string & templateFullId) const;
const CRmgTemplate* getTemplateByName(const std::string & templateName) const;
std::vector<const CRmgTemplate *> getTemplates() const; std::vector<const CRmgTemplate *> getTemplates() const;
private: private:
std::map<std::string, CRmgTemplate> templates; std::map<std::string, CRmgTemplate> templates; //FIXME: doesn't IHandlerBase cover this?
std::map<std::string, CRmgTemplate> templatesByName;
}; };

View File

@ -24,14 +24,18 @@ WindowNewMap::WindowNewMap(QWidget *parent) :
loadUserSettings(); loadUserSettings();
show(); //setup initial parameters - can depend on loaded settings
//setup initial parameters
mapGenOptions.setWidth(ui->widthTxt->text().toInt()); mapGenOptions.setWidth(ui->widthTxt->text().toInt());
mapGenOptions.setHeight(ui->heightTxt->text().toInt()); mapGenOptions.setHeight(ui->heightTxt->text().toInt());
bool twoLevel = ui->twoLevelCheck->isChecked(); bool twoLevel = ui->twoLevelCheck->isChecked();
mapGenOptions.setHasTwoLevels(twoLevel); mapGenOptions.setHasTwoLevels(twoLevel);
mapGenOptions.setPlayerCount(ui->humanCombo->currentText().toInt());
mapGenOptions.setCompOnlyPlayerCount(ui->cpuCombo->currentText().toInt());
updateTemplateList(); updateTemplateList();
loadLastTemplate();
show();
} }
WindowNewMap::~WindowNewMap() WindowNewMap::~WindowNewMap()
@ -42,7 +46,7 @@ WindowNewMap::~WindowNewMap()
void WindowNewMap::loadUserSettings() void WindowNewMap::loadUserSettings()
{ {
//load window settings //load last saved settings
QSettings s(Ui::teamName, Ui::appName); QSettings s(Ui::teamName, Ui::appName);
auto width = s.value(newMapWidth); auto width = s.value(newMapWidth);
@ -108,23 +112,26 @@ void WindowNewMap::loadUserSettings()
ui->monsterOpt4->setChecked(true); break; ui->monsterOpt4->setChecked(true); break;
} }
} }
}
void WindowNewMap::loadLastTemplate()
{
//this requires already loaded template list
QSettings s(Ui::teamName, Ui::appName);
auto templateName = s.value(newMapTemplate); auto templateName = s.value(newMapTemplate);
if (templateName.isValid()) if (templateName.isValid())
{ {
updateTemplateList(); auto qstr = templateName.toString();
auto* templ = VLC->tplh->getTemplate(templateName.toString().toStdString()); //Template might have been removed, then silently comboBox will be set to empty string
if (templ) auto index = ui->templateCombo->findText(qstr);
{ ui->templateCombo->setCurrentIndex(index);
ui->templateCombo->setCurrentText(templateName.toString()); on_templateCombo_activated(index);
//TODO: validate inside this method }
mapGenOptions.setMapTemplate(templ); else
} {
else QMessageBox::critical(this, "", "Failed to load template name");
{
//Display problem on status bar
}
} }
} }
@ -163,10 +170,14 @@ void WindowNewMap::saveUserSettings()
s.setValue(newMapMonsterStrength, static_cast<int>(monster)); s.setValue(newMapMonsterStrength, static_cast<int>(monster));
auto templateName = ui->templateCombo->currentText(); auto templateName = ui->templateCombo->currentText();
if (templateName.size()) if (templateName.size() && templateName != defaultTemplate)
{ {
s.setValue(newMapTemplate, templateName); s.setValue(newMapTemplate, templateName);
} }
else
{
s.setValue(newMapTemplate, "");
}
} }
void WindowNewMap::on_cancelButton_clicked() void WindowNewMap::on_cancelButton_clicked()
@ -351,7 +362,7 @@ void WindowNewMap::on_templateCombo_activated(int index)
return; return;
} }
auto * templ = VLC->tplh->getTemplate(ui->templateCombo->currentText().toStdString()); auto * templ = VLC->tplh->getTemplateByName(ui->templateCombo->currentText().toStdString());
mapGenOptions.setMapTemplate(templ); mapGenOptions.setMapTemplate(templ);
} }
@ -390,7 +401,7 @@ void WindowNewMap::updateTemplateList()
if(templates.empty()) if(templates.empty())
return; return;
ui->templateCombo->addItem("[default]"); ui->templateCombo->addItem(defaultTemplate);
for(auto * templ : templates) for(auto * templ : templates)
{ {

View File

@ -23,6 +23,8 @@ class WindowNewMap : public QDialog
const QString newMapMonsterStrength = "NewMapWindow/MonsterStrength"; const QString newMapMonsterStrength = "NewMapWindow/MonsterStrength";
const QString newMapTemplate = "NewMapWindow/Template"; const QString newMapTemplate = "NewMapWindow/Template";
const QString defaultTemplate = "[default]";
const int playerLimit = 8; const int playerLimit = 8;
const std::map<int, int> players const std::map<int, int> players
@ -81,6 +83,7 @@ private:
void updateTemplateList(); void updateTemplateList();
void loadUserSettings(); void loadUserSettings();
void loadLastTemplate();
void saveUserSettings(); void saveUserSettings();
private: private: