Merge pull request #306 from ultima-soul/import_amap_map

Add Import Map from Advance Map 1.92 feature.
This commit is contained in:
garak 2021-11-08 13:10:44 -05:00 committed by GitHub
commit f64b03d684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 251 additions and 5 deletions

View file

@ -2642,6 +2642,8 @@
<addaction name="actionTileset_Editor"/> <addaction name="actionTileset_Editor"/>
<addaction name="actionRegion_Map_Editor"/> <addaction name="actionRegion_Map_Editor"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionImport_Map_from_Advance_Map_1_92"/>
<addaction name="separator"/>
<addaction name="actionOpen_Project_in_Text_Editor"/> <addaction name="actionOpen_Project_in_Text_Editor"/>
</widget> </widget>
<widget class="QMenu" name="menuHelp"> <widget class="QMenu" name="menuHelp">
@ -2956,6 +2958,11 @@
<string>Open Config Folder</string> <string>Open Config Folder</string>
</property> </property>
</action> </action>
<action name="actionImport_Map_from_Advance_Map_1_92">
<property name="text">
<string>Import Map from Advance Map 1.92...</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<customwidgets> <customwidgets>

16
include/core/mapparser.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef MAPPARSER_H
#define MAPPARSER_H
#include "maplayout.h"
#include "project.h"
#include <QList>
#include <QString>
class MapParser
{
public:
MapParser();
MapLayout *parse(QString filepath, bool *error, Project *project);
};
#endif // MAPPARSER_H

View file

@ -132,8 +132,10 @@ private slots:
void onTilesetsSaved(QString, QString); void onTilesetsSaved(QString, QString);
void onWildMonDataChanged(); void onWildMonDataChanged();
void openNewMapPopupWindow(int, QVariant); void openNewMapPopupWindow(int, QVariant);
void openNewMapPopupWindowImportMap(MapLayout *);
void onNewMapCreated(); void onNewMapCreated();
void onMapCacheCleared(); void onMapCacheCleared();
void importMapFromAdvanceMap1_92();
void onMapRulerStatusChanged(const QString &); void onMapRulerStatusChanged(const QString &);
void applyUserShortcuts(); void applyUserShortcuts();
@ -193,6 +195,7 @@ private slots:
void on_action_Export_Map_Image_triggered(); void on_action_Export_Map_Image_triggered();
void on_actionExport_Stitched_Map_Image_triggered(); void on_actionExport_Stitched_Map_Image_triggered();
void on_actionExport_Map_Timelapse_Image_triggered(); void on_actionExport_Map_Timelapse_Image_triggered();
void on_actionImport_Map_from_Advance_Map_1_92_triggered();
void on_comboBox_ConnectionDirection_currentTextChanged(const QString &arg1); void on_comboBox_ConnectionDirection_currentTextChanged(const QString &arg1);
void on_spinBox_ConnectionOffset_valueChanged(int offset); void on_spinBox_ConnectionOffset_valueChanged(int offset);

View file

@ -93,6 +93,7 @@ public:
Tileset* loadTileset(QString, Tileset *tileset = nullptr); Tileset* loadTileset(QString, Tileset *tileset = nullptr);
Tileset* getTileset(QString, bool forceLoad = false); Tileset* getTileset(QString, bool forceLoad = false);
QMap<QString, QStringList> tilesetLabels; QMap<QString, QStringList> tilesetLabels;
QList<QString> tilesetLabelsOrdered;
Blockdata readBlockdata(QString); Blockdata readBlockdata(QString);
bool loadBlockdata(MapLayout*); bool loadBlockdata(MapLayout*);
@ -103,7 +104,7 @@ public:
void deleteFile(QString path); void deleteFile(QString path);
bool readMapGroups(); bool readMapGroups();
Map* addNewMapToGroup(QString, int, Map*, bool); Map* addNewMapToGroup(QString, int, Map*, bool, bool);
QString getNewMapName(); QString getNewMapName();
QString getProjectTitle(); QString getProjectTitle();

View file

