Refactor map zooming into the Editor

This commit is contained in:
BigBahss 2020-10-13 06:07:31 -04:00
parent db20d01f3c
commit 5247dd18c0
4 changed files with 47 additions and 68 deletions

View file

@ -148,6 +148,7 @@ public:
bool selectingEvent = false;
void shouldReselectEvents();
void scaleMapView(int);
private:
void setConnectionItemsVisible(bool);
@ -198,6 +199,7 @@ private slots:
void onHoveredMapMovementPermissionChanged(int, int);
void onHoveredMapMovementPermissionCleared();
void onSelectedMetatilesChanged();
void onWheelZoom(int);
signals:
void objectsChanged();
@ -206,7 +208,6 @@ signals:
void wildMonDataChanged();
void warpEventDoubleClicked(QString mapName, QString warpNum);
void currentMetatilesSelectionChanged();
void wheelZoom(int delta);
};
#endif // EDITOR_H

View file

@ -109,11 +109,6 @@ public:
Q_INVOKABLE QList<float> getMetatileLayerOpacity();
Q_INVOKABLE void setMetatileLayerOpacity(QList<float> order);
public slots:
void scaleMapView(int);
void onWheelZoom(int);
private slots:
void on_action_Open_Project_triggered();
void on_action_Reload_Project_triggered();

View file

@ -887,6 +887,47 @@ void Editor::onSelectedMetatilesChanged() {
this->redrawCurrentMetatilesSelection();
}
void Editor::onWheelZoom(int s) {
// Don't zoom the map when the user accidentally scrolls while performing a magic fill. (ctrl + middle button click)
if (!(QApplication::mouseButtons() & Qt::MiddleButton)) {
scaleMapView(s);
}
}
void Editor::scaleMapView(int s) {
if ((scale_exp + s) <= 5 && (scale_exp + s) >= -2) // sane limits
{
if (s == 0)
s = -scale_exp;
scale_exp += s;
double base = scale_base;
double exp = scale_exp;
double sfactor = pow(base, s);
const auto mapAnchor = ui->graphicsView_Map->transformationAnchor();
const auto connectionsAnchor = ui->graphicsView_Connections->transformationAnchor();
ui->graphicsView_Map->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->graphicsView_Connections->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->graphicsView_Map->scale(sfactor, sfactor);
ui->graphicsView_Connections->scale(sfactor, sfactor);
int width = static_cast<int>(ceil((scene->width()) * pow(base, exp))) + 2;
int height = static_cast<int>(ceil((scene->height()) * pow(base, exp))) + 2;
QSize viewSize = ui->scrollAreaWidgetContents_5->size();
int minWidth = qMin(width, viewSize.width());
int minHeight = qMin(height, viewSize.height());
ui->graphicsView_Map->setFixedSize(minWidth, minHeight);
ui->graphicsView_Connections->setFixedSize(minWidth, minHeight);
ui->graphicsView_Map->setTransformationAnchor(mapAnchor);
ui->graphicsView_Connections->setTransformationAnchor(connectionsAnchor);
}
}
void Editor::onHoveredMapMetatileChanged(int x, int y) {
this->playerViewRect->updateLocation(x, y);
this->cursorMapTileRect->updateLocation(x, y);
@ -1209,7 +1250,7 @@ bool Editor::displayMap() {
scene = new QGraphicsScene;
MapSceneEventFilter *filter = new MapSceneEventFilter();
scene->installEventFilter(filter);
connect(filter, &MapSceneEventFilter::wheelZoom, this, &Editor::wheelZoom);
connect(filter, &MapSceneEventFilter::wheelZoom, this, &Editor::onWheelZoom);
}
if (map_item && scene) {

View file

@ -148,7 +148,6 @@ void MainWindow::initEditor() {
connect(this->editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString)));
connect(this->editor, SIGNAL(currentMetatilesSelectionChanged()), this, SLOT(currentMetatilesSelectionChanged()));
connect(this->editor, SIGNAL(wildMonDataChanged()), this, SLOT(onWildMonDataChanged()));
connect(this->editor, &Editor::wheelZoom, this, &MainWindow::onWheelZoom);
this->loadUserSettings();
@ -1349,11 +1348,11 @@ void MainWindow::on_mainTabBar_tabBarClicked(int index)
}
void MainWindow::on_actionZoom_In_triggered() {
scaleMapView(1);
editor->scaleMapView(1);
}
void MainWindow::on_actionZoom_Out_triggered() {
scaleMapView(-1);
editor->scaleMapView(-1);
}
void MainWindow::on_actionBetter_Cursors_triggered() {
@ -1430,65 +1429,8 @@ void MainWindow::on_actionMap_Shift_triggered()
on_toolButton_Shift_clicked();
}
void MainWindow::onWheelZoom(int s) {
// Don't zoom the map when the user accidentally scrolls while performing a magic fill. (ctrl + middle button click)
if (!(QApplication::mouseButtons() & Qt::MiddleButton)) {
scaleMapView(s);
}
}
void MainWindow::scaleMapView(int s) {
if ((editor->scale_exp + s) <= 5 && (editor->scale_exp + s) >= -2) // sane limits
{
if (s == 0)
{
s = -editor->scale_exp;
}
editor->scale_exp += s;
double base = editor->scale_base;
double exp = editor->scale_exp;
double sfactor = pow(base,s);
ui->graphicsView_Map->setUpdatesEnabled(false);
const auto mapAnchor = ui->graphicsView_Map->transformationAnchor();
const auto connectionsAnchor = ui->graphicsView_Connections->transformationAnchor();
ui->graphicsView_Map->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->graphicsView_Connections->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->graphicsView_Map->scale(sfactor,sfactor);
ui->graphicsView_Connections->scale(sfactor,sfactor);
int width = static_cast<int>(ceil((editor->scene->width()) * pow(base,exp))) + 2;
int height = static_cast<int>(ceil((editor->scene->height()) * pow(base,exp))) + 2;
QSize viewSize = ui->scrollAreaWidgetContents_5->size();
if (width < viewSize.width()) {
ui->graphicsView_Map->setFixedWidth(width);
ui->graphicsView_Connections->setFixedWidth(width);
} else {
ui->graphicsView_Map->setFixedWidth(viewSize.width());
ui->graphicsView_Connections->setFixedWidth(viewSize.width());
}
if (height < viewSize.height()) {
ui->graphicsView_Map->setFixedHeight(height);
ui->graphicsView_Connections->setFixedHeight(height);
} else {
ui->graphicsView_Map->setFixedHeight(viewSize.height());
ui->graphicsView_Connections->setFixedHeight(viewSize.height());
}
ui->graphicsView_Map->setTransformationAnchor(mapAnchor);
ui->graphicsView_Connections->setTransformationAnchor(connectionsAnchor);
ui->graphicsView_Map->setUpdatesEnabled(true);
}
}
void MainWindow::resetMapViewScale() {
scaleMapView(0);
editor->scaleMapView(0);
}
void MainWindow::addNewEvent(QString event_type)