Fix zoom functionality, change shortcuts

This commit is contained in:
Diegoisawesome 2018-09-27 01:14:07 -05:00
parent f6f4b4024a
commit 88d1ea846a
9 changed files with 120 additions and 35 deletions

View file

@ -109,6 +109,9 @@ public:
int copiedMetatileSelectionHeight = 0;
QList<uint16_t> *copiedMetatileSelection = new QList<uint16_t>;
int scale_exp = 0;
double scale_base = sqrt(2); // adjust scale factor with this
QString map_edit_mode;
QString prev_edit_mode;
QCursor cursor;

View file

@ -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<int>(editor->scene->width()) + 2, static_cast<int>(editor->scene->height()) + 2);
double base = editor->scale_base;
double exp = editor->scale_exp;
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;
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<int>(editor->scene->width()) + 2, static_cast<int>(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<int>(editor->scene->width()) + 2, static_cast<int>(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<int>((editor->scene->width() + 2) * pow(base,exp));
int height = static_cast<int>((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<int>(ceil((editor->scene->width()) * pow(base,exp))) + 2;
int height = static_cast<int>(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)

View file

@ -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<QStandardItem*> *mapGroupsModel;
Editor *editor = nullptr;
QIcon* mapIcon;
MapSceneEventFilter *mapSceneEventFilter;
void setMap(QString);
void redrawMapScene();
void loadDataStructures();

View file

@ -378,8 +378,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<height>608</height>
<width>481</width>
<height>606</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_8">
@ -687,8 +687,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>324</width>
<height>77</height>
<width>300</width>
<height>70</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
@ -799,8 +799,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>307</width>
<height>387</height>
<width>304</width>
<height>372</height>
</rect>
</property>
<property name="sizePolicy">
@ -1116,8 +1116,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>381</width>
<height>657</height>
<width>385</width>
<height>655</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_7">
@ -1294,8 +1294,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>432</width>
<height>596</height>
<width>428</width>
<height>586</height>
</rect>
</property>
<property name="sizePolicy">
@ -1914,8 +1914,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>826</width>
<height>557</height>
<width>829</width>
<height>552</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_14">
@ -2087,7 +2087,7 @@
<x>0</x>
<y>0</y>
<width>1117</width>
<height>21</height>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -2195,7 +2195,7 @@
<string>Zoom In</string>
</property>
<property name="shortcut">
<string>+</string>
<string>Ctrl+=</string>
</property>
</action>
<action name="actionZoom_Out">
@ -2203,7 +2203,7 @@
<string>Zoom Out</string>
</property>
<property name="shortcut">
<string>-</string>
<string>Ctrl+-</string>
</property>
</action>
<action name="actionBetter_Cursors">

View file

@ -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() {

2
map.h
View file

@ -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;

23
mapsceneeventfilter.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "mapsceneeventfilter.h"
#include <QEvent>
#include <QGraphicsSceneWheelEvent>
MapSceneEventFilter::MapSceneEventFilter(QObject *parent) : QObject(parent)
{
}
bool MapSceneEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::GraphicsSceneWheel)
{
QGraphicsSceneWheelEvent *wheelEvent = static_cast<QGraphicsSceneWheelEvent *>(event);
if (wheelEvent->modifiers() & Qt::ControlModifier)
{
emit zoom(wheelEvent->delta() > 0 ? 1 : -1);
event->accept();
return true;
}
}
return false;
}

19
mapsceneeventfilter.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef MAPSCENEEVENTFILTER_H
#define MAPSCENEEVENTFILTER_H
#include <QObject>
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

View file

@ -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