@ -20,8 +20,10 @@ public:
Map *map; Map *map;
int group; int group;
bool existingLayout; bool existingLayout;
bool importedMap;
QString layoutId; QString layoutId;
void init(int, int, QString, QString); void init(int, int, QString, QString);
void initImportMap(MapLayout *);
void useLayout(QString); void useLayout(QString);
void connectSignals(); void connectSignals();
@ -32,6 +34,8 @@ private:
Ui::NewMapPopup *ui; Ui::NewMapPopup *ui;
Project *project; Project *project;
void setDefaultValues(int, QString); void setDefaultValues(int, QString);
void setDefaultValuesImportMap(MapLayout *);
void setDefaultValuesProjectConfig(bool, MapLayout*);
bool checkNewMapDimensions(); bool checkNewMapDimensions();
bool checkNewMapGroup(); bool checkNewMapGroup();

View file

@ -22,6 +22,7 @@ SOURCES += src/core/block.cpp \
src/core/imageexport.cpp \ src/core/imageexport.cpp \
src/core/map.cpp \ src/core/map.cpp \
src/core/maplayout.cpp \ src/core/maplayout.cpp \
src/core/mapparser.cpp \
src/core/metatile.cpp \ src/core/metatile.cpp \
src/core/metatileparser.cpp \ src/core/metatileparser.cpp \
src/core/paletteutil.cpp \ src/core/paletteutil.cpp \
@ -93,6 +94,7 @@ HEADERS += include/core/block.h \
include/core/map.h \ include/core/map.h \
include/core/mapconnection.h \ include/core/mapconnection.h \
include/core/maplayout.h \ include/core/maplayout.h \
include/core/mapparser.h \
include/core/metatile.h \ include/core/metatile.h \
include/core/metatileparser.h \ include/core/metatileparser.h \
include/core/paletteutil.h \ include/core/paletteutil.h \

97
src/core/mapparser.cpp Normal file
View file

@ -0,0 +1,97 @@
#include "mapparser.h"
#include "config.h"
#include "log.h"
#include "project.h"
MapParser::MapParser()
{
}
MapLayout *MapParser::parse(QString filepath, bool *error, Project *project)
{
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
*error = true;
logError(QString("Could not open Advance Map 1.92 Map .map file '%1': ").arg(filepath) + file.errorString());
return nullptr;
}
QByteArray in = file.readAll();
file.close();
if (in.length() < 20 || in.length() % 2 != 0) {
*error = true;
logError(QString("Advance Map 1.92 Map .map file '%1' is an unexpected size.").arg(filepath));
return nullptr;
}
int borderWidth = static_cast<unsigned char>(in.at(16)); // 0 in RSE .map files
int borderHeight = static_cast<unsigned char>(in.at(17)); // 0 in RSE .map files
int numBorderTiles = borderWidth * borderHeight; // 0 if RSE
int mapDataOffset = 20 + (numBorderTiles * 2); // FRLG .map files store border metatile data after the header
int mapWidth = static_cast<unsigned char>(in.at(0)) |
(static_cast<unsigned char>(in.at(1)) << 8) |
(static_cast<unsigned char>(in.at(2)) << 16) |
(static_cast<unsigned char>(in.at(3)) << 24);
int mapHeight = static_cast<unsigned char>(in.at(4)) |
(static_cast<unsigned char>(in.at(5)) << 8) |
(static_cast<unsigned char>(in.at(6)) << 16) |
(static_cast<unsigned char>(in.at(7)) << 24);
int mapPrimaryTilesetNum = static_cast<unsigned char>(in.at(8)) |
(static_cast<unsigned char>(in.at(9)) << 8) |
(static_cast<unsigned char>(in.at(10)) << 16) |
(static_cast<unsigned char>(in.at(11)) << 24);
int mapSecondaryTilesetNum = static_cast<unsigned char>(in.at(12)) |
(static_cast<unsigned char>(in.at(13)) << 8) |
(static_cast<unsigned char>(in.at(14)) << 16) |
(static_cast<unsigned char>(in.at(15)) << 24);
int numMetatiles = mapWidth * mapHeight;
int expectedFileSize = 20 + (numBorderTiles * 2) + (numMetatiles * 2);
if (in.length() != expectedFileSize) {
*error = true;
logError(QString(".map file is an unexpected size. Expected %1 bytes, but it has %2 bytes.").arg(expectedFileSize).arg(in.length()));
return nullptr;
}
Blockdata blockdata;
for (int i = mapDataOffset; (i + 1) < in.length(); i += 2) {
uint16_t word = static_cast<uint16_t>((in[i] & 0xff) + ((in[i + 1] & 0xff) << 8));
blockdata.append(word);
}
Blockdata border;
if (numBorderTiles != 0) {
for (int i = 20; (i + 1) < mapDataOffset; i += 2) {
uint16_t word = static_cast<uint16_t>((in[i] & 0xff) + ((in[i + 1] & 0xff) << 8));
border.append(word);
}
}
MapLayout *mapLayout = new MapLayout();
mapLayout->width = QString::number(mapWidth);
mapLayout->height = QString::number(mapHeight);
mapLayout->border_width = (borderWidth == 0) ? QString::number(2) : QString::number(borderWidth);
mapLayout->border_height = (borderHeight == 0) ? QString::number(2) : QString::number(borderHeight);
QList<QString> tilesets = project->tilesetLabelsOrdered;
if (mapPrimaryTilesetNum > tilesets.size())
mapLayout->tileset_primary_label = tilesets.at(0);
else
mapLayout->tileset_primary_label = tilesets.at(mapPrimaryTilesetNum);
if (mapSecondaryTilesetNum > tilesets.size())
mapLayout->tileset_secondary_label = tilesets.at(1);
else
mapLayout->tileset_secondary_label = tilesets.at(mapSecondaryTilesetNum);
mapLayout->blockdata = blockdata;
if (!border.isEmpty()) {
mapLayout->border = border;
}
return mapLayout;
}

View file

@ -15,6 +15,7 @@
#include "editcommands.h" #include "editcommands.h"
#include "flowlayout.h" #include "flowlayout.h"
#include "shortcut.h" #include "shortcut.h"
#include "mapparser.h"
#include <QFileDialog> #include <QFileDialog>
#include <QClipboard> #include <QClipboard>
@ -1161,8 +1162,9 @@ void MainWindow::onNewMapCreated() {
int newMapGroup = this->newmapprompt->group; int newMapGroup = this->newmapprompt->group;
Map *newMap = this->newmapprompt->map; Map *newMap = this->newmapprompt->map;
bool existingLayout = this->newmapprompt->existingLayout; bool existingLayout = this->newmapprompt->existingLayout;
bool importedMap = this->newmapprompt->importedMap;
newMap = editor->project->addNewMapToGroup(newMapName, newMapGroup, newMap, existingLayout); newMap = editor->project->addNewMapToGroup(newMapName, newMapGroup, newMap, existingLayout, importedMap);
logInfo(QString("Created a new map named %1.").arg(newMapName)); logInfo(QString("Created a new map named %1.").arg(newMapName));
@ -1215,6 +1217,24 @@ void MainWindow::openNewMapPopupWindow(int type, QVariant data) {
this->newmapprompt->setAttribute(Qt::WA_DeleteOnClose); this->newmapprompt->setAttribute(Qt::WA_DeleteOnClose);
} }
void MainWindow::openNewMapPopupWindowImportMap(MapLayout *mapLayout) {
if (!this->newmapprompt) {
this->newmapprompt = new NewMapPopup(this, this->editor->project);
}
if (!this->newmapprompt->isVisible()) {
this->newmapprompt->show();
} else {
this->newmapprompt->raise();
this->newmapprompt->activateWindow();
}
this->newmapprompt->initImportMap(mapLayout);
connect(this->newmapprompt, SIGNAL(applied()), this, SLOT(onNewMapCreated()));
connect(this->newmapprompt, &QObject::destroyed, [=](QObject *) { this->newmapprompt = nullptr; });
this->newmapprompt->setAttribute(Qt::WA_DeleteOnClose);
}
void MainWindow::on_action_NewMap_triggered() { void MainWindow::on_action_NewMap_triggered() {
openNewMapPopupWindow(MapSortOrder::Group, 0); openNewMapPopupWindow(MapSortOrder::Group, 0);
} }
@ -2726,6 +2746,38 @@ void MainWindow::on_actionExport_Map_Timelapse_Image_triggered() {
showExportMapImageWindow(ImageExporterMode::Timelapse); showExportMapImageWindow(ImageExporterMode::Timelapse);
} }
void MainWindow::on_actionImport_Map_from_Advance_Map_1_92_triggered(){
importMapFromAdvanceMap1_92();
}
void MainWindow::importMapFromAdvanceMap1_92()
{
QString filepath = QFileDialog::getOpenFileName(
this,
QString("Import Map from Advance Map 1.92"),
this->editor->project->root,
"Advance Map 1.92 Map Files (*.map)");
if (filepath.isEmpty()) {
return;
}
MapParser parser;
bool error = false;
MapLayout *mapLayout = parser.parse(filepath, &error, editor->project);
if (error) {
QMessageBox msgBox(this);
msgBox.setText("Failed to import map from Advance Map 1.92 .map file.");
QString message = QString("The .map file could not be processed. View porymap.log for specific errors.");
msgBox.setInformativeText(message);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Icon::Critical);
msgBox.exec();
return;
}
openNewMapPopupWindowImportMap(mapLayout);
}
void MainWindow::showExportMapImageWindow(ImageExporterMode mode) { void MainWindow::showExportMapImageWindow(ImageExporterMode mode) {
if (!editor->project) return; if (!editor->project) return;

View file

@ -1848,7 +1848,7 @@ bool Project::readMapGroups() {
return true; return true;
} }
Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool existingLayout) { Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool existingLayout, bool importedMap) {
mapNames.append(mapName); mapNames.append(mapName);
mapGroups.insert(mapName, groupNum); mapGroups.insert(mapName, groupNum);
groupedMapNames[groupNum].append(mapName); groupedMapNames[groupNum].append(mapName);
@ -1861,8 +1861,12 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum, Map *newMap, bool
if (!existingLayout) { if (!existingLayout) {
mapLayouts.insert(newMap->layoutId, newMap->layout); mapLayouts.insert(newMap->layoutId, newMap->layout);
mapLayoutsTable.append(newMap->layoutId); mapLayoutsTable.append(newMap->layoutId);
setNewMapBlockdata(newMap); if (!importedMap) {
setNewMapBorder(newMap); setNewMapBlockdata(newMap);
}
if (newMap->layout->border.isEmpty()) {
setNewMapBorder(newMap);
}
} }
loadLayoutTilesets(newMap->layout); loadLayoutTilesets(newMap->layout);
@ -1910,6 +1914,7 @@ QMap<QString, QStringList> Project::getTilesetLabels() {
QStringList secondaryTilesets; QStringList secondaryTilesets;
allTilesets.insert("primary", primaryTilesets); allTilesets.insert("primary", primaryTilesets);
allTilesets.insert("secondary", secondaryTilesets); allTilesets.insert("secondary", secondaryTilesets);
QList<QString> tilesetLabelsOrdered;
QString filename = "data/tilesets/headers.inc"; QString filename = "data/tilesets/headers.inc";
QString headers_text = parser.readTextFile(root + "/" + filename); QString headers_text = parser.readTextFile(root + "/" + filename);
@ -1938,8 +1943,10 @@ QMap<QString, QStringList> Project::getTilesetLabels() {
allTilesets["secondary"].append(tilesetLabel); allTilesets["secondary"].append(tilesetLabel);
else else
allTilesets["primary"].append(tilesetLabel); allTilesets["primary"].append(tilesetLabel);
tilesetLabelsOrdered.append(tilesetLabel);
} }
this->tilesetLabels = allTilesets; this->tilesetLabels = allTilesets;
this->tilesetLabelsOrdered = tilesetLabelsOrdered;
return allTilesets; return allTilesets;
} }

View file

@ -17,6 +17,7 @@ NewMapPopup::NewMapPopup(QWidget *parent, Project *project) :
ui->setupUi(this); ui->setupUi(this);
this->project = project; this->project = project;
this->existingLayout = false; this->existingLayout = false;
this->importedMap = false;
} }
NewMapPopup::~NewMapPopup() NewMapPopup::~NewMapPopup()
@ -41,6 +42,12 @@ void NewMapPopup::init(int type, int group, QString sec, QString layoutId) {
connectSignals(); connectSignals();
} }
void NewMapPopup::initImportMap(MapLayout *mapLayout) {
this->importedMap = true;
setDefaultValuesImportMap(mapLayout);
connectSignals();
}
bool NewMapPopup::checkNewMapDimensions() { bool NewMapPopup::checkNewMapDimensions() {
int numMetatiles = project->getMapDataSize(ui->spinBox_NewMap_Width->value(), ui->spinBox_NewMap_Height->value()); int numMetatiles = project->getMapDataSize(ui->spinBox_NewMap_Width->value(), ui->spinBox_NewMap_Height->value());
int maxMetatiles = project->getMaxMapDataSize(); int maxMetatiles = project->getMaxMapDataSize();
@ -136,6 +143,42 @@ void NewMapPopup::setDefaultValues(int groupNum, QString mapSec) {
ui->frame_NewMap_Options->setEnabled(true); ui->frame_NewMap_Options->setEnabled(true);
setDefaultValuesProjectConfig(false, NULL);
}
void NewMapPopup::setDefaultValuesImportMap(MapLayout *mapLayout) {
ui->lineEdit_NewMap_Name->setText(project->getNewMapName());
QMap<QString, QStringList> tilesets = project->getTilesetLabels();
ui->comboBox_NewMap_Primary_Tileset->addItems(tilesets.value("primary"));
ui->comboBox_NewMap_Secondary_Tileset->addItems(tilesets.value("secondary"));
ui->comboBox_NewMap_Group->addItems(project->groupNames);
ui->comboBox_NewMap_Group->setCurrentText(project->groupNames.at(0));
ui->spinBox_NewMap_Width->setValue(mapLayout->width.toInt(nullptr, 0));
ui->spinBox_NewMap_Height->setValue(mapLayout->height.toInt(nullptr, 0));
ui->comboBox_NewMap_Primary_Tileset->setCurrentText(mapLayout->tileset_primary_label);
ui->comboBox_NewMap_Secondary_Tileset->setCurrentText(mapLayout->tileset_secondary_label);
ui->comboBox_NewMap_Type->addItems(project->mapTypes);
ui->comboBox_NewMap_Location->addItems(project->mapSectionValueToName.values());
ui->checkBox_NewMap_Show_Location->setChecked(true);
ui->frame_NewMap_Options->setEnabled(true);
setDefaultValuesProjectConfig(true, mapLayout);
map = new Map();
map->layout = new MapLayout();
map->layout->blockdata = mapLayout->blockdata;
if (!mapLayout->border.isEmpty()) {
map->layout->border = mapLayout->border;
}
}
void NewMapPopup::setDefaultValuesProjectConfig(bool importedMap, MapLayout *mapLayout) {
switch (projectConfig.getBaseGameVersion()) switch (projectConfig.getBaseGameVersion())
{ {
case BaseGameVersion::pokeruby: case BaseGameVersion::pokeruby:
@ -164,11 +207,19 @@ void NewMapPopup::setDefaultValues(int groupNum, QString mapSec) {
break; break;
} }
if (projectConfig.getUseCustomBorderSize()) { if (projectConfig.getUseCustomBorderSize()) {
if (importedMap) {
ui->spinBox_NewMap_BorderWidth->setValue(mapLayout->border_width.toInt(nullptr, 0));
ui->spinBox_NewMap_BorderHeight->setValue(mapLayout->border_height.toInt(nullptr, 0));
}
ui->spinBox_NewMap_BorderWidth->setVisible(true); ui->spinBox_NewMap_BorderWidth->setVisible(true);
ui->spinBox_NewMap_BorderHeight->setVisible(true); ui->spinBox_NewMap_BorderHeight->setVisible(true);
ui->label_NewMap_BorderWidth->setVisible(true); ui->label_NewMap_BorderWidth->setVisible(true);
ui->label_NewMap_BorderHeight->setVisible(true); ui->label_NewMap_BorderHeight->setVisible(true);
} else { } else {
if (importedMap) {
ui->spinBox_NewMap_BorderWidth->setValue(DEFAULT_BORDER_WIDTH);
ui->spinBox_NewMap_BorderHeight->setValue(DEFAULT_BORDER_HEIGHT);
}
ui->spinBox_NewMap_BorderWidth->setVisible(false); ui->spinBox_NewMap_BorderWidth->setVisible(false);
ui->spinBox_NewMap_BorderHeight->setVisible(false); ui->spinBox_NewMap_BorderHeight->setVisible(false);
ui->label_NewMap_BorderWidth->setVisible(false); ui->label_NewMap_BorderWidth->setVisible(false);
@ -242,6 +293,12 @@ void NewMapPopup::on_pushButton_NewMap_Accept_clicked() {
layout->blockdata_path = QString("data/layouts/%1/map.bin").arg(newMapName); layout->blockdata_path = QString("data/layouts/%1/map.bin").arg(newMapName);
} }
if (this->importedMap) {
layout->blockdata = map->layout->blockdata;
if (!map->layout->border.isEmpty())
layout->border = map->layout->border;
}
if (this->ui->checkBox_NewMap_Flyable->isChecked()) { if (this->ui->checkBox_NewMap_Flyable->isChecked()) {
newMap->isFlyable = "TRUE"; newMap->isFlyable = "TRUE";
} }