From 6b0034e52384cd3d1053d6fb5afeed5c41c01a5f Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 3 Jul 2024 11:34:27 -0400 Subject: [PATCH] Fix border visibility in API --- include/editor.h | 8 +--- include/mainwindow.h | 2 +- src/editor.cpp | 88 ++++++++++++++---------------------- src/mainwindow.cpp | 9 ++-- src/scriptapi/apiutility.cpp | 2 +- 5 files changed, 41 insertions(+), 68 deletions(-) diff --git a/include/editor.h b/include/editor.h index 5b3ed90e..4906a4e1 100644 --- a/include/editor.h +++ b/include/editor.h @@ -165,9 +165,7 @@ private: const QImage collisionPlaceholder = QImage(":/images/collisions_unknown.png"); QPixmap collisionSheetPixmap; - void setConnectionItemsVisible(bool); - void setBorderItemsVisible(bool, qreal = 1); - void setConnectionsEditable(bool); + void updateBorderVisibility(); QPoint calculateConnectionPosition(const MapConnection *connection, const QPixmap &pixmap); void redrawConnection(ConnectionPixmapItem* connectionItem); void createConnectionItem(MapConnection* connection); @@ -197,8 +195,6 @@ private slots: void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item); void onConnectionMoved(MapConnection*); void onConnectionItemSelected(ConnectionPixmapItem* connectionItem); - void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem); - void onConnectionDirectionChanged(QString newDirection); void onHoveredMovementPermissionChanged(uint16_t, uint16_t); void onHoveredMovementPermissionCleared(); void onHoveredMetatileSelectionChanged(uint16_t); @@ -213,7 +209,7 @@ private slots: signals: void objectsChanged(); - void loadMapRequested(QString, QString); + void connectionItemDoubleClicked(QString, QString); void wildMonDataChanged(); void warpEventDoubleClicked(QString, int, Event::Group); void currentMetatilesSelectionChanged(); diff --git a/include/mainwindow.h b/include/mainwindow.h index 51bfecc3..72067d5c 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -177,7 +177,7 @@ private slots: void copy(); void paste(); - void onLoadMapRequested(QString, QString); + void onConnectionItemDoubleClicked(QString, QString); void onMapChanged(Map *map); void onMapNeedsRedrawing(); void onTilesetsSaved(QString, QString); diff --git a/src/editor.cpp b/src/editor.cpp index 39de3a8a..d5a0dbd2 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -99,9 +99,7 @@ void Editor::setEditingMap() { if (events_group) { events_group->setVisible(false); } - setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionsEditable(false); + updateBorderVisibility(); this->cursorMapTileRect->stopSingleTileMode(); this->cursorMapTileRect->setActive(true); @@ -123,9 +121,7 @@ void Editor::setEditingCollision() { if (events_group) { events_group->setVisible(false); } - setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionsEditable(false); + updateBorderVisibility(); this->cursorMapTileRect->setSingleTileMode(); this->cursorMapTileRect->setActive(true); @@ -146,9 +142,7 @@ void Editor::setEditingObjects() { if (collision_item) { collision_item->setVisible(false); } - setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionItemsVisible(ui->checkBox_ToggleBorder->isChecked()); - setConnectionsEditable(false); + updateBorderVisibility(); this->cursorMapTileRect->setSingleTileMode(); this->cursorMapTileRect->setActive(false); updateWarpEventWarnings(); @@ -179,10 +173,6 @@ void Editor::setEditingConnections() { map_item->draw(); map_item->setVisible(true); populateConnectionsList(); - if (selected_connection_item) { - // TODO: Do we need additional handling here again - redrawConnection(selected_connection_item); - } maskNonVisibleConnectionTiles(); } if (collision_item) { @@ -191,9 +181,7 @@ void Editor::setEditingConnections() { if (events_group) { events_group->setVisible(false); } - setBorderItemsVisible(true, 0.4); - setConnectionItemsVisible(true); - setConnectionsEditable(true); + updateBorderVisibility(); this->cursorMapTileRect->setSingleTileMode(); this->cursorMapTileRect->setActive(false); } @@ -804,20 +792,6 @@ void Editor::removeConnection(ConnectionPixmapItem* connectionItem) { emit editedMapData(); } -void Editor::setConnectionItemsVisible(bool visible) { - for (ConnectionPixmapItem* item : connection_items) { - item->setVisible(visible); - item->setEnabled(visible); - } -} - -void Editor::setBorderItemsVisible(bool visible, qreal opacity) { - for (QGraphicsPixmapItem* item : borderItems) { - item->setVisible(visible); - item->setOpacity(opacity); - } -} - QPoint Editor::calculateConnectionPosition(const MapConnection *connection, const QPixmap &pixmap) { int x = 0, y = 0; const int mWidth = 16, mHeight = 16; @@ -883,7 +857,7 @@ void Editor::redrawConnection(ConnectionPixmapItem* connectionItem) { // TODO: Generalize, maybe use in addConnectionToList connection instead of full render void Editor::updateConnectionOffset(int offset) { - if (!selected_connection_item) + /*if (!selected_connection_item) return; selected_connection_item->blockSignals(true); @@ -895,7 +869,7 @@ void Editor::updateConnectionOffset(int offset) { } selected_connection_item->blockSignals(false); updateMirroredConnectionOffset(selected_connection_item->connection); - maskNonVisibleConnectionTiles(); + maskNonVisibleConnectionTiles();*/ } void Editor::onConnectionMoved(MapConnection* connection) { @@ -907,13 +881,6 @@ void Editor::onConnectionMoved(MapConnection* connection) { emit editedMapData(); } -void Editor::setConnectionsEditable(bool editable) { - for (ConnectionPixmapItem* item : connection_items) { - item->setEditable(editable); - item->updateHighlight(item == selected_connection_item); - } -} - void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) { if (!connectionItem) return; @@ -925,6 +892,7 @@ void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) { // TODO: Handle the highlight done in redrawConnection? } +// TODO: Inaccurate if there are multiple connections from the same map void Editor::setSelectedConnectionFromMap(QString mapName) { // Search for the first connection that connects to the given map map. for (ConnectionPixmapItem* item : connection_items) { @@ -935,21 +903,9 @@ void Editor::setSelectedConnectionFromMap(QString mapName) { } } -void Editor::onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem) { - emit loadMapRequested(connectionItem->connection->map_name, map->name); -} - -void Editor::onConnectionDirectionChanged(QString newDirection) { - /* // Connections TODO: - ui->comboBox_ConnectionDirection->blockSignals(true); - ui->comboBox_ConnectionDirection->setCurrentText(newDirection); - ui->comboBox_ConnectionDirection->blockSignals(false); - */ -} - void Editor::onBorderMetatilesChanged() { displayMapBorder(); - setBorderItemsVisible(ui->checkBox_ToggleBorder->isChecked()); + updateBorderVisibility(); // TODO: Why do we need to call this here } void Editor::onHoveredMovementPermissionChanged(uint16_t collision, uint16_t elevation) { @@ -1606,7 +1562,9 @@ void Editor::createConnectionItem(MapConnection* connection) { connect(item, &ConnectionPixmapItem::connectionMoved, this, &Editor::onConnectionMoved); connect(item, &ConnectionPixmapItem::connectionItemSelected, this, &Editor::onConnectionItemSelected); - connect(item, &ConnectionPixmapItem::connectionItemDoubleClicked, this, &Editor::onConnectionItemDoubleClicked); + connect(item, &ConnectionPixmapItem::connectionItemDoubleClicked, [this, item] { + emit this->connectionItemDoubleClicked(item->connection->map_name, map->name); + }); connection_items.append(item); } @@ -1886,13 +1844,33 @@ void Editor::updateSecondaryTileset(QString tilesetLabel, bool forceLoad) void Editor::toggleBorderVisibility(bool visible, bool enableScriptCallback) { - this->setBorderItemsVisible(visible); - this->setConnectionItemsVisible(visible); porymapConfig.setShowBorder(visible); + updateBorderVisibility(); if (enableScriptCallback) Scripting::cb_BorderVisibilityToggled(visible); } +void Editor::updateBorderVisibility() { + // On the connections tab, the border is always visible, and the connections can be edited. + bool editingConnections = (ui->mainTabBar->currentIndex() == 3); + bool visible = (editingConnections || ui->checkBox_ToggleBorder->isChecked()); + + // Update border + const qreal borderOpacity = editingConnections ? 0.4 : 1; + for (QGraphicsPixmapItem* item : borderItems) { + item->setVisible(visible); + item->setOpacity(borderOpacity); + } + + // Update map connections + for (ConnectionPixmapItem* item : connection_items) { + item->setVisible(visible); + item->setEditable(editingConnections); + item->setEnabled(visible); + item->updateHighlight(item == selected_connection_item); + } +} + void Editor::updateCustomMapHeaderValues(QTableWidget *table) { map->customHeaders = CustomAttributesTable::getAttributes(table); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c5142799..b90f9523 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -298,7 +298,7 @@ void MainWindow::checkForUpdates(bool) {} void MainWindow::initEditor() { this->editor = new Editor(ui); connect(this->editor, &Editor::objectsChanged, this, &MainWindow::updateObjects); - connect(this->editor, &Editor::loadMapRequested, this, &MainWindow::onLoadMapRequested); + connect(this->editor, &Editor::connectionItemDoubleClicked, this, &MainWindow::onConnectionItemDoubleClicked); connect(this->editor, &Editor::warpEventDoubleClicked, this, &MainWindow::openWarpMap); connect(this->editor, &Editor::currentMetatilesSelectionChanged, this, &MainWindow::currentMetatilesSelectionChanged); connect(this->editor, &Editor::wildMonDataChanged, this, &MainWindow::onWildMonDataChanged); @@ -2419,8 +2419,8 @@ void MainWindow::clickToolButtonFromEditMode(QString editMode) { } } -void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) { - if (userSetMap(mapName, true)) +void MainWindow::onConnectionItemDoubleClicked(QString mapName, QString fromMapName) { + if (mapName != fromMapName && userSetMap(mapName, true)) editor->setSelectedConnectionFromMap(fromMapName); } @@ -2706,8 +2706,7 @@ void MainWindow::on_checkBox_smartPaths_stateChanged(int selected) void MainWindow::on_checkBox_ToggleBorder_stateChanged(int selected) { - bool visible = selected != 0; - editor->toggleBorderVisibility(visible); + editor->toggleBorderVisibility(selected != 0); } void MainWindow::on_actionTileset_Editor_triggered() diff --git a/src/scriptapi/apiutility.cpp b/src/scriptapi/apiutility.cpp index 38ff3e39..1e44b1b7 100644 --- a/src/scriptapi/apiutility.cpp +++ b/src/scriptapi/apiutility.cpp @@ -168,7 +168,7 @@ bool ScriptUtility::getGridVisibility() { } void ScriptUtility::setBorderVisibility(bool visible) { - window->editor->toggleBorderVisibility(visible, false); + window->ui->checkBox_ToggleBorder->setChecked(visible); } bool ScriptUtility::getBorderVisibility() {