From 62009373f73a25d668af9d03029708a0c9e23af6 Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 5 Jan 2019 17:59:57 -0500 Subject: [PATCH] city map editor --- forms/mainwindow.ui | 280 +++++++++++++++++++-------------- include/editor.h | 6 + include/mainwindow.h | 3 + include/ui/citymappixmapitem.h | 41 +++++ porymap.pro | 2 + src/editor.cpp | 65 ++++++++ src/mainwindow.cpp | 9 ++ src/ui/citymappixmapitem.cpp | 63 ++++++++ 8 files changed, 349 insertions(+), 120 deletions(-) create mode 100644 include/ui/citymappixmapitem.h create mode 100644 src/ui/citymappixmapitem.cpp diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 02a3969c..fc2237cd 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -3091,126 +3091,6 @@ - - - - 80 - 380 - 261 - 251 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 259 - 249 - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - false - - - false - - - QAbstractScrollArea::AdjustIgnored - - - QGraphicsView::NoDrag - - - - - - - Qt::Horizontal - - - - 166 - 16 - - - - - - - - Qt::Vertical - - - - 16 - 166 - - - - - - - - Qt::Horizontal - - - - 166 - 16 - - - - - - - - Qt::Vertical - - - - 16 - 166 - - - - - - - @@ -3485,6 +3365,166 @@ + + + + 40 + 350 + 341 + 281 + + + + + + + + + City Map: + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + save + + + + + + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 337 + 235 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + false + + + false + + + QAbstractScrollArea::AdjustIgnored + + + QGraphicsView::NoDrag + + + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + + + diff --git a/include/editor.h b/include/editor.h index f84600b9..5f10a979 100644 --- a/include/editor.h +++ b/include/editor.h @@ -20,6 +20,7 @@ #include "collisionpixmapitem.h" #include "mappixmapitem.h" #include "regionmappixmapitem.h" +#include "citymappixmapitem.h" #include "regionmaplayoutpixmapitem.h" #include "regionmapeditor.h" #include "settings.h" @@ -85,12 +86,14 @@ public: void loadRegionMapData(); QGraphicsScene *scene_region_map_image = nullptr; + QGraphicsScene *scene_city_map_image = nullptr; QGraphicsScene *scene_region_map_layout = nullptr; QGraphicsScene *scene_region_map_tiles = nullptr; QGraphicsScene *scene_city_map_tiles = nullptr; TilemapTileSelector *mapsquare_selector_item = nullptr; TilemapTileSelector *city_map_selector_item = nullptr; RegionMapPixmapItem *region_map_item = nullptr; + CityMapPixmapItem *city_map_item = nullptr; RegionMapLayoutPixmapItem *region_map_layout_item = nullptr; void displayRegionMap(); @@ -100,6 +103,8 @@ public: void updateRegionMapLayoutOptions(int); void displayRegionMapTileSelector(); void displayCityMapTileSelector(); + void displayCityMap(QString); + void loadCityMaps(); void onRegionMapTileSelectorSelectedTileChanged(); void onRegionMapTileSelectorHoveredTileChanged(unsigned); @@ -113,6 +118,7 @@ private slots: void onHoveredRegionMapTileChanged(int, int); void onHoveredRegionMapTileCleared(); void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); + void mouseEvent_city_map(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); public: QString rmStatusbarMessage; diff --git a/include/mainwindow.h b/include/mainwindow.h index 1f5c3eb5..c875091b 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -134,6 +134,9 @@ private slots: void on_pushButton_clicked(); + void on_pushButton_CityMap_save_clicked(); + void on_comboBox_CityMap_picker_currentTextChanged(const QString &); + void on_checkBox_smartPaths_stateChanged(int selected); void on_checkBox_Visibility_clicked(bool checked); diff --git a/include/ui/citymappixmapitem.h b/include/ui/citymappixmapitem.h new file mode 100644 index 00000000..eefafd22 --- /dev/null +++ b/include/ui/citymappixmapitem.h @@ -0,0 +1,41 @@ +#ifndef CITYMAPPIXMAPITEM_H +#define CITYMAPPIXMAPITEM_H + +#include "tilemaptileselector.h" +#include +#include + +class CityMapPixmapItem : public QObject, public QGraphicsPixmapItem { + Q_OBJECT +public: + CityMapPixmapItem(QString fname, TilemapTileSelector *tile_selector) { + this->file = fname; + this->tile_selector = tile_selector; + setAcceptHoverEvents(true); + init(); + } + TilemapTileSelector *tile_selector; + + QString file; + + int width; + int height; + + QByteArray data; + + void init(); + void save(); + void create(QString); + virtual void paint(QGraphicsSceneMouseEvent*); + virtual void draw(); + +signals: + void mouseEvent(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); + void hoveredRegionMapTileChanged(int x, int y); + void hoveredRegionMapTileCleared(); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent*); +}; + +#endif // CITYMAPPIXMAPITEM_H diff --git a/porymap.pro b/porymap.pro index 98ba5e2e..e7d84510 100644 --- a/porymap.pro +++ b/porymap.pro @@ -43,6 +43,7 @@ SOURCES += src/core/block.cpp \ src/ui/imageproviders.cpp \ src/ui/mappixmapitem.cpp \ src/ui/regionmappixmapitem.cpp \ + src/ui/citymappixmapitem.cpp \ src/ui/mapsceneeventfilter.cpp \ src/ui/metatilelayersitem.cpp \ src/ui/metatileselector.cpp \ @@ -97,6 +98,7 @@ HEADERS += include/core/block.h \ include/ui/imageproviders.h \ include/ui/mappixmapitem.h \ include/ui/regionmappixmapitem.h \ + include/ui/citymappixmapitem.h \ include/ui/mapsceneeventfilter.h \ include/ui/metatilelayersitem.h \ include/ui/metatileselector.h \ diff --git a/src/editor.cpp b/src/editor.cpp index 713c72c3..2de3a84b 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include static bool selectingEvent = false; @@ -1295,6 +1296,17 @@ void Editor::deleteEvent(Event *event) { //updateSelectedObjects(); } +void Editor::loadCityMaps() { + // + QDir directory(project->root + "/graphics/pokenav/city_maps"); + QStringList files = directory.entryList(QStringList() << "*.bin", QDir::Files); + QStringList without_bin; + for (QString file : files) { + without_bin.append(file.remove(".bin")); + } + this->ui->comboBox_CityMap_picker->addItems(without_bin); +} + void Editor::loadRegionMapData() { // this->region_map->init(project); @@ -1363,6 +1375,46 @@ void Editor::displayRegionMapImage() { this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize()); } +/* +if (!scene) { + scene = new QGraphicsScene; + MapSceneEventFilter *filter = new MapSceneEventFilter(); + scene->installEventFilter(filter); + connect(filter, &MapSceneEventFilter::wheelZoom, this, &Editor::wheelZoom); + } + + if (map_item && scene) { + scene->removeItem(map_item); + delete map_item; + } +*/ +void Editor::displayCityMap(QString f) { + // + QString file = this->project->root + "/graphics/pokenav/city_maps/" + f + ".bin"; + + if (!scene_city_map_image) { + scene_city_map_image = new QGraphicsScene; + } + if (city_map_item && scene_city_map_image) { + scene_city_map_image->removeItem(city_map_item); + delete city_map_item; + } + + city_map_item = new CityMapPixmapItem(file, this->city_map_selector_item); + city_map_item->draw(); + + connect(city_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, CityMapPixmapItem*)), + this, SLOT(mouseEvent_city_map(QGraphicsSceneMouseEvent*, CityMapPixmapItem*))); + + scene_city_map_image->addItem(city_map_item); + scene_city_map_image->setSceneRect(this->scene_city_map_image->sceneRect()); + + this->ui->graphicsView_City_Map->setScene(scene_city_map_image); + this->ui->graphicsView_City_Map->setFixedSize(QSize(82,82)); + // set fixed size? +} + +// TODO: add if (item) and if(scene) checks because called more than once per instance void Editor::displayRegionMapLayout() { // this->region_map_layout_item = new RegionMapLayoutPixmapItem(this->region_map, this->mapsquare_selector_item); @@ -1489,6 +1541,19 @@ void Editor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPix } } +void Editor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityMapPixmapItem *item) { + // + if (event->buttons() & Qt::RightButton) { + // + //item->select(event); + } else if (event->buttons() & Qt::MiddleButton) { + // TODO: add functionality here? replace or? + } else { + // + item->paint(event); + } +} + // It doesn't seem to be possible to prevent the mousePress event // from triggering both event's DraggablePixmapItem and the background mousePress. // Since the DraggablePixmapItem's event fires first, we can set a temp diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9aebd89a..7e236504 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -264,6 +264,7 @@ bool MainWindow::openProject(QString dir) { loadDataStructures(); populateMapList(); editor->loadRegionMapData(); + editor->loadCityMaps(); success = setMap(getDefaultMap(), true); } else { setWindowTitle(editor->project->getProjectTitle()); @@ -1999,6 +2000,14 @@ void MainWindow::on_pushButton_RM_Options_save_clicked() { this->editor->region_map_layout_item->draw(); } +void MainWindow::on_pushButton_CityMap_save_clicked() { + this->editor->city_map_item->save(); +} + +void MainWindow::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { + this->editor->displayCityMap(file); +} + void MainWindow::closeEvent(QCloseEvent *event) { porymapConfig.setGeometry( this->saveGeometry(), diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp new file mode 100644 index 00000000..39ab54cc --- /dev/null +++ b/src/ui/citymappixmapitem.cpp @@ -0,0 +1,63 @@ +#include "citymappixmapitem.h" +#include "imageproviders.h" + +#include +#include +#include + + + +// read to byte array from filename +void CityMapPixmapItem::init() { + // TODO: are they always 10x10 squares? + width = 10; + height = 10; + + QFile binFile(file); + if (!binFile.open(QIODevice::ReadOnly)) return; + + data = binFile.readAll(); + binFile.close(); +} + +void CityMapPixmapItem::draw() { + QImage image(width * 8, height * 8, QImage::Format_RGBA8888); + + QPainter painter(&image); + for (int i = 0; i < data.size() / 2; i++) { + QImage img = this->tile_selector->tileImg(data[i * 2]);// need to skip every other tile + int x = i % width; + int y = i / width; + QPoint pos = QPoint(x * 8, y * 8); + painter.drawImage(pos, img); + } + painter.end(); + + this->setPixmap(QPixmap::fromImage(image)); +} + +void CityMapPixmapItem::save() { + // + QFile binFile(file); + if (!binFile.open(QIODevice::WriteOnly)) return; + binFile.write(data); + binFile.close(); +} + +void CityMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { + // + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = 2 * (x + y * width); + data[index] = static_cast(this->tile_selector->selectedTile); + + draw(); +} + +void CityMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + emit mouseEvent(event, this); +}