diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 39ed4178..e33dbb75 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -18,6 +18,14 @@ class RegionMapEntry { public: + RegionMapEntry()=default; + RegionMapEntry(int x_, int y_, int width_, int height_, QString name_) { + this-> x = x_; + this-> y = y_; + this-> width = width_; + this-> height = height_; + this-> name = name_; + } int x; int y; int width; @@ -98,6 +106,8 @@ public: QVector getTiles(); void setTiles(QVector tileIds); + QString fixCase(QString); + private: int layout_width_; int layout_height_; @@ -112,8 +122,6 @@ private: int img_index_(int x, int y); int layout_index_(int x, int y); - - QString fix_case(QString); }; #endif // REGIONMAP_H diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index ea019695..831c0fe5 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -92,11 +92,11 @@ private: void displayCityMapTileSelector(); void displayCityMap(QString name); void displayRegionMapEntryOptions(); - void uodateRegionMapEntryOptions(int);// - void updateRegionMapEntryOptions(QString);// + void updateRegionMapEntryOptions(QString); void importTileImage(bool city = false); bool createCityMap(QString name); + bool tryInsertNewMapEntry(QString); void closeEvent(QCloseEvent* event); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 0c4fc5bb..64c54265 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -163,11 +163,17 @@ void RegionMap::saveLayout() { entries_text += "\nconst struct RegionMapLocation gRegionMapEntries[] = {\n"; + int longest = 1; + for (auto sec : project->mapSectionNameToValue.keys()) { + if (sec.length() > longest) longest = sec.length(); + } + for (auto sec : project->mapSectionNameToValue.keys()) { if (!mapSecToMapEntry.contains(sec)) continue; RegionMapEntry entry = mapSecToMapEntry.value(sec); - entries_text += " [" + sec + "] = {" + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " - + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + entry.name + "},\n"; + entries_text += " [" + sec + QString("]%1= {").arg(QString(" ").repeated(1 + longest - sec.length())) + + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " + + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + entry.name + "},\n"; } entries_text += "};\n\n#endif // GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n"; @@ -201,11 +207,11 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { if (!name.isEmpty()) { this->map_squares[index].map_name = name; this->project->mapSecToMapHoverName->insert(sec, name); - QString sName = fix_case(sec); + QString sName = fixCase(sec); sMapNamesMap.insert(sName, name); if (!mapSecToMapEntry.keys().contains(sec)) { sMapNames.append(sName); - RegionMapEntry entry = {x, y, 1, 1, sName}; + RegionMapEntry entry(x, y, 1, 1, sName); mapSecToMapEntry.insert(sec, entry); } } @@ -342,7 +348,7 @@ int RegionMap::getMapSquareIndex(int x, int y) { // For turning a MAPSEC_NAME into a unique identifier sMapName-style variable. // CAPS_WITH_UNDERSCORE to CamelCase -QString RegionMap::fix_case(QString caps) { +QString RegionMap::fixCase(QString caps) { bool big = true; QString camel; diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 17a9253c..b61598a5 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -214,6 +214,15 @@ void RegionMapEditor::displayRegionMapEntryOptions() { } void RegionMapEditor::updateRegionMapEntryOptions(QString section) { + bool enabled = section == "MAPSEC_NONE" ? false : true; + + this->ui->spinBox_RM_Entry_x->setEnabled(enabled); + this->ui->spinBox_RM_Entry_y->setEnabled(enabled); + this->ui->spinBox_RM_Entry_width->setEnabled(enabled); + this->ui->spinBox_RM_Entry_height->setEnabled(enabled); + + // if the key is not in the entries map, add it + this->ui->comboBox_RM_Entry_MapSection->setCurrentText(section); this->activeEntry = section; this->region_map_entries_item->currentSection = section; @@ -328,6 +337,17 @@ bool RegionMapEditor::createCityMap(QString name) { return !errored; } +bool RegionMapEditor::tryInsertNewMapEntry(QString mapsec) { + if (!this->region_map->mapSecToMapEntry.keys().contains(mapsec) && mapsec != "MAPSEC_NONE") { + RegionMapEntry entry(0, 0, 1, 1, region_map->fixCase(mapsec)); + this->region_map->sMapNamesMap.insert(region_map->fixCase(mapsec), QString()); + this->region_map->mapSecToMapEntry.insert(mapsec, entry); + this->region_map->sMapNames.append(region_map->fixCase(mapsec)); + return true; + } + return false; +} + void RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged(unsigned id) { this->selectedImageTile = id; } @@ -485,6 +505,7 @@ void RegionMapEditor::on_comboBox_RM_Entry_MapSection_activated(const QString &t } void RegionMapEditor::on_spinBox_RM_Entry_x_valueChanged(int x) { + tryInsertNewMapEntry(activeEntry); this->region_map->mapSecToMapEntry[activeEntry].setX(x); int idx = this->region_map->getMapSquareIndex(this->region_map->mapSecToMapEntry.value(activeEntry).x + this->region_map->padLeft, this->region_map->mapSecToMapEntry.value(activeEntry).y + this->region_map->padTop); @@ -494,6 +515,7 @@ void RegionMapEditor::on_spinBox_RM_Entry_x_valueChanged(int x) { } void RegionMapEditor::on_spinBox_RM_Entry_y_valueChanged(int y) { + tryInsertNewMapEntry(activeEntry); this->region_map->mapSecToMapEntry[activeEntry].setY(y); int idx = this->region_map->getMapSquareIndex(this->region_map->mapSecToMapEntry.value(activeEntry).x + this->region_map->padLeft, this->region_map->mapSecToMapEntry.value(activeEntry).y + this->region_map->padTop); @@ -503,12 +525,14 @@ void RegionMapEditor::on_spinBox_RM_Entry_y_valueChanged(int y) { } void RegionMapEditor::on_spinBox_RM_Entry_width_valueChanged(int width) { + tryInsertNewMapEntry(activeEntry); this->region_map->mapSecToMapEntry[activeEntry].setWidth(width); this->region_map_entries_item->draw(); this->hasUnsavedChanges = true; } void RegionMapEditor::on_spinBox_RM_Entry_height_valueChanged(int height) { + tryInsertNewMapEntry(activeEntry); this->region_map->mapSecToMapEntry[activeEntry].setHeight(height); this->region_map_entries_item->draw(); this->hasUnsavedChanges = true;