From 88d1ea846a3975c172d1ed0d3678cfeb161a2925 Mon Sep 17 00:00:00 2001 From: Diegoisawesome Date: Thu, 27 Sep 2018 01:14:07 -0500 Subject: [PATCH] Fix zoom functionality, change shortcuts --- editor.h | 3 ++ mainwindow.cpp | 63 ++++++++++++++++++++++++++++++++--------- mainwindow.h | 4 +++ mainwindow.ui | 30 ++++++++++---------- map.cpp | 5 ++-- map.h | 2 -- mapsceneeventfilter.cpp | 23 +++++++++++++++ mapsceneeventfilter.h | 19 +++++++++++++ porymap.pro | 6 ++-- 9 files changed, 120 insertions(+), 35 deletions(-) create mode 100644 mapsceneeventfilter.cpp create mode 100644 mapsceneeventfilter.h diff --git a/editor.h b/editor.h index eca36d5e..b2d45900 100755 --- a/editor.h +++ b/editor.h @@ -109,6 +109,9 @@ public: int copiedMetatileSelectionHeight = 0; QList *copiedMetatileSelection = new QList; + int scale_exp = 0; + double scale_base = sqrt(2); // adjust scale factor with this + QString map_edit_mode; QString prev_edit_mode; QCursor cursor; diff --git a/mainwindow.cpp b/mainwindow.cpp index 53ba465a..35392f2c 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -39,6 +39,12 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->newEventToolButton, SIGNAL(newEventAdded(QString)), this, SLOT(addNewEvent(QString))); new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo())); + new QShortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale())); + + ui->actionZoom_In->setShortcuts({QKeySequence("Ctrl++"), QKeySequence("Ctrl+=")}); + + mapSceneEventFilter = new MapSceneEventFilter(this); + connect(mapSceneEventFilter, &MapSceneEventFilter::zoom, this, &MainWindow::scaleMapView); editor = new Editor(ui); connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects())); @@ -158,11 +164,23 @@ void MainWindow::on_action_Open_Project_triggered() } void MainWindow::setMap(QString map_name) { + bool needsEventFilter = false; + qDebug() << QString("setMap(%1)").arg(map_name); if (map_name.isNull()) { return; } + + if (!editor->scene) { + needsEventFilter = true; + } + editor->setMap(map_name); + + if (needsEventFilter) { + editor->scene->installEventFilter(mapSceneEventFilter); + } + redrawMapScene(); displayMapProperties(); @@ -183,16 +201,22 @@ void MainWindow::redrawMapScene() ui->graphicsView_Map->setScene(editor->scene); ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect()); - ui->graphicsView_Map->setFixedSize(static_cast(editor->scene->width()) + 2, static_cast(editor->scene->height()) + 2); + + double base = editor->scale_base; + double exp = editor->scale_exp; + + int width = static_cast(ceil((editor->scene->width()) * pow(base,exp))) + 2; + int height = static_cast(ceil((editor->scene->height()) * pow(base,exp))) + 2; + ui->graphicsView_Map->setFixedSize(width, height); ui->graphicsView_Objects_Map->setScene(editor->scene); ui->graphicsView_Objects_Map->setSceneRect(editor->scene->sceneRect()); - ui->graphicsView_Objects_Map->setFixedSize(static_cast(editor->scene->width()) + 2, static_cast(editor->scene->height()) + 2); + ui->graphicsView_Objects_Map->setFixedSize(width, height); ui->graphicsView_Objects_Map->editor = editor; ui->graphicsView_Connections->setScene(editor->scene); ui->graphicsView_Connections->setSceneRect(editor->scene->sceneRect()); - ui->graphicsView_Connections->setFixedSize(static_cast(editor->scene->width()) + 2, static_cast(editor->scene->height()) + 2); + ui->graphicsView_Connections->setFixedSize(width, height); ui->graphicsView_Metatiles->setScene(editor->scene_metatiles); //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect()); @@ -651,19 +675,32 @@ void MainWindow::on_actionMap_Shift_triggered() } void MainWindow::scaleMapView(int s) { - editor->map->scale_exp += s; + if ((editor->scale_exp + s) <= 5 && (editor->scale_exp + s) >= -2) // sane limits + { + if (s == 0) + { + s = -editor->scale_exp; + } - double base = editor->map->scale_base; - double exp = editor->map->scale_exp; - double sfactor = pow(base,s); + editor->scale_exp += s; - ui->graphicsView_Map->scale(sfactor,sfactor); - ui->graphicsView_Objects_Map->scale(sfactor,sfactor); + double base = editor->scale_base; + double exp = editor->scale_exp; + double sfactor = pow(base,s); - int width = static_cast((editor->scene->width() + 2) * pow(base,exp)); - int height = static_cast((editor->scene->height() + 2) * pow(base,exp)); - ui->graphicsView_Map->setFixedSize(width, height); - ui->graphicsView_Objects_Map->setFixedSize(width, height); + ui->graphicsView_Map->scale(sfactor,sfactor); + ui->graphicsView_Objects_Map->scale(sfactor,sfactor); + + int width = static_cast(ceil((editor->scene->width()) * pow(base,exp))) + 2; + int height = static_cast(ceil((editor->scene->height()) * pow(base,exp))) + 2; + ui->graphicsView_Map->setFixedSize(width, height); + ui->graphicsView_Objects_Map->setFixedSize(width, height); + ui->graphicsView_Connections->setFixedSize(width, height); + } +} + +void MainWindow::resetMapViewScale() { + scaleMapView(0); } void MainWindow::addNewEvent(QString event_type) diff --git a/mainwindow.h b/mainwindow.h index e1191c3a..783d2e94 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -12,6 +12,7 @@ #include "project.h" #include "map.h" #include "editor.h" +#include "mapsceneeventfilter.h" namespace Ui { class MainWindow; @@ -121,12 +122,15 @@ private slots: void on_checkBox_ToggleBorder_stateChanged(int arg1); + void resetMapViewScale(); + private: Ui::MainWindow *ui; QStandardItemModel *mapListModel; QList *mapGroupsModel; Editor *editor = nullptr; QIcon* mapIcon; + MapSceneEventFilter *mapSceneEventFilter; void setMap(QString); void redrawMapScene(); void loadDataStructures(); diff --git a/mainwindow.ui b/mainwindow.ui index 36600ee5..7e4d2067 100755 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -378,8 +378,8 @@ 0 0 - 469 - 608 + 481 + 606 @@ -687,8 +687,8 @@ 0 0 - 324 - 77 + 300 + 70 @@ -799,8 +799,8 @@ 0 0 - 307 - 387 + 304 + 372 @@ -1116,8 +1116,8 @@ 0 0 - 381 - 657 + 385 + 655 @@ -1294,8 +1294,8 @@ 0 0 - 432 - 596 + 428 + 586 @@ -1914,8 +1914,8 @@ 0 0 - 826 - 557 + 829 + 552 @@ -2087,7 +2087,7 @@ 0 0 1117 - 21 + 20 @@ -2195,7 +2195,7 @@ Zoom In - + + Ctrl+= @@ -2203,7 +2203,7 @@ Zoom Out - - + Ctrl+- diff --git a/map.cpp b/map.cpp index 54298a5c..e05b9d82 100755 --- a/map.cpp +++ b/map.cpp @@ -525,11 +525,10 @@ bool Map::hasUnsavedChanges() { } void Map::hoveredTileChanged(int x, int y, int block) { - emit statusBarMessage(QString("X: %1, Y: %2, Metatile: 0x%3, Scale = %4x") + emit statusBarMessage(QString("X: %1, Y: %2, Metatile: 0x%3") .arg(x) .arg(y) - .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()) - .arg(QString::number(pow(this->scale_base,this->scale_exp)))); + .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper())); } void Map::clearHoveredTile() { diff --git a/map.h b/map.h index b7c36b2e..8b4484f7 100755 --- a/map.h +++ b/map.h @@ -141,8 +141,6 @@ public: QString show_location; QString battle_scene; MapLayout *layout; - int scale_exp = 0; - double scale_base = sqrt(2); // adjust scale factor with this bool isPersistedToFile = true; diff --git a/mapsceneeventfilter.cpp b/mapsceneeventfilter.cpp new file mode 100644 index 00000000..bbfc5f25 --- /dev/null +++ b/mapsceneeventfilter.cpp @@ -0,0 +1,23 @@ +#include "mapsceneeventfilter.h" +#include +#include + +MapSceneEventFilter::MapSceneEventFilter(QObject *parent) : QObject(parent) +{ + +} + +bool MapSceneEventFilter::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::GraphicsSceneWheel) + { + QGraphicsSceneWheelEvent *wheelEvent = static_cast(event); + if (wheelEvent->modifiers() & Qt::ControlModifier) + { + emit zoom(wheelEvent->delta() > 0 ? 1 : -1); + event->accept(); + return true; + } + } + return false; +} diff --git a/mapsceneeventfilter.h b/mapsceneeventfilter.h new file mode 100644 index 00000000..e239f642 --- /dev/null +++ b/mapsceneeventfilter.h @@ -0,0 +1,19 @@ +#ifndef MAPSCENEEVENTFILTER_H +#define MAPSCENEEVENTFILTER_H + +#include + +class MapSceneEventFilter : public QObject +{ + Q_OBJECT +protected: + bool eventFilter(QObject *obj, QEvent *event) override; +public: + explicit MapSceneEventFilter(QObject *parent = nullptr); + +signals: + void zoom(int delta); +public slots: +}; + +#endif // MAPSCENEEVENTFILTER_H diff --git a/porymap.pro b/porymap.pro index f1d68efb..ba778baf 100755 --- a/porymap.pro +++ b/porymap.pro @@ -30,7 +30,8 @@ SOURCES += main.cpp\ neweventtoolbutton.cpp \ noscrollcombobox.cpp \ noscrollspinbox.cpp \ - heallocation.cpp + heallocation.cpp \ + mapsceneeventfilter.cpp HEADERS += mainwindow.h \ project.h \ @@ -47,7 +48,8 @@ HEADERS += mainwindow.h \ neweventtoolbutton.h \ noscrollcombobox.h \ noscrollspinbox.h \ - heallocation.h + heallocation.h \ + mapsceneeventfilter.h FORMS += mainwindow.ui \ objectpropertiesframe.ui