Reopen Porymap to last-opened map/layout

This commit is contained in:
GriffinR 2024-10-17 11:46:47 -04:00
parent b5c50e7660
commit 5e9ab4c7c7
5 changed files with 38 additions and 128 deletions

View file

@ -406,8 +406,7 @@ public:
reset();
}
virtual void reset() override {
this->recentMap = QString();
this->recentLayout = QString();
this->recentMapOrLayout = QString();
this->useEncounterJson = true;
this->customScripts.clear();
this->readKeys.clear();
@ -419,8 +418,7 @@ public:
QList<bool> getCustomScriptsEnabled();
QString projectDir;
QString recentMap;
QString recentLayout;
QString recentMapOrLayout;
bool useEncounterJson;
protected:

View file

@ -373,8 +373,6 @@ private:
bool tilesetNeedsRedraw = false;
bool setDefaultView();
bool setRecentView();
bool setLayout(QString layoutId);
bool setMap(QString, bool scroll = false);
void unsetMap();
@ -398,12 +396,6 @@ private:
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
bool setInitialMap();
bool setInitialLayout();
QString getDefaultMap();
QString getDefaultLayout();
void setRecentMapConfig(QString map_name);
void setRecentLayoutConfig(QString layoutId);
void saveGlobalConfigs();
void refreshRecentProjectsMenu();

View file

@ -278,13 +278,7 @@ uint32_t KeyValueConfigBase::getConfigUint32(QString key, QString value, uint32_
return qMin(max, qMax(min, result));
}
const QMap<MapSortOrder, QString> mapSortOrderMap = {
{MapSortOrder::SortByGroup, "group"},
{MapSortOrder::SortByLayout, "layout"},
{MapSortOrder::SortByArea, "area"},
};
const QMap<QString, MapSortOrder> mapSortOrderReverseMap = {
const QMap<QString, MapSortOrder> mapSortOrderMap = {
{"group", MapSortOrder::SortByGroup},
{"layout", MapSortOrder::SortByLayout},
{"area", MapSortOrder::SortByArea},
@ -316,8 +310,8 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
this->prettyCursors = getConfigBool(key, value);
} else if (key == "map_sort_order") {
QString sortOrder = value.toLower();
if (mapSortOrderReverseMap.contains(sortOrder)) {
this->mapSortOrder = mapSortOrderReverseMap.value(sortOrder);
if (mapSortOrderMap.contains(sortOrder)) {
this->mapSortOrder = mapSortOrderMap.value(sortOrder);
} else {
this->mapSortOrder = MapSortOrder::SortByGroup;
logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.").arg(value));
@ -438,7 +432,7 @@ QMap<QString, QString> PorymapConfig::getKeyValueMap() {
map.insert("project_manually_closed", this->projectManuallyClosed ? "1" : "0");
map.insert("reopen_on_launch", this->reopenOnLaunch ? "1" : "0");
map.insert("pretty_cursors", this->prettyCursors ? "1" : "0");
map.insert("map_sort_order", mapSortOrderMap.value(this->mapSortOrder));
map.insert("map_sort_order", mapSortOrderMap.key(this->mapSortOrder));
map.insert("main_window_geometry", stringFromByteArray(this->mainWindowGeometry));
map.insert("main_window_state", stringFromByteArray(this->mainWindowState));
map.insert("map_splitter_state", stringFromByteArray(this->mapSplitterState));
@ -740,8 +734,8 @@ void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
} else if (key == "enable_map_allow_flags") {
this->mapAllowFlagsEnabled = getConfigBool(key, value);
#ifdef CONFIG_BACKWARDS_COMPATABILITY
} else if (key == "recent_map") {
userConfig.recentMap = value;
} else if (key == "recent_map_or_layout") {
userConfig.recentMapOrLayout = value;
} else if (key == "use_encounter_json") {
userConfig.useEncounterJson = getConfigBool(key, value);
} else if (key == "custom_scripts") {
@ -1035,10 +1029,8 @@ QString UserConfig::getConfigFilepath() {
}
void UserConfig::parseConfigKeyValue(QString key, QString value) {
if (key == "recent_map") {
this->recentMap = value;
} else if (key == "recent_layout") {
this->recentLayout = value;
if (key == "recent_map_or_layout") {
this->recentMapOrLayout = value;
} else if (key == "use_encounter_json") {
this->useEncounterJson = getConfigBool(key, value);
} else if (key == "custom_scripts") {
@ -1054,8 +1046,7 @@ void UserConfig::setUnreadKeys() {
QMap<QString, QString> UserConfig::getKeyValueMap() {
QMap<QString, QString> map;
map.insert("recent_map", this->recentMap);
map.insert("recent_layout", this->recentLayout);
map.insert("recent_map_or_layout", this->recentMapOrLayout);
map.insert("use_encounter_json", QString::number(this->useEncounterJson));
map.insert("custom_scripts", this->outputCustomScripts());
return map;

View file

@ -1319,7 +1319,7 @@ bool Editor::setLayout(QString layoutId) {
return true;
}
void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item) {
void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *) {
if (!this->getEditingLayout()) {
return;
}
@ -1332,7 +1332,7 @@ void Editor::onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *
}
}
void Editor::onMapEndPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *item) {
void Editor::onMapEndPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *) {
if (!this->getEditingLayout()) {
return;
}

View file

@ -696,83 +696,29 @@ bool MainWindow::isProjectOpen() {
return editor && editor->project;
}
bool MainWindow::setDefaultView() {
if (porymapConfig.mapSortOrder == MapSortOrder::SortByLayout) {
return setLayout(getDefaultLayout());
} else {
return setMap(getDefaultMap(), true);
}
}
bool MainWindow::setRecentView() {
if (porymapConfig.mapSortOrder == MapSortOrder::SortByLayout) {
return setLayout(userConfig.recentLayout);
} else {
return setMap(userConfig.recentMap, true);
}
}
QString MainWindow::getDefaultMap() {
if (editor && editor->project) {
QList<QStringList> names = editor->project->groupedMapNames;
if (!names.isEmpty()) {
QString recentMap = userConfig.recentMap;
if (!recentMap.isNull() && recentMap.length() > 0) {
for (int i = 0; i < names.length(); i++) {
if (names.value(i).contains(recentMap)) {
return recentMap;
}
}
}
// Failing that, just get the first map in the list.
for (int i = 0; i < names.length(); i++) {
QStringList list = names.value(i);
if (list.length()) {
return list.value(0);
}
}
}
}
return QString();
}
bool MainWindow::setInitialMap() {
QStringList names;
if (editor && editor->project)
names = editor->project->mapNames;
// Try to set most recently-opened map, if it's still in the list.
QString recentMap = userConfig.recentMap;
if (!recentMap.isEmpty() && names.contains(recentMap) && setMap(recentMap, true))
const QString recent = userConfig.recentMapOrLayout;
if (editor->project->mapNames.contains(recent)) {
// User recently had a map open that still exists.
if (setMap(recent, true))
return true;
// Failing that, try loading maps in the map list sequentially.
for (auto name : names) {
if (name != recentMap && setMap(name, true))
} else if (editor->project->mapLayoutsTable.contains(recent)) {
// User recently had a layout open that still exists.
if (setLayout(recent))
return true;
}
logError("Failed to load any maps.");
return false;
}
bool MainWindow::setInitialLayout() {
QStringList names;
if (editor && editor->project)
names = editor->project->mapLayoutsTable;
// Try to set most recently-opened layout, if it's still in the list.
QString recentLayout = userConfig.recentLayout;
if (!recentLayout.isEmpty() && names.contains(recentLayout) && setLayout(recentLayout))
// Failed to open recent map/layout, or no recent map/layout. Try opening maps then layouts sequentially.
for (const auto &name : editor->project->mapNames) {
if (name != recent && setMap(name, true))
return true;
// Failing that, try loading maps in the map list sequentially.
for (auto name : names) {
if (name != recentLayout && setLayout(name))
}
for (const auto &id : editor->project->mapLayoutsTable) {
if (id != recent && setLayout(id))
return true;
}
logError("Failed to load any layouts.");
logError("Failed to load any maps or layouts.");
return false;
}
@ -824,25 +770,13 @@ void MainWindow::openSubWindow(QWidget * window) {
}
}
QString MainWindow::getDefaultLayout() {
if (editor && editor->project) {
QString recentLayout = userConfig.recentLayout;
if (!recentLayout.isEmpty() && editor->project->mapLayoutsTable.contains(recentLayout)) {
return recentLayout;
} else if (!editor->project->mapLayoutsTable.isEmpty()) {
return editor->project->mapLayoutsTable.first();
}
}
return QString();
}
QString MainWindow::getExistingDirectory(QString dir) {
return FileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly);
}
void MainWindow::on_action_Open_Project_triggered()
{
QString dir = getExistingDirectory(!userConfig.recentMap.isEmpty() ? userConfig.recentMap : ".");
QString dir = getExistingDirectory(!projectConfig.projectDir.isEmpty() ? userConfig.projectDir : ".");
if (!dir.isEmpty())
openProject(dir);
}
@ -904,11 +838,16 @@ bool MainWindow::userSetMap(QString map_name, bool scrollTreeView) {
bool MainWindow::setMap(QString map_name, bool scroll) {
// if map name is empty, clear & disable map ui
if (map_name.isEmpty() || map_name == DYNAMIC_MAP_NAME) {
if (map_name.isEmpty()) {
unsetMap();
return false;
}
if (map_name == DYNAMIC_MAP_NAME) {
logInfo(QString("Cannot set map to '%1'").arg(DYNAMIC_MAP_NAME));
return false;
}
logInfo(QString("Setting map to '%1'").arg(map_name));
if (!editor || !editor->setMap(map_name)) {
@ -944,7 +883,7 @@ bool MainWindow::setMap(QString map_name, bool scroll) {
connect(editor->layout, &Layout::layoutChanged, this, &MainWindow::onLayoutChanged, Qt::UniqueConnection);
connect(editor->layout, &Layout::needsRedrawing, this, &MainWindow::onLayoutNeedsRedrawing, Qt::UniqueConnection);
userConfig.recentMap = map_name;
userConfig.recentMapOrLayout = map_name;
Scripting::cb_MapOpened(map_name);
prefab.updatePrefabUi(editor->layout);
@ -974,7 +913,7 @@ bool MainWindow::setLayout(QString layoutId) {
updateTilesetEditor();
setRecentLayoutConfig(layoutId);
userConfig.recentMapOrLayout = layoutId;
return true;
}
@ -1050,16 +989,6 @@ void MainWindow::openWarpMap(QString map_name, int event_id, Event::Group event_
}
}
void MainWindow::setRecentMapConfig(QString mapName) {
userConfig.recentMap = mapName;
userConfig.recentLayout = "";
}
void MainWindow::setRecentLayoutConfig(QString layoutId) {
userConfig.recentLayout = layoutId;
userConfig.recentMap = "";
}
void MainWindow::displayMapProperties() {
// Block signals to the comboboxes while they are being modified
const QSignalBlocker blocker1(ui->comboBox_Song);