This commit is contained in:
garak 2018-09-20 20:35:57 +00:00 committed by GitHub
commit 4afdd43487
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 30 deletions

View file

@ -19,7 +19,7 @@
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QScroller>
#include <math.h>
#include <cmath>
#include <QProcess>
#include <QSysInfo>
#include <QDesktopServices>
@ -54,8 +54,15 @@ MainWindow::MainWindow(QWidget *parent) :
if (settings.contains(key)) {
QString default_dir = settings.value(key).toStringList().last();
if (!default_dir.isNull()) {
qDebug() << QString("default_dir: '%1'").arg(default_dir);
openProject(default_dir);
QDir dir(default_dir);
if (dir.exists()) {
qDebug() << QString("default_dir: '%1'").arg(default_dir);
openProject(default_dir);
}
else {
qDebug() << "No project found at" << default_dir;
closeProject();
}
}
}
@ -101,6 +108,12 @@ void MainWindow::openProject(QString dir) {
setStatusBarMessage(QString("Opened project %1").arg(dir));
}
void MainWindow::closeProject() {
QSettings settings;
settings.remove("recent_projects");
//ui->setupUi(this);
}
QString MainWindow::getDefaultMap() {
if (editor && editor->project) {
QList<QStringList> names = editor->project->groupedMapNames;
@ -156,15 +169,30 @@ void MainWindow::on_action_Open_Project_triggered()
}
}
void MainWindow::on_action_Close_Project_triggered()
{
closeProject();
QApplication::quit();
}
void MainWindow::setMap(QString map_name) {
// if clicking on map group, open first map in group
if (map_name.startsWith("gMapGroup")) {
int groupNum = map_name.remove("gMapGroup").toInt();
map_name = editor->project->groupedMapNames[groupNum][0];
}
qDebug() << QString("setMap(%1)").arg(map_name);
if (map_name.isNull()) {
return;
}
editor->setMap(map_name);
redrawMapScene();
scaleMapView(0);
displayMapProperties();
ui->mapList->scrollTo(mapListIndexes.value(map_name));
ui->mapList->setCurrentIndex(mapListIndexes.value(map_name));
setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - porymap");
connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
@ -426,6 +454,7 @@ void MainWindow::populateMapList() {
QString map_name = names.value(j);
QStandardItem *map = createMapItem(map_name, i, j);
group->appendRow(map);
mapListIndexes.insert(map_name, map->index());
}
}
@ -438,6 +467,7 @@ void MainWindow::populateMapList() {
ui->mapList->setUpdatesEnabled(true);
ui->mapList->expandToDepth(2);
ui->mapList->repaint();
ui->mapList->setExpandsOnDoubleClick(false);
}
QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) {
@ -488,6 +518,7 @@ void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction)
int numMapsInGroup = groupItem->rowCount();
QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup);
groupItem->appendRow(newMapItem);
mapListIndexes.insert(newMapName, newMapItem->index());
setMap(newMapName);
}
@ -649,20 +680,77 @@ void MainWindow::on_actionMap_Shift_triggered()
on_toolButton_Shift_clicked();
}
void MainWindow::scaleMapView(int s) {
editor->map->scale_exp += s;
void MainWindow::wheelEvent(QWheelEvent *event) {
const int THRESHOLD = 16;
QPoint moved = event->pixelDelta();
if (event->modifiers() & Qt::ControlModifier) {
ui->scrollArea->verticalScrollBar()->setEnabled(false);
ui->scrollArea->horizontalScrollBar()->setEnabled(false);
if (moved.y() >= THRESHOLD) {
scaleMapView(1);
event->accept();
}
else if (moved.y() <= -THRESHOLD) {
scaleMapView(-1);
event->accept();
}
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *event) {
// when CTRL key is released after wheelEvent zooming, re-enable scrollbars
if(event->key() == Qt::Key_Control) {
ui->scrollArea->verticalScrollBar()->setEnabled(true);
ui->scrollArea->horizontalScrollBar()->setEnabled(true);
}
}
void MainWindow::scaleMapView(int s) {
if (s == 0) { // reset to default scale then scale again
int scaleTo = editor->project->scale_exp;
if (scaleTo != 0) {
resetMapScale(scaleTo);
scaleMapView(scaleTo);
}
return;
}
editor->project->scale_exp += s;
double base = editor->project->scale_base;
double exp = editor->project->scale_exp;
double base = editor->map->scale_base;
double exp = editor->map->scale_exp;
double sfactor = pow(base,s);
ui->graphicsView_Map->scale(sfactor,sfactor);
ui->graphicsView_Objects_Map->scale(sfactor,sfactor);
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);
auto newWidth = floor((editor->scene->width() + 8) * pow(base,exp) + 2);
auto newHeight = floor((editor->scene->height() + 8) * pow(base,exp) + 2);
ui->graphicsView_Map->setFixedSize(newWidth, newHeight);
ui->graphicsView_Objects_Map->setFixedSize(newWidth, newHeight);
}
void MainWindow::resetMapScale(int mag)
{
// easiest way to un-scale map
if (mag > 0) {
for (int i = 0; i < mag; i++) {
scaleMapView(-1);
}
}
else {
mag = -mag;
for (int i = 0; i < mag; i++) {
scaleMapView(1);
}
}
}
void MainWindow::addNewEvent(QString event_type)

View file

@ -9,6 +9,8 @@
#include <QGraphicsItemGroup>
#include <QGraphicsSceneMouseEvent>
#include <QAbstractItemModel>
#include <QWheelEvent>
#include <QKeyEvent>
#include "project.h"
#include "map.h"
#include "editor.h"
@ -25,11 +27,15 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
virtual void wheelEvent(QWheelEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);
public slots:
void setStatusBarMessage(QString message, int timeout = 0);
private slots:
void on_action_Open_Project_triggered();
void on_action_Close_Project_triggered();
void on_mapList_activated(const QModelIndex &index);
void on_action_Save_Project_triggered();
void openWarpMap(QString map_name, QString warp_num);
@ -125,6 +131,7 @@ private:
Ui::MainWindow *ui;
QStandardItemModel *mapListModel;
QList<QStandardItem*> *mapGroupsModel;
QMap<QString, QModelIndex> mapListIndexes;
Editor *editor = nullptr;
QIcon* mapIcon;
void setMap(QString);
@ -133,6 +140,7 @@ private:
void populateMapList();
QString getExistingDirectory(QString);
void openProject(QString dir);
void closeProject();
QString getDefaultMap();
void setRecentMap(QString map_name);
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
@ -145,6 +153,7 @@ private:
void checkToolButtons();
void scaleMapView(int);
void resetMapScale(int);
};
enum MapListUserRoles {

View file

@ -378,8 +378,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<height>608</height>
<width>545</width>
<height>587</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>256</width>
<height>74</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
@ -797,10 +797,10 @@
</property>
<property name="geometry">
<rect>
<x>0</x>
<x>8</x>
<y>0</y>
<width>307</width>
<height>387</height>
<width>222</width>
<height>353</height>
</rect>
</property>
<property name="sizePolicy">
@ -1116,8 +1116,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>381</width>
<height>657</height>
<width>371</width>
<height>643</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>578</height>
</rect>
</property>
<property name="sizePolicy">
@ -1914,8 +1914,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>826</width>
<height>557</height>
<width>818</width>
<height>539</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>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -2095,6 +2095,7 @@
<string>File</string>
</property>
<addaction name="action_Open_Project"/>
<addaction name="action_Close_Project"/>
<addaction name="action_Save"/>
<addaction name="action_Save_Project"/>
<addaction name="separator"/>
@ -2268,6 +2269,11 @@
<string>P</string>
</property>
</action>
<action name="action_Close_Project">
<property name="text">
<string>Close Project</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>

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;

View file

@ -116,12 +116,16 @@ public:
QString readCIncbin(QString text, QString label);
QMap<QString, int> readCDefines(QString text, QStringList prefixes);
int scale_exp = 0;
double scale_base = sqrt(2); // adjust scale factor with this
static int getNumTilesPrimary();
static int getNumTilesTotal();
static int getNumMetatilesPrimary();
static int getNumMetatilesTotal();
static int getNumPalettesPrimary();
static int getNumPalettesTotal();
private:
QString getMapLayoutsTableFilepath();
QString getMapLayoutFilepath(QString);