From 574151aebbf594f65cc973a7945ed898bddacafc Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 13 Dec 2021 11:41:46 -0500 Subject: [PATCH] Clean up API changes --- include/mainwindow.h | 6 ++-- include/ui/cursortilerect.h | 1 + src/core/editcommands.cpp | 12 +++++--- src/mainwindow.cpp | 20 ++++++-------- src/mainwindow_scriptapi.cpp | 53 ++++++++++++++++++++++++++++++------ src/ui/cursortilerect.cpp | 5 ++++ src/ui/mappixmapitem.cpp | 4 ++- 7 files changed, 73 insertions(+), 28 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index 2830906e..0efb157b 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -124,6 +124,10 @@ public: Q_INVOKABLE int getMaxPrimaryTilesetMetatiles(); Q_INVOKABLE int getNumSecondaryTilesetMetatiles(); Q_INVOKABLE int getMaxSecondaryTilesetMetatiles(); + Q_INVOKABLE int getNumPrimaryTilesetTiles(); + Q_INVOKABLE int getMaxPrimaryTilesetTiles(); + Q_INVOKABLE int getNumSecondaryTilesetTiles(); + Q_INVOKABLE int getMaxSecondaryTilesetTiles(); Q_INVOKABLE bool isPrimaryTileset(QString tilesetName); Q_INVOKABLE bool isSecondaryTileset(QString tilesetName); Q_INVOKABLE QString getPrimaryTileset(); @@ -396,8 +400,6 @@ private: QString getEventGroupFromTabWidget(QWidget *tab); void closeSupplementaryWindows(); void setWindowDisabled(bool); - void setMainTabInternal(int); - void setMapViewTabInternal(int); void initTilesetEditor(); bool initRegionMapEditor(); diff --git a/include/ui/cursortilerect.h b/include/ui/cursortilerect.h index 1ccb60f7..26b4d929 100644 --- a/include/ui/cursortilerect.h +++ b/include/ui/cursortilerect.h @@ -59,6 +59,7 @@ public: void updateLocation(int x, int y); void updateSelectionSize(int width, int height); void setActive(bool active); + bool getActive(); bool *enabled; private: bool active; diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index c9929503..be795cd9 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -209,7 +209,7 @@ void ResizeMap::redo() { if (!map) return; - map->setBlockdata(newMetatiles); + map->layout->blockdata = newMetatiles; map->setDimensions(newMapWidth, newMapHeight, false); map->layout->border = newBorder; @@ -223,7 +223,7 @@ void ResizeMap::redo() { void ResizeMap::undo() { if (!map) return; - map->setBlockdata(oldMetatiles); + map->layout->blockdata = oldMetatiles; map->setDimensions(oldMapWidth, oldMapHeight, false); map->layout->border = oldBorder; @@ -510,9 +510,11 @@ void ScriptEditMap::redo() { if (!map) return; - map->setBlockdata(newMetatiles); if (newMapWidth != map->getWidth() || newMapHeight != map->getHeight()) { + map->layout->blockdata = newMetatiles; map->setDimensions(newMapWidth, newMapHeight, false); + } else { + map->setBlockdata(newMetatiles); } map->layout->lastCommitMapBlocks.blocks = newMetatiles; @@ -524,9 +526,11 @@ void ScriptEditMap::redo() { void ScriptEditMap::undo() { if (!map) return; - map->setBlockdata(oldMetatiles); if (oldMapWidth != map->getWidth() || oldMapHeight != map->getHeight()) { + map->layout->blockdata = oldMetatiles; map->setDimensions(oldMapWidth, oldMapHeight, false); + } else { + map->setBlockdata(oldMetatiles); } map->layout->lastCommitMapBlocks.blocks = oldMetatiles; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 95a9630d..fa6faad7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -676,7 +676,7 @@ void MainWindow::redrawMapScene() void MainWindow::refreshMapScene() { - setMainTabInternal(ui->mainTabBar->currentIndex()); + on_mainTabBar_tabBarClicked(ui->mainTabBar->currentIndex()); ui->graphicsView_Map->setScene(editor->scene); ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect()); @@ -1663,12 +1663,12 @@ void MainWindow::on_action_Save_triggered() { void MainWindow::on_mapViewTab_tabBarClicked(int index) { - Scripting::cb_MapViewTabChanged(ui->mapViewTab->currentIndex(), index); - setMapViewTabInternal(index); -} + int oldIndex = ui->mapViewTab->currentIndex(); + if (index != oldIndex) + Scripting::cb_MapViewTabChanged(oldIndex, index); + + ui->mapViewTab->setCurrentIndex(index); -void MainWindow::setMapViewTabInternal(int index) -{ if (index == 0) { editor->setEditingMap(); } else if (index == 1) { @@ -1687,11 +1687,7 @@ void MainWindow::on_mainTabBar_tabBarClicked(int index) int oldIndex = ui->mainTabBar->currentIndex(); if (index != oldIndex) Scripting::cb_MainTabChanged(oldIndex, index); - setMainTabInternal(index); -} -void MainWindow::setMainTabInternal(int index) -{ ui->mainTabBar->setCurrentIndex(index); int tabIndexToStackIndex[5] = {0, 0, 1, 2, 3}; @@ -1699,7 +1695,7 @@ void MainWindow::setMainTabInternal(int index) if (index == 0) { ui->stackedWidget_MapEvents->setCurrentIndex(0); - setMapViewTabInternal(ui->mapViewTab->currentIndex()); + on_mapViewTab_tabBarClicked(ui->mapViewTab->currentIndex()); clickToolButtonFromEditMode(editor->map_edit_mode); } else if (index == 1) { ui->stackedWidget_MapEvents->setCurrentIndex(1); @@ -1747,7 +1743,7 @@ void MainWindow::on_actionCursor_Tile_Outline_triggered() porymapConfig.setShowCursorTile(enabled); this->editor->settings->cursorTileRectEnabled = enabled; if (this->editor->map_item->has_mouse) { - this->editor->cursorMapTileRect->setVisible(enabled); + this->editor->cursorMapTileRect->setVisible(enabled && this->editor->cursorMapTileRect->getActive()); ui->graphicsView_Map->scene()->update(); } } diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 0f6bd8a1..02d4747f 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -22,7 +22,7 @@ QJSValue MainWindow::getBlock(int x, int y) { // isn't ignored. Ideally the setMetatileTiles functions would properly // set each of the map spaces that use the modified metatile so that // the cache could be used, though this would lkely still require a -// full read of the map and its border/connections. +// full read of the map. void MainWindow::tryRedrawMapArea(bool forceRedraw) { if (!forceRedraw) return; @@ -655,6 +655,31 @@ int MainWindow::getMaxSecondaryTilesetMetatiles() { return this->editor->project->getNumMetatilesTotal() - this->editor->project->getNumMetatilesPrimary(); } + +int MainWindow::getNumPrimaryTilesetTiles() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_primary) + return 0; + return this->editor->map->layout->tileset_primary->tiles.length(); +} + +int MainWindow::getMaxPrimaryTilesetTiles() { + if (!this->editor || !this->editor->project) + return 0; + return this->editor->project->getNumTilesPrimary(); +} + +int MainWindow::getNumSecondaryTilesetTiles() { + if (!this->editor || !this->editor->map || !this->editor->map->layout || !this->editor->map->layout->tileset_secondary) + return 0; + return this->editor->map->layout->tileset_secondary->tiles.length(); +} + +int MainWindow::getMaxSecondaryTilesetTiles() { + if (!this->editor || !this->editor->project) + return 0; + return this->editor->project->getNumTilesTotal() - this->editor->project->getNumTilesPrimary(); +} + bool MainWindow::isPrimaryTileset(QString tilesetName) { if (!this->editor || !this->editor->project) return false; @@ -823,12 +848,19 @@ QString MainWindow::getMetatileLabel(int metatileId) { return metatile->label; } -// TODO: Validate label void MainWindow::setMetatileLabel(int metatileId, QString label) { Metatile * metatile = this->getMetatile(metatileId); if (!metatile) return; + QRegularExpression expression("[_A-Za-z0-9]*$"); + QRegularExpressionValidator validator(expression); + int pos = 0; + if (validator.validate(label, pos) != QValidator::Acceptable) { + logError(QString("Invalid metatile label %1").arg(label)); + return; + } + if (this->tilesetEditor && this->tilesetEditor->getSelectedMetatile() == metatileId) { this->tilesetEditor->setMetatileLabel(label); } else if (metatile->label != label) { @@ -932,12 +964,12 @@ void MainWindow::setMetatileTiles(int metatileId, QJSValue tilesObj, int tileSta // Write to metatile using as many of the given Tiles as possible int numTileObjs = qMin(tilesObj.property("length").toInt(), numTiles); int i = 0; - for (; i < numTileObjs; i++) - metatile->tiles[i] = Scripting::toTile(tilesObj.property(i)); + for (; i < numTileObjs; i++, tileStart++) + metatile->tiles[tileStart] = Scripting::toTile(tilesObj.property(i)); // Fill remainder of specified length with empty Tiles - for (; i < numTiles; i++) - metatile->tiles[i] = Tile(); + for (; i < numTiles; i++, tileStart++) + metatile->tiles[tileStart] = Tile(); this->saveMetatilesByMetatileId(metatileId); this->needsFullRedraw = true; @@ -952,7 +984,7 @@ void MainWindow::setMetatileTiles(int metatileId, int tileId, bool xflip, bool y // Write to metatile using Tiles of the specified value Tile tile = Tile(tileId, xflip, yflip, palette); - for (int i = 0; i < numTiles; i++) + for (int i = tileStart; i <= tileEnd; i++) metatile->tiles[i] = tile; this->saveMetatilesByMetatileId(metatileId); @@ -999,7 +1031,10 @@ int MainWindow::getMainTab() { void MainWindow::setMainTab(int index) { if (!this->ui || !this->ui->mainTabBar || index < 0 || index >= this->ui->mainTabBar->count()) return; - this->setMainTabInternal(index); + // Can't select Wild Encounters tab if it's disabled + if (index == 4 && !projectConfig.getEncounterJsonActive()) + return; + this->on_mainTabBar_tabBarClicked(index); } int MainWindow::getMapViewTab() { @@ -1011,5 +1046,5 @@ int MainWindow::getMapViewTab() { void MainWindow::setMapViewTab(int index) { if (this->getMainTab() != 0 || !this->ui->mapViewTab || index < 0 || index >= this->ui->mapViewTab->count()) return; - this->setMapViewTabInternal(index); + this->on_mapViewTab_tabBarClicked(index); } diff --git a/src/ui/cursortilerect.cpp b/src/ui/cursortilerect.cpp index 5f0f9399..4eed7c94 100644 --- a/src/ui/cursortilerect.cpp +++ b/src/ui/cursortilerect.cpp @@ -24,6 +24,11 @@ void CursorTileRect::setActive(bool active) this->active = active; } +bool CursorTileRect::getActive() +{ + return active; +} + void CursorTileRect::initAnchor(int coordX, int coordY) { this->anchorCoordX = coordX; diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index 512104d1..8623e59a 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -683,8 +683,10 @@ void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { setCursor(this->settings->mapCursor); } } -void MapPixmapItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) { +void MapPixmapItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event) { this->has_mouse = true; + QPoint pos = Metatile::coordFromPixmapCoord(event->pos()); + emit this->hoveredMapMetatileChanged(pos); } void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { emit this->hoveredMapMetatileCleared();