From 7cffe07579c9a6296c4f48b28a3f981140ffcfb9 Mon Sep 17 00:00:00 2001 From: garak Date: Fri, 28 Dec 2018 13:26:18 -0500 Subject: [PATCH 01/17] add region map image editor with basic functionality --- .gitignore | 10 +- forms/mainwindow.ui | 785 ++++++++++++++++++++++++++++++- include/core/regionmapeditor.h | 179 +++++++ include/editor.h | 30 ++ include/mainwindow.h | 2 + include/ui/regionmappixmapitem.h | 36 ++ include/ui/tilemaptileselector.h | 51 ++ porymap.pro | 6 + src/core/regionmapeditor.cpp | 402 ++++++++++++++++ src/editor.cpp | 98 +++- src/mainwindow.cpp | 7 + src/ui/regionmappixmapitem.cpp | 73 +++ src/ui/tilemaptileselector.cpp | 112 +++++ 13 files changed, 1783 insertions(+), 8 deletions(-) create mode 100644 include/core/regionmapeditor.h create mode 100644 include/ui/regionmappixmapitem.h create mode 100644 include/ui/tilemaptileselector.h create mode 100644 src/core/regionmapeditor.cpp create mode 100644 src/ui/regionmappixmapitem.cpp create mode 100644 src/ui/tilemaptileselector.cpp diff --git a/.gitignore b/.gitignore index 3d265d53..bde28f17 100644 --- a/.gitignore +++ b/.gitignore @@ -2,12 +2,14 @@ porymap.pro.user *.autosave *.stash *.o -moc_* -qrc_* porymap.app* porymap +porymap.cfg +porymap.log Makefile -# Generated UI header +# Qt generated files ui_*.h - +moc_*.h +moc_*.cpp +qrc_*.cpp diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 1aa69cdd..c1e76938 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -237,7 +237,7 @@ - 0 + 4 false @@ -1066,8 +1066,8 @@ 8 0 - 221 - 328 + 222 + 353 @@ -2693,6 +2693,785 @@ + + + Region Map + + + + + 460 + 420 + 301 + 181 + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 283 + 179 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 19 + 9 + 381 + 291 + + + + 0 + + + + Background Image + + + + + 30 + 20 + 321 + 211 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 319 + 209 + + + + + 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 + + + + + + + + + + + Map Layout + + + + + 20 + 20 + 321 + 211 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 319 + 209 + + + + + 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 + + + + + + + + + + + + + 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 + + + + + + + + + + + 440 + 20 + 351 + 291 + + + + 0 + + + + + + 30 + 20 + 281 + 261 + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 263 + 259 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + false + + + + 50 + 40 + 261 + 170 + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + Connected Map + + + + + + + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> + + + true + + + + + + + Weather + + + + + + + <html><head/><body><p>The default weather for this map.</p></body></html> + + + true + + + + + + + Type + + + + + + + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> + + + true + + + + + + + Show Location Name + + + + + + + <html><head/><body><p>Whether or not to display the location name when the player enters the map.</p></body></html> + + + + + + + + + + Battle scene + + + + + + + <html><head/><body><p>Determines the type of battle scene graphics to use.</p></body></html> + + + true + + + + + + + + diff --git a/include/core/regionmapeditor.h b/include/core/regionmapeditor.h new file mode 100644 index 00000000..2875ef81 --- /dev/null +++ b/include/core/regionmapeditor.h @@ -0,0 +1,179 @@ +#ifndef REGIONMAP_H +#define REGIONMAP_H + +#include "project.h" +#include "map.h" +#include "tilemaptileselector.h" +//#include "block.h" + +#include +#include +#include +#include +#include +#include + +// if editing map bins, will need to remake the graphics when editing +// are the scenes set in the editor / project / mainwindow files? + +/* + * - display the region map background image + * - edit the region_map_layout.h layout + * - edit city maps metatile layout and JUST save the mapname_num.bin + * - edit + * who edits pokenav_city_maps 1 and 2? + * users can: - add the incbins probably themselves + * - add + * - edit region map background image + * + * + * + * + * Editor: + * - void displayCityMapMetatileSelector + * - void displayRegionMapTileSelector + * - void selectRegionMapTile(QString mapname) + * - QGraphicsScene *scene_city_map_metatiles + * - TilemapTileSelector *city_map_metatile_selector_item + * - Tileset *city_map_squares (or tileset_city_map?) + * - Tileset *tileset_region_map + * + * MainWindow: + * + * + * Project: + * + */ + +// rename this struct +struct CityMapPosition +{ + // + //QString filename; // eg. dewford_0 + QString tilemap;// eg. "dewford_0" + int x; + int y; +}; + +// class that holds data for each square in this project +// struct? +// TODO: change char / uint8_t to unsigned +class RegionMapSquare +{ +public: + // + // are positions layout positions? (yes) so out of bounds are all (-1, -1) <-- how it's used in code + // (GetRegionMapLocationPosition) + // or image positions + int x = -1;// x position, 0-indexed from top left + int y = -1;// y position, 0-indexed from top left + uint8_t tile_img_id;// tilemap ids for the background image + bool has_map = false;// whether this square is linked to a map or is empty + QString map_name;// name of the map associated with this square (if has_map is true): eg. "MAUVILLE_CITY" + // ^ use project mapsec to names table + bool has_city_map;// whether there is a city map on this grid + //QList city_maps; + QString city_map_name;// filename of the city_map tilemap + //bool is_flyable;//? needed ? + friend class RegionMap;// not necessary if instance? what +}; + +class RegionMap : public QObject +{ + Q_OBJECT +//public: +// explicit Map(QObject *parent = nullptr); + +public: + RegionMap() = default; + + ~RegionMap() { + delete mapname_abbr; + delete layout_map_names; + //delete background_image_tiles; + //delete map_squares; + //delete background_image_selector_item; + }; + + static QMap> ruby_city_maps_; + static QString mapSecToMapConstant(QString); + + //RegionMapSquare *map_squares = nullptr;// array of RegionMapSquares + QList map_squares; + + QString temp_path;// delete this + QString city_map_squares_path; + QString region_map_png_path; + QString region_map_bin_path;// = QString::null; + QString city_map_header_path;//dafuq is this? + QString region_map_layout_path; + + //QMap something;// name of map : info about city map, position in layoit, etc. + //QMap regionMapLayoutTng; // mapName : tilemaptileselector + // maybe position data to select correct square when changing map on side but only if map is a valid + //QList *background_image_tiles;// the visible ones anyways // using list because replace + //TilemapTileSelector *background_image_selector_item;// ? + QMap *mapname_abbr;// layout shortcuts mapname:region_map_layout defines (both ways) + // make this a QHash?? <-- no because something + QStringList *layout_map_names; + // uint8_t border_tile; + + bool hasUnsavedChanges(); + + void init(Project*);//QString); + + // parseutil.cpp ? + void readBkgImgBin(); + void readCityMaps();// more complicated + void readLayout(QMap*); + + QString newAbbr(QString);// makes a *unique* 5 character abbreviation from mapname to add to mapname_abbr + + // editing functions + // if they are booleans, returns true if successful? + bool placeTile(char, int, int);// place tile at x, y + bool removeTile(char, int, int);// replaces with 0x00 byte at x,y + bool placeMap(QString, int, int); + bool removeMap(QString, int, int); + bool removeMap(QString);// remove all instances of map + + void save(); + void saveBkgImgBin(); + void saveLayout(); + void saveCityMaps(); + + void update();// update the view in case something is broken? + void resize(int, int); + void setWidth(int); + void setHeight(int); + int width(); + int height(); + QSize imgSize(); + unsigned getTileId(int, int); + + // implement these here? + void undo(); + void redo(); + + void test(QMap*);// remove when done testing obvi + +// TODO: move read / write functions to private (and others) +private: + // + int layout_width_; + int layout_height_; + int img_width_; + int img_height_; + int img_index_(int, int);// returns index int at x,y args (x + y * width_ * 2) // 2 because + int layout_index_(int, int); + +//protected: + // + +//signals: + // +}; + +//TilemapTileSelector *city_map_metatile_selector_item = nullptr; + +#endif // REGIONMAP_H diff --git a/include/editor.h b/include/editor.h index 4b7f71a0..b28f0ebf 100644 --- a/include/editor.h +++ b/include/editor.h @@ -19,6 +19,8 @@ #include "currentselectedmetatilespixmapitem.h" #include "collisionpixmapitem.h" #include "mappixmapitem.h" +#include "regionmappixmapitem.h" +#include "regionmapeditor.h" #include "settings.h" #include "movablerect.h" #include "cursortilerect.h" @@ -77,6 +79,33 @@ public: void updateCustomMapHeaderValues(QTableWidget *); Tileset *getCurrentMapPrimaryTileset(); +// + RegionMap *region_map; + void loadRegionMapData(); + + QGraphicsScene *scene_region_map_image = nullptr; + QGraphicsScene *scene_region_map_layout = nullptr;//? + QGraphicsScene *scene_region_map_tiles = nullptr; + TilemapTileSelector *mapsquare_selector_item = nullptr; + RegionMapPixmapItem *region_map_item = nullptr; + + void displayRegionMap(); + void displayRegionMapTileSelector(); + + // selectedTileChanged, hoveredTileChanged, hoveredTileCleared + void onRegionMapTileSelectorSelectedTileChanged(); + void onRegionMapTileSelectorHoveredTileChanged(unsigned); + void onRegionMapTileSelectorHoveredTileCleared(); + +private slots: + void onHoveredRegionMapTileChanged(int, int); + void onHoveredRegionMapTileCleared(); + void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); + +public: + QString regionMapTabStatusbarMessage;// TODO: make this name not terrible +// + DraggablePixmapItem *addMapEvent(Event *event); void selectMapEvent(DraggablePixmapItem *object); void selectMapEvent(DraggablePixmapItem *object, bool toggle); @@ -105,6 +134,7 @@ public: QGraphicsScene *scene_selected_border_metatiles = nullptr; QGraphicsScene *scene_collision_metatiles = nullptr; QGraphicsScene *scene_elevation_metatiles = nullptr; + MetatileSelector *metatile_selector_item = nullptr; BorderMetatilesPixmapItem *selected_border_metatiles_item = nullptr; diff --git a/include/mainwindow.h b/include/mainwindow.h index bb7d98c1..3cbe1fc0 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -65,6 +65,8 @@ private slots: void on_checkBox_AllowBiking_clicked(bool checked); void on_checkBox_AllowEscapeRope_clicked(bool checked); + void on_tabWidget_Region_Map_currentChanged(int); + void on_tabWidget_currentChanged(int index); void on_actionUndo_triggered(); diff --git a/include/ui/regionmappixmapitem.h b/include/ui/regionmappixmapitem.h new file mode 100644 index 00000000..12de3385 --- /dev/null +++ b/include/ui/regionmappixmapitem.h @@ -0,0 +1,36 @@ +#ifndef REGIONMAPPIXMAPITEM_H +#define REGIONMAPPIXMAPITEM_H + +#include "regionmapeditor.h" +#include "tilemaptileselector.h" +#include + +class RegionMapPixmapItem : public QObject, public QGraphicsPixmapItem { + Q_OBJECT +public: + RegionMapPixmapItem(RegionMap *rmap, TilemapTileSelector *tile_selector) { + this->region_map = rmap; + this->tile_selector = tile_selector; + setAcceptHoverEvents(true); + } + RegionMap *region_map; + TilemapTileSelector *tile_selector; + + virtual void paint(QGraphicsSceneMouseEvent*); + virtual void select(QGraphicsSceneMouseEvent*); + virtual void draw(); + +signals: + void mouseEvent(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); + void hoveredRegionMapTileChanged(int x, int y); + void hoveredRegionMapTileCleared(); + +protected: + void hoverMoveEvent(QGraphicsSceneHoverEvent*); + void hoverLeaveEvent(QGraphicsSceneHoverEvent*); + void mousePressEvent(QGraphicsSceneMouseEvent*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*); +}; + +#endif // REGIONMAPPIXMAPITEM_H diff --git a/include/ui/tilemaptileselector.h b/include/ui/tilemaptileselector.h new file mode 100644 index 00000000..8bb52050 --- /dev/null +++ b/include/ui/tilemaptileselector.h @@ -0,0 +1,51 @@ +#ifndef TILEMAPTILESELECTOR_H +#define TILEMAPTILESELECTOR_H + +#include "selectablepixmapitem.h" + +class TilemapTileSelector: public SelectablePixmapItem { + Q_OBJECT +public: + TilemapTileSelector(QPixmap pixmap): SelectablePixmapItem(8, 8, 1, 1) { + this->pixmap = pixmap; + this->numTilesWide = 16; + this->selectedTile = 0x00; + setAcceptHoverEvents(true); + } + void draw(); + void select(unsigned tileId); + unsigned getSelectedTile(); + + int pixelWidth; + int pixelHeight; + + unsigned selectedTile; + + // TODO: which of these need to be made public? + // call this tilemap? or is tilemap the binary file? + QPixmap pixmap;// pointer? + QImage currTile;// image of just the currently selected tile to draw onto graphicsview + QImage tileImg(unsigned tileId); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*); + void hoverMoveEvent(QGraphicsSceneHoverEvent*); + void hoverLeaveEvent(QGraphicsSceneHoverEvent*); + +private: + int numTilesWide; + int numTiles; + void updateSelectedTile(); + unsigned getTileId(int x, int y); + QPoint getTileIdCoords(unsigned); + unsigned getValidTileId(unsigned);// TODO: implement this to prevent segfaults + +signals: + void hoveredTileChanged(unsigned); + void hoveredTileCleared(); + void selectedTileChanged(unsigned); +}; + +#endif // TILEMAPTILESELECTOR_H diff --git a/porymap.pro b/porymap.pro index cfd02755..3868a8a2 100644 --- a/porymap.pro +++ b/porymap.pro @@ -28,6 +28,7 @@ SOURCES += src/core/block.cpp \ src/core/parseutil.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ + src/core/regionmapeditor.cpp \ src/ui/aboutporymap.cpp \ src/ui/bordermetatilespixmapitem.cpp \ src/ui/collisionpixmapitem.cpp \ @@ -40,6 +41,7 @@ SOURCES += src/core/block.cpp \ src/ui/graphicsview.cpp \ src/ui/imageproviders.cpp \ src/ui/mappixmapitem.cpp \ + src/ui/regionmappixmapitem.cpp \ src/ui/mapsceneeventfilter.cpp \ src/ui/metatilelayersitem.cpp \ src/ui/metatileselector.cpp \ @@ -53,6 +55,7 @@ SOURCES += src/core/block.cpp \ src/ui/tileseteditor.cpp \ src/ui/tileseteditormetatileselector.cpp \ src/ui/tileseteditortileselector.cpp \ + src/ui/tilemaptileselector.cpp \ src/ui/newmappopup.cpp \ src/config.cpp \ src/editor.cpp \ @@ -78,6 +81,7 @@ HEADERS += include/core/block.h \ include/core/parseutil.h \ include/core/tile.h \ include/core/tileset.h \ + include/core/regionmapeditor.h \ include/ui/aboutporymap.h \ include/ui/bordermetatilespixmapitem.h \ include/ui/collisionpixmapitem.h \ @@ -90,6 +94,7 @@ HEADERS += include/core/block.h \ include/ui/graphicsview.h \ include/ui/imageproviders.h \ include/ui/mappixmapitem.h \ + include/ui/regionmappixmapitem.h \ include/ui/mapsceneeventfilter.h \ include/ui/metatilelayersitem.h \ include/ui/metatileselector.h \ @@ -103,6 +108,7 @@ HEADERS += include/core/block.h \ include/ui/tileseteditor.h \ include/ui/tileseteditormetatileselector.h \ include/ui/tileseteditortileselector.h \ + include/ui/tilemaptileselector.h \ include/ui/newmappopup.h \ include/config.h \ include/editor.h \ diff --git a/src/core/regionmapeditor.cpp b/src/core/regionmapeditor.cpp new file mode 100644 index 00000000..73b27c0f --- /dev/null +++ b/src/core/regionmapeditor.cpp @@ -0,0 +1,402 @@ +#include "regionmapeditor.h" + +#include +#include +#include +#include + +#include + + + +// TODO: add logging / config stuff + +// IN: ROUTE_101 ... OUT: MAP_ROUTE101 +// eg. SOUTHERN_ISLAND -> MAP_SOUTHERN_ISLAND -> SouthernIsland(n) -> SouthernIsland_Exterior +// MT_CHIMNEY -> MAP_MT_CHIMNEY -> MtChimney(y) +// ROUTE_101 -> MAP_ROUTE101 -> Route101(y) +// TODO: move this maybe? would I be able to call it from this file if it was in map.cpp? +QString RegionMap::mapSecToMapConstant(QString mapSectionName) { + // + QString mapConstantName = "MAP_"; + QString sectionNameTemp = mapSectionName.replace("ROUTE_","ROUTE"); + mapConstantName += sectionNameTemp; + return mapConstantName; +} + +// TODO: verify these are in the correct order +// also TODO: read this from the project somehow +QMap> RegionMap::ruby_city_maps_ = QMap>({ + {"LavaridgeTown", { + {"lavaridge_0.bin", 5, 3}, + }}, + {"FallarborTown", { + {"fallarbor_0.bin", 3, 0}, + }}, + {"FortreeCity", { + {"fortree_0.bin", 12, 0}, + }}, + {"SlateportCity", { + {"slateport_0.bin", 8, 10}, + {"slateport_1.bin", 8, 11}, + }}, + {"RustboroCity", { + {"rustboro_0.bin", 0, 5}, + {"rustboro_1.bin", 0, 6}, + }}, + {"PacifidlogTown", { + {"pacifidlog_0.bin", 17, 10}, + }}, + {"MauvilleCity", { + {"mauville_0.bin", 8, 6}, + {"mauville_1.bin", 9, 6}, + }}, + {"OldaleTown", { + {"oldale_0.bin", 4, 9}, + }}, + {"LilycoveCity", { + {"lilycove_0.bin", 18, 3}, + {"lilycove_1.bin", 19, 3}, + }}, + {"LittlerootTown", { + {"littleroot_0.bin", 4, 11}, + }}, + {"DewfordTown", { + {"dewford_0.bin", 2, 14}, + }}, + {"SootopolisCity", { + {"sootopolis_0.bin", 21, 7}, + }}, + {"EverGrandeCity", { + {"ever_grande_0.bin", 27, 8}, + {"ever_grande_1.bin", 27, 9}, + }}, + {"VerdanturfTown", { + {"verdanturf_0.bin", 4, 6}, + }}, + {"MossdeepCity", { + {"mossdeep_0.bin", 24, 5}, + {"mossdeep_1.bin", 25, 5}, + }}, + {"PetalburgCity", { + {"petalburg_0.bin", 1, 9}, + }}, +}); + +void RegionMap::init(Project *pro) { + QString path = pro->root; + // + // TODO: in the future, allow these to be adjustable (and save values) + // possibly use a config file? + layout_width_ = 28; + layout_height_ = 15; + + img_width_ = layout_width_ + 4; + img_height_ = layout_height_ + 5; + + //city_map_squares_path = QString(); + temp_path = path;// delete this + region_map_bin_path = path + "/graphics/pokenav/region_map_map.bin"; + region_map_png_path = path + "/graphics/pokenav/region_map.png"; + region_map_layout_path = path + "/src/data/region_map_layout.h"; + + readBkgImgBin(); + readLayout(pro->mapConstantsToMapNames); + readCityMaps(); + + //tryGetMap(); + + //saveBkgImgBin(); + //saveLayout(); + test(pro->mapConstantsToMapNames); +} + +// as of now, this needs to be called first because it initializes all the +// RegionMapSquare s in the list +// TODO: if the tileId is not valid for the provided image, make sure it does not crash +void RegionMap::readBkgImgBin() { + QFile binFile(region_map_bin_path); + if (!binFile.open(QIODevice::ReadOnly)) return; + + QByteArray mapBinData = binFile.readAll(); + binFile.close(); + + // the two is because lines are skipped for some reason + // (maybe that is because there could be multiple layers?) + // background image is also 32x20 + for (int m = 0; m < img_height_; m++) { + for (int n = 0; n < img_width_; n++) { + RegionMapSquare square;// = + square.tile_img_id = mapBinData.at(n + m * img_width_ * 2); + map_squares.append(square); + } + } +} + +void RegionMap::saveBkgImgBin() { + QByteArray data(4096,0);// use a constant here? maybe read the original size? + + for (int m = 0; m < img_height_; m++) { + for (int n = 0; n < img_width_; n++) { + data[n + m * img_width_ * 2] = map_squares[n + m * img_width_].tile_img_id; + } + } + + QFile file(region_map_bin_path); + if (!file.open(QIODevice::WriteOnly)) return; + file.write(data); + file.close(); +} + +// done +void RegionMap::readLayout(QMap *qmap) { + QFile file(region_map_layout_path); + if (!file.open(QIODevice::ReadOnly)) return; + + QMap * abbr = new QMap; + + QString line, text; + QStringList *captured = new QStringList; + + QTextStream in(&file); + while (!in.atEnd()) { + line = in.readLine(); + if (line.startsWith("#define")) { + QStringList split = line.split(QRegularExpression("\\s+")); + abbr->insert(split[2].replace("MAPSEC_",""), split[1]); + } else { + text += line.remove(" "); + } + } + QRegularExpression re("{(.*?)}"); + *captured = re.match(text).captured(1).split(","); + captured->removeAll({}); + + // replace abbreviations with names + for (int i = 0; i < captured->length(); i++) { + QString value = (*captured)[i]; + if (value.startsWith("R(")) {// routes are different + captured->replace(i, QString("ROUTE_%1").arg(value.mid(2,3))); + } else { + captured->replace(i, abbr->key(value)); + } + } + + // TODO: improve this? + for (int m = 0, i = 0; m < layout_height_; m++) { + for (int n = 0; n < layout_width_; n++) { + i = img_index_(n,m); + QString secname = (*captured)[layout_index_(n,m)]; + if (secname != "NOTHING") map_squares[i].has_map = true; + map_squares[i].map_name = qmap->value(mapSecToMapConstant(secname)); + map_squares[i].x = n; + map_squares[i].y = m; + } + } + mapname_abbr = abbr; + layout_map_names = captured; + file.close(); +} + +// does it matter that it doesn't save in order? +// do i need to use a QList ?? +void RegionMap::saveLayout() { + // + QString layout_text = ""; + QString mapsec = "MAPSEC_"; + QString define = "#define "; + QString array_start = "static const u8 sRegionMapLayout[] =\n{"; + QString array_close = "\n};\n"; + QString tab = " "; + + for (QString key : mapname_abbr->keys()) { + layout_text += define + mapname_abbr->value(key) + tab + mapsec + key + "\n"; + } + + layout_text += "\n" + array_start;// + + array_close;//oops + + //qDebug() << *layout_map_names; + int cnt = 0; + for (QString s : *layout_map_names) { + // + if (!(cnt % layout_width_)) { + layout_text += "\n" + tab; + } + if (s.startsWith("ROUTE_")) { + layout_text += QString("R(%1)").arg(s.replace("ROUTE_","")) + ", "; + } else { + layout_text += mapname_abbr->value(s) + ", "; + } + cnt++; + } + + //layout_text. + layout_text += array_close; + + QFile file(region_map_layout_path); + if (!file.open(QIODevice::WriteOnly)) return; + file.write(layout_text.toUtf8()); + file.close(); +} + +void RegionMap::readCityMaps() { + // + //for (int m = 0; m < layout_height_; m++) { + // QString tester; + // for (int n = 0; n < layout_width_; n++) { + // tester += (QString::number(img_index_(n,m)).rightJustified(3, '.') + " "); + // } + // qDebug() << tester; + //} + //for (auto map : map_squares) { + for (int map = 0; map < map_squares.size(); map++) { + // + if (map_squares[map].has_map) { + // + if (ruby_city_maps_.contains(map_squares[map].map_name)) { + map_squares[map].has_city_map = true; + //map_squares[map].city_map_name = ruby_city_maps_.value(map_squares[map].map_name)[0].tilemap; + QList city_maps = ruby_city_maps_.value(map_squares[map].map_name); + for (auto city_map : city_maps) { + // + if (city_map.x == map_squares[map].x + && city_map.y == map_squares[map].y) + // + map_squares[map].city_map_name = city_map.tilemap; + } + } + } + } +} + +//done +QString RegionMap::newAbbr(QString mapname) { + QString abbr; + QStringList words = mapname.split("_"); + + if (words.length() == 1) { + abbr = (words[0] + "_____X").left(6); + } else { + abbr = (words.front() + "___X").left(4) + "_" + words.back().at(0); + } + + // to guarantee unique abbreviations (up to 14) + QString extra_chars = "23456789BCDEF"; + int count = 0; + while ((*mapname_abbr).values().contains(abbr)) { + abbr.replace(5,1,extra_chars[count]); + count++; + } + return abbr; +} + +// layout coords to image index +int RegionMap::img_index_(int x, int y) { + return ((x + 1) + (y + 2) * img_width_); +} + +// layout coords to layout index +int RegionMap::layout_index_(int x, int y) { + return (x + y * layout_width_); +} + +// img coords to layout index? +// img coords to img index? + +void RegionMap::test(QMap* qmap) { + // + bool debug_rmap = false; + + if (debug_rmap) { + for (auto square : map_squares) { + qDebug() << "(" << square.x << "," << square.y << ")" + << square.tile_img_id + << square.has_map + << square.map_name + << square.has_city_map + << square.city_map_name + ; + //if (qmap->contains(mapSecToMapConstant(square.map_name))) + // extras += qmap->value(mapSecToMapConstant(square.map_name)) + " "; + //else + // extras += "nothing "; + } + + QPixmap png(region_map_png_path); + //png.load(region_map_png_path); + qDebug() << "png num 8x8 tiles" << QString("0x%1").arg((png.width()/8) * (png.height() / 8), 2, 16, QChar('0')); + } + +} + +int RegionMap::width() { + return this->img_width_; +} + +int RegionMap::height() { + return this->img_height_; +} + +// TODO: remove +2 and put elsewhere +QSize RegionMap::imgSize() { + return QSize(img_width_ * 8 + 2, img_height_ * 8 + 2); +} + +// TODO: rename to getTileIdAt()? +unsigned RegionMap::getTileId(int x, int y) { + // + return map_squares[x + y * img_width_].tile_img_id; + //qDebug() << x << y; + //return 0; +} + +// sidenote: opening the map from MAPSEC_x will not always be right +// there needs to be a mapsections to mapname QMap +// otherwie, look for the first map with right substring +// mapConstantsToMapNames [MAP_ROUTE106] = "Route106" +// eg. SOUTHERN_ISLAND -> MAP_SOUTHERN_ISLAND -> SouthernIsland(n) -> SouthernIsland_Exterior +// MT_CHIMNEY -> MAP_MT_CHIMNEY -> MtChimney(y) +// ROUTE_101 -> MAP_ROUTE101 -> Route101(y) +// (or synchronize these for consistency in the repos :: underscore / no underscore) + +// TODO: change debugs to logs +void RegionMap::save() { + // + qDebug() << "saving region map image tilemap at" << region_map_bin_path << "\n" + ;//<< "saving region map layout at" << region_map_layout_path << "\n"; + + saveBkgImgBin(); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/editor.cpp b/src/editor.cpp index 5674721b..9cfce50e 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -19,12 +19,16 @@ Editor::Editor(Ui::MainWindow* ui) this->settings = new Settings(); this->playerViewRect = new MovableRect(&this->settings->playerViewRectEnabled, 30 * 8, 20 * 8, qRgb(255, 255, 255)); this->cursorMapTileRect = new CursorTileRect(&this->settings->cursorTileRectEnabled, qRgb(255, 255, 255)); + this->region_map = new RegionMap;// TODO: why is this here? } void Editor::saveProject() { if (project) { project->saveAllMaps(); project->saveAllDataStructures(); + if (region_map) { + region_map->save(); + } } } @@ -33,6 +37,9 @@ void Editor::save() { project->saveMap(map); project->saveAllDataStructures(); } + if (project && region_map) { + region_map->save(); + } } void Editor::undo() { @@ -600,6 +607,9 @@ void Editor::displayMap() { scene->addItem(this->playerViewRect); scene->addItem(this->cursorMapTileRect); + displayRegionMapTileSelector();//? + displayRegionMap(); + if (map_item) { map_item->setVisible(false); } @@ -1261,6 +1271,93 @@ void Editor::deleteEvent(Event *event) { //updateSelectedObjects(); } +void Editor::loadRegionMapData() { + // + region_map->init(project); +} + +// TODO: get this to display on a decent scale +void Editor::displayRegionMapTileSelector() { + // + this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_png_path)); + this->mapsquare_selector_item->draw(); + + this->scene_region_map_tiles = new QGraphicsScene; + this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); + + connect(this->mapsquare_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &Editor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? + connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileChanged, + this, &Editor::onRegionMapTileSelectorHoveredTileChanged); + connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileCleared, + this, &Editor::onRegionMapTileSelectorHoveredTileCleared); + + this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth + 2, + this->mapsquare_selector_item->pixelHeight + 2); +} + +// TODO: change the signal slot to new syntax +void Editor::displayRegionMap() { + // + this->region_map_item = new RegionMapPixmapItem(this->region_map, this->mapsquare_selector_item); + connect(region_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*)), + this, SLOT(mouseEvent_region_map(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*))); + connect(region_map_item, SIGNAL(hoveredRegionMapTileChanged(int, int)), + this, SLOT(onHoveredRegionMapTileChanged(int, int))); + connect(region_map_item, SIGNAL(hoveredRegionMapTileCleared()), + this, SLOT(onHoveredRegionMapTileCleared())); + this->region_map_item->draw(); + + this->scene_region_map_image = new QGraphicsScene; + this->scene_region_map_image->addItem(this->region_map_item); + this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); + + //this->scene_region_map_image->scale(2, 2); + this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize()); + //this->ui->graphicsView_Region_Map_BkgImg->scale(2.0, 2.0); +} + +void Editor::onRegionMapTileSelectorSelectedTileChanged() { + // +} + +void Editor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { + QString message = QString("Tile: 0x") + QString("%1").arg(tileId, 4, 16, QChar('0')).toUpper(); + this->ui->statusBar->showMessage(message); +} + +void Editor::onRegionMapTileSelectorHoveredTileCleared() { + // + QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->mapsquare_selector_item->selectedTile, 4, 16, QChar('0')).toUpper(); + this->ui->statusBar->showMessage(message); +} + +void Editor::onHoveredRegionMapTileChanged(int x, int y) { + // + regionMapTabStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); + this->ui->statusBar->showMessage(regionMapTabStatusbarMessage); +} + +void Editor::onHoveredRegionMapTileCleared() { + // + this->ui->statusBar->clearMessage(); +} + +void Editor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *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 @@ -1274,6 +1371,5 @@ void Editor::objectsView_onMousePress(QMouseEvent *event) { selected_events->append(first); updateSelectedEvents(); } - selectingEvent = false; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 982f3be8..0bf6e874 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -263,11 +263,13 @@ bool MainWindow::openProject(QString dir) { setWindowTitle(editor->project->getProjectTitle()); loadDataStructures(); populateMapList(); + editor->loadRegionMapData(); success = setMap(getDefaultMap(), true); } else { setWindowTitle(editor->project->getProjectTitle()); loadDataStructures(); populateMapList(); + editor->loadRegionMapData(); } if (success) { @@ -596,6 +598,11 @@ void MainWindow::on_checkBox_AllowEscapeRope_clicked(bool checked) } } +void MainWindow::on_tabWidget_Region_Map_currentChanged(int index) { + // + ui->stackedWidget_RM_Options->setCurrentIndex(index); +} + void MainWindow::loadDataStructures() { Project *project = editor->project; project->readMapLayouts(); diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp new file mode 100644 index 00000000..4c751898 --- /dev/null +++ b/src/ui/regionmappixmapitem.cpp @@ -0,0 +1,73 @@ +#include "regionmappixmapitem.h" +#include "imageproviders.h" + + + +// the function that draws the map on the scene +// (qnqlogous to Map::render) +// TODO: figure out why this is being called twice!! +void RegionMapPixmapItem::draw() { + if (!region_map) return; + + QImage image(region_map->width() * 8, region_map->height() * 8, QImage::Format_RGBA8888); + + QPainter painter(&image); + for (int i = 0; i < region_map->map_squares.size(); i++) { + QImage img = this->tile_selector->tileImg(region_map->map_squares[i].tile_img_id); + int x = i % region_map->width(); + int y = i / region_map->width(); + QPoint pos = QPoint(x * 8, y * 8); + painter.drawImage(pos, img); + } + painter.end(); + + this->setPixmap(QPixmap::fromImage(image)); +} + +void RegionMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { + if (region_map) { + if (event->type() == QEvent::GraphicsSceneMousePress) { + } else { + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + this->region_map->map_squares[x + y * region_map->width()].tile_img_id = this->tile_selector->selectedTile; + } + draw(); + } +} + +void RegionMapPixmapItem::select(QGraphicsSceneMouseEvent *event) { + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + this->tile_selector->select(this->region_map->getTileId(x, y)); +} + +void RegionMapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { + int x = static_cast(event->pos().x()) / 8; + int y = static_cast(event->pos().y()) / 8; + emit this->hoveredRegionMapTileChanged(x, y); +} + +void RegionMapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { + emit this->hoveredRegionMapTileCleared(); +} + +void RegionMapPixmapItem::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); +} + +void RegionMapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + int x = static_cast(event->pos().x()) / 8; + int y = static_cast(event->pos().y()) / 8; + emit this->hoveredRegionMapTileChanged(x, y); + emit mouseEvent(event, this); +} + +void RegionMapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + emit mouseEvent(event, this); +} diff --git a/src/ui/tilemaptileselector.cpp b/src/ui/tilemaptileselector.cpp new file mode 100644 index 00000000..b109ceb2 --- /dev/null +++ b/src/ui/tilemaptileselector.cpp @@ -0,0 +1,112 @@ +#include "tilemaptileselector.h" + +#include + +void TilemapTileSelector::draw() { + size_t width_ = this->pixmap.width(); + this->pixelWidth = width_; + size_t height_ = this->pixmap.height(); + this->pixelHeight = height_; + size_t ntiles_ = (width_/8) * (height_/8);// length_ + + this->numTilesWide = width_ / 8; + this->numTiles = ntiles_; + + this->setPixmap(this->pixmap); + this->drawSelection(); +} + +//* +void TilemapTileSelector::select(unsigned tileId) { + QPoint coords = this->getTileIdCoords(tileId); + SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); + this->selectedTile = tileId; + emit selectedTileChanged(tileId); +} +//*/ + +//* +void TilemapTileSelector::updateSelectedTile() { + QPoint origin = this->getSelectionStart(); + this->selectedTile = this->getTileId(origin.x(), origin.y()); +} +//*/ + +unsigned TilemapTileSelector::getSelectedTile() { + return this->selectedTile; +} + +//* +unsigned TilemapTileSelector::getTileId(int x, int y) { + int index = y * this->numTilesWide + x; + return index < this->numTiles ? index : this->numTiles % index; +} +//*/ + +//* +void TilemapTileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) { + SelectablePixmapItem::mousePressEvent(event); + this->updateSelectedTile(); + emit selectedTileChanged(this->selectedTile); +} +//*/ + +//* +void TilemapTileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + SelectablePixmapItem::mouseMoveEvent(event); + this->updateSelectedTile(); + emit hoveredTileChanged(this->selectedTile); + emit selectedTileChanged(this->selectedTile); +} +//*/ + +//* +void TilemapTileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + SelectablePixmapItem::mouseReleaseEvent(event); + this->updateSelectedTile(); + emit selectedTileChanged(this->selectedTile); +} +//*/ + +//* +void TilemapTileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { + QPoint pos = this->getCellPos(event->pos()); + unsigned tileId = this->getTileId(pos.x(), pos.y()); + emit this->hoveredTileChanged(tileId); +} +//*/ + +//* +void TilemapTileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) { + emit this->hoveredTileCleared(); +} +//*/ + +//* +QPoint TilemapTileSelector::getTileIdCoords(unsigned tileId) { + int index = tileId < this->numTiles ? tileId : this->numTiles % tileId;// TODO: change this? + return QPoint(index % this->numTilesWide, index / this->numTilesWide);// ? is this right? +} +//*/ + +QImage TilemapTileSelector::tileImg(unsigned tileId) { + // + QPoint pos = getTileIdCoords(tileId); + return pixmap.copy(pos.x() * 8, pos.y() * 8, 8, 8).toImage(); +} + + + + + + + + + + + + + + + + From a9098cfd7a228f67aa9569588eb857a730bcb78c Mon Sep 17 00:00:00 2001 From: garak Date: Fri, 4 Jan 2019 23:04:14 -0500 Subject: [PATCH 02/17] add layout view to region map editor --- forms/mainwindow.ui | 228 ++++++++++++---------- include/core/regionmapeditor.h | 92 ++++----- include/editor.h | 20 +- include/mainwindow.h | 1 + include/project.h | 3 + include/ui/regionmaplayoutpixmapitem.h | 42 ++++ include/ui/tilemaptileselector.h | 2 +- porymap.pro | 2 + src/core/regionmapeditor.cpp | 257 +++++++++++++------------ src/editor.cpp | 147 +++++++++++++- src/mainwindow.cpp | 13 ++ src/project.cpp | 1 + src/ui/regionmaplayoutpixmapitem.cpp | 85 ++++++++ src/ui/tilemaptileselector.cpp | 40 +--- 14 files changed, 597 insertions(+), 336 deletions(-) create mode 100644 include/ui/regionmaplayoutpixmapitem.h create mode 100644 src/ui/regionmaplayoutpixmapitem.cpp diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index c1e76938..02a3969c 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -237,7 +237,7 @@ - 4 + 0 false @@ -2778,7 +2778,7 @@ - + true @@ -2847,10 +2847,10 @@ - 30 - 20 - 321 - 211 + 0 + 0 + 371 + 261 @@ -2867,8 +2867,8 @@ 0 0 - 319 - 209 + 369 + 259 @@ -2972,10 +2972,10 @@ - 20 - 20 - 321 - 211 + 0 + 0 + 371 + 261 @@ -2992,8 +2992,8 @@ 0 0 - 319 - 209 + 369 + 259 @@ -3357,20 +3357,20 @@ - + false - 50 - 40 - 261 - 170 + 10 + 30 + 331 + 211 - + 0 0 @@ -3381,93 +3381,107 @@ QFrame::Raised - - - - - Connected Map - - - - - - - <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> - - - true - - - - - - - Weather - - - - - - - <html><head/><body><p>The default weather for this map.</p></body></html> - - - true - - - - - - - Type - - - - - - - <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> - - - true - - - - - - - Show Location Name - - - - - - - <html><head/><body><p>Whether or not to display the location name when the player enters the map.</p></body></html> - - - - - - - - - - Battle scene - - - - - - - <html><head/><body><p>Determines the type of battle scene graphics to use.</p></body></html> - - - true - - - - + + + + 210 + 170 + 107 + 32 + + + + Set Values + + + + + + 13 + 11 + 301 + 111 + + + + + + + Map Section + + + + + + + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> + + + true + + + + + + + Map Name + + + + + + + + + + City Map + + + + + + + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> + + + true + + + + + + + + + 20 + 130 + 121 + 26 + + + + + + + x + + + + + + + + + + y + + + + + + + + diff --git a/include/core/regionmapeditor.h b/include/core/regionmapeditor.h index 2875ef81..a16e77f1 100644 --- a/include/core/regionmapeditor.h +++ b/include/core/regionmapeditor.h @@ -4,7 +4,6 @@ #include "project.h" #include "map.h" #include "tilemaptileselector.h" -//#include "block.h" #include #include @@ -13,48 +12,28 @@ #include #include -// if editing map bins, will need to remake the graphics when editing -// are the scenes set in the editor / project / mainwindow files? -/* - * - display the region map background image - * - edit the region_map_layout.h layout - * - edit city maps metatile layout and JUST save the mapname_num.bin - * - edit - * who edits pokenav_city_maps 1 and 2? - * users can: - add the incbins probably themselves - * - add - * - edit region map background image - * - * - * - * - * Editor: - * - void displayCityMapMetatileSelector - * - void displayRegionMapTileSelector - * - void selectRegionMapTile(QString mapname) - * - QGraphicsScene *scene_city_map_metatiles - * - TilemapTileSelector *city_map_metatile_selector_item - * - Tileset *city_map_squares (or tileset_city_map?) - * - Tileset *tileset_region_map - * - * MainWindow: - * - * - * Project: - * - */ -// rename this struct +// rename this struct? struct CityMapPosition { // - //QString filename; // eg. dewford_0 + //QString filename; // eg. dewford_0 ? QString tilemap;// eg. "dewford_0" int x; int y; }; +struct RegionMapEntry +{ + // + int x; + int y; + int width; + int height; + QString name;// mapsection +}; + // class that holds data for each square in this project // struct? // TODO: change char / uint8_t to unsigned @@ -62,17 +41,14 @@ class RegionMapSquare { public: // - // are positions layout positions? (yes) so out of bounds are all (-1, -1) <-- how it's used in code - // (GetRegionMapLocationPosition) - // or image positions int x = -1;// x position, 0-indexed from top left int y = -1;// y position, 0-indexed from top left uint8_t tile_img_id;// tilemap ids for the background image bool has_map = false;// whether this square is linked to a map or is empty - QString map_name;// name of the map associated with this square (if has_map is true): eg. "MAUVILLE_CITY" - // ^ use project mapsec to names table - bool has_city_map;// whether there is a city map on this grid - //QList city_maps; + QString map_name;// name of the map associated with this square (if has_map is true): eg. "MAUVILLE_CITY" (TODO: REMOVE) + QString mapsec; + uint8_t secid; + bool has_city_map = false;// whether there is a city map on this grid QString city_map_name;// filename of the city_map tilemap //bool is_flyable;//? needed ? friend class RegionMap;// not necessary if instance? what @@ -87,17 +63,13 @@ class RegionMap : public QObject public: RegionMap() = default; - ~RegionMap() { - delete mapname_abbr; - delete layout_map_names; - //delete background_image_tiles; - //delete map_squares; - //delete background_image_selector_item; - }; + ~RegionMap() {}; static QMap> ruby_city_maps_; static QString mapSecToMapConstant(QString); + Project *project; + //RegionMapSquare *map_squares = nullptr;// array of RegionMapSquares QList map_squares; @@ -107,16 +79,16 @@ public: QString region_map_bin_path;// = QString::null; QString city_map_header_path;//dafuq is this? QString region_map_layout_path; + QString region_map_entries_path; + QString region_map_layout_bin_path; + QString region_map_city_map_tiles_path; - //QMap something;// name of map : info about city map, position in layoit, etc. - //QMap regionMapLayoutTng; // mapName : tilemaptileselector - // maybe position data to select correct square when changing map on side but only if map is a valid - //QList *background_image_tiles;// the visible ones anyways // using list because replace - //TilemapTileSelector *background_image_selector_item;// ? - QMap *mapname_abbr;// layout shortcuts mapname:region_map_layout defines (both ways) - // make this a QHash?? <-- no because something - QStringList *layout_map_names; - // uint8_t border_tile; + QByteArray mapBinData; + + QMap sMapNames;// {"{/sMapName_/}LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} + QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} + //QList> mapSecToMapEntry; + QMap mapSecToMapEntry;// TODO: add to this on creation of new map bool hasUnsavedChanges(); @@ -125,7 +97,7 @@ public: // parseutil.cpp ? void readBkgImgBin(); void readCityMaps();// more complicated - void readLayout(QMap*); + void readLayout(); QString newAbbr(QString);// makes a *unique* 5 character abbreviation from mapname to add to mapname_abbr @@ -140,6 +112,7 @@ public: void save(); void saveBkgImgBin(); void saveLayout(); + void saveOptions(int, QString, QString, int, int); void saveCityMaps(); void update();// update the view in case something is broken? @@ -150,12 +123,13 @@ public: int height(); QSize imgSize(); unsigned getTileId(int, int); + int getMapSquareIndex(int, int); // implement these here? void undo(); void redo(); - void test(QMap*);// remove when done testing obvi + void test();// remove when done testing obvi // TODO: move read / write functions to private (and others) private: @@ -166,6 +140,8 @@ private: int img_height_; int img_index_(int, int);// returns index int at x,y args (x + y * width_ * 2) // 2 because int layout_index_(int, int); + void fillMapSquaresFromLayout(); + QString fix_case(QString);// CAPS_WITH_UNDERSCORE to CamelCase //protected: // diff --git a/include/editor.h b/include/editor.h index b28f0ebf..f84600b9 100644 --- a/include/editor.h +++ b/include/editor.h @@ -20,6 +20,7 @@ #include "collisionpixmapitem.h" #include "mappixmapitem.h" #include "regionmappixmapitem.h" +#include "regionmaplayoutpixmapitem.h" #include "regionmapeditor.h" #include "settings.h" #include "movablerect.h" @@ -79,31 +80,42 @@ public: void updateCustomMapHeaderValues(QTableWidget *); Tileset *getCurrentMapPrimaryTileset(); -// +// TODO: move these to appropriate place RegionMap *region_map; void loadRegionMapData(); QGraphicsScene *scene_region_map_image = nullptr; - QGraphicsScene *scene_region_map_layout = 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; + RegionMapLayoutPixmapItem *region_map_layout_item = nullptr; void displayRegionMap(); + void displayRegionMapImage(); + void displayRegionMapLayout(); + void displayRegionMapLayoutOptions(); + void updateRegionMapLayoutOptions(int); void displayRegionMapTileSelector(); + void displayCityMapTileSelector(); - // selectedTileChanged, hoveredTileChanged, hoveredTileCleared void onRegionMapTileSelectorSelectedTileChanged(); void onRegionMapTileSelectorHoveredTileChanged(unsigned); void onRegionMapTileSelectorHoveredTileCleared(); + void onRegionMapLayoutSelectedTileChanged(int); + void onRegionMapLayoutHoveredTileChanged(int); + void onRegionMapLayoutHoveredTileCleared(); + private slots: void onHoveredRegionMapTileChanged(int, int); void onHoveredRegionMapTileCleared(); void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); public: - QString regionMapTabStatusbarMessage;// TODO: make this name not terrible + QString rmStatusbarMessage; // DraggablePixmapItem *addMapEvent(Event *event); diff --git a/include/mainwindow.h b/include/mainwindow.h index 3cbe1fc0..1f5c3eb5 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -68,6 +68,7 @@ private slots: void on_tabWidget_Region_Map_currentChanged(int); void on_tabWidget_currentChanged(int index); + void on_pushButton_RM_Options_save_clicked(); void on_actionUndo_triggered(); diff --git a/include/project.h b/include/project.h index 89306895..5331b6aa 100644 --- a/include/project.h +++ b/include/project.h @@ -47,6 +47,9 @@ public: Map* loadMap(QString); Map* getMap(QString); + // other options include: InGameName, PopUpName, ???? + QMap *mapSecToMapHoverName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} + QMap *tileset_cache = nullptr; Tileset* loadTileset(QString, Tileset *tileset = nullptr); Tileset* getTileset(QString, bool forceLoad = false); diff --git a/include/ui/regionmaplayoutpixmapitem.h b/include/ui/regionmaplayoutpixmapitem.h new file mode 100644 index 00000000..1fca1581 --- /dev/null +++ b/include/ui/regionmaplayoutpixmapitem.h @@ -0,0 +1,42 @@ +#ifndef REGIONMAPLAYOUTPIXMAPITEM_H +#define REGIONMAPLAYOUTPIXMAPITEM_H + +#include "tilemaptileselector.h" +//#include "regionmappixmapitem.h" +#include "regionmapeditor.h" + +class RegionMapLayoutPixmapItem : public SelectablePixmapItem { + Q_OBJECT +public: + RegionMapLayoutPixmapItem(RegionMap *rmap, TilemapTileSelector *ts) : SelectablePixmapItem(8, 8, 1, 1) { + // + this->region_map = rmap; + this->tile_selector = ts; + setAcceptHoverEvents(true); + } + RegionMap *region_map;// inherited from RegionMapPixmapItem? + TilemapTileSelector *tile_selector; + int selectedTile;// index in map_squares + void draw(); + void select(int, int); + void setDefaultSelection(); + +private: + void updateSelectedTile(); + +// can I implement these if they are virtual? +signals: + void mouseEvent(QGraphicsSceneMouseEvent *, RegionMapLayoutPixmapItem *); + void hoveredTileChanged(int); + void hoveredTileCleared(); + void selectedTileChanged(int); + +protected: + void hoverMoveEvent(QGraphicsSceneHoverEvent*); + void hoverLeaveEvent(QGraphicsSceneHoverEvent*); + void mousePressEvent(QGraphicsSceneMouseEvent*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*); +}; + +#endif // REGIONMAPLAYOUTPIXMAPITEM_H diff --git a/include/ui/tilemaptileselector.h b/include/ui/tilemaptileselector.h index 8bb52050..1bf94984 100644 --- a/include/ui/tilemaptileselector.h +++ b/include/ui/tilemaptileselector.h @@ -8,7 +8,7 @@ class TilemapTileSelector: public SelectablePixmapItem { public: TilemapTileSelector(QPixmap pixmap): SelectablePixmapItem(8, 8, 1, 1) { this->pixmap = pixmap; - this->numTilesWide = 16; + this->numTilesWide = pixmap.width() / 8; this->selectedTile = 0x00; setAcceptHoverEvents(true); } diff --git a/porymap.pro b/porymap.pro index 3868a8a2..98ba5e2e 100644 --- a/porymap.pro +++ b/porymap.pro @@ -34,6 +34,7 @@ SOURCES += src/core/block.cpp \ src/ui/collisionpixmapitem.cpp \ src/ui/connectionpixmapitem.cpp \ src/ui/currentselectedmetatilespixmapitem.cpp \ + src/ui/regionmaplayoutpixmapitem.cpp \ src/ui/cursortilerect.cpp \ src/ui/customattributestable.cpp \ src/ui/eventpropertiesframe.cpp \ @@ -87,6 +88,7 @@ HEADERS += include/core/block.h \ include/ui/collisionpixmapitem.h \ include/ui/connectionpixmapitem.h \ include/ui/currentselectedmetatilespixmapitem.h \ + include/ui/regionmaplayoutpixmapitem.h \ include/ui/cursortilerect.h \ include/ui/customattributestable.h \ include/ui/eventpropertiesframe.h \ diff --git a/src/core/regionmapeditor.cpp b/src/core/regionmapeditor.cpp index 73b27c0f..1242500f 100644 --- a/src/core/regionmapeditor.cpp +++ b/src/core/regionmapeditor.cpp @@ -83,8 +83,10 @@ QMap> RegionMap::ruby_city_maps_ = QMaproot; + this->project = pro; // // TODO: in the future, allow these to be adjustable (and save values) // possibly use a config file? @@ -99,20 +101,24 @@ void RegionMap::init(Project *pro) { region_map_bin_path = path + "/graphics/pokenav/region_map_map.bin"; region_map_png_path = path + "/graphics/pokenav/region_map.png"; region_map_layout_path = path + "/src/data/region_map_layout.h"; + region_map_entries_path = path + "/src/data/region_map/region_map_entries.h"; + region_map_layout_bin_path = path + "/graphics/pokenav/region_map_section_layout.bin"; + region_map_city_map_tiles_path = path + "/graphics/pokenav/zoom_tiles.png";// TODO: rename png to map_squares in pokeemerald readBkgImgBin(); - readLayout(pro->mapConstantsToMapNames); + readLayout(); readCityMaps(); //tryGetMap(); //saveBkgImgBin(); //saveLayout(); - test(pro->mapConstantsToMapNames); + + //test(); } // as of now, this needs to be called first because it initializes all the -// RegionMapSquare s in the list +// `RegionMapSquare`s in the list // TODO: if the tileId is not valid for the provided image, make sure it does not crash void RegionMap::readBkgImgBin() { QFile binFile(region_map_bin_path); @@ -121,7 +127,7 @@ void RegionMap::readBkgImgBin() { QByteArray mapBinData = binFile.readAll(); binFile.close(); - // the two is because lines are skipped for some reason + // the two multiplier is because lines are skipped for some reason // (maybe that is because there could be multiple layers?) // background image is also 32x20 for (int m = 0; m < img_height_; m++) { @@ -148,113 +154,123 @@ void RegionMap::saveBkgImgBin() { file.close(); } -// done -void RegionMap::readLayout(QMap *qmap) { - QFile file(region_map_layout_path); +// TODO: reorganize this into project? the i/o stuff. use regionMapSections +void RegionMap::readLayout() { + // + QFile file(region_map_entries_path); if (!file.open(QIODevice::ReadOnly)) return; - QMap * abbr = new QMap; + QString line; + // TODO: put these in Project, and keep in order + //QMap sMapNames;// {"sMapName_LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} + //QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} + //QList<> mapSecToMapEntry;// {"MAPSEC_LITTLEROOT_TOWN" : } - QString line, text; - QStringList *captured = new QStringList; + // new map ffor mapSecToMapHoverName + QMap *qmap = new QMap; QTextStream in(&file); while (!in.atEnd()) { line = in.readLine(); - if (line.startsWith("#define")) { - QStringList split = line.split(QRegularExpression("\\s+")); - abbr->insert(split[2].replace("MAPSEC_",""), split[1]); - } else { - text += line.remove(" "); + if (line.startsWith("static const u8")) { + QRegularExpression reBefore("sMapName_(.*)\\["); + QRegularExpression reAfter("_\\(\"(.*)\""); + QString const_name = reBefore.match(line).captured(1); + QString full_name = reAfter.match(line).captured(1); + sMapNames.insert(const_name, full_name); + } else if (line.contains("MAPSEC")) { + QRegularExpression reBefore("\\[(.*)\\]"); + QRegularExpression reAfter("{(.*)}"); + QStringList entry = reAfter.match(line).captured(1).remove(" ").split(","); + QString mapsec = reBefore.match(line).captured(1); + QString insertion = entry[4].remove("sMapName_"); + qmap->insert(mapsec, sMapNames[insertion]); + // can make this a map, the order doesn't really matter + mapSecToMapEntry[mapsec] = + // x y width height name + {entry[0].toInt(), entry[1].toInt(), entry[2].toInt(), entry[3].toInt(), insertion} + ; + // ^ when loading this info to city maps, loop over mapSecToMapEntry and + // add x and y map sqyare when width or height >1 + // indexOf because mapsecs is just a qstringlist + //text += line.remove(" "); } } - QRegularExpression re("{(.*?)}"); - *captured = re.match(text).captured(1).split(","); - captured->removeAll({}); + file.close(); - // replace abbreviations with names - for (int i = 0; i < captured->length(); i++) { - QString value = (*captured)[i]; - if (value.startsWith("R(")) {// routes are different - captured->replace(i, QString("ROUTE_%1").arg(value.mid(2,3))); - } else { - captured->replace(i, abbr->key(value)); - } - } + project->mapSecToMapHoverName = qmap; + + QFile binFile(region_map_layout_bin_path); + if (!binFile.open(QIODevice::ReadOnly)) return; + QByteArray mapBinData = binFile.readAll(); + binFile.close(); // TODO: improve this? - for (int m = 0, i = 0; m < layout_height_; m++) { + for (int m = 0; m < layout_height_; m++) { for (int n = 0; n < layout_width_; n++) { - i = img_index_(n,m); - QString secname = (*captured)[layout_index_(n,m)]; - if (secname != "NOTHING") map_squares[i].has_map = true; - map_squares[i].map_name = qmap->value(mapSecToMapConstant(secname)); + int i = img_index_(n,m); + map_squares[i].secid = static_cast(mapBinData.at(layout_index_(n,m))); + QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(n,m)))]; + //qDebug() << i << map_squares[i].secid << secname; + if (secname != "MAPSEC_NONE") map_squares[i].has_map = true; + map_squares[i].mapsec = secname; + map_squares[i].map_name = sMapNames.value(mapSecToMapEntry.value(secname).name);//[mapSecToMapEntry[secname].name]; map_squares[i].x = n; map_squares[i].y = m; } } - mapname_abbr = abbr; - layout_map_names = captured; - file.close(); } -// does it matter that it doesn't save in order? -// do i need to use a QList ?? +/// saves: +// region_map_entries_path +// region_map_layout_bin_path (layout as tilemap instead of how it is in ruby) +// done +// TODO: consider keeping QMaps in order void RegionMap::saveLayout() { - // - QString layout_text = ""; - QString mapsec = "MAPSEC_"; - QString define = "#define "; - QString array_start = "static const u8 sRegionMapLayout[] =\n{"; - QString array_close = "\n};\n"; - QString tab = " "; + QString entries_text; + QString layout_text; - for (QString key : mapname_abbr->keys()) { - layout_text += define + mapname_abbr->value(key) + tab + mapsec + key + "\n"; + entries_text += "#ifndef GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n"; + entries_text += "#define GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n\n"; + + // note: this doesn't necessarily keep order because it is a QMap + for (auto it : this->project->mapSecToMapHoverName->keys()) { + entries_text += "static const u8 sMapName_" + fix_case(it) + "[] = _(\"" + this->project->mapSecToMapHoverName->value(it) + "\");\n"; } - layout_text += "\n" + array_start;// + + array_close;//oops + entries_text += "\nconst struct RegionMapLocation gRegionMapEntries[] = {\n"; - //qDebug() << *layout_map_names; - int cnt = 0; - for (QString s : *layout_map_names) { - // - if (!(cnt % layout_width_)) { - layout_text += "\n" + tab; - } - if (s.startsWith("ROUTE_")) { - layout_text += QString("R(%1)").arg(s.replace("ROUTE_","")) + ", "; - } else { - layout_text += mapname_abbr->value(s) + ", "; - } - cnt++; + for (auto sec : mapSecToMapEntry.keys()) { + struct RegionMapEntry entry = mapSecToMapEntry.value(sec); + entries_text += " [" + sec + "] = {" + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " + + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + fix_case(sec) + "},\n";//entry.name } + entries_text += "};\n\n#endif // GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n"; - //layout_text. - layout_text += array_close; - - QFile file(region_map_layout_path); - if (!file.open(QIODevice::WriteOnly)) return; - file.write(layout_text.toUtf8()); - file.close(); + project->saveTextFile(region_map_entries_path, entries_text); + + QByteArray data; + for (int m = 0; m < layout_height_; m++) { + for (int n = 0; n < layout_width_; n++) { + int i = img_index_(n,m); + data.append(map_squares[i].secid); + } + } + QFile bfile(region_map_layout_bin_path); + if (!bfile.open(QIODevice::WriteOnly)) return; + bfile.write(data); + bfile.close(); } +// beyond broken void RegionMap::readCityMaps() { // - //for (int m = 0; m < layout_height_; m++) { - // QString tester; - // for (int n = 0; n < layout_width_; n++) { - // tester += (QString::number(img_index_(n,m)).rightJustified(3, '.') + " "); - // } - // qDebug() << tester; - //} - //for (auto map : map_squares) { for (int map = 0; map < map_squares.size(); map++) { // if (map_squares[map].has_map) { // if (ruby_city_maps_.contains(map_squares[map].map_name)) { - map_squares[map].has_city_map = true; + //map_squares[map].has_city_map = true; //map_squares[map].city_map_name = ruby_city_maps_.value(map_squares[map].map_name)[0].tilemap; QList city_maps = ruby_city_maps_.value(map_squares[map].map_name); for (auto city_map : city_maps) { @@ -262,6 +278,7 @@ void RegionMap::readCityMaps() { if (city_map.x == map_squares[map].x && city_map.y == map_squares[map].y) // + map_squares[map].has_city_map = true; map_squares[map].city_map_name = city_map.tilemap; } } @@ -269,27 +286,6 @@ void RegionMap::readCityMaps() { } } -//done -QString RegionMap::newAbbr(QString mapname) { - QString abbr; - QStringList words = mapname.split("_"); - - if (words.length() == 1) { - abbr = (words[0] + "_____X").left(6); - } else { - abbr = (words.front() + "___X").left(4) + "_" + words.back().at(0); - } - - // to guarantee unique abbreviations (up to 14) - QString extra_chars = "23456789BCDEF"; - int count = 0; - while ((*mapname_abbr).values().contains(abbr)) { - abbr.replace(5,1,extra_chars[count]); - count++; - } - return abbr; -} - // layout coords to image index int RegionMap::img_index_(int x, int y) { return ((x + 1) + (y + 2) * img_width_); @@ -300,10 +296,7 @@ int RegionMap::layout_index_(int x, int y) { return (x + y * layout_width_); } -// img coords to layout index? -// img coords to img index? - -void RegionMap::test(QMap* qmap) { +void RegionMap::test() { // bool debug_rmap = false; @@ -316,17 +309,11 @@ void RegionMap::test(QMap* qmap) { << square.has_city_map << square.city_map_name ; - //if (qmap->contains(mapSecToMapConstant(square.map_name))) - // extras += qmap->value(mapSecToMapConstant(square.map_name)) + " "; - //else - // extras += "nothing "; } QPixmap png(region_map_png_path); - //png.load(region_map_png_path); qDebug() << "png num 8x8 tiles" << QString("0x%1").arg((png.width()/8) * (png.height() / 8), 2, 16, QChar('0')); } - } int RegionMap::width() { @@ -346,26 +333,57 @@ QSize RegionMap::imgSize() { unsigned RegionMap::getTileId(int x, int y) { // return map_squares[x + y * img_width_].tile_img_id; - //qDebug() << x << y; - //return 0; } -// sidenote: opening the map from MAPSEC_x will not always be right -// there needs to be a mapsections to mapname QMap -// otherwie, look for the first map with right substring -// mapConstantsToMapNames [MAP_ROUTE106] = "Route106" -// eg. SOUTHERN_ISLAND -> MAP_SOUTHERN_ISLAND -> SouthernIsland(n) -> SouthernIsland_Exterior -// MT_CHIMNEY -> MAP_MT_CHIMNEY -> MtChimney(y) -// ROUTE_101 -> MAP_ROUTE101 -> Route101(y) -// (or synchronize these for consistency in the repos :: underscore / no underscore) - // TODO: change debugs to logs void RegionMap::save() { // qDebug() << "saving region map image tilemap at" << region_map_bin_path << "\n" - ;//<< "saving region map layout at" << region_map_layout_path << "\n"; + << "saving region map layout at" << region_map_layout_path << "\n" + ; saveBkgImgBin(); + saveLayout(); +} + +// save Options (temp) +void RegionMap::saveOptions(int index, QString sec, QString name, int x, int y) { + // + // TODO:req need to reindex in city_maps if changing x and y + // TODO: save [sec] sMapName_ properly + // so instead of taking index, maybe go by img_index_(x,y) + this->project->mapSecToMapHoverName->insert(sec, name); + this->map_squares[index].mapsec = sec; + this->map_squares[index].map_name = name;// TODO: display in editor with this map & remove this field + this->map_squares[index].x = x; + this->map_squares[index].y = y; +} + +// from x, y of image +// TODO: make sure this returns a valid index +int RegionMap::getMapSquareIndex(int x, int y) { + // + int index = (x + y * img_width_); + return index < map_squares.length() - 1 ? index : 0; +} + +// For turning a MAPSEC_NAME into a unique identifier sMapName-style variable. +QString RegionMap::fix_case(QString caps) { + bool big = true; + QString camel; + + for (auto ch : caps.remove(QRegularExpression("({.*})")).remove("MAPSEC")) { + if (ch == '_' || ch == ' ') { + big = true; + continue; + } + if (big) { + camel += ch.toUpper(); + big = false; + } + else camel += ch.toLower(); + } + return camel; } @@ -397,6 +415,5 @@ void RegionMap::save() { - diff --git a/src/editor.cpp b/src/editor.cpp index 9cfce50e..86680762 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -453,6 +453,17 @@ bool Editor::setMap(QString map_name) { selected_events->clear(); displayMap(); updateSelectedEvents(); + + if (region_map) { + for (auto square : region_map->map_squares) { + if (square.mapsec == map->location) { + int img_x = square.x + 1; + int img_y = square.y + 2; + this->region_map_layout_item->select(img_x, img_y); + break; + } + } + } } return true; @@ -602,6 +613,7 @@ void Editor::displayMap() { displayMapBorder(); displayMapGrid(); +<<<<<<< abc873464dc736dcfdda4a20df61516f45478844 this->playerViewRect->setZValue(1000); this->cursorMapTileRect->setZValue(1001); scene->addItem(this->playerViewRect); @@ -610,6 +622,8 @@ void Editor::displayMap() { displayRegionMapTileSelector();//? displayRegionMap(); +======= +>>>>>>> add layout view to region map editor if (map_item) { map_item->setVisible(false); } @@ -621,6 +635,14 @@ void Editor::displayMap() { } } +void Editor::displayRegionMap() { + displayRegionMapTileSelector(); + displayCityMapTileSelector(); + displayRegionMapImage(); + displayRegionMapLayout(); + displayRegionMapLayoutOptions(); +} + void Editor::displayMetatileSelector() { if (metatile_selector_item && metatile_selector_item->scene()) { metatile_selector_item->scene()->removeItem(metatile_selector_item); @@ -1273,7 +1295,8 @@ void Editor::deleteEvent(Event *event) { void Editor::loadRegionMapData() { // - region_map->init(project); + this->region_map->init(project); + displayRegionMap(); } // TODO: get this to display on a decent scale @@ -1297,8 +1320,29 @@ void Editor::displayRegionMapTileSelector() { this->mapsquare_selector_item->pixelHeight + 2); } +void Editor::displayCityMapTileSelector() { + // city_map_selector_item + this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_city_map_tiles_path)); + this->city_map_selector_item->draw(); + + this->scene_city_map_tiles = new QGraphicsScene; + this->scene_city_map_tiles->addItem(this->city_map_selector_item); + + /*connect(this->city_map_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &Editor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? + connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, + this, &Editor::onRegionMapTileSelectorHoveredTileChanged); + connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileCleared, + this, &Editor::onRegionMapTileSelectorHoveredTileCleared);*/ + + this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth + 2, + this->city_map_selector_item->pixelHeight + 2); +} + // TODO: change the signal slot to new syntax -void Editor::displayRegionMap() { +// TODO: add scalability? +void Editor::displayRegionMapImage() { // this->region_map_item = new RegionMapPixmapItem(this->region_map, this->mapsquare_selector_item); connect(region_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*)), @@ -1313,10 +1357,51 @@ void Editor::displayRegionMap() { this->scene_region_map_image->addItem(this->region_map_item); this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); - //this->scene_region_map_image->scale(2, 2); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize()); - //this->ui->graphicsView_Region_Map_BkgImg->scale(2.0, 2.0); +} + +void Editor::displayRegionMapLayout() { + // + this->region_map_layout_item = new RegionMapLayoutPixmapItem(this->region_map, this->mapsquare_selector_item); + //* + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::selectedTileChanged, + this, &Editor::onRegionMapLayoutSelectedTileChanged);// TODO: remove this? + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileChanged, + this, &Editor::onRegionMapLayoutHoveredTileChanged); + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileCleared, + this, &Editor::onRegionMapLayoutHoveredTileCleared); + //*/ + this->region_map_layout_item->draw(); + this->region_map_layout_item->setDefaultSelection(); + + this->scene_region_map_layout = new QGraphicsScene; + this->scene_region_map_layout->addItem(region_map_layout_item); + this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); + + this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize()); +} + +void Editor::displayRegionMapLayoutOptions() { + // + this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); + + this->ui->frame_RM_Options->setEnabled(true); + + // TODO: change these values to variables + this->ui->spinBox_RM_Options_x->setMaximum(27); + this->ui->spinBox_RM_Options_y->setMaximum(14); + + updateRegionMapLayoutOptions(65); +} + +void Editor::updateRegionMapLayoutOptions(int index) { + // + this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec));//this->region_map->map_squares[index].map_name); + this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); + this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); + this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); } void Editor::onRegionMapTileSelectorSelectedTileChanged() { @@ -1330,18 +1415,62 @@ void Editor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { void Editor::onRegionMapTileSelectorHoveredTileCleared() { // - QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->mapsquare_selector_item->selectedTile, 4, 16, QChar('0')).toUpper(); + //QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->region_map_layout_item->selectedTile, 4, 16, QChar('0')).toUpper(); + //this->ui->statusBar->showMessage(message); +} + +void Editor::onRegionMapLayoutSelectedTileChanged(int index) { + // + QString message = QString(); + if (this->region_map->map_squares[index].has_map) { + // + message = QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");//.remove("{NAME_END}") + } + this->ui->statusBar->showMessage(message); + + updateRegionMapLayoutOptions(index); +} + +void Editor::onRegionMapLayoutHoveredTileChanged(int index) { + // TODO: change to x, y coords not index + QString message = QString(); + int x = this->region_map->map_squares[index].x; + int y = this->region_map->map_squares[index].y; + if (x >= 0 && y >= 0) { + message = QString("(%1, %2)").arg(x).arg(y); + if (this->region_map->map_squares[index].has_map) { + // + message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); + } + } + this->ui->statusBar->showMessage(message); +} + +void Editor::onRegionMapLayoutHoveredTileCleared() { + // + int index = this->region_map_layout_item->selectedTile; + QString message = QString(); + int x = this->region_map->map_squares[index].x; + int y = this->region_map->map_squares[index].y; + if (x >= 0 && y >= 0) { + message = QString("(%1, %2)").arg(x).arg(y); + if (this->region_map->map_squares[index].has_map) { + // + message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); + } + } this->ui->statusBar->showMessage(message); } void Editor::onHoveredRegionMapTileChanged(int x, int y) { - // - regionMapTabStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); - this->ui->statusBar->showMessage(regionMapTabStatusbarMessage); + rmStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); + this->ui->statusBar->showMessage(rmStatusbarMessage); } void Editor::onHoveredRegionMapTileCleared() { - // this->ui->statusBar->clearMessage(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0bf6e874..9aebd89a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1986,6 +1986,19 @@ void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) { ui->graphicsView_Metatiles->setFixedSize(size.width() + 2, size.height() + 2); } +void MainWindow::on_pushButton_RM_Options_save_clicked() { + // + this->editor->region_map->saveOptions( + // + this->editor->region_map_layout_item->selectedTile, + this->ui->comboBox_RM_ConnectedMap->currentText(), + this->ui->lineEdit_RM_MapName->text(), + this->ui->spinBox_RM_Options_x->value(), + this->ui->spinBox_RM_Options_y->value() + ); + this->editor->region_map_layout_item->draw(); +} + void MainWindow::closeEvent(QCloseEvent *event) { porymapConfig.setGeometry( this->saveGeometry(), diff --git a/src/project.cpp b/src/project.cpp index d763efba..d7e99ce3 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1512,6 +1512,7 @@ void Project::readRegionMapSections() { QString filepath = root + "/include/constants/region_map_sections.h"; QStringList prefixes = (QStringList() << "MAPSEC_"); readCDefinesSorted(filepath, prefixes, regionMapSections); + regionMapSections->removeAll("MAPSEC_SUBTRACT_KANTO");// TODO: fix this (in repos?) } void Project::readItemNames() { diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp new file mode 100644 index 00000000..24de0e53 --- /dev/null +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -0,0 +1,85 @@ +#include "regionmaplayoutpixmapitem.h" + + + +// TODO: make this connected (by duplicating scene rect maybe?) to background image tab +// +void RegionMapLayoutPixmapItem::draw() { + if (!region_map) return; + + QImage image(region_map->width() * 8, region_map->height() * 8, QImage::Format_RGBA8888); + + QPainter painter(&image); + for (int i = 0; i < region_map->map_squares.size(); i++) { + QImage bottom_img = this->tile_selector->tileImg(region_map->map_squares[i].tile_img_id); + QImage top_img(8, 8, QImage::Format_RGBA8888); + if (region_map->map_squares[i].has_map) { + top_img.fill(Qt::gray); + } else { + top_img.fill(Qt::black); + } + int x = i % region_map->width(); + int y = i / region_map->width(); + QPoint pos = QPoint(x * 8, y * 8); + painter.setOpacity(1); + painter.drawImage(pos, bottom_img); + painter.save(); + painter.setOpacity(0.55); + painter.drawImage(pos, top_img); + painter.restore(); + } + painter.end(); + + this->setPixmap(QPixmap::fromImage(image)); + this->drawSelection(); +} + +void RegionMapLayoutPixmapItem::setDefaultSelection() { + this->select(1,2); +} + +void RegionMapLayoutPixmapItem::select(int x, int y) { + int index = this->region_map->getMapSquareIndex(x, y); + SelectablePixmapItem::select(x, y, 0, 0); + this->selectedTile = index; + this->updateSelectedTile(); + + emit selectedTileChanged(index); +} + +void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { + QPoint pos = this->getCellPos(event->pos()); + int index = this->region_map->getMapSquareIndex(pos.x(), pos.y()); + if (this->region_map->map_squares[index].x >= 0 + && this->region_map->map_squares[index].y >= 0) { + SelectablePixmapItem::mousePressEvent(event); + this->updateSelectedTile(); + emit selectedTileChanged(this->selectedTile); + } +} + +void RegionMapLayoutPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + SelectablePixmapItem::mouseMoveEvent(event); + this->updateSelectedTile(); + emit hoveredTileChanged(this->selectedTile); + emit selectedTileChanged(this->selectedTile); +} + +void RegionMapLayoutPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + // +} + +void RegionMapLayoutPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { + QPoint pos = this->getCellPos(event->pos()); + int tileId = this->region_map->getMapSquareIndex(pos.x(), pos.y()); + emit this->hoveredTileChanged(tileId); +} + +void RegionMapLayoutPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) { + emit this->hoveredTileCleared(); +} + +void RegionMapLayoutPixmapItem::updateSelectedTile() { + QPoint origin = this->getSelectionStart(); + this->selectedTile = this->region_map->getMapSquareIndex(origin.x(), origin.y()); +} diff --git a/src/ui/tilemaptileselector.cpp b/src/ui/tilemaptileselector.cpp index b109ceb2..af7dfdc5 100644 --- a/src/ui/tilemaptileselector.cpp +++ b/src/ui/tilemaptileselector.cpp @@ -7,7 +7,7 @@ void TilemapTileSelector::draw() { this->pixelWidth = width_; size_t height_ = this->pixmap.height(); this->pixelHeight = height_; - size_t ntiles_ = (width_/8) * (height_/8);// length_ + size_t ntiles_ = (width_/8) * (height_/8); this->numTilesWide = width_ / 8; this->numTiles = ntiles_; @@ -16,97 +16,63 @@ void TilemapTileSelector::draw() { this->drawSelection(); } -//* void TilemapTileSelector::select(unsigned tileId) { QPoint coords = this->getTileIdCoords(tileId); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); this->selectedTile = tileId; emit selectedTileChanged(tileId); } -//*/ -//* void TilemapTileSelector::updateSelectedTile() { QPoint origin = this->getSelectionStart(); this->selectedTile = this->getTileId(origin.x(), origin.y()); } -//*/ unsigned TilemapTileSelector::getSelectedTile() { return this->selectedTile; } -//* unsigned TilemapTileSelector::getTileId(int x, int y) { int index = y * this->numTilesWide + x; return index < this->numTiles ? index : this->numTiles % index; } -//*/ -//* void TilemapTileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) { SelectablePixmapItem::mousePressEvent(event); this->updateSelectedTile(); emit selectedTileChanged(this->selectedTile); } -//*/ -//* void TilemapTileSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { SelectablePixmapItem::mouseMoveEvent(event); this->updateSelectedTile(); emit hoveredTileChanged(this->selectedTile); emit selectedTileChanged(this->selectedTile); } -//*/ -//* void TilemapTileSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { SelectablePixmapItem::mouseReleaseEvent(event); this->updateSelectedTile(); emit selectedTileChanged(this->selectedTile); } -//*/ -//* void TilemapTileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPoint pos = this->getCellPos(event->pos()); unsigned tileId = this->getTileId(pos.x(), pos.y()); emit this->hoveredTileChanged(tileId); } -//*/ -//* void TilemapTileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) { emit this->hoveredTileCleared(); } -//*/ -//* QPoint TilemapTileSelector::getTileIdCoords(unsigned tileId) { - int index = tileId < this->numTiles ? tileId : this->numTiles % tileId;// TODO: change this? - return QPoint(index % this->numTilesWide, index / this->numTilesWide);// ? is this right? + int index = tileId < this->numTiles ? tileId : this->numTiles % tileId; + return QPoint(index % this->numTilesWide, index / this->numTilesWide); } -//*/ QImage TilemapTileSelector::tileImg(unsigned tileId) { // QPoint pos = getTileIdCoords(tileId); return pixmap.copy(pos.x() * 8, pos.y() * 8, 8, 8).toImage(); } - - - - - - - - - - - - - - - - From 0f02d7b95584d8a5e1be65271401c976c7287342 Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 5 Jan 2019 13:02:14 -0500 Subject: [PATCH 03/17] fix saving region map layout --- src/core/regionmapeditor.cpp | 13 +++++++++---- src/editor.cpp | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/regionmapeditor.cpp b/src/core/regionmapeditor.cpp index 1242500f..a4912852 100644 --- a/src/core/regionmapeditor.cpp +++ b/src/core/regionmapeditor.cpp @@ -211,10 +211,9 @@ void RegionMap::readLayout() { int i = img_index_(n,m); map_squares[i].secid = static_cast(mapBinData.at(layout_index_(n,m))); QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(n,m)))]; - //qDebug() << i << map_squares[i].secid << secname; if (secname != "MAPSEC_NONE") map_squares[i].has_map = true; map_squares[i].mapsec = secname; - map_squares[i].map_name = sMapNames.value(mapSecToMapEntry.value(secname).name);//[mapSecToMapEntry[secname].name]; + map_squares[i].map_name = sMapNames.value(mapSecToMapEntry.value(secname).name); map_squares[i].x = n; map_squares[i].y = m; } @@ -352,9 +351,15 @@ void RegionMap::saveOptions(int index, QString sec, QString name, int x, int y) // TODO:req need to reindex in city_maps if changing x and y // TODO: save [sec] sMapName_ properly // so instead of taking index, maybe go by img_index_(x,y) - this->project->mapSecToMapHoverName->insert(sec, name); + if (!sec.isEmpty()) { + this->map_squares[index].has_map = true; + this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf(sec)); + } this->map_squares[index].mapsec = sec; - this->map_squares[index].map_name = name;// TODO: display in editor with this map & remove this field + if (!name.isEmpty()) { + this->map_squares[index].map_name = name;// TODO: display in editor with this map & remove this field + this->project->mapSecToMapHoverName->insert(sec, name); + } this->map_squares[index].x = x; this->map_squares[index].y = y; } diff --git a/src/editor.cpp b/src/editor.cpp index 86680762..713c72c3 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -28,6 +28,7 @@ void Editor::saveProject() { project->saveAllDataStructures(); if (region_map) { region_map->save(); + displayRegionMap(); } } } @@ -39,6 +40,7 @@ void Editor::save() { } if (project && region_map) { region_map->save(); + displayRegionMap(); } } From 62009373f73a25d668af9d03029708a0c9e23af6 Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 5 Jan 2019 17:59:57 -0500 Subject: [PATCH 04/17] 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); +} From d3a16d34df7f42fea7440c339c34dd39233e37fb Mon Sep 17 00:00:00 2001 From: garak Date: Mon, 7 Jan 2019 14:46:27 -0500 Subject: [PATCH 05/17] move region map editor to its own window --- forms/mainwindow.ui | 838 +---------------- forms/regionmapeditor.ui | 875 ++++++++++++++++++ .../core/{regionmapeditor.h => regionmap.h} | 0 include/editor.h | 50 +- include/mainwindow.h | 10 +- include/ui/regionmapeditor.h | 76 ++ include/ui/regionmaplayoutpixmapitem.h | 3 +- include/ui/regionmappixmapitem.h | 2 +- porymap.pro | 7 +- .../{regionmapeditor.cpp => regionmap.cpp} | 9 +- src/editor.cpp | 292 ------ src/mainwindow.cpp | 26 +- src/ui/regionmapeditor.cpp | 353 +++++++ 13 files changed, 1339 insertions(+), 1202 deletions(-) create mode 100644 forms/regionmapeditor.ui rename include/core/{regionmapeditor.h => regionmap.h} (100%) create mode 100644 include/ui/regionmapeditor.h rename src/core/{regionmapeditor.cpp => regionmap.cpp} (99%) create mode 100644 src/ui/regionmapeditor.cpp diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index fc2237cd..61aaa45a 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -2693,839 +2693,6 @@ - - - Region Map - - - - - 460 - 420 - 301 - 181 - - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOn - - - Qt::ScrollBarAsNeeded - - - QAbstractScrollArea::AdjustIgnored - - - true - - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 283 - 179 - - - - - 0 - 0 - - - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - 19 - 9 - 381 - 291 - - - - 0 - - - - Background Image - - - - - 0 - 0 - 371 - 261 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 369 - 259 - - - - - 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 - - - - - - - - - - - Map Layout - - - - - 0 - 0 - 371 - 261 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 369 - 259 - - - - - 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 - - - - - - - - - - - - - 440 - 20 - 351 - 291 - - - - 0 - - - - - - 30 - 20 - 281 - 261 - - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOn - - - Qt::ScrollBarAsNeeded - - - QAbstractScrollArea::AdjustIgnored - - - true - - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 263 - 259 - - - - - 0 - 0 - - - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - false - - - - 10 - 30 - 331 - 211 - - - - - 0 - 0 - - - - QFrame::StyledPanel - - - QFrame::Raised - - - - - 210 - 170 - 107 - 32 - - - - Set Values - - - - - - 13 - 11 - 301 - 111 - - - - - - - Map Section - - - - - - - <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> - - - true - - - - - - - Map Name - - - - - - - - - - City Map - - - - - - - <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> - - - true - - - - - - - - - 20 - 130 - 121 - 26 - - - - - - - x - - - - - - - - - - y - - - - - - - - - - - - - - - 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 - - - - - - - - - - - @@ -3582,6 +2749,7 @@ + @@ -3808,6 +2976,10 @@ Cursor Tile Outline + + + Region Map Editor + diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui new file mode 100644 index 00000000..5b9e193d --- /dev/null +++ b/forms/regionmapeditor.ui @@ -0,0 +1,875 @@ + + + RegionMapEditor + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 450 + 250 + 301 + 181 + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 283 + 179 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 19 + 9 + 321 + 221 + + + + 0 + + + + Background Image + + + + + -10 + -10 + 331 + 211 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 329 + 209 + + + + + 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 + + + + + + + + + + + Map Layout + + + + + -10 + -10 + 331 + 211 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 329 + 209 + + + + + 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 + + + + + + + + + + + + + 440 + 20 + 351 + 221 + + + + 0 + + + + + + 30 + 20 + 281 + 171 + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 263 + 169 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + false + + + + 10 + 0 + 331 + 211 + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + 210 + 170 + 107 + 32 + + + + Set Values + + + + + + 13 + 11 + 301 + 111 + + + + + + + Map Section + + + + + + + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> + + + true + + + + + + + Map Name + + + + + + + + + + City Map + + + + + + + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> + + + true + + + + + + + + + 20 + 130 + 138 + 26 + + + + + + + x + + + + + + + + + + y + + + + + + + + + + + + + + + 10 + 250 + 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 + + + + + + + + + + + + + + + 0 + 0 + 800 + 22 + + + + + File + + + + + + + + + Save + + + Ctrl+S + + + + + + diff --git a/include/core/regionmapeditor.h b/include/core/regionmap.h similarity index 100% rename from include/core/regionmapeditor.h rename to include/core/regionmap.h diff --git a/include/editor.h b/include/editor.h index 5f10a979..3f022388 100644 --- a/include/editor.h +++ b/include/editor.h @@ -19,10 +19,6 @@ #include "currentselectedmetatilespixmapitem.h" #include "collisionpixmapitem.h" #include "mappixmapitem.h" -#include "regionmappixmapitem.h" -#include "citymappixmapitem.h" -#include "regionmaplayoutpixmapitem.h" -#include "regionmapeditor.h" #include "settings.h" #include "movablerect.h" #include "cursortilerect.h" @@ -81,49 +77,6 @@ public: void updateCustomMapHeaderValues(QTableWidget *); Tileset *getCurrentMapPrimaryTileset(); -// TODO: move these to appropriate place - RegionMap *region_map; - 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(); - void displayRegionMapImage(); - void displayRegionMapLayout(); - void displayRegionMapLayoutOptions(); - void updateRegionMapLayoutOptions(int); - void displayRegionMapTileSelector(); - void displayCityMapTileSelector(); - void displayCityMap(QString); - void loadCityMaps(); - - void onRegionMapTileSelectorSelectedTileChanged(); - void onRegionMapTileSelectorHoveredTileChanged(unsigned); - void onRegionMapTileSelectorHoveredTileCleared(); - - void onRegionMapLayoutSelectedTileChanged(int); - void onRegionMapLayoutHoveredTileChanged(int); - void onRegionMapLayoutHoveredTileCleared(); - -private slots: - void onHoveredRegionMapTileChanged(int, int); - void onHoveredRegionMapTileCleared(); - void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); - void mouseEvent_city_map(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); - -public: - QString rmStatusbarMessage; -// - DraggablePixmapItem *addMapEvent(Event *event); void selectMapEvent(DraggablePixmapItem *object); void selectMapEvent(DraggablePixmapItem *object, bool toggle); @@ -152,7 +105,6 @@ public: QGraphicsScene *scene_selected_border_metatiles = nullptr; QGraphicsScene *scene_collision_metatiles = nullptr; QGraphicsScene *scene_elevation_metatiles = nullptr; - MetatileSelector *metatile_selector_item = nullptr; BorderMetatilesPixmapItem *selected_border_metatiles_item = nullptr; @@ -164,7 +116,7 @@ public: QString map_edit_mode; QString prev_edit_mode; - + int scale_exp = 0; double scale_base = sqrt(2); // adjust scale factor with this qreal collisionOpacity = 0.5; diff --git a/include/mainwindow.h b/include/mainwindow.h index c875091b..b437f879 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -15,6 +15,7 @@ #include "map.h" #include "editor.h" #include "tileseteditor.h" +#include "regionmapeditor.h" #include "filterchildrenproxymodel.h" #include "newmappopup.h" @@ -65,10 +66,7 @@ private slots: void on_checkBox_AllowBiking_clicked(bool checked); void on_checkBox_AllowEscapeRope_clicked(bool checked); - void on_tabWidget_Region_Map_currentChanged(int); - void on_tabWidget_currentChanged(int index); - void on_pushButton_RM_Options_save_clicked(); void on_actionUndo_triggered(); @@ -134,9 +132,6 @@ 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); @@ -166,9 +161,12 @@ private slots: void on_tableWidget_CustomHeaderFields_cellChanged(int row, int column); void on_horizontalSlider_MetatileZoom_valueChanged(int value); + void on_actionRegion_Map_Editor_triggered(); + private: Ui::MainWindow *ui; TilesetEditor *tilesetEditor = nullptr; + RegionMapEditor *regionMapEditor = nullptr; FilterChildrenProxyModel *mapListProxyModel; NewMapPopup *newmapprompt = nullptr; QStandardItemModel *mapListModel; diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h new file mode 100644 index 00000000..62bef62d --- /dev/null +++ b/include/ui/regionmapeditor.h @@ -0,0 +1,76 @@ +#ifndef REGIONMAPEDITOR_H +#define REGIONMAPEDITOR_H + +#include "regionmappixmapitem.h" +#include "citymappixmapitem.h" +#include "regionmaplayoutpixmapitem.h" +#include "regionmap.h" + +#include +#include +#include + +namespace Ui { +class RegionMapEditor; +} + +class RegionMapEditor : public QMainWindow +{ + Q_OBJECT + +public: + explicit RegionMapEditor(QWidget *parent = 0, Project *pro = nullptr); + ~RegionMapEditor(); + +// TODO: make members that are not called outside of this private + RegionMap *region_map; + + 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 loadRegionMapData(); + void displayRegionMap(); + void displayRegionMapImage(); + void displayRegionMapLayout(); + void displayRegionMapLayoutOptions(); + void updateRegionMapLayoutOptions(int); + void displayRegionMapTileSelector(); + void displayCityMapTileSelector(); + void displayCityMap(QString); + void loadCityMaps(); + + void onRegionMapTileSelectorSelectedTileChanged(); + void onRegionMapTileSelectorHoveredTileChanged(unsigned); + void onRegionMapTileSelectorHoveredTileCleared(); + + void onRegionMapLayoutSelectedTileChanged(int); + void onRegionMapLayoutHoveredTileChanged(int); + void onRegionMapLayoutHoveredTileCleared(); + +private: + Ui::RegionMapEditor *ui; + Project *project; + + QString rmStatusbarMessage; + +private slots: + void on_action_RegionMap_Save_triggered(); + void on_tabWidget_Region_Map_currentChanged(int); + void on_pushButton_RM_Options_save_clicked(); + void on_pushButton_CityMap_save_clicked(); + void on_comboBox_CityMap_picker_currentTextChanged(const QString &); + void onHoveredRegionMapTileChanged(int, int); + void onHoveredRegionMapTileCleared(); + void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); + void mouseEvent_city_map(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); +}; + +#endif // REGIONMAPEDITOR_H diff --git a/include/ui/regionmaplayoutpixmapitem.h b/include/ui/regionmaplayoutpixmapitem.h index 1fca1581..8031f475 100644 --- a/include/ui/regionmaplayoutpixmapitem.h +++ b/include/ui/regionmaplayoutpixmapitem.h @@ -2,8 +2,7 @@ #define REGIONMAPLAYOUTPIXMAPITEM_H #include "tilemaptileselector.h" -//#include "regionmappixmapitem.h" -#include "regionmapeditor.h" +#include "regionmap.h" class RegionMapLayoutPixmapItem : public SelectablePixmapItem { Q_OBJECT diff --git a/include/ui/regionmappixmapitem.h b/include/ui/regionmappixmapitem.h index 12de3385..0fc11b8c 100644 --- a/include/ui/regionmappixmapitem.h +++ b/include/ui/regionmappixmapitem.h @@ -1,7 +1,7 @@ #ifndef REGIONMAPPIXMAPITEM_H #define REGIONMAPPIXMAPITEM_H -#include "regionmapeditor.h" +#include "regionmap.h" #include "tilemaptileselector.h" #include diff --git a/porymap.pro b/porymap.pro index e7d84510..d2101b98 100644 --- a/porymap.pro +++ b/porymap.pro @@ -29,6 +29,7 @@ SOURCES += src/core/block.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ src/core/regionmapeditor.cpp \ + src/core/regionmap.cpp \ src/ui/aboutporymap.cpp \ src/ui/bordermetatilespixmapitem.cpp \ src/ui/collisionpixmapitem.cpp \ @@ -58,6 +59,7 @@ SOURCES += src/core/block.cpp \ src/ui/tileseteditormetatileselector.cpp \ src/ui/tileseteditortileselector.cpp \ src/ui/tilemaptileselector.cpp \ + src/ui/regionmapeditor.cpp \ src/ui/newmappopup.cpp \ src/config.cpp \ src/editor.cpp \ @@ -84,7 +86,8 @@ HEADERS += include/core/block.h \ include/core/tile.h \ include/core/tileset.h \ include/core/regionmapeditor.h \ - include/ui/aboutporymap.h \ + include/core/regionmap.h \ + include/ui/aboutporymap.h \ include/ui/bordermetatilespixmapitem.h \ include/ui/collisionpixmapitem.h \ include/ui/connectionpixmapitem.h \ @@ -113,6 +116,7 @@ HEADERS += include/core/block.h \ include/ui/tileseteditormetatileselector.h \ include/ui/tileseteditortileselector.h \ include/ui/tilemaptileselector.h \ + include/ui/regionmapeditor.h \ include/ui/newmappopup.h \ include/config.h \ include/editor.h \ @@ -125,6 +129,7 @@ FORMS += forms/mainwindow.ui \ forms/eventpropertiesframe.ui \ forms/tileseteditor.ui \ forms/paletteeditor.ui \ + forms/regionmapeditor.ui \ forms/newmappopup.ui \ forms/aboutporymap.ui diff --git a/src/core/regionmapeditor.cpp b/src/core/regionmap.cpp similarity index 99% rename from src/core/regionmapeditor.cpp rename to src/core/regionmap.cpp index a4912852..061f1550 100644 --- a/src/core/regionmapeditor.cpp +++ b/src/core/regionmap.cpp @@ -1,4 +1,4 @@ -#include "regionmapeditor.h" +#include "regionmap.h" #include #include @@ -108,13 +108,6 @@ void RegionMap::init(Project *pro) { readBkgImgBin(); readLayout(); readCityMaps(); - - //tryGetMap(); - - //saveBkgImgBin(); - //saveLayout(); - - //test(); } // as of now, this needs to be called first because it initializes all the diff --git a/src/editor.cpp b/src/editor.cpp index 2de3a84b..692370e0 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -20,17 +20,12 @@ Editor::Editor(Ui::MainWindow* ui) this->settings = new Settings(); this->playerViewRect = new MovableRect(&this->settings->playerViewRectEnabled, 30 * 8, 20 * 8, qRgb(255, 255, 255)); this->cursorMapTileRect = new CursorTileRect(&this->settings->cursorTileRectEnabled, qRgb(255, 255, 255)); - this->region_map = new RegionMap;// TODO: why is this here? } void Editor::saveProject() { if (project) { project->saveAllMaps(); project->saveAllDataStructures(); - if (region_map) { - region_map->save(); - displayRegionMap(); - } } } @@ -39,10 +34,6 @@ void Editor::save() { project->saveMap(map); project->saveAllDataStructures(); } - if (project && region_map) { - region_map->save(); - displayRegionMap(); - } } void Editor::undo() { @@ -456,17 +447,6 @@ bool Editor::setMap(QString map_name) { selected_events->clear(); displayMap(); updateSelectedEvents(); - - if (region_map) { - for (auto square : region_map->map_squares) { - if (square.mapsec == map->location) { - int img_x = square.x + 1; - int img_y = square.y + 2; - this->region_map_layout_item->select(img_x, img_y); - break; - } - } - } } return true; @@ -616,17 +596,11 @@ void Editor::displayMap() { displayMapBorder(); displayMapGrid(); -<<<<<<< abc873464dc736dcfdda4a20df61516f45478844 this->playerViewRect->setZValue(1000); this->cursorMapTileRect->setZValue(1001); scene->addItem(this->playerViewRect); scene->addItem(this->cursorMapTileRect); - displayRegionMapTileSelector();//? - displayRegionMap(); - -======= ->>>>>>> add layout view to region map editor if (map_item) { map_item->setVisible(false); } @@ -638,14 +612,6 @@ void Editor::displayMap() { } } -void Editor::displayRegionMap() { - displayRegionMapTileSelector(); - displayCityMapTileSelector(); - displayRegionMapImage(); - displayRegionMapLayout(); - displayRegionMapLayoutOptions(); -} - void Editor::displayMetatileSelector() { if (metatile_selector_item && metatile_selector_item->scene()) { metatile_selector_item->scene()->removeItem(metatile_selector_item); @@ -1296,264 +1262,6 @@ 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); - displayRegionMap(); -} - -// TODO: get this to display on a decent scale -void Editor::displayRegionMapTileSelector() { - // - this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_png_path)); - this->mapsquare_selector_item->draw(); - - this->scene_region_map_tiles = new QGraphicsScene; - this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); - - connect(this->mapsquare_selector_item, &TilemapTileSelector::selectedTileChanged, - this, &Editor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? - connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileChanged, - this, &Editor::onRegionMapTileSelectorHoveredTileChanged); - connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileCleared, - this, &Editor::onRegionMapTileSelectorHoveredTileCleared); - - this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth + 2, - this->mapsquare_selector_item->pixelHeight + 2); -} - -void Editor::displayCityMapTileSelector() { - // city_map_selector_item - this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_city_map_tiles_path)); - this->city_map_selector_item->draw(); - - this->scene_city_map_tiles = new QGraphicsScene; - this->scene_city_map_tiles->addItem(this->city_map_selector_item); - - /*connect(this->city_map_selector_item, &TilemapTileSelector::selectedTileChanged, - this, &Editor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? - connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, - this, &Editor::onRegionMapTileSelectorHoveredTileChanged); - connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileCleared, - this, &Editor::onRegionMapTileSelectorHoveredTileCleared);*/ - - this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth + 2, - this->city_map_selector_item->pixelHeight + 2); -} - -// TODO: change the signal slot to new syntax -// TODO: add scalability? -void Editor::displayRegionMapImage() { - // - this->region_map_item = new RegionMapPixmapItem(this->region_map, this->mapsquare_selector_item); - connect(region_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*)), - this, SLOT(mouseEvent_region_map(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*))); - connect(region_map_item, SIGNAL(hoveredRegionMapTileChanged(int, int)), - this, SLOT(onHoveredRegionMapTileChanged(int, int))); - connect(region_map_item, SIGNAL(hoveredRegionMapTileCleared()), - this, SLOT(onHoveredRegionMapTileCleared())); - this->region_map_item->draw(); - - this->scene_region_map_image = new QGraphicsScene; - this->scene_region_map_image->addItem(this->region_map_item); - this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); - - this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); - 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); - //* - connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::selectedTileChanged, - this, &Editor::onRegionMapLayoutSelectedTileChanged);// TODO: remove this? - connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileChanged, - this, &Editor::onRegionMapLayoutHoveredTileChanged); - connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileCleared, - this, &Editor::onRegionMapLayoutHoveredTileCleared); - //*/ - this->region_map_layout_item->draw(); - this->region_map_layout_item->setDefaultSelection(); - - this->scene_region_map_layout = new QGraphicsScene; - this->scene_region_map_layout->addItem(region_map_layout_item); - this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); - - this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize()); -} - -void Editor::displayRegionMapLayoutOptions() { - // - this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); - - this->ui->frame_RM_Options->setEnabled(true); - - // TODO: change these values to variables - this->ui->spinBox_RM_Options_x->setMaximum(27); - this->ui->spinBox_RM_Options_y->setMaximum(14); - - updateRegionMapLayoutOptions(65); -} - -void Editor::updateRegionMapLayoutOptions(int index) { - // - this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec));//this->region_map->map_squares[index].map_name); - this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); - this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); - this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); -} - -void Editor::onRegionMapTileSelectorSelectedTileChanged() { - // -} - -void Editor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { - QString message = QString("Tile: 0x") + QString("%1").arg(tileId, 4, 16, QChar('0')).toUpper(); - this->ui->statusBar->showMessage(message); -} - -void Editor::onRegionMapTileSelectorHoveredTileCleared() { - // - //QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->region_map_layout_item->selectedTile, 4, 16, QChar('0')).toUpper(); - //this->ui->statusBar->showMessage(message); -} - -void Editor::onRegionMapLayoutSelectedTileChanged(int index) { - // - QString message = QString(); - if (this->region_map->map_squares[index].has_map) { - // - message = QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");//.remove("{NAME_END}") - } - this->ui->statusBar->showMessage(message); - - updateRegionMapLayoutOptions(index); -} - -void Editor::onRegionMapLayoutHoveredTileChanged(int index) { - // TODO: change to x, y coords not index - QString message = QString(); - int x = this->region_map->map_squares[index].x; - int y = this->region_map->map_squares[index].y; - if (x >= 0 && y >= 0) { - message = QString("(%1, %2)").arg(x).arg(y); - if (this->region_map->map_squares[index].has_map) { - // - message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); - } - } - this->ui->statusBar->showMessage(message); -} - -void Editor::onRegionMapLayoutHoveredTileCleared() { - // - int index = this->region_map_layout_item->selectedTile; - QString message = QString(); - int x = this->region_map->map_squares[index].x; - int y = this->region_map->map_squares[index].y; - if (x >= 0 && y >= 0) { - message = QString("(%1, %2)").arg(x).arg(y); - if (this->region_map->map_squares[index].has_map) { - // - message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); - } - } - this->ui->statusBar->showMessage(message); -} - -void Editor::onHoveredRegionMapTileChanged(int x, int y) { - rmStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); - this->ui->statusBar->showMessage(rmStatusbarMessage); -} - -void Editor::onHoveredRegionMapTileCleared() { - this->ui->statusBar->clearMessage(); -} - -void Editor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *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); - } -} - -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 7e236504..0c3d3304 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -263,14 +263,11 @@ bool MainWindow::openProject(QString dir) { setWindowTitle(editor->project->getProjectTitle()); loadDataStructures(); populateMapList(); - editor->loadRegionMapData(); - editor->loadCityMaps(); success = setMap(getDefaultMap(), true); } else { setWindowTitle(editor->project->getProjectTitle()); loadDataStructures(); populateMapList(); - editor->loadRegionMapData(); } if (success) { @@ -599,11 +596,6 @@ void MainWindow::on_checkBox_AllowEscapeRope_clicked(bool checked) } } -void MainWindow::on_tabWidget_Region_Map_currentChanged(int index) { - // - ui->stackedWidget_RM_Options->setCurrentIndex(index); -} - void MainWindow::loadDataStructures() { Project *project = editor->project; project->readMapLayouts(); @@ -2004,8 +1996,22 @@ 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::on_actionRegion_Map_Editor_triggered() { + if (!this->regionMapEditor) { + this->regionMapEditor = new RegionMapEditor(this, this->editor->project); + this->regionMapEditor->loadRegionMapData(); + this->regionMapEditor->loadCityMaps(); + connect(this->regionMapEditor, &QObject::destroyed, [=](QObject *) { this->regionMapEditor = nullptr; }); + this->regionMapEditor->setAttribute(Qt::WA_DeleteOnClose); + } + + if (!this->regionMapEditor->isVisible()) { + this->regionMapEditor->show(); + } else if (this->regionMapEditor->isMinimized()) { + this->regionMapEditor->showNormal(); + } else { + this->regionMapEditor->activateWindow(); + } } void MainWindow::closeEvent(QCloseEvent *event) { diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp new file mode 100644 index 00000000..f3298832 --- /dev/null +++ b/src/ui/regionmapeditor.cpp @@ -0,0 +1,353 @@ +#include "regionmapeditor.h" +#include "ui_regionmapeditor.h" + +#include + +RegionMapEditor::RegionMapEditor(QWidget *parent, Project *pro) : + QMainWindow(parent), + ui(new Ui::RegionMapEditor) +{ + ui->setupUi(this); + this->project = pro; + this->region_map = new RegionMap; +} + +RegionMapEditor::~RegionMapEditor() +{ + delete ui; +} + + + +void RegionMapEditor::on_action_RegionMap_Save_triggered() { + qDebug() << "Region Map Save Triggered"; + if (project && region_map) { + qDebug() << "actually saving"; + region_map->save(); + displayRegionMap(); + } +} + + +void RegionMapEditor::loadRegionMapData() { + // + this->region_map->init(project); + displayRegionMap(); +} + +void RegionMapEditor::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 RegionMapEditor::displayRegionMap() { + displayRegionMapTileSelector(); + displayCityMapTileSelector(); + displayRegionMapImage(); + displayRegionMapLayout(); + displayRegionMapLayoutOptions(); +} + +// TODO: change the signal slot to new syntax +// TODO: add scalability? +void RegionMapEditor::displayRegionMapImage() { + // + this->region_map_item = new RegionMapPixmapItem(this->region_map, this->mapsquare_selector_item); + connect(region_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*)), + this, SLOT(mouseEvent_region_map(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*))); + connect(region_map_item, SIGNAL(hoveredRegionMapTileChanged(int, int)), + this, SLOT(onHoveredRegionMapTileChanged(int, int))); + connect(region_map_item, SIGNAL(hoveredRegionMapTileCleared()), + this, SLOT(onHoveredRegionMapTileCleared())); + this->region_map_item->draw(); + + this->scene_region_map_image = new QGraphicsScene; + this->scene_region_map_image->addItem(this->region_map_item); + this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); + + this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize()); +} + +// TODO: add if (item) and if(scene) checks because called more than once per instance +void RegionMapEditor::displayRegionMapLayout() { + // + this->region_map_layout_item = new RegionMapLayoutPixmapItem(this->region_map, this->mapsquare_selector_item); + //* + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::selectedTileChanged, + this, &RegionMapEditor::onRegionMapLayoutSelectedTileChanged);// TODO: remove this? + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileChanged, + this, &RegionMapEditor::onRegionMapLayoutHoveredTileChanged); + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileCleared, + this, &RegionMapEditor::onRegionMapLayoutHoveredTileCleared); + //*/ + this->region_map_layout_item->draw(); + this->region_map_layout_item->setDefaultSelection(); + + this->scene_region_map_layout = new QGraphicsScene; + this->scene_region_map_layout->addItem(region_map_layout_item); + this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); + + this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize()); +} + +void RegionMapEditor::displayRegionMapLayoutOptions() { + // + this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); + + this->ui->frame_RM_Options->setEnabled(true); + + // TODO: change these values to variables + this->ui->spinBox_RM_Options_x->setMaximum(27); + this->ui->spinBox_RM_Options_y->setMaximum(14); + + updateRegionMapLayoutOptions(65); +} + +void RegionMapEditor::updateRegionMapLayoutOptions(int index) { + // + this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec));//this->region_map->map_squares[index].map_name); + this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); + this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); + this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); +} + +// TODO: get this to display on a decent scale +void RegionMapEditor::displayRegionMapTileSelector() { + // + this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_png_path)); + this->mapsquare_selector_item->draw(); + + this->scene_region_map_tiles = new QGraphicsScene; + this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); + + connect(this->mapsquare_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? + connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileChanged, + this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); + connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileCleared, + this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared); + + this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth + 2, + this->mapsquare_selector_item->pixelHeight + 2); +} + +void RegionMapEditor::displayCityMapTileSelector() { + // city_map_selector_item + this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_city_map_tiles_path)); + this->city_map_selector_item->draw(); + + this->scene_city_map_tiles = new QGraphicsScene; + this->scene_city_map_tiles->addItem(this->city_map_selector_item); + + /*connect(this->city_map_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? + connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, + this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); + connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileCleared, + this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared);*/ + + this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth + 2, + this->city_map_selector_item->pixelHeight + 2); +} + +void RegionMapEditor::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? +} + + + + +//// + +void RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged() { + // +} + +void RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { + QString message = QString("Tile: 0x") + QString("%1").arg(tileId, 4, 16, QChar('0')).toUpper(); + this->ui->statusbar->showMessage(message); +} + +void RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared() { + // + //QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->region_map_layout_item->selectedTile, 4, 16, QChar('0')).toUpper(); + //this->ui->statusbar->showMessage(message); +} + +void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { + // + QString message = QString(); + if (this->region_map->map_squares[index].has_map) { + // + message = QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");//.remove("{NAME_END}") + } + this->ui->statusbar->showMessage(message); + + updateRegionMapLayoutOptions(index); +} + +void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { + // TODO: change to x, y coords not index + QString message = QString(); + int x = this->region_map->map_squares[index].x; + int y = this->region_map->map_squares[index].y; + if (x >= 0 && y >= 0) { + message = QString("(%1, %2)").arg(x).arg(y); + if (this->region_map->map_squares[index].has_map) { + // + message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); + } + } + this->ui->statusbar->showMessage(message); +} + +void RegionMapEditor::onRegionMapLayoutHoveredTileCleared() { + // + int index = this->region_map_layout_item->selectedTile; + QString message = QString(); + int x = this->region_map->map_squares[index].x; + int y = this->region_map->map_squares[index].y; + if (x >= 0 && y >= 0) { + message = QString("(%1, %2)").arg(x).arg(y); + if (this->region_map->map_squares[index].has_map) { + // + message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); + } + } + this->ui->statusbar->showMessage(message); +} + +void RegionMapEditor::onHoveredRegionMapTileChanged(int x, int y) { + rmStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); + this->ui->statusbar->showMessage(rmStatusbarMessage); +} + +void RegionMapEditor::onHoveredRegionMapTileCleared() { + this->ui->statusbar->clearMessage(); +} + +void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *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); + } +} + +void RegionMapEditor::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); + } +} + + + + + + + + +//// + +void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { + // + this->ui->stackedWidget_RM_Options->setCurrentIndex(index); +} + +void RegionMapEditor::on_pushButton_RM_Options_save_clicked() { + // + this->region_map->saveOptions( + // + this->region_map_layout_item->selectedTile, + this->ui->comboBox_RM_ConnectedMap->currentText(), + this->ui->lineEdit_RM_MapName->text(), + this->ui->spinBox_RM_Options_x->value(), + this->ui->spinBox_RM_Options_y->value() + ); + this->region_map_layout_item->draw(); +} + +void RegionMapEditor::on_pushButton_CityMap_save_clicked() { + this->city_map_item->save(); +} + +void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { + this->displayCityMap(file); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 83f3cef52e5a97e6fca5240e63ebc5dbf64fc726 Mon Sep 17 00:00:00 2001 From: garak Date: Tue, 8 Jan 2019 21:03:54 -0500 Subject: [PATCH 06/17] clean rme UI, undo / redo history, new city maps --- forms/regionmapeditor.ui | 1717 ++++++++++++++++++-------------- include/core/historyitem.h | 15 + include/core/regionmap.h | 7 + include/ui/citymappixmapitem.h | 6 + include/ui/regionmapeditor.h | 30 + src/core/historyitem.cpp | 9 + src/core/map.cpp | 1 - src/ui/citymappixmapitem.cpp | 9 +- src/ui/regionmapeditor.cpp | 217 +++- src/ui/regionmappixmapitem.cpp | 4 +- 10 files changed, 1230 insertions(+), 785 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 5b9e193d..932a8d0d 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -6,706 +6,327 @@ 0 0 - 800 - 600 + 709 + 619 - MainWindow + Region Map Editor - + - 450 + 10 250 - 301 - 181 + 20 + 20 - - - 0 - 0 - + + + - - Qt::ScrollBarAlwaysOn + + + + + 30 + 250 + 20 + 20 + - - Qt::ScrollBarAsNeeded + + - - - QAbstractScrollArea::AdjustIgnored + + + + + 10 + 280 + 691 + 271 + - - true + + Qt::Horizontal - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 283 - 179 - - + + + + QLayout::SetDefaultConstraint + + + + + + + City Map: + + + + + + + + + + + 0 + 0 + + + + <html><head/><body><p>Add a new city map.</p></body></html> + + + + + + + :/icons/add.ico + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + save + + + + + + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 420 + 225 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + + 0 + 0 + + + + false + + + false + + + QAbstractScrollArea::AdjustIgnored + + + QGraphicsView::NoDrag + + + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + + + + 0 0 - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - 19 - 9 - 321 - 221 - - - - 0 - - - - Background Image - - - - - -10 - -10 - 331 - 211 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 329 - 209 - - - - - 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 - - - - - - - - - - - Map Layout - - - - - -10 - -10 - 331 - 211 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 329 - 209 - - - - - 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 - - - - - - - - - - - - - 440 - 20 - 351 - 221 - - - - 0 - - - - - - 30 - 20 - 281 - 171 - - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOn - - - Qt::ScrollBarAsNeeded - - - QAbstractScrollArea::AdjustIgnored - - - true - - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 263 - 169 - - - - - 0 - 0 - - - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + - false + true - 10 + 8 0 - 331 - 211 + 242 + 269 - + 0 0 - - QFrame::StyledPanel - - - QFrame::Raised - - - - - 210 - 170 - 107 - 32 - + + + QLayout::SetDefaultConstraint - - Set Values + + 0 - - - - - 13 - 11 - 301 - 111 - + + 0 - - - - - Map Section - - - - - - - <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> - - - true - - - - - - - Map Name - - - - - - - - - - City Map - - - - - - - <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> - - - true - - - - - - - - - 20 - 130 - 138 - 26 - + + 0 - - - - - x - - - - - - - - - - y - - - - - - - - - - - - - - - 10 - 250 - 341 - 281 - - - - - - - - - City Map: + + 0 + + + 0 + + + + + Qt::Vertical + + + QSizePolicy::Preferred + + + + 20 + 40 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored - - - - - + + Qt::Horizontal @@ -717,126 +338,670 @@ - - - - save + + + + Qt::Horizontal - + + + 40 + 20 + + + + + + + + + + 10 + 550 + 20 + 20 + + + + + 0 + 0 + + + + + + + + + + + 30 + 550 + 20 + 20 + + + + + 0 + 0 + + + + - + + + + + + 681 + 550 + 20 + 20 + + + + + 0 + 0 + + + + - + + + + + + 661 + 550 + 20 + 20 + + + + + 0 + 0 + + + + + + + + + + + 661 + 250 + 20 + 20 + + + + + + + + + + + 681 + 250 + 20 + 20 + + + + - + + + + + + 10 + 10 + 691 + 241 + + + + + + + 0 + + + + Background Image + + + + + -1 + -1 + 341 + 213 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 339 + 211 + + + + + 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 + + + + + + + + + + + Map Layout + + + + + -1 + -1 + 341 + 213 + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 339 + 211 + + + + + 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 + + + + + + + + + - - - - 1 - 0 - + + + 1 - - true - - - - - 0 - 0 - 337 - 235 - - - - - 0 + + + + + 0 + 10 + 342 + 230 + - - 0 + + + 0 + 0 + - - 0 + + Qt::ScrollBarAlwaysOn - - 0 + + Qt::ScrollBarAsNeeded - - 0 + + QAbstractScrollArea::AdjustIgnored - - - - - 0 - 0 - + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 324 + 228 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint - - false + + 0 - - false + + 0 - - QAbstractScrollArea::AdjustIgnored + + 0 - - QGraphicsView::NoDrag + + 0 - - - - - - Qt::Horizontal + + 0 - - - 166 - 16 - - - - - - - - Qt::Vertical - - - - 16 - 166 - - - - - - - - Qt::Horizontal - - - - 166 - 16 - - - - - - - - Qt::Vertical - - - - 16 - 166 - - - - - + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + false + + + + 0 + 10 + 342 + 230 + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + 10 + 170 + 107 + 32 + + + + Set Values + + + + + + 13 + 11 + 301 + 111 + + + + + + + Map Section + + + + + + + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> + + + true + + + + + + + Map Name + + + + + + + + + + City Map + + + + + + + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> + + + true + + + + + + + + + 20 + 130 + 138 + 26 + + + + + + + x + + + + + + + + + + y + + + + + + + + + + + + 210 + 170 + 113 + 32 + + + + Delete Square + + + @@ -848,7 +1013,7 @@ 0 0 - 800 + 709 22 @@ -858,7 +1023,15 @@ + + + Edit + + + + + @@ -869,6 +1042,22 @@ Ctrl+S + + + Undo + + + Ctrl+Z + + + + + Redo + + + Ctrl+Y + + diff --git a/include/core/historyitem.h b/include/core/historyitem.h index d56392d2..6ccda292 100644 --- a/include/core/historyitem.h +++ b/include/core/historyitem.h @@ -12,4 +12,19 @@ public: ~HistoryItem(); }; +enum RegionMapEditorBox { + BackgroundImage = 1, + CityMapImage = 2, +}; + +class RegionMapHistoryItem { +public: + int which;// region map or city map + int index; + unsigned tile; + unsigned prev; + RegionMapHistoryItem(int type, int index, unsigned prev, unsigned tile); + ~RegionMapHistoryItem(); +}; + #endif // HISTORYITEM_H diff --git a/include/core/regionmap.h b/include/core/regionmap.h index a16e77f1..7584a41d 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -4,6 +4,8 @@ #include "project.h" #include "map.h" #include "tilemaptileselector.h" +#include "history.h" +#include "historyitem.h" #include #include @@ -73,6 +75,8 @@ public: //RegionMapSquare *map_squares = nullptr;// array of RegionMapSquares QList map_squares; + History history; + QString temp_path;// delete this QString city_map_squares_path; QString region_map_png_path; @@ -125,6 +129,8 @@ public: unsigned getTileId(int, int); int getMapSquareIndex(int, int); + void deleteLayoutSquare(int); + // implement these here? void undo(); void redo(); @@ -134,6 +140,7 @@ public: // TODO: move read / write functions to private (and others) private: // + //History> *history;// (index, tile) int layout_width_; int layout_height_; int img_width_; diff --git a/include/ui/citymappixmapitem.h b/include/ui/citymappixmapitem.h index eefafd22..8162bb04 100644 --- a/include/ui/citymappixmapitem.h +++ b/include/ui/citymappixmapitem.h @@ -18,6 +18,7 @@ public: QString file; + // TODO: make private and use access functions int width; int height; @@ -28,6 +29,11 @@ public: void create(QString); virtual void paint(QGraphicsSceneMouseEvent*); virtual void draw(); + int getIndexAt(int, int); + +//private: +// int width; +// int height; signals: void mouseEvent(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index 62bef62d..ffc2bd3d 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -5,6 +5,8 @@ #include "citymappixmapitem.h" #include "regionmaplayoutpixmapitem.h" #include "regionmap.h" +#include "history.h" +#include "historyitem.h" #include #include @@ -55,17 +57,45 @@ public: void onRegionMapLayoutHoveredTileChanged(int); void onRegionMapLayoutHoveredTileCleared(); + void undo(); + void redo(); + private: Ui::RegionMapEditor *ui; Project *project; QString rmStatusbarMessage; + History history; + + double scaleUpFactor = 2.0; + double scaleDownFactor = 1.0 / scaleUpFactor; + + double scaleRegionMapTiles = 1.0; + double scaleRegionMapImage = 1.0; + double scaleCityMapTiles = 1.0; + double scaleCityMapImage = 1.0; + + void scaleUp(QGraphicsView *, qreal factor, qreal width, qreal height); + + bool createCityMap(QString); private slots: void on_action_RegionMap_Save_triggered(); + void on_action_RegionMap_Undo_triggered(); + void on_action_RegionMap_Redo_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_save_clicked(); + void on_pushButton_RM_Options_delete_clicked(); void on_pushButton_CityMap_save_clicked(); + void on_pushButton_CityMap_add_clicked();// + void on_pushButton_Zoom_In_Image_Tiles_clicked(); + void on_pushButton_Zoom_Out_Image_Tiles_clicked(); + void on_pushButton_Zoom_In_City_Tiles_clicked(); + void on_pushButton_Zoom_Out_City_Tiles_clicked(); + void on_pushButton_Zoom_In_City_Map_clicked();// + void on_pushButton_Zoom_Out_City_Map_clicked(); + void on_pushButton_Zoom_In_Map_Image_clicked(); + void on_pushButton_Zoom_Out_Map_Image_clicked();// void on_comboBox_CityMap_picker_currentTextChanged(const QString &); void onHoveredRegionMapTileChanged(int, int); void onHoveredRegionMapTileCleared(); diff --git a/src/core/historyitem.cpp b/src/core/historyitem.cpp index ded89353..1a8c44d2 100644 --- a/src/core/historyitem.cpp +++ b/src/core/historyitem.cpp @@ -9,3 +9,12 @@ HistoryItem::HistoryItem(Blockdata *metatiles, int layoutWidth, int layoutHeight HistoryItem::~HistoryItem() { if (this->metatiles) delete this->metatiles; } + +RegionMapHistoryItem::RegionMapHistoryItem(int which_, int index_, unsigned prev_, unsigned tile_) { + this->which = which_; + this->index = index_; + this->prev = prev_; + this->tile = tile_; +} + +RegionMapHistoryItem::~RegionMapHistoryItem() {} diff --git a/src/core/map.cpp b/src/core/map.cpp index e31825b3..d52c4771 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -342,7 +342,6 @@ void Map::_floodFillCollisionElevation(int x, int y, uint16_t collision, uint16_ } } - void Map::undo() { HistoryItem *commit = metatileHistory.back(); if (!commit) diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index 39ab54cc..f7f47b5e 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -37,7 +37,7 @@ void CityMapPixmapItem::draw() { } void CityMapPixmapItem::save() { - // + // TODO: logError / logWarn if fail QFile binFile(file); if (!binFile.open(QIODevice::WriteOnly)) return; binFile.write(data); @@ -49,7 +49,7 @@ 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); + int index = getIndexAt(x, y); data[index] = static_cast(this->tile_selector->selectedTile); draw(); @@ -61,3 +61,8 @@ void CityMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { int y = static_cast(pos.y()) / 8; emit mouseEvent(event, this); } + +int CityMapPixmapItem::getIndexAt(int x, int y) { + // + return 2 * (x + y * width); +} diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index f3298832..2f0303fa 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -2,14 +2,21 @@ #include "ui_regionmapeditor.h" #include +#include +#include +#include +#include +#include +#include RegionMapEditor::RegionMapEditor(QWidget *parent, Project *pro) : QMainWindow(parent), ui(new Ui::RegionMapEditor) { - ui->setupUi(this); + this->ui->setupUi(this); this->project = pro; this->region_map = new RegionMap; + this->setFixedSize(this->size());//statusbar->setSizeGripEnabled(false); } RegionMapEditor::~RegionMapEditor() @@ -157,7 +164,8 @@ void RegionMapEditor::displayCityMapTileSelector() { this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth + 2, - this->city_map_selector_item->pixelHeight + 2); + this->city_map_selector_item->pixelHeight + 2); + //this->ui->graphicsView_City_Map_Tiles->scale(2,2); } void RegionMapEditor::displayCityMap(QString f) { @@ -182,10 +190,39 @@ void RegionMapEditor::displayCityMap(QString f) { 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? + this->ui->graphicsView_City_Map->setFixedSize(QSize(8 * city_map_item->width + 2, 8 * city_map_item->height + 2)); } +bool RegionMapEditor::createCityMap(QString name) { + // + bool errored = false; + + QString file = this->project->root + "/graphics/pokenav/city_maps/" + name + ".bin"; + + uint8_t filler = 0x30; + uint8_t border = 0x7; + uint8_t blank = 0x1; + QByteArray new_data(400, filler); + for (int i = 0; i < new_data.size(); i++) { + if (i % 2) continue; + int x = i % 20; + int y = i / 20; + if (y <= 1 || y >= 8 || x <= 3 || x >= 16) + new_data[i] = border; + else + new_data[i] = blank; + } + + QFile binFile(file); + if (!binFile.open(QIODevice::WriteOnly)) errored = true; + binFile.write(new_data); + binFile.close(); + + loadCityMaps(); + this->ui->comboBox_CityMap_picker->setCurrentText(name); + + return !errored; +} @@ -211,7 +248,7 @@ void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { QString message = QString(); if (this->region_map->map_squares[index].has_map) { // - message = QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + message = QString("\t %1").arg(this->project->mapSecToMapHoverName->value( this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");//.remove("{NAME_END}") } this->ui->statusbar->showMessage(message); @@ -228,7 +265,7 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { message = QString("(%1, %2)").arg(x).arg(y); if (this->region_map->map_squares[index].has_map) { // - message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + message += QString("\t %1").arg(this->project->mapSecToMapHoverName->value( this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); } } @@ -245,7 +282,7 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileCleared() { message = QString("(%1, %2)").arg(x).arg(y); if (this->region_map->map_squares[index].has_map) { // - message += QString("Map: %1").arg(this->project->mapSecToMapHoverName->value( + message += QString("\t %1").arg(this->project->mapSecToMapHoverName->value( this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); } } @@ -270,6 +307,13 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg // TODO: add functionality here? replace or? } else { // + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = this->region_map->getMapSquareIndex(x, y); + RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, index, + this->region_map->map_squares[index].tile_img_id, this->mapsquare_selector_item->getSelectedTile()); + history.push(commit); item->paint(event); } } @@ -283,6 +327,13 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM // TODO: add functionality here? replace or? } else { // + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = this->city_map_item->getIndexAt(x, y); + RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::CityMapImage, index, + this->city_map_item->data[index], this->city_map_selector_item->getSelectedTile()); + history.push(commit); item->paint(event); } } @@ -318,12 +369,154 @@ void RegionMapEditor::on_pushButton_CityMap_save_clicked() { this->city_map_item->save(); } +void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() { + qDebug() << "delete it fat"; +} + +void RegionMapEditor::on_pushButton_CityMap_add_clicked() { + QDialog popup(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + popup.setWindowTitle("New City Map"); + popup.setWindowModality(Qt::NonModal); + + QFormLayout form(&popup); + + QLineEdit *input = new QLineEdit(); + form.addRow(new QLabel("Name:"), input); + + QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup); + + QString name; + + form.addRow(&buttonBox); + connect(&buttonBox, SIGNAL(rejected()), &popup, SLOT(reject())); + connect(&buttonBox, &QDialogButtonBox::accepted, [&popup, &input, &name](){ + name = input->text().remove(QRegularExpression("[^a-zA-Z0-9_]+")); + if (!name.isEmpty()) + popup.accept(); + }); + + if (popup.exec() == QDialog::Accepted) { + createCityMap(name); + } +} + +void RegionMapEditor::on_action_RegionMap_Undo_triggered() { + RegionMapHistoryItem *commit = history.current(); + if (!commit) return; + + uint8_t tile = static_cast(commit->prev); + + history.back(); + + switch (commit->which) + { + case RegionMapEditorBox::BackgroundImage: + history.back();// TODO: why do I need to do this? + this->region_map->map_squares[commit->index].tile_img_id = tile; + this->region_map_item->draw(); + break; + case RegionMapEditorBox::CityMapImage: + this->city_map_item->data[commit->index] = tile; + this->city_map_item->draw(); + break; + } +} + +void RegionMapEditor::on_action_RegionMap_Redo_triggered() { + RegionMapHistoryItem *commit = history.next(); + if (!commit) return; + + uint8_t tile = static_cast(commit->tile); + + switch (commit->which) + { + case RegionMapEditorBox::BackgroundImage: + history.next();// TODO: why do I need to do this? + this->region_map->map_squares[commit->index].tile_img_id = tile; + this->region_map_item->draw(); + break; + case RegionMapEditorBox::CityMapImage: + this->city_map_item->data[commit->index] = tile; + this->city_map_item->draw(); + break; + } +} + void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); } +// TODO: prevent huge images with limits on zoom +void RegionMapEditor::on_pushButton_Zoom_In_Image_Tiles_clicked() { + // + scaleRegionMapTiles *= 2.0; + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, + this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); + this->ui->graphicsView_RegionMap_Tiles->scale(2.0, 2.0); +} + +void RegionMapEditor::on_pushButton_Zoom_Out_Image_Tiles_clicked() { + // + scaleRegionMapTiles /= 2.0; + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, + this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); + this->ui->graphicsView_RegionMap_Tiles->scale(0.5, 0.5); +} + +void RegionMapEditor::on_pushButton_Zoom_In_City_Tiles_clicked() { + // + scaleCityMapTiles *= 2.0; + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, + this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); + this->ui->graphicsView_City_Map_Tiles->scale(2.0,2.0); +} + +void RegionMapEditor::on_pushButton_Zoom_Out_City_Tiles_clicked() { + // + scaleCityMapTiles /= 2.0; + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, + this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); + this->ui->graphicsView_City_Map_Tiles->scale(0.5,0.5); +} + +void RegionMapEditor::on_pushButton_Zoom_In_City_Map_clicked() { + // + scaleCityMapImage *= 2.0; + this->ui->graphicsView_City_Map->setFixedSize(QSize(8 * city_map_item->width * scaleCityMapImage + 2, 8 * city_map_item->height * scaleCityMapImage + 2)); + this->ui->graphicsView_City_Map->scale(2.0,2.0); +} + +void RegionMapEditor::on_pushButton_Zoom_Out_City_Map_clicked() { + // + scaleCityMapImage /= 2.0; + this->ui->graphicsView_City_Map->setFixedSize(QSize(8 * city_map_item->width * scaleCityMapImage + 2, 8 * city_map_item->height * scaleCityMapImage + 2)); + this->ui->graphicsView_City_Map->scale(0.5,0.5); +} + +void RegionMapEditor::on_pushButton_Zoom_In_Map_Image_clicked() { + // + //qDebug() << "scale:" << scaleRegionMapImage; + scaleRegionMapImage *= 2.0; + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_BkgImg->scale(2.0,2.0); + this->ui->graphicsView_Region_Map_Layout->scale(2.0,2.0); +} + +void RegionMapEditor::on_pushButton_Zoom_Out_Map_Image_clicked() { + // + if (scaleRegionMapImage <= 1.0) return; + scaleRegionMapImage /= 2.0; + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_BkgImg->scale(0.5,0.5); + this->ui->graphicsView_Region_Map_Layout->scale(0.5,0.5); +} + + + @@ -331,16 +524,6 @@ void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QStrin - - - - - - - - - - diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index 4c751898..6a685542 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -6,6 +6,7 @@ // the function that draws the map on the scene // (qnqlogous to Map::render) // TODO: figure out why this is being called twice!! +// (this also affects the history) void RegionMapPixmapItem::draw() { if (!region_map) return; @@ -31,7 +32,8 @@ void RegionMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; int y = static_cast(pos.y()) / 8; - this->region_map->map_squares[x + y * region_map->width()].tile_img_id = this->tile_selector->selectedTile; + int index = x + y * region_map->width(); + this->region_map->map_squares[index].tile_img_id = this->tile_selector->selectedTile; } draw(); } From c75ce5db1d50777cc0e0d976edb30867da054285 Mon Sep 17 00:00:00 2001 From: garak Date: Sun, 13 Jan 2019 19:27:28 -0500 Subject: [PATCH 07/17] resize region map, move map squares --- forms/regionmapeditor.ui | 29 ++- include/core/regionmap.h | 73 ++---- include/ui/regionmapeditor.h | 59 ++--- include/ui/regionmaplayoutpixmapitem.h | 10 +- src/core/regionmap.cpp | 245 +++++++------------ src/ui/regionmapeditor.cpp | 314 +++++++++++++------------ src/ui/regionmaplayoutpixmapitem.cpp | 31 ++- 7 files changed, 358 insertions(+), 403 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 932a8d0d..19a8f45c 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -17,7 +17,7 @@ - 10 + 30 250 20 20 @@ -30,7 +30,7 @@ - 30 + 10 250 20 20 @@ -358,7 +358,7 @@ - 10 + 30 550 20 20 @@ -377,7 +377,7 @@ - 30 + 10 550 20 20 @@ -396,7 +396,7 @@ - 681 + 661 550 20 20 @@ -415,7 +415,7 @@ - 661 + 681 550 20 20 @@ -434,7 +434,7 @@ - 661 + 681 250 20 20 @@ -447,7 +447,7 @@ - 681 + 661 250 20 20 @@ -457,7 +457,7 @@ - - + 10 @@ -727,7 +727,7 @@ - 1 + 0 @@ -1029,6 +1029,7 @@ + @@ -1058,6 +1059,14 @@ Ctrl+Y + + + Resize + + + Ctrl+R + + diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 7584a41d..733c8462 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -16,91 +17,75 @@ -// rename this struct? struct CityMapPosition { - // - //QString filename; // eg. dewford_0 ? - QString tilemap;// eg. "dewford_0" + QString tilemap; int x; int y; }; struct RegionMapEntry { - // int x; int y; int width; int height; - QString name;// mapsection + QString name; }; -// class that holds data for each square in this project -// struct? -// TODO: change char / uint8_t to unsigned class RegionMapSquare { public: - // - int x = -1;// x position, 0-indexed from top left - int y = -1;// y position, 0-indexed from top left - uint8_t tile_img_id;// tilemap ids for the background image - bool has_map = false;// whether this square is linked to a map or is empty - QString map_name;// name of the map associated with this square (if has_map is true): eg. "MAUVILLE_CITY" (TODO: REMOVE) + int x = -1; + int y = -1; + uint8_t tile_img_id; + bool has_map = false; + QString map_name; QString mapsec; uint8_t secid; - bool has_city_map = false;// whether there is a city map on this grid - QString city_map_name;// filename of the city_map tilemap - //bool is_flyable;//? needed ? - friend class RegionMap;// not necessary if instance? what + bool has_city_map = false; + QString city_map_name; + bool duplicated = false; }; class RegionMap : public QObject { Q_OBJECT -//public: -// explicit Map(QObject *parent = nullptr); public: RegionMap() = default; ~RegionMap() {}; - static QMap> ruby_city_maps_; static QString mapSecToMapConstant(QString); Project *project; - //RegionMapSquare *map_squares = nullptr;// array of RegionMapSquares - QList map_squares; + //QList map_squares; + QVector map_squares; History history; - QString temp_path;// delete this - QString city_map_squares_path; QString region_map_png_path; - QString region_map_bin_path;// = QString::null; - QString city_map_header_path;//dafuq is this? - QString region_map_layout_path; + QString region_map_bin_path; QString region_map_entries_path; QString region_map_layout_bin_path; - QString region_map_city_map_tiles_path; + QString city_map_tiles_path; QByteArray mapBinData; - QMap sMapNames;// {"{/sMapName_/}LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} + QMap sMapNamesMap;// {"{/sMapName_/}LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} - //QList> mapSecToMapEntry; QMap mapSecToMapEntry;// TODO: add to this on creation of new map + QVector sMapNames; + bool hasUnsavedChanges(); - void init(Project*);//QString); + void init(Project*); - // parseutil.cpp ? void readBkgImgBin(); - void readCityMaps();// more complicated + void readCityMaps(); void readLayout(); QString newAbbr(QString);// makes a *unique* 5 character abbreviation from mapname to add to mapname_abbr @@ -119,7 +104,6 @@ public: void saveOptions(int, QString, QString, int, int); void saveCityMaps(); - void update();// update the view in case something is broken? void resize(int, int); void setWidth(int); void setHeight(int); @@ -129,18 +113,12 @@ public: unsigned getTileId(int, int); int getMapSquareIndex(int, int); - void deleteLayoutSquare(int); + void resetSquare(int); - // implement these here? - void undo(); - void redo(); - - void test();// remove when done testing obvi + void test();// remove // TODO: move read / write functions to private (and others) private: - // - //History> *history;// (index, tile) int layout_width_; int layout_height_; int img_width_; @@ -150,13 +128,6 @@ private: void fillMapSquaresFromLayout(); QString fix_case(QString);// CAPS_WITH_UNDERSCORE to CamelCase -//protected: - // - -//signals: - // }; -//TilemapTileSelector *city_map_metatile_selector_item = nullptr; - #endif // REGIONMAP_H diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index ffc2bd3d..e41abd13 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -24,32 +24,11 @@ public: explicit RegionMapEditor(QWidget *parent = 0, Project *pro = nullptr); ~RegionMapEditor(); -// TODO: make members that are not called outside of this private RegionMap *region_map; - 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 loadRegionMapData(); - void displayRegionMap(); - void displayRegionMapImage(); - void displayRegionMapLayout(); - void displayRegionMapLayoutOptions(); - void updateRegionMapLayoutOptions(int); - void displayRegionMapTileSelector(); - void displayCityMapTileSelector(); - void displayCityMap(QString); void loadCityMaps(); - void onRegionMapTileSelectorSelectedTileChanged(); void onRegionMapTileSelectorHoveredTileChanged(unsigned); void onRegionMapTileSelectorHoveredTileCleared(); @@ -60,14 +39,17 @@ public: void undo(); void redo(); + void resize(int, int); + private: Ui::RegionMapEditor *ui; Project *project; - QString rmStatusbarMessage; History history; - double scaleUpFactor = 2.0; + int currIndex = 65;// TODO: automatic this from width * 2 + 1 + + double scaleUpFactor = 2.0;// TODO double scaleDownFactor = 1.0 / scaleUpFactor; double scaleRegionMapTiles = 1.0; @@ -75,7 +57,27 @@ private: double scaleCityMapTiles = 1.0; double scaleCityMapImage = 1.0; - void scaleUp(QGraphicsView *, qreal factor, qreal width, qreal height); + 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; + + RegionMapLayoutPixmapItem *region_map_layout_item = nullptr; + RegionMapPixmapItem *region_map_item = nullptr; + CityMapPixmapItem *city_map_item = nullptr; + + void displayRegionMap(); + void displayRegionMapImage(); + void displayRegionMapLayout(); + void displayRegionMapLayoutOptions(); + void updateRegionMapLayoutOptions(int); + void displayRegionMapTileSelector(); + void displayCityMapTileSelector(); + void displayCityMap(QString); bool createCityMap(QString); @@ -83,20 +85,23 @@ private slots: void on_action_RegionMap_Save_triggered(); void on_action_RegionMap_Undo_triggered(); void on_action_RegionMap_Redo_triggered(); + void on_action_RegionMap_Resize_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_save_clicked(); void on_pushButton_RM_Options_delete_clicked(); void on_pushButton_CityMap_save_clicked(); - void on_pushButton_CityMap_add_clicked();// + void on_pushButton_CityMap_add_clicked(); void on_pushButton_Zoom_In_Image_Tiles_clicked(); void on_pushButton_Zoom_Out_Image_Tiles_clicked(); void on_pushButton_Zoom_In_City_Tiles_clicked(); void on_pushButton_Zoom_Out_City_Tiles_clicked(); - void on_pushButton_Zoom_In_City_Map_clicked();// + void on_pushButton_Zoom_In_City_Map_clicked(); void on_pushButton_Zoom_Out_City_Map_clicked(); void on_pushButton_Zoom_In_Map_Image_clicked(); - void on_pushButton_Zoom_Out_Map_Image_clicked();// + void on_pushButton_Zoom_Out_Map_Image_clicked(); void on_comboBox_CityMap_picker_currentTextChanged(const QString &); + void on_spinBox_RM_Options_x_valueChanged(int); + void on_spinBox_RM_Options_y_valueChanged(int); void onHoveredRegionMapTileChanged(int, int); void onHoveredRegionMapTileCleared(); void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); diff --git a/include/ui/regionmaplayoutpixmapitem.h b/include/ui/regionmaplayoutpixmapitem.h index 8031f475..f3d2a258 100644 --- a/include/ui/regionmaplayoutpixmapitem.h +++ b/include/ui/regionmaplayoutpixmapitem.h @@ -8,22 +8,22 @@ class RegionMapLayoutPixmapItem : public SelectablePixmapItem { Q_OBJECT public: RegionMapLayoutPixmapItem(RegionMap *rmap, TilemapTileSelector *ts) : SelectablePixmapItem(8, 8, 1, 1) { - // this->region_map = rmap; this->tile_selector = ts; setAcceptHoverEvents(true); } - RegionMap *region_map;// inherited from RegionMapPixmapItem? + RegionMap *region_map; TilemapTileSelector *tile_selector; - int selectedTile;// index in map_squares + int selectedTile; + int highlightedTile; void draw(); void select(int, int); - void setDefaultSelection(); + void select(int); + void highlight(int, int, int); private: void updateSelectedTile(); -// can I implement these if they are virtual? signals: void mouseEvent(QGraphicsSceneMouseEvent *, RegionMapLayoutPixmapItem *); void hoveredTileChanged(int); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 061f1550..bc70d329 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -1,117 +1,41 @@ #include "regionmap.h" +#include "log.h" #include #include #include #include - #include -// TODO: add logging / config stuff - -// IN: ROUTE_101 ... OUT: MAP_ROUTE101 -// eg. SOUTHERN_ISLAND -> MAP_SOUTHERN_ISLAND -> SouthernIsland(n) -> SouthernIsland_Exterior -// MT_CHIMNEY -> MAP_MT_CHIMNEY -> MtChimney(y) -// ROUTE_101 -> MAP_ROUTE101 -> Route101(y) -// TODO: move this maybe? would I be able to call it from this file if it was in map.cpp? -QString RegionMap::mapSecToMapConstant(QString mapSectionName) { - // - QString mapConstantName = "MAP_"; - QString sectionNameTemp = mapSectionName.replace("ROUTE_","ROUTE"); - mapConstantName += sectionNameTemp; - return mapConstantName; -} - -// TODO: verify these are in the correct order -// also TODO: read this from the project somehow -QMap> RegionMap::ruby_city_maps_ = QMap>({ - {"LavaridgeTown", { - {"lavaridge_0.bin", 5, 3}, - }}, - {"FallarborTown", { - {"fallarbor_0.bin", 3, 0}, - }}, - {"FortreeCity", { - {"fortree_0.bin", 12, 0}, - }}, - {"SlateportCity", { - {"slateport_0.bin", 8, 10}, - {"slateport_1.bin", 8, 11}, - }}, - {"RustboroCity", { - {"rustboro_0.bin", 0, 5}, - {"rustboro_1.bin", 0, 6}, - }}, - {"PacifidlogTown", { - {"pacifidlog_0.bin", 17, 10}, - }}, - {"MauvilleCity", { - {"mauville_0.bin", 8, 6}, - {"mauville_1.bin", 9, 6}, - }}, - {"OldaleTown", { - {"oldale_0.bin", 4, 9}, - }}, - {"LilycoveCity", { - {"lilycove_0.bin", 18, 3}, - {"lilycove_1.bin", 19, 3}, - }}, - {"LittlerootTown", { - {"littleroot_0.bin", 4, 11}, - }}, - {"DewfordTown", { - {"dewford_0.bin", 2, 14}, - }}, - {"SootopolisCity", { - {"sootopolis_0.bin", 21, 7}, - }}, - {"EverGrandeCity", { - {"ever_grande_0.bin", 27, 8}, - {"ever_grande_1.bin", 27, 9}, - }}, - {"VerdanturfTown", { - {"verdanturf_0.bin", 4, 6}, - }}, - {"MossdeepCity", { - {"mossdeep_0.bin", 24, 5}, - {"mossdeep_1.bin", 25, 5}, - }}, - {"PetalburgCity", { - {"petalburg_0.bin", 1, 9}, - }}, -}); - // TODO: add version arg to this from Editor Setings void RegionMap::init(Project *pro) { QString path = pro->root; this->project = pro; - // + // TODO: in the future, allow these to be adjustable (and save values) - // possibly use a config file? - layout_width_ = 28; - layout_height_ = 15; + // possibly use a config file? save to include/constants/region_map.h? + layout_width_ = 36;//28; + layout_height_ = 25;//15; img_width_ = layout_width_ + 4; img_height_ = layout_height_ + 5; - //city_map_squares_path = QString(); - temp_path = path;// delete this - region_map_bin_path = path + "/graphics/pokenav/region_map_map.bin"; - region_map_png_path = path + "/graphics/pokenav/region_map.png"; - region_map_layout_path = path + "/src/data/region_map_layout.h"; - region_map_entries_path = path + "/src/data/region_map/region_map_entries.h"; + region_map_bin_path = path + "/graphics/pokenav/region_map_map.bin"; + region_map_png_path = path + "/graphics/pokenav/region_map.png"; + region_map_entries_path = path + "/src/data/region_map/region_map_entries.h"; region_map_layout_bin_path = path + "/graphics/pokenav/region_map_section_layout.bin"; - region_map_city_map_tiles_path = path + "/graphics/pokenav/zoom_tiles.png";// TODO: rename png to map_squares in pokeemerald + city_map_tiles_path = path + "/graphics/pokenav/zoom_tiles.png"; + // TODO: rename png to map_squares in pokeemerald readBkgImgBin(); readLayout(); readCityMaps(); + + //resize(40,30); } -// as of now, this needs to be called first because it initializes all the -// `RegionMapSquare`s in the list // TODO: if the tileId is not valid for the provided image, make sure it does not crash void RegionMap::readBkgImgBin() { QFile binFile(region_map_bin_path); @@ -120,12 +44,13 @@ void RegionMap::readBkgImgBin() { QByteArray mapBinData = binFile.readAll(); binFile.close(); - // the two multiplier is because lines are skipped for some reason - // (maybe that is because there could be multiple layers?) - // background image is also 32x20 + if (mapBinData.size() < img_height_ * img_width_) { + logError(QString("The region map tilemap at %1 is too small.").arg(region_map_bin_path)); + return; + } for (int m = 0; m < img_height_; m++) { for (int n = 0; n < img_width_; n++) { - RegionMapSquare square;// = + RegionMapSquare square; square.tile_img_id = mapBinData.at(n + m * img_width_ * 2); map_squares.append(square); } @@ -155,7 +80,7 @@ void RegionMap::readLayout() { QString line; // TODO: put these in Project, and keep in order - //QMap sMapNames;// {"sMapName_LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} + //QMap sMapNamesMap;// {"sMapName_LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} //QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} //QList<> mapSecToMapEntry;// {"MAPSEC_LITTLEROOT_TOWN" : } @@ -170,14 +95,15 @@ void RegionMap::readLayout() { QRegularExpression reAfter("_\\(\"(.*)\""); QString const_name = reBefore.match(line).captured(1); QString full_name = reAfter.match(line).captured(1); - sMapNames.insert(const_name, full_name); + sMapNames.append(const_name); + sMapNamesMap.insert(const_name, full_name); } else if (line.contains("MAPSEC")) { QRegularExpression reBefore("\\[(.*)\\]"); QRegularExpression reAfter("{(.*)}"); QStringList entry = reAfter.match(line).captured(1).remove(" ").split(","); QString mapsec = reBefore.match(line).captured(1); QString insertion = entry[4].remove("sMapName_"); - qmap->insert(mapsec, sMapNames[insertion]); + qmap->insert(mapsec, sMapNamesMap[insertion]); // can make this a map, the order doesn't really matter mapSecToMapEntry[mapsec] = // x y width height name @@ -191,7 +117,9 @@ void RegionMap::readLayout() { } file.close(); - project->mapSecToMapHoverName = qmap; + //qDebug() << "sMapNames" << sMapNames; + + project->mapSecToMapHoverName = qmap;// TODO: is this map necessary? QFile binFile(region_map_layout_bin_path); if (!binFile.open(QIODevice::ReadOnly)) return; @@ -206,18 +134,13 @@ void RegionMap::readLayout() { QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(n,m)))]; if (secname != "MAPSEC_NONE") map_squares[i].has_map = true; map_squares[i].mapsec = secname; - map_squares[i].map_name = sMapNames.value(mapSecToMapEntry.value(secname).name); + map_squares[i].map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name);// TODO: this is atrocious map_squares[i].x = n; map_squares[i].y = m; } } } -/// saves: -// region_map_entries_path -// region_map_layout_bin_path (layout as tilemap instead of how it is in ruby) -// done -// TODO: consider keeping QMaps in order void RegionMap::saveLayout() { QString entries_text; QString layout_text; @@ -225,9 +148,8 @@ void RegionMap::saveLayout() { entries_text += "#ifndef GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n"; entries_text += "#define GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n\n"; - // note: this doesn't necessarily keep order because it is a QMap - for (auto it : this->project->mapSecToMapHoverName->keys()) { - entries_text += "static const u8 sMapName_" + fix_case(it) + "[] = _(\"" + this->project->mapSecToMapHoverName->value(it) + "\");\n"; + for (auto sName : sMapNames) { + entries_text += "static const u8 sMapName_" + sName + "[] = _(\"" + sMapNamesMap.value(sName) + "\");\n"; } entries_text += "\nconst struct RegionMapLocation gRegionMapEntries[] = {\n"; @@ -235,7 +157,7 @@ void RegionMap::saveLayout() { for (auto sec : mapSecToMapEntry.keys()) { struct RegionMapEntry entry = mapSecToMapEntry.value(sec); entries_text += " [" + sec + "] = {" + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " - + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + fix_case(sec) + "},\n";//entry.name + + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + entry.name + "},\n"; } entries_text += "};\n\n#endif // GUARD_DATA_REGION_MAP_REGION_MAP_ENTRIES_H\n"; @@ -254,29 +176,7 @@ void RegionMap::saveLayout() { bfile.close(); } -// beyond broken -void RegionMap::readCityMaps() { - // - for (int map = 0; map < map_squares.size(); map++) { - // - if (map_squares[map].has_map) { - // - if (ruby_city_maps_.contains(map_squares[map].map_name)) { - //map_squares[map].has_city_map = true; - //map_squares[map].city_map_name = ruby_city_maps_.value(map_squares[map].map_name)[0].tilemap; - QList city_maps = ruby_city_maps_.value(map_squares[map].map_name); - for (auto city_map : city_maps) { - // - if (city_map.x == map_squares[map].x - && city_map.y == map_squares[map].y) - // - map_squares[map].has_city_map = true; - map_squares[map].city_map_name = city_map.tilemap; - } - } - } - } -} +void RegionMap::readCityMaps() {} // layout coords to image index int RegionMap::img_index_(int x, int y) { @@ -316,53 +216,53 @@ int RegionMap::height() { return this->img_height_; } -// TODO: remove +2 and put elsewhere QSize RegionMap::imgSize() { return QSize(img_width_ * 8 + 2, img_height_ * 8 + 2); } // TODO: rename to getTileIdAt()? unsigned RegionMap::getTileId(int x, int y) { - // return map_squares[x + y * img_width_].tile_img_id; } -// TODO: change debugs to logs void RegionMap::save() { - // - qDebug() << "saving region map image tilemap at" << region_map_bin_path << "\n" - << "saving region map layout at" << region_map_layout_path << "\n" - ; - + logInfo("Saving region map info."); saveBkgImgBin(); saveLayout(); + // TODO: re-select proper tile + // TODO: add region map dimensions to config } -// save Options (temp) -void RegionMap::saveOptions(int index, QString sec, QString name, int x, int y) { - // - // TODO:req need to reindex in city_maps if changing x and y - // TODO: save [sec] sMapName_ properly - // so instead of taking index, maybe go by img_index_(x,y) +void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { + int index = getMapSquareIndex(x + 1, y + 2); if (!sec.isEmpty()) { this->map_squares[index].has_map = true; this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf(sec)); + this->map_squares[index].mapsec = sec; + if (!name.isEmpty()) { + this->map_squares[index].map_name = name; + this->project->mapSecToMapHoverName->insert(sec, name); + if (!mapSecToMapEntry.keys().contains(sec)) { + QString sName = fix_case(sec); + sMapNames.append(sName); + sMapNamesMap.insert(sName, name); + struct RegionMapEntry entry = {x, y, 1, 1, sName};// TODO: change width, height? + mapSecToMapEntry.insert(sec, entry); + } + } + this->map_squares[index].x = x; + this->map_squares[index].y = y; + this->map_squares[index].duplicated = false; } - this->map_squares[index].mapsec = sec; - if (!name.isEmpty()) { - this->map_squares[index].map_name = name;// TODO: display in editor with this map & remove this field - this->project->mapSecToMapHoverName->insert(sec, name); - } - this->map_squares[index].x = x; - this->map_squares[index].y = y; + resetSquare(id); } // from x, y of image -// TODO: make sure this returns a valid index int RegionMap::getMapSquareIndex(int x, int y) { // int index = (x + y * img_width_); - return index < map_squares.length() - 1 ? index : 0; + //qDebug() << "index:" << index; + return index < map_squares.length() ? index : 0; } // For turning a MAPSEC_NAME into a unique identifier sMapName-style variable. @@ -384,9 +284,44 @@ QString RegionMap::fix_case(QString caps) { return camel; } - - - +void RegionMap::resetSquare(int index) { + this->map_squares[index].mapsec = "MAPSEC_NONE"; + this->map_squares[index].map_name = QString(); + this->map_squares[index].has_map = false; + this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf("MAPSEC_NONE")); + this->map_squares[index].has_city_map = false; + this->map_squares[index].city_map_name = QString(); + this->map_squares[index].duplicated = false; + logInfo(QString("Reset map square at (%1, %2).").arg(this->map_squares[index].x).arg(this->map_squares[index].y)); +} + +void RegionMap::resize(int newWidth, int newHeight) { + // + QVector new_squares; + + for (int y = 0; y < newHeight; y++) { + for (int x = 0; x < newWidth; x++) { + RegionMapSquare square; + if (x < img_width_ && y < img_height_) { + square = map_squares[getMapSquareIndex(x, y)]; + } else if (x < newWidth - 3 && y < newHeight - 3) { + square.tile_img_id = 0; + square.x = x; + square.y = y; + square.mapsec = "MAPSEC_NONE"; + } else { + square.tile_img_id = 0; + } + new_squares.append(square); + } + } + + this->map_squares = new_squares; + this->img_width_ = newWidth; + this->img_height_ = newHeight; + this->layout_width_ = newWidth - 4; + this->layout_height_ = newHeight - 5; +} diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 2f0303fa..bde1e97e 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -7,7 +7,6 @@ #include #include #include -#include RegionMapEditor::RegionMapEditor(QWidget *parent, Project *pro) : QMainWindow(parent), @@ -16,34 +15,39 @@ RegionMapEditor::RegionMapEditor(QWidget *parent, Project *pro) : this->ui->setupUi(this); this->project = pro; this->region_map = new RegionMap; - this->setFixedSize(this->size());//statusbar->setSizeGripEnabled(false); + this->setFixedSize(this->size()); } RegionMapEditor::~RegionMapEditor() { delete ui; + delete region_map; + delete region_map_item; + delete mapsquare_selector_item; + delete region_map_layout_item; + delete scene_region_map_image; + delete city_map_selector_item; + delete city_map_item; + delete scene_city_map_tiles; + delete scene_city_map_image; + delete scene_region_map_layout; + delete scene_region_map_tiles; } - - void RegionMapEditor::on_action_RegionMap_Save_triggered() { - qDebug() << "Region Map Save Triggered"; if (project && region_map) { - qDebug() << "actually saving"; region_map->save(); displayRegionMap(); } } - void RegionMapEditor::loadRegionMapData() { - // this->region_map->init(project); + this->currIndex = this->region_map->width() * 2 + 1; displayRegionMap(); } void RegionMapEditor::loadCityMaps() { - // QDir directory(project->root + "/graphics/pokenav/city_maps"); QStringList files = directory.entryList(QStringList() << "*.bin", QDir::Files); QStringList without_bin; @@ -61,115 +65,134 @@ void RegionMapEditor::displayRegionMap() { displayRegionMapLayoutOptions(); } -// TODO: change the signal slot to new syntax -// TODO: add scalability? void RegionMapEditor::displayRegionMapImage() { - // + if (!scene_region_map_image) { + this->scene_region_map_image = new QGraphicsScene; + } + if (region_map_item && scene_region_map_image) { + scene_region_map_image->removeItem(region_map_item); + delete region_map_item; + } + this->region_map_item = new RegionMapPixmapItem(this->region_map, this->mapsquare_selector_item); - connect(region_map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*)), - this, SLOT(mouseEvent_region_map(QGraphicsSceneMouseEvent*, RegionMapPixmapItem*))); - connect(region_map_item, SIGNAL(hoveredRegionMapTileChanged(int, int)), - this, SLOT(onHoveredRegionMapTileChanged(int, int))); - connect(region_map_item, SIGNAL(hoveredRegionMapTileCleared()), - this, SLOT(onHoveredRegionMapTileCleared())); this->region_map_item->draw(); - this->scene_region_map_image = new QGraphicsScene; + connect(this->region_map_item, &RegionMapPixmapItem::mouseEvent, + this, &RegionMapEditor::mouseEvent_region_map); + connect(this->region_map_item, &RegionMapPixmapItem::hoveredRegionMapTileChanged, + this, &RegionMapEditor::onHoveredRegionMapTileChanged); + connect(this->region_map_item, &RegionMapPixmapItem::hoveredRegionMapTileCleared, + this, &RegionMapEditor::onHoveredRegionMapTileCleared); + this->scene_region_map_image->addItem(this->region_map_item); - this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); + //this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize()); + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); } -// TODO: add if (item) and if(scene) checks because called more than once per instance void RegionMapEditor::displayRegionMapLayout() { - // + if (!scene_region_map_layout) { + this->scene_region_map_layout = new QGraphicsScene; + } + if (region_map_layout_item && scene_region_map_layout) { + this->scene_region_map_layout->removeItem(region_map_layout_item); + delete region_map_layout_item; + } + this->region_map_layout_item = new RegionMapLayoutPixmapItem(this->region_map, this->mapsquare_selector_item); - //* + connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::selectedTileChanged, - this, &RegionMapEditor::onRegionMapLayoutSelectedTileChanged);// TODO: remove this? + this, &RegionMapEditor::onRegionMapLayoutSelectedTileChanged); connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileChanged, this, &RegionMapEditor::onRegionMapLayoutHoveredTileChanged); connect(this->region_map_layout_item, &RegionMapLayoutPixmapItem::hoveredTileCleared, this, &RegionMapEditor::onRegionMapLayoutHoveredTileCleared); - //*/ - this->region_map_layout_item->draw(); - this->region_map_layout_item->setDefaultSelection(); - this->scene_region_map_layout = new QGraphicsScene; + this->region_map_layout_item->draw(); + this->region_map_layout_item->select(this->currIndex); + this->scene_region_map_layout->addItem(region_map_layout_item); - this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); + //this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize()); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); } void RegionMapEditor::displayRegionMapLayoutOptions() { - // this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); this->ui->frame_RM_Options->setEnabled(true); // TODO: change these values to variables - this->ui->spinBox_RM_Options_x->setMaximum(27); - this->ui->spinBox_RM_Options_y->setMaximum(14); + this->ui->spinBox_RM_Options_x->setMaximum(this->region_map->width() - 5); + this->ui->spinBox_RM_Options_y->setMaximum(this->region_map->height() - 4); - updateRegionMapLayoutOptions(65); + updateRegionMapLayoutOptions(currIndex); } void RegionMapEditor::updateRegionMapLayoutOptions(int index) { - // + this->ui->spinBox_RM_Options_x->blockSignals(true); + this->ui->spinBox_RM_Options_y->blockSignals(true); this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec));//this->region_map->map_squares[index].map_name); this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); + this->ui->spinBox_RM_Options_x->blockSignals(false); + this->ui->spinBox_RM_Options_y->blockSignals(false); } -// TODO: get this to display on a decent scale void RegionMapEditor::displayRegionMapTileSelector() { - // + if (!scene_region_map_tiles) { + this->scene_region_map_tiles = new QGraphicsScene; + } + if (mapsquare_selector_item && scene_region_map_tiles) { + this->scene_region_map_tiles->removeItem(mapsquare_selector_item); + delete mapsquare_selector_item; + } + this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_png_path)); this->mapsquare_selector_item->draw(); - this->scene_region_map_tiles = new QGraphicsScene; this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); - connect(this->mapsquare_selector_item, &TilemapTileSelector::selectedTileChanged, - this, &RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileChanged, this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileCleared, this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared); this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth + 2, - this->mapsquare_selector_item->pixelHeight + 2); + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, + this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); } void RegionMapEditor::displayCityMapTileSelector() { - // city_map_selector_item - this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_city_map_tiles_path)); + if (!scene_city_map_tiles) { + this->scene_city_map_tiles = new QGraphicsScene; + } + if (city_map_selector_item && scene_city_map_tiles) { + scene_city_map_tiles->removeItem(city_map_selector_item); + delete city_map_selector_item; + } + + this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->city_map_tiles_path)); this->city_map_selector_item->draw(); - this->scene_city_map_tiles = new QGraphicsScene; this->scene_city_map_tiles->addItem(this->city_map_selector_item); + this->scene_city_map_tiles->setSceneRect(this->scene_city_map_tiles->sceneRect());// ? - /*connect(this->city_map_selector_item, &TilemapTileSelector::selectedTileChanged, - this, &RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged);// TODO: remove this? - connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, + // TODO: + /*connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileCleared, this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared);*/ this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth + 2, - this->city_map_selector_item->pixelHeight + 2); - //this->ui->graphicsView_City_Map_Tiles->scale(2,2); + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, + this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); } void RegionMapEditor::displayCityMap(QString f) { - // QString file = this->project->root + "/graphics/pokenav/city_maps/" + f + ".bin"; if (!scene_city_map_image) { @@ -183,22 +206,23 @@ void RegionMapEditor::displayCityMap(QString f) { 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*))); + connect(this->city_map_item, &CityMapPixmapItem::mouseEvent, + this, &RegionMapEditor::mouseEvent_city_map); 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(8 * city_map_item->width + 2, 8 * city_map_item->height + 2)); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width * scaleCityMapImage + 2, + 8 * city_map_item->height * scaleCityMapImage + 2); } bool RegionMapEditor::createCityMap(QString name) { - // bool errored = false; QString file = this->project->root + "/graphics/pokenav/city_maps/" + name + ".bin"; + // TODO: use project config for these values? uint8_t filler = 0x30; uint8_t border = 0x7; uint8_t blank = 0x1; @@ -224,32 +248,21 @@ bool RegionMapEditor::createCityMap(QString name) { return !errored; } - - -//// - -void RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged() { - // -} - void RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { QString message = QString("Tile: 0x") + QString("%1").arg(tileId, 4, 16, QChar('0')).toUpper(); this->ui->statusbar->showMessage(message); } void RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared() { - // - //QString message = QString("Selected Tile: 0x") + QString("%1").arg(this->region_map_layout_item->selectedTile, 4, 16, QChar('0')).toUpper(); - //this->ui->statusbar->showMessage(message); + this->ui->statusbar->clearMessage(); } void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { - // QString message = QString(); + this->currIndex = index; if (this->region_map->map_squares[index].has_map) { - // message = QString("\t %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");//.remove("{NAME_END}") + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");// ruby-specific } this->ui->statusbar->showMessage(message); @@ -264,7 +277,6 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { if (x >= 0 && y >= 0) { message = QString("(%1, %2)").arg(x).arg(y); if (this->region_map->map_squares[index].has_map) { - // message += QString("\t %1").arg(this->project->mapSecToMapHoverName->value( this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); } @@ -273,25 +285,12 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { } void RegionMapEditor::onRegionMapLayoutHoveredTileCleared() { - // - int index = this->region_map_layout_item->selectedTile; - QString message = QString(); - int x = this->region_map->map_squares[index].x; - int y = this->region_map->map_squares[index].y; - if (x >= 0 && y >= 0) { - message = QString("(%1, %2)").arg(x).arg(y); - if (this->region_map->map_squares[index].has_map) { - // - message += QString("\t %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); - } - } - this->ui->statusbar->showMessage(message); + this->ui->statusbar->clearMessage(); } void RegionMapEditor::onHoveredRegionMapTileChanged(int x, int y) { - rmStatusbarMessage = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); - this->ui->statusbar->showMessage(rmStatusbarMessage); + QString message = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); + this->ui->statusbar->showMessage(message); } void RegionMapEditor::onHoveredRegionMapTileCleared() { @@ -299,34 +298,27 @@ void RegionMapEditor::onHoveredRegionMapTileCleared() { } void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *item) { - // if (event->buttons() & Qt::RightButton) { - // item->select(event); - } else if (event->buttons() & Qt::MiddleButton) { - // TODO: add functionality here? replace or? + } else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - // QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; int y = static_cast(pos.y()) / 8; int index = this->region_map->getMapSquareIndex(x, y); + if (index > this->region_map->map_squares.size() - 1) return; RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, index, this->region_map->map_squares[index].tile_img_id, this->mapsquare_selector_item->getSelectedTile()); history.push(commit); - item->paint(event); + item->paint(event);//*/ } } void RegionMapEditor::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? + if (event->buttons() & Qt::RightButton) {// TODO + } else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - // QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; int y = static_cast(pos.y()) / 8; @@ -338,31 +330,33 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM } } - - - - - - - -//// - void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { - // this->ui->stackedWidget_RM_Options->setCurrentIndex(index); } +void RegionMapEditor::on_spinBox_RM_Options_x_valueChanged(int x) { + int y = this->ui->spinBox_RM_Options_y->value(); + int red = this->region_map->getMapSquareIndex(x + 1, y + 2); + this->region_map_layout_item->highlight(x, y, red); +} + +void RegionMapEditor::on_spinBox_RM_Options_y_valueChanged(int y) { + int x = this->ui->spinBox_RM_Options_x->value(); + int red = this->region_map->getMapSquareIndex(x + 1, y + 2); + this->region_map_layout_item->highlight(x, y, red); +} + void RegionMapEditor::on_pushButton_RM_Options_save_clicked() { - // this->region_map->saveOptions( - // - this->region_map_layout_item->selectedTile, + this->region_map_layout_item->selectedTile,// TODO: remove this->ui->comboBox_RM_ConnectedMap->currentText(), this->ui->lineEdit_RM_MapName->text(), this->ui->spinBox_RM_Options_x->value(), this->ui->spinBox_RM_Options_y->value() ); + this->region_map_layout_item->highlightedTile = -1; this->region_map_layout_item->draw(); + // TODO: update selected tile index } void RegionMapEditor::on_pushButton_CityMap_save_clicked() { @@ -370,7 +364,9 @@ void RegionMapEditor::on_pushButton_CityMap_save_clicked() { } void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() { - qDebug() << "delete it fat"; + this->region_map->resetSquare(this->region_map_layout_item->selectedTile); + this->region_map_layout_item->draw(); + this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); } void RegionMapEditor::on_pushButton_CityMap_add_clicked() { @@ -400,7 +396,40 @@ void RegionMapEditor::on_pushButton_CityMap_add_clicked() { } } +void RegionMapEditor::on_action_RegionMap_Resize_triggered() { + QDialog popup(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + popup.setWindowTitle("New Region Map Dimensions"); + popup.setWindowModality(Qt::NonModal); + + QFormLayout form(&popup); + + QSpinBox *widthSpinBox = new QSpinBox(); + QSpinBox *heightSpinBox = new QSpinBox(); + widthSpinBox->setMinimum(32); + heightSpinBox->setMinimum(20); + widthSpinBox->setMaximum(64);// TODO: come up with real (meaningful) limits? + heightSpinBox->setMaximum(40); + widthSpinBox->setValue(this->region_map->width()); + heightSpinBox->setValue(this->region_map->height()); + form.addRow(new QLabel("Width"), widthSpinBox); + form.addRow(new QLabel("Height"), heightSpinBox); + + QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup); + + form.addRow(&buttonBox); + connect(&buttonBox, SIGNAL(rejected()), &popup, SLOT(reject())); + connect(&buttonBox, SIGNAL(accepted()), &popup, SLOT(accept())); + + if (popup.exec() == QDialog::Accepted) { + resize(widthSpinBox->value(), heightSpinBox->value()); + } +} + void RegionMapEditor::on_action_RegionMap_Undo_triggered() { + undo(); +} + +void RegionMapEditor::undo() { RegionMapHistoryItem *commit = history.current(); if (!commit) return; @@ -423,6 +452,10 @@ void RegionMapEditor::on_action_RegionMap_Undo_triggered() { } void RegionMapEditor::on_action_RegionMap_Redo_triggered() { + redo(); +} + +void RegionMapEditor::redo() { RegionMapHistoryItem *commit = history.next(); if (!commit) return; @@ -442,15 +475,18 @@ void RegionMapEditor::on_action_RegionMap_Redo_triggered() { } } +void RegionMapEditor::resize(int w, int h) { + this->region_map->resize(w, h); + this->currIndex = 2 * w + 1; + displayRegionMap(); +} + void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); } - - -// TODO: prevent huge images with limits on zoom void RegionMapEditor::on_pushButton_Zoom_In_Image_Tiles_clicked() { - // + if (scaleRegionMapTiles >= 8.0) return; scaleRegionMapTiles *= 2.0; this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); @@ -458,7 +494,7 @@ void RegionMapEditor::on_pushButton_Zoom_In_Image_Tiles_clicked() { } void RegionMapEditor::on_pushButton_Zoom_Out_Image_Tiles_clicked() { - // + if (scaleRegionMapTiles <= 1.0) return; scaleRegionMapTiles /= 2.0; this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); @@ -466,7 +502,7 @@ void RegionMapEditor::on_pushButton_Zoom_Out_Image_Tiles_clicked() { } void RegionMapEditor::on_pushButton_Zoom_In_City_Tiles_clicked() { - // + if (scaleCityMapTiles >= 8.0) return; scaleCityMapTiles *= 2.0; this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); @@ -474,7 +510,7 @@ void RegionMapEditor::on_pushButton_Zoom_In_City_Tiles_clicked() { } void RegionMapEditor::on_pushButton_Zoom_Out_City_Tiles_clicked() { - // + if (scaleCityMapTiles <= 1.0) return; scaleCityMapTiles /= 2.0; this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); @@ -482,22 +518,25 @@ void RegionMapEditor::on_pushButton_Zoom_Out_City_Tiles_clicked() { } void RegionMapEditor::on_pushButton_Zoom_In_City_Map_clicked() { - // + if (scaleCityMapImage >= 8.0) return; scaleCityMapImage *= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(QSize(8 * city_map_item->width * scaleCityMapImage + 2, 8 * city_map_item->height * scaleCityMapImage + 2)); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width * scaleCityMapImage + 2, + 8 * city_map_item->height * scaleCityMapImage + 2); this->ui->graphicsView_City_Map->scale(2.0,2.0); } void RegionMapEditor::on_pushButton_Zoom_Out_City_Map_clicked() { - // + if (scaleCityMapImage <= 1.0) return; scaleCityMapImage /= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(QSize(8 * city_map_item->width * scaleCityMapImage + 2, 8 * city_map_item->height * scaleCityMapImage + 2)); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width * scaleCityMapImage + 2, + 8 * city_map_item->height * scaleCityMapImage + 2); this->ui->graphicsView_City_Map->scale(0.5,0.5); } void RegionMapEditor::on_pushButton_Zoom_In_Map_Image_clicked() { - // - //qDebug() << "scale:" << scaleRegionMapImage; + resize(40,30);// test + return; + if (scaleRegionMapImage >= 8.0) return; scaleRegionMapImage *= 2.0; this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); @@ -506,7 +545,6 @@ void RegionMapEditor::on_pushButton_Zoom_In_Map_Image_clicked() { } void RegionMapEditor::on_pushButton_Zoom_Out_Map_Image_clicked() { - // if (scaleRegionMapImage <= 1.0) return; scaleRegionMapImage /= 2.0; this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); @@ -514,23 +552,3 @@ void RegionMapEditor::on_pushButton_Zoom_Out_Map_Image_clicked() { this->ui->graphicsView_Region_Map_BkgImg->scale(0.5,0.5); this->ui->graphicsView_Region_Map_Layout->scale(0.5,0.5); } - - - - - - - - - - - - - - - - - - - - diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp index 24de0e53..5f97e529 100644 --- a/src/ui/regionmaplayoutpixmapitem.cpp +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -2,8 +2,6 @@ -// TODO: make this connected (by duplicating scene rect maybe?) to background image tab -// void RegionMapLayoutPixmapItem::draw() { if (!region_map) return; @@ -14,7 +12,11 @@ void RegionMapLayoutPixmapItem::draw() { QImage bottom_img = this->tile_selector->tileImg(region_map->map_squares[i].tile_img_id); QImage top_img(8, 8, QImage::Format_RGBA8888); if (region_map->map_squares[i].has_map) { - top_img.fill(Qt::gray); + if (i == highlightedTile) { + top_img.fill(Qt::red); + } else { + top_img.fill(Qt::gray); + } } else { top_img.fill(Qt::black); } @@ -34,10 +36,6 @@ void RegionMapLayoutPixmapItem::draw() { this->drawSelection(); } -void RegionMapLayoutPixmapItem::setDefaultSelection() { - this->select(1,2); -} - void RegionMapLayoutPixmapItem::select(int x, int y) { int index = this->region_map->getMapSquareIndex(x, y); SelectablePixmapItem::select(x, y, 0, 0); @@ -47,11 +45,30 @@ void RegionMapLayoutPixmapItem::select(int x, int y) { emit selectedTileChanged(index); } +void RegionMapLayoutPixmapItem::select(int index) { + int x = index % this->region_map->width(); + int y = index / this->region_map->width(); + SelectablePixmapItem::select(x, y, 0, 0); + this->selectedTile = index; + this->updateSelectedTile(); + + emit selectedTileChanged(index); +} + +void RegionMapLayoutPixmapItem::highlight(int x, int y, int red) { + // TODO: check if out of bounds and return + // if it is not empty, color it red + this->highlightedTile = red; + draw(); + SelectablePixmapItem::select(x + 1, y + 2, 0, 0); +} + void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { QPoint pos = this->getCellPos(event->pos()); int index = this->region_map->getMapSquareIndex(pos.x(), pos.y()); if (this->region_map->map_squares[index].x >= 0 && this->region_map->map_squares[index].y >= 0) { + //if (index > this->region_map->width() * 2) { SelectablePixmapItem::mousePressEvent(event); this->updateSelectedTile(); emit selectedTileChanged(this->selectedTile); From 41f3780c8a16f5c4bf0934945d29d8601b707f82 Mon Sep 17 00:00:00 2001 From: garak Date: Tue, 15 Jan 2019 17:06:18 -0500 Subject: [PATCH 08/17] add region map data to config, fix some bugs --- forms/regionmapeditor.ui | 12 +++ include/config.h | 5 + include/core/mapconnection.h | 9 ++ include/core/regionmap.h | 8 +- include/core/regionmapgenerator.h | 77 ++++++++++++++ include/project.h | 3 +- include/ui/citymappixmapitem.h | 13 ++- include/ui/regionmapeditor.h | 9 +- porymap.pro | 2 + src/config.cpp | 21 ++++ src/core/regionmap.cpp | 111 +++++--------------- src/core/regionmapgenerator.cpp | 148 +++++++++++++++++++++++++++ src/ui/citymappixmapitem.cpp | 31 +++--- src/ui/regionmapeditor.cpp | 93 +++++++++++------ src/ui/regionmaplayoutpixmapitem.cpp | 7 +- src/ui/regionmappixmapitem.cpp | 4 - 16 files changed, 399 insertions(+), 154 deletions(-) create mode 100644 include/core/regionmapgenerator.h create mode 100644 src/core/regionmapgenerator.cpp diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 19a8f45c..010322a1 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -1031,8 +1031,15 @@ + + + Tools + + + + @@ -1067,6 +1074,11 @@ Ctrl+R + + + Generate... + + diff --git a/include/config.h b/include/config.h index 7f63871f..624f87cd 100644 --- a/include/config.h +++ b/include/config.h @@ -4,6 +4,7 @@ #include #include #include +#include enum MapSortOrder { Group = 0, @@ -36,6 +37,7 @@ public: this->metatilesZoom = 30; this->showPlayerView = false; this->showCursorTile = true; + this->regionMapDimensions = QSize(32, 20); } void setRecentProject(QString project); void setRecentMap(QString map); @@ -46,6 +48,7 @@ public: void setMetatilesZoom(int zoom); void setShowPlayerView(bool enabled); void setShowCursorTile(bool enabled); + void setRegionMapDimensions(int width, int height); QString getRecentProject(); QString getRecentMap(); MapSortOrder getMapSortOrder(); @@ -55,6 +58,7 @@ public: int getMetatilesZoom(); bool getShowPlayerView(); bool getShowCursorTile(); + QSize getRegionMapDimensions(); protected: QString getConfigFilepath(); void parseConfigKeyValue(QString key, QString value); @@ -76,6 +80,7 @@ private: int metatilesZoom; bool showPlayerView; bool showCursorTile; + QSize regionMapDimensions; }; extern PorymapConfig porymapConfig; diff --git a/include/core/mapconnection.h b/include/core/mapconnection.h index a0fe4404..8823e7bc 100644 --- a/include/core/mapconnection.h +++ b/include/core/mapconnection.h @@ -2,6 +2,7 @@ #define MAPCONNECTION_H #include +#include class MapConnection { public: @@ -10,4 +11,12 @@ public: QString map_name; }; +inline bool operator==(const MapConnection &c1, const MapConnection &c2) { + return c1.map_name == c2.map_name; +} + +inline uint qHash(const MapConnection &key) { + return qHash(key.map_name); +} + #endif // MAPCONNECTION_H diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 733c8462..ae804148 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -61,9 +61,13 @@ public: Project *project; - //QList map_squares; QVector map_squares; + const int padLeft = 1; + const int padRight = 3; + const int padTop = 2; + const int padBottom = 3; + History history; QString region_map_png_path; @@ -115,8 +119,6 @@ public: void resetSquare(int); - void test();// remove - // TODO: move read / write functions to private (and others) private: int layout_width_; diff --git a/include/core/regionmapgenerator.h b/include/core/regionmapgenerator.h new file mode 100644 index 00000000..54539009 --- /dev/null +++ b/include/core/regionmapgenerator.h @@ -0,0 +1,77 @@ +#ifndef GUARD_REGIONMAPGENERATOR_H +#define GUARD_REGIONMAPGENERATOR_H + +#include "project.h" +#include "regionmap.h" +#include "map.h" + +#include +#include +#include +#include +#include +#include + +class GeneratorEntry +{ + GeneratorEntry(QString name_, int x_, int y_, int width_, int height_) { + this->name = name_; + this->x = x_; + this->y = y_; + this->width = width_; + this->height = height_; + } + int x; + int y; + int width; + int height; + QString name; + friend class RegionMapGenerator; +public: + friend QDebug operator<<(QDebug, const GeneratorEntry &); +}; + +class RegionMapGenerator +{ + // +public: + // + RegionMapGenerator() = default; + RegionMapGenerator(Project *); + ~RegionMapGenerator() {}; + + Project *project = nullptr; + + QVector map_squares; + + QStack> forks;//? + QSet connections;// + // + + void generate(QString); + + void center();// center the map squares so they arent hanging over + +private: + // + int square_block_width_ = 20; + int square_block_height_ = 20; + + int width_; + int height_; + + //QPoint + + QList entries_; + QMap entries; + + void bfs(int);// breadth first search of a graph + void search(int); + void dfs(int, QVector &);// depth first search + void sort(); + void ts();// topological sort + void populateSquares(); + +}; + +#endif // GUARD_REGIONMAPGENERATOR_H diff --git a/include/project.h b/include/project.h index 5331b6aa..b4270bff 100644 --- a/include/project.h +++ b/include/project.h @@ -47,8 +47,7 @@ public: Map* loadMap(QString); Map* getMap(QString); - // other options include: InGameName, PopUpName, ???? - QMap *mapSecToMapHoverName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} + QMap *mapSecToMapHoverName; QMap *tileset_cache = nullptr; Tileset* loadTileset(QString, Tileset *tileset = nullptr); diff --git a/include/ui/citymappixmapitem.h b/include/ui/citymappixmapitem.h index 8162bb04..0fc461b5 100644 --- a/include/ui/citymappixmapitem.h +++ b/include/ui/citymappixmapitem.h @@ -2,6 +2,7 @@ #define CITYMAPPIXMAPITEM_H #include "tilemaptileselector.h" + #include #include @@ -18,10 +19,6 @@ public: QString file; - // TODO: make private and use access functions - int width; - int height; - QByteArray data; void init(); @@ -30,10 +27,12 @@ public: virtual void paint(QGraphicsSceneMouseEvent*); virtual void draw(); int getIndexAt(int, int); + int width(); + int height(); -//private: -// int width; -// int height; +private: + int width_; + int height_; signals: void mouseEvent(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index e41abd13..f1dd6507 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -29,6 +29,8 @@ public: void loadRegionMapData(); void loadCityMaps(); + void onRegionMapTileSelectorSelectedTileChanged(unsigned); + void onCityMapTileSelectorSelectedTileChanged(unsigned); void onRegionMapTileSelectorHoveredTileChanged(unsigned); void onRegionMapTileSelectorHoveredTileCleared(); @@ -47,9 +49,11 @@ private: History history; - int currIndex = 65;// TODO: automatic this from width * 2 + 1 + int currIndex; + unsigned selectedCityTile; + unsigned selectedImageTile; - double scaleUpFactor = 2.0;// TODO + double scaleUpFactor = 2.0; double scaleDownFactor = 1.0 / scaleUpFactor; double scaleRegionMapTiles = 1.0; @@ -86,6 +90,7 @@ private slots: void on_action_RegionMap_Undo_triggered(); void on_action_RegionMap_Redo_triggered(); void on_action_RegionMap_Resize_triggered(); + void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_save_clicked(); void on_pushButton_RM_Options_delete_clicked(); diff --git a/porymap.pro b/porymap.pro index d2101b98..7a72c3d8 100644 --- a/porymap.pro +++ b/porymap.pro @@ -30,6 +30,7 @@ SOURCES += src/core/block.cpp \ src/core/tileset.cpp \ src/core/regionmapeditor.cpp \ src/core/regionmap.cpp \ + src/core/regionmapgenerator.cpp \ src/ui/aboutporymap.cpp \ src/ui/bordermetatilespixmapitem.cpp \ src/ui/collisionpixmapitem.cpp \ @@ -87,6 +88,7 @@ HEADERS += include/core/block.h \ include/core/tileset.h \ include/core/regionmapeditor.h \ include/core/regionmap.h \ + include/core/regionmapgenerator.h \ include/ui/aboutporymap.h \ include/ui/bordermetatilespixmapitem.h \ include/ui/collisionpixmapitem.h \ diff --git a/src/config.cpp b/src/config.cpp index aa21a1af..32ad12fe 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -156,6 +156,17 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { if (!ok) { logWarn(QString("Invalid config value for show_cursor_tile: '%1'. Must be 0 or 1.").arg(value)); } + } else if (key == "region_map_dimensions") { + bool ok1, ok2; + QStringList dims = value.split("x"); + int w = dims[0].toInt(&ok1); + int h = dims[1].toInt(&ok2); + if (!ok1 || !ok2) { + logWarn("Cannot parse region map dimensions. Using default values instead."); + this->regionMapDimensions = QSize(32, 20); + } else { + this->regionMapDimensions = QSize(w, h); + } } else { logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key)); } @@ -176,6 +187,8 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("metatiles_zoom", QString("%1").arg(this->metatilesZoom)); map.insert("show_player_view", this->showPlayerView ? "1" : "0"); map.insert("show_cursor_tile", this->showCursorTile ? "1" : "0"); + map.insert("region_map_dimensions", QString("%1x%2").arg(this->regionMapDimensions.width()) + .arg(this->regionMapDimensions.height())); return map; } @@ -246,6 +259,10 @@ void PorymapConfig::setShowCursorTile(bool enabled) { this->save(); } +void PorymapConfig::setRegionMapDimensions(int width, int height) { + this->regionMapDimensions = QSize(width, height);//QString("%1x%2").arg(width).arg(height); +} + QString PorymapConfig::getRecentProject() { return this->recentProject; } @@ -290,6 +307,10 @@ bool PorymapConfig::getShowCursorTile() { return this->showCursorTile; } +QSize PorymapConfig::getRegionMapDimensions() { + return this->regionMapDimensions; +} + const QMap baseGameVersionMap = { {BaseGameVersion::pokeruby, "pokeruby"}, {BaseGameVersion::pokeemerald, "pokeemerald"}, diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index bc70d329..7dde15dd 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -1,5 +1,6 @@ #include "regionmap.h" #include "log.h" +#include "config.h" #include #include @@ -9,31 +10,26 @@ -// TODO: add version arg to this from Editor Setings void RegionMap::init(Project *pro) { QString path = pro->root; this->project = pro; - // TODO: in the future, allow these to be adjustable (and save values) - // possibly use a config file? save to include/constants/region_map.h? - layout_width_ = 36;//28; - layout_height_ = 25;//15; + QSize dimensions = porymapConfig.getRegionMapDimensions(); + img_width_ = dimensions.width(); + img_height_ = dimensions.height(); - img_width_ = layout_width_ + 4; - img_height_ = layout_height_ + 5; + layout_width_ = img_width_ - this->padLeft - this->padRight; + layout_height_ = img_height_ - this->padTop - this->padBottom; region_map_bin_path = path + "/graphics/pokenav/region_map_map.bin"; region_map_png_path = path + "/graphics/pokenav/region_map.png"; region_map_entries_path = path + "/src/data/region_map/region_map_entries.h"; region_map_layout_bin_path = path + "/graphics/pokenav/region_map_section_layout.bin"; city_map_tiles_path = path + "/graphics/pokenav/zoom_tiles.png"; - // TODO: rename png to map_squares in pokeemerald readBkgImgBin(); readLayout(); readCityMaps(); - - //resize(40,30); } // TODO: if the tileId is not valid for the provided image, make sure it does not crash @@ -58,7 +54,7 @@ void RegionMap::readBkgImgBin() { } void RegionMap::saveBkgImgBin() { - QByteArray data(4096,0);// use a constant here? maybe read the original size? + QByteArray data(4096,0);// use a constant here? maybe read the original size? for (int m = 0; m < img_height_; m++) { for (int n = 0; n < img_width_; n++) { @@ -79,12 +75,7 @@ void RegionMap::readLayout() { if (!file.open(QIODevice::ReadOnly)) return; QString line; - // TODO: put these in Project, and keep in order - //QMap sMapNamesMap;// {"sMapName_LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} - //QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} - //QList<> mapSecToMapEntry;// {"MAPSEC_LITTLEROOT_TOWN" : } - // new map ffor mapSecToMapHoverName QMap *qmap = new QMap; QTextStream in(&file); @@ -103,23 +94,16 @@ void RegionMap::readLayout() { QStringList entry = reAfter.match(line).captured(1).remove(" ").split(","); QString mapsec = reBefore.match(line).captured(1); QString insertion = entry[4].remove("sMapName_"); - qmap->insert(mapsec, sMapNamesMap[insertion]); - // can make this a map, the order doesn't really matter - mapSecToMapEntry[mapsec] = - // x y width height name - {entry[0].toInt(), entry[1].toInt(), entry[2].toInt(), entry[3].toInt(), insertion} - ; - // ^ when loading this info to city maps, loop over mapSecToMapEntry and - // add x and y map sqyare when width or height >1 - // indexOf because mapsecs is just a qstringlist - //text += line.remove(" "); + qmap->insert(mapsec, sMapNamesMap.value(insertion)); + mapSecToMapEntry[mapsec] = { + // x y width height name + entry[0].toInt(), entry[1].toInt(), entry[2].toInt(), entry[3].toInt(), insertion + }; } } file.close(); - //qDebug() << "sMapNames" << sMapNames; - - project->mapSecToMapHoverName = qmap;// TODO: is this map necessary? + project->mapSecToMapHoverName = qmap; QFile binFile(region_map_layout_bin_path); if (!binFile.open(QIODevice::ReadOnly)) return; @@ -154,7 +138,8 @@ void RegionMap::saveLayout() { entries_text += "\nconst struct RegionMapLocation gRegionMapEntries[] = {\n"; - for (auto sec : mapSecToMapEntry.keys()) { + for (auto sec : *(project->regionMapSections)) { + if (!mapSecToMapEntry.contains(sec)) continue; struct RegionMapEntry entry = mapSecToMapEntry.value(sec); entries_text += " [" + sec + "] = {" + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " + QString::number(entry.width) + ", " + QString::number(entry.height) + ", sMapName_" + entry.name + "},\n"; @@ -180,7 +165,7 @@ void RegionMap::readCityMaps() {} // layout coords to image index int RegionMap::img_index_(int x, int y) { - return ((x + 1) + (y + 2) * img_width_); + return ((x + this->padLeft) + (y + this->padTop) * img_width_); } // layout coords to layout index @@ -188,26 +173,6 @@ int RegionMap::layout_index_(int x, int y) { return (x + y * layout_width_); } -void RegionMap::test() { - // - bool debug_rmap = false; - - if (debug_rmap) { - for (auto square : map_squares) { - qDebug() << "(" << square.x << "," << square.y << ")" - << square.tile_img_id - << square.has_map - << square.map_name - << square.has_city_map - << square.city_map_name - ; - } - - QPixmap png(region_map_png_path); - qDebug() << "png num 8x8 tiles" << QString("0x%1").arg((png.width()/8) * (png.height() / 8), 2, 16, QChar('0')); - } -} - int RegionMap::width() { return this->img_width_; } @@ -229,12 +194,12 @@ void RegionMap::save() { logInfo("Saving region map info."); saveBkgImgBin(); saveLayout(); - // TODO: re-select proper tile - // TODO: add region map dimensions to config + porymapConfig.setRegionMapDimensions(this->img_width_, this->img_height_); } void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { - int index = getMapSquareIndex(x + 1, y + 2); + resetSquare(id); + int index = getMapSquareIndex(x + this->padLeft, y + this->padTop); if (!sec.isEmpty()) { this->map_squares[index].has_map = true; this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf(sec)); @@ -254,14 +219,12 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { this->map_squares[index].y = y; this->map_squares[index].duplicated = false; } - resetSquare(id); + //resetSquare(id); } // from x, y of image int RegionMap::getMapSquareIndex(int x, int y) { - // int index = (x + y * img_width_); - //qDebug() << "index:" << index; return index < map_squares.length() ? index : 0; } @@ -304,7 +267,7 @@ void RegionMap::resize(int newWidth, int newHeight) { RegionMapSquare square; if (x < img_width_ && y < img_height_) { square = map_squares[getMapSquareIndex(x, y)]; - } else if (x < newWidth - 3 && y < newHeight - 3) { + } else if (x < newWidth - this->padRight && y < newHeight - this->padBottom) { square.tile_img_id = 0; square.x = x; square.y = y; @@ -319,34 +282,6 @@ void RegionMap::resize(int newWidth, int newHeight) { this->map_squares = new_squares; this->img_width_ = newWidth; this->img_height_ = newHeight; - this->layout_width_ = newWidth - 4; - this->layout_height_ = newHeight - 5; + this->layout_width_ = newWidth - this->padLeft - this->padRight; + this->layout_height_ = newHeight - this->padTop - this->padBottom; } - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/core/regionmapgenerator.cpp b/src/core/regionmapgenerator.cpp new file mode 100644 index 00000000..5cbabc8c --- /dev/null +++ b/src/core/regionmapgenerator.cpp @@ -0,0 +1,148 @@ +#include "regionmapgenerator.h" + + + +RegionMapGenerator::RegionMapGenerator(Project *project_) { + this->project = project_; +} + +QDebug operator<<(QDebug debug, const GeneratorEntry &entry) +{ + debug.nospace() << "Entry_" << entry.name + << " (" << entry.x << ", " << entry.y << ")" + << " [" << entry.width << " x " << entry.height << "]" + ; + return debug; +} + +// +void RegionMapGenerator::generate(QString mapName) { + // + int i = project->mapNames->indexOf(mapName); + //Map *map = project->loadMap(mapName); + qDebug() << "generating region map from:" << mapName; + search(i); + + populateSquares(); +} + +// use a progress bar because this is rather slow (because loadMap) +// TODO: use custom functions to load only necessary data from maps? +// need connections and dimensions +// maybe use gMapGroup0.numMaps as hint for size of progress +void RegionMapGenerator::bfs(int start) { + // + int size = project->mapNames->size(); + + //* + + QVector visited(size, false); + QList queue; + + visited[start] = true; + queue.append(start); + + while (!queue.isEmpty()) { + start = queue.first(); + + qDebug() << project->mapNames->at(start); + Map *map = project->loadMap(project->mapNames->at(start)); + + if (!map) break; + + this->entries.insert(map->location, { + map->name, 0, 0, map->getWidth() / square_block_width_, map->getHeight() / square_block_height_ + }); + + queue.removeFirst(); + + // get all connected map indexes + // if not visited, mark it visited and insert into queue + for (auto c : map->connections) { + int i = project->mapNames->indexOf(c->map_name); + if (!visited[i] && c->direction != "dive") { + visited[i] = true; + queue.append(i); + } + } + //delete map; + } + //*/ + qDebug() << "search complete";// << entries.keys(); + //return; +} + +// +void RegionMapGenerator::dfs(int start, QVector &visited) { + // + visited[start] = true; + + Map *map = project->loadMap(project->mapNames->at(start)); + //qDebug() << map->name; + this->entries.insert(map->location, { + map->name, 0, 0, map->getWidth() / square_block_width_, map->getHeight() / square_block_height_ + }); + + // place map on the grid? + + for (auto c : map->connections) { + int i = project->mapNames->indexOf(c->map_name); + if (!visited[i] && c->direction != "dive") { + dfs(i, visited); + } + } +} + +void RegionMapGenerator::search(int start) { + // + int size = project->mapNames->size(); + QVector visited(size, false); + + dfs(start, visited); +} + +void RegionMapGenerator::populateSquares() { + // + for (auto entry : entries.values()) { + qDebug() << entry;//.name << entry.width << "x" << entry.height; + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index f7f47b5e..b1224aa9 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -1,5 +1,6 @@ #include "citymappixmapitem.h" #include "imageproviders.h" +#include "log.h" #include #include @@ -7,11 +8,9 @@ -// read to byte array from filename void CityMapPixmapItem::init() { - // TODO: are they always 10x10 squares? - width = 10; - height = 10; + width_ = 10; + height_ = 10; QFile binFile(file); if (!binFile.open(QIODevice::ReadOnly)) return; @@ -21,13 +20,13 @@ void CityMapPixmapItem::init() { } void CityMapPixmapItem::draw() { - QImage image(width * 8, height * 8, QImage::Format_RGBA8888); + 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; + int x = i % width_; + int y = i / width_; QPoint pos = QPoint(x * 8, y * 8); painter.drawImage(pos, img); } @@ -37,15 +36,16 @@ void CityMapPixmapItem::draw() { } void CityMapPixmapItem::save() { - // TODO: logError / logWarn if fail QFile binFile(file); - if (!binFile.open(QIODevice::WriteOnly)) return; + if (!binFile.open(QIODevice::WriteOnly)) { + logError(QString("Cannot save city map tilemap to %1.").arg(file)); + 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; @@ -63,6 +63,13 @@ void CityMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { } int CityMapPixmapItem::getIndexAt(int x, int y) { - // - return 2 * (x + y * width); + return 2 * (x + y * this->width_); +} + +int CityMapPixmapItem::width() { + return this->width_; +} + +int CityMapPixmapItem::height() { + return this->height_; } diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index bde1e97e..226e7bd0 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -1,5 +1,7 @@ #include "regionmapeditor.h" #include "ui_regionmapeditor.h" +#include "regionmapgenerator.h" +#include "config.h" #include #include @@ -43,7 +45,7 @@ void RegionMapEditor::on_action_RegionMap_Save_triggered() { void RegionMapEditor::loadRegionMapData() { this->region_map->init(project); - this->currIndex = this->region_map->width() * 2 + 1; + this->currIndex = this->region_map->width() * this->region_map->padTop + this->region_map->padLeft; displayRegionMap(); } @@ -85,7 +87,7 @@ void RegionMapEditor::displayRegionMapImage() { this, &RegionMapEditor::onHoveredRegionMapTileCleared); this->scene_region_map_image->addItem(this->region_map_item); - //this->scene_region_map_image->setSceneRect(this->scene_region_map_image->sceneRect()); + this->scene_region_map_image->setSceneRect(this->scene_region_map_image->itemsBoundingRect()); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); @@ -113,20 +115,24 @@ void RegionMapEditor::displayRegionMapLayout() { this->region_map_layout_item->select(this->currIndex); this->scene_region_map_layout->addItem(region_map_layout_item); - //this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->sceneRect()); + this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->itemsBoundingRect()); this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); } void RegionMapEditor::displayRegionMapLayoutOptions() { + this->ui->comboBox_RM_ConnectedMap->clear(); this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); this->ui->frame_RM_Options->setEnabled(true); - // TODO: change these values to variables - this->ui->spinBox_RM_Options_x->setMaximum(this->region_map->width() - 5); - this->ui->spinBox_RM_Options_y->setMaximum(this->region_map->height() - 4); + this->ui->spinBox_RM_Options_x->setMaximum( + this->region_map->width() - this->region_map->padLeft - this->region_map->padRight - 1 + ); + this->ui->spinBox_RM_Options_y->setMaximum( + this->region_map->height() - this->region_map->padTop - this->region_map->padBottom - 1 + ); updateRegionMapLayoutOptions(currIndex); } @@ -134,7 +140,7 @@ void RegionMapEditor::displayRegionMapLayoutOptions() { void RegionMapEditor::updateRegionMapLayoutOptions(int index) { this->ui->spinBox_RM_Options_x->blockSignals(true); this->ui->spinBox_RM_Options_y->blockSignals(true); - this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec));//this->region_map->map_squares[index].map_name); + this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec)); this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); @@ -156,6 +162,8 @@ void RegionMapEditor::displayRegionMapTileSelector() { this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); + connect(this->mapsquare_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged); connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileChanged, this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); connect(this->mapsquare_selector_item, &TilemapTileSelector::hoveredTileCleared, @@ -164,6 +172,8 @@ void RegionMapEditor::displayRegionMapTileSelector() { this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); + + this->mapsquare_selector_item->select(this->selectedImageTile); } void RegionMapEditor::displayCityMapTileSelector() { @@ -179,17 +189,15 @@ void RegionMapEditor::displayCityMapTileSelector() { this->city_map_selector_item->draw(); this->scene_city_map_tiles->addItem(this->city_map_selector_item); - this->scene_city_map_tiles->setSceneRect(this->scene_city_map_tiles->sceneRect());// ? - // TODO: - /*connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileChanged, - this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged); - connect(this->city_map_selector_item, &TilemapTileSelector::hoveredTileCleared, - this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared);*/ + connect(this->city_map_selector_item, &TilemapTileSelector::selectedTileChanged, + this, &RegionMapEditor::onCityMapTileSelectorSelectedTileChanged); this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); + + this->city_map_selector_item->select(this->selectedCityTile); } void RegionMapEditor::displayCityMap(QString f) { @@ -213,8 +221,8 @@ void RegionMapEditor::displayCityMap(QString f) { 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(8 * city_map_item->width * scaleCityMapImage + 2, - 8 * city_map_item->height * scaleCityMapImage + 2); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, + 8 * city_map_item->height() * scaleCityMapImage + 2); } bool RegionMapEditor::createCityMap(QString name) { @@ -222,7 +230,6 @@ bool RegionMapEditor::createCityMap(QString name) { QString file = this->project->root + "/graphics/pokenav/city_maps/" + name + ".bin"; - // TODO: use project config for these values? uint8_t filler = 0x30; uint8_t border = 0x7; uint8_t blank = 0x1; @@ -248,6 +255,14 @@ bool RegionMapEditor::createCityMap(QString name) { return !errored; } +void RegionMapEditor::onRegionMapTileSelectorSelectedTileChanged(unsigned id) { + this->selectedImageTile = id; +} + +void RegionMapEditor::onCityMapTileSelectorSelectedTileChanged(unsigned id) { + this->selectedCityTile = id; +} + void RegionMapEditor::onRegionMapTileSelectorHoveredTileChanged(unsigned tileId) { QString message = QString("Tile: 0x") + QString("%1").arg(tileId, 4, 16, QChar('0')).toUpper(); this->ui->statusbar->showMessage(message); @@ -270,7 +285,6 @@ void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { } void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { - // TODO: change to x, y coords not index QString message = QString(); int x = this->region_map->map_squares[index].x; int y = this->region_map->map_squares[index].y; @@ -289,7 +303,8 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileCleared() { } void RegionMapEditor::onHoveredRegionMapTileChanged(int x, int y) { - QString message = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); + QString message = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); this->ui->statusbar->showMessage(message); } @@ -300,7 +315,7 @@ void RegionMapEditor::onHoveredRegionMapTileCleared() { void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *item) { if (event->buttons() & Qt::RightButton) { item->select(event); - } else if (event->buttons() & Qt::MiddleButton) {// TODO + //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; @@ -310,14 +325,14 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, index, this->region_map->map_squares[index].tile_img_id, this->mapsquare_selector_item->getSelectedTile()); history.push(commit); - item->paint(event);//*/ + item->paint(event); } } void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityMapPixmapItem *item) { // if (event->buttons() & Qt::RightButton) {// TODO - } else if (event->buttons() & Qt::MiddleButton) {// TODO + //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; @@ -332,23 +347,34 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { this->ui->stackedWidget_RM_Options->setCurrentIndex(index); + switch (index) + { + case 0: + this->ui->pushButton_Zoom_In_Image_Tiles->setVisible(true); + this->ui->pushButton_Zoom_Out_Image_Tiles->setVisible(true); + break; + case 1: + this->ui->pushButton_Zoom_In_Image_Tiles->setVisible(false); + this->ui->pushButton_Zoom_Out_Image_Tiles->setVisible(false); + break; + } } void RegionMapEditor::on_spinBox_RM_Options_x_valueChanged(int x) { int y = this->ui->spinBox_RM_Options_y->value(); - int red = this->region_map->getMapSquareIndex(x + 1, y + 2); + int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); this->region_map_layout_item->highlight(x, y, red); } void RegionMapEditor::on_spinBox_RM_Options_y_valueChanged(int y) { int x = this->ui->spinBox_RM_Options_x->value(); - int red = this->region_map->getMapSquareIndex(x + 1, y + 2); + int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); this->region_map_layout_item->highlight(x, y, red); } void RegionMapEditor::on_pushButton_RM_Options_save_clicked() { this->region_map->saveOptions( - this->region_map_layout_item->selectedTile,// TODO: remove + this->region_map_layout_item->selectedTile, this->ui->comboBox_RM_ConnectedMap->currentText(), this->ui->lineEdit_RM_MapName->text(), this->ui->spinBox_RM_Options_x->value(), @@ -356,7 +382,6 @@ void RegionMapEditor::on_pushButton_RM_Options_save_clicked() { ); this->region_map_layout_item->highlightedTile = -1; this->region_map_layout_item->draw(); - // TODO: update selected tile index } void RegionMapEditor::on_pushButton_CityMap_save_clicked() { @@ -407,7 +432,7 @@ void RegionMapEditor::on_action_RegionMap_Resize_triggered() { QSpinBox *heightSpinBox = new QSpinBox(); widthSpinBox->setMinimum(32); heightSpinBox->setMinimum(20); - widthSpinBox->setMaximum(64);// TODO: come up with real (meaningful) limits? + widthSpinBox->setMaximum(60);// w * h * 2 <= 4960 heightSpinBox->setMaximum(40); widthSpinBox->setValue(this->region_map->width()); heightSpinBox->setValue(this->region_map->height()); @@ -520,22 +545,20 @@ void RegionMapEditor::on_pushButton_Zoom_Out_City_Tiles_clicked() { void RegionMapEditor::on_pushButton_Zoom_In_City_Map_clicked() { if (scaleCityMapImage >= 8.0) return; scaleCityMapImage *= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width * scaleCityMapImage + 2, - 8 * city_map_item->height * scaleCityMapImage + 2); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, + 8 * city_map_item->height() * scaleCityMapImage + 2); this->ui->graphicsView_City_Map->scale(2.0,2.0); } void RegionMapEditor::on_pushButton_Zoom_Out_City_Map_clicked() { if (scaleCityMapImage <= 1.0) return; scaleCityMapImage /= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width * scaleCityMapImage + 2, - 8 * city_map_item->height * scaleCityMapImage + 2); + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, + 8 * city_map_item->height() * scaleCityMapImage + 2); this->ui->graphicsView_City_Map->scale(0.5,0.5); } void RegionMapEditor::on_pushButton_Zoom_In_Map_Image_clicked() { - resize(40,30);// test - return; if (scaleRegionMapImage >= 8.0) return; scaleRegionMapImage *= 2.0; this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); @@ -552,3 +575,9 @@ void RegionMapEditor::on_pushButton_Zoom_Out_Map_Image_clicked() { this->ui->graphicsView_Region_Map_BkgImg->scale(0.5,0.5); this->ui->graphicsView_Region_Map_Layout->scale(0.5,0.5); } + +void RegionMapEditor::on_action_RegionMap_Generate_triggered() { + // + RegionMapGenerator generator(this->project); + generator.generate("LittlerootTown"); +} diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp index 5f97e529..2da3ae5d 100644 --- a/src/ui/regionmaplayoutpixmapitem.cpp +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -56,11 +56,9 @@ void RegionMapLayoutPixmapItem::select(int index) { } void RegionMapLayoutPixmapItem::highlight(int x, int y, int red) { - // TODO: check if out of bounds and return - // if it is not empty, color it red this->highlightedTile = red; draw(); - SelectablePixmapItem::select(x + 1, y + 2, 0, 0); + SelectablePixmapItem::select(x + this->region_map->padLeft, y + this->region_map->padTop, 0, 0); } void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { @@ -68,7 +66,6 @@ void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) int index = this->region_map->getMapSquareIndex(pos.x(), pos.y()); if (this->region_map->map_squares[index].x >= 0 && this->region_map->map_squares[index].y >= 0) { - //if (index > this->region_map->width() * 2) { SelectablePixmapItem::mousePressEvent(event); this->updateSelectedTile(); emit selectedTileChanged(this->selectedTile); @@ -99,4 +96,6 @@ void RegionMapLayoutPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) void RegionMapLayoutPixmapItem::updateSelectedTile() { QPoint origin = this->getSelectionStart(); this->selectedTile = this->region_map->getMapSquareIndex(origin.x(), origin.y()); + this->highlightedTile = -1; + draw(); } diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index 6a685542..c75341d7 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -3,10 +3,6 @@ -// the function that draws the map on the scene -// (qnqlogous to Map::render) -// TODO: figure out why this is being called twice!! -// (this also affects the history) void RegionMapPixmapItem::draw() { if (!region_map) return; From a951fc85da03951f6aed071d4c3b51b03773706e Mon Sep 17 00:00:00 2001 From: garak Date: Tue, 22 Jan 2019 15:06:49 -0500 Subject: [PATCH 09/17] region map ui updates and other fixes --- forms/regionmapeditor.ui | 1898 +++++++++++++------------- include/core/historyitem.h | 15 +- include/core/regionmap.h | 7 +- include/ui/citymappixmapitem.h | 6 + include/ui/regionmapeditor.h | 29 +- include/ui/tilemaptileselector.h | 2 +- src/core/historyitem.cpp | 21 +- src/core/regionmap.cpp | 25 +- src/ui/citymappixmapitem.cpp | 33 + src/ui/regionmapeditor.cpp | 282 ++-- src/ui/regionmaplayoutpixmapitem.cpp | 5 +- src/ui/regionmappixmapitem.cpp | 12 +- 12 files changed, 1267 insertions(+), 1068 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 010322a1..6e2e06ce 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -6,133 +6,801 @@ 0 0 - 709 - 619 + 829 + 644 Region Map Editor - - - - - 30 - 250 - 20 - 20 - - - - + - - - - - - 10 - 250 - 20 - 20 - - - - - - - - - - - 10 - 280 - 691 - 271 - - - - Qt::Horizontal - - - - - QLayout::SetDefaultConstraint + + + + + + Qt::Vertical - - - - - - City Map: - - - - - - - - - - - 0 - 0 - - - - <html><head/><body><p>Add a new city map.</p></body></html> - - - - - - - :/icons/add.ico - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - save - - - - - - - + + false + + + + Qt::Horizontal + + + false + + + + + 393 + 251 + + + + 0 + + + + Background Image + + + + + + + 1 + 1 + + + + true + + + + + 0 + 0 + 345 + 188 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + + 0 + 0 + + + + false + + + false + + + QAbstractScrollArea::AdjustIgnored + + + QGraphicsView::NoDrag + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + + + + + + Map Layout + + + + + + + 1 + 0 + + + + true + + + + + 0 + 0 + 345 + 188 + + + + + 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 + + + + + + + + + + + + + + + 320 + 200 + + + + 0 + + + + + + + + 0 + 0 + + + + + 162 + 162 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + + + true + + + Qt::AlignHCenter|Qt::AlignTop + + + + true + + + + 8 + 0 + 281 + 225 + + + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + true + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustIgnored + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + false + + + + 0 + 0 + + + + + 300 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + 1 + + + + + 13 + 11 + 281 + 91 + + + + + + + Map Section + + + + + + + + 0 + 0 + + + + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> + + + true + + + + + + + Map Name + + + + + + + + + + City Map + + + + + + + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> + + + true + + + + + + + + + 10 + 100 + 138 + 26 + + + + + + + x + + + + + + + + + + y + + + + + + + + + + + + 10 + 190 + 113 + 32 + + + + Delete Square + + + + + + + + + + + Qt::Horizontal + + + false + + + + + QLayout::SetDefaultConstraint + + + + + + + City Map: + + + + + + + + + + + 0 + 0 + + + + <html><head/><body><p>Add a new city map.</p></body></html> + + + + + + + :/icons/add.ico + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 1 + 0 + + + + + 102 + 102 + + + + true + + + + + 0 + 0 + 440 + 267 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + + 0 + 0 + + + + false + + + false + + + QAbstractScrollArea::AdjustIgnored + + + QGraphicsView::NoDrag + + + + + + + Qt::Vertical + + + + 16 + 166 + + + + + + + + Qt::Horizontal + + + + 166 + 16 + + + + + + + + + + + - - 1 + + 0 0 + + + 82 + 82 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAsNeeded + + + QAbstractScrollArea::AdjustIgnored + true - + + Qt::AlignHCenter|Qt::AlignTop + + + + true + - 0 + 8 0 - 420 - 225 + 254 + 311 - + + + 0 + 0 + + + + + QLayout::SetDefaultConstraint + 0 @@ -148,76 +816,66 @@ 0 - - + + + + Qt::Vertical + + + QSizePolicy::Expanding + + + + 20 + 40 + + + + + + Qt::Horizontal - 166 - 16 + 40 + 20 - - - Qt::Vertical + + + true - - - 16 - 166 - - - - - - - - Qt::Vertical - - - - 16 - 166 - - - - - - - + 0 0 - - false + + Qt::ScrollBarAlwaysOff - - false + + Qt::ScrollBarAlwaysOff QAbstractScrollArea::AdjustIgnored - - QGraphicsView::NoDrag - - - + + Qt::Horizontal - 166 - 16 + 40 + 20 @@ -225,795 +883,175 @@ + + + + + + + + + + 30 + 50 + + + + + 30 + 160 + + + + 1 + + + 4 + + + Qt::Vertical + + + QSlider::TicksAbove + + + 1 + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 30 + 0 + + + + + 30 + 160 + + + + 1 + + + 4 + + + Qt::Vertical + + + QSlider::TicksAbove + + + 1 + + - - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOn - - - Qt::ScrollBarAsNeeded - - - QAbstractScrollArea::AdjustIgnored - - - true - - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 242 - 269 - - - - - 0 - 0 - - - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - QSizePolicy::Preferred - - - - 20 - 40 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - 30 - 550 - 20 - 20 - - - - - 0 - 0 - - - - + - - - - - - 10 - 550 - 20 - 20 - - - - - 0 - 0 - - - - - - - - - - - 661 - 550 - 20 - 20 - - - - - 0 - 0 - - - - - - - - - - - 681 - 550 - 20 - 20 - - - - - 0 - 0 - - - - + - - - - - - 681 - 250 - 20 - 20 - - - - + - - - - - - 661 - 250 - 20 - 20 - - - - - - - - - - - 10 - 10 - 691 - 241 - - - - - - - 0 - - - - Background Image - - - - - -1 - -1 - 341 - 213 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 339 - 211 - - - - - 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 - - - - - - - + + + + + + + + 30 + 50 + + + + + 30 + 160 + + + + 1 + + + 4 + + + Qt::Vertical + + + QSlider::TicksBelow + + + 1 + - - - Map Layout - - - - - -1 - -1 - 341 - 213 - - - - - 1 - 0 - - - - true - - - - - 0 - 0 - 339 - 211 - - - - - 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 - - - - - - - + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 30 + 0 + + + + + 30 + 160 + + + + 1 + + + 4 + + + Qt::Vertical + + + QSlider::TicksBelow + + + 1 + - - - - - - 0 - - - - - - 0 - 10 - 342 - 230 - - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOn - - - Qt::ScrollBarAsNeeded - - - QAbstractScrollArea::AdjustIgnored - - - true - - - Qt::AlignHCenter|Qt::AlignTop - - - - true - - - - 8 - 0 - 324 - 228 - - - - - 0 - 0 - - - - - QLayout::SetDefaultConstraint - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - true - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - Qt::ScrollBarAlwaysOff - - - QAbstractScrollArea::AdjustIgnored - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - false - - - - 0 - 10 - 342 - 230 - - - - - 0 - 0 - - - - QFrame::StyledPanel - - - QFrame::Raised - - - - - 10 - 170 - 107 - 32 - - - - Set Values - - - - - - 13 - 11 - 301 - 111 - - - - - - - Map Section - - - - - - - <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is display when the player enters it.</p></body></html> - - - true - - - - - - - Map Name - - - - - - - - - - City Map - - - - - - - <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> - - - true - - - - - - - - - 20 - 130 - 138 - 26 - - - - - - - x - - - - - - - - - - y - - - - - - - - - - - - 210 - 170 - 113 - 32 - - - - Delete Square - - - - - - - - + + + + 0 0 - 709 + 829 22 diff --git a/include/core/historyitem.h b/include/core/historyitem.h index 6ccda292..7d87795d 100644 --- a/include/core/historyitem.h +++ b/include/core/historyitem.h @@ -2,6 +2,7 @@ #define HISTORYITEM_H #include "blockdata.h" +//#include "regionmap.h" class HistoryItem { public: @@ -15,15 +16,19 @@ public: enum RegionMapEditorBox { BackgroundImage = 1, CityMapImage = 2, + BackroundResize = 3, }; class RegionMapHistoryItem { public: - int which;// region map or city map - int index; - unsigned tile; - unsigned prev; - RegionMapHistoryItem(int type, int index, unsigned prev, unsigned tile); + int which; + int mapWidth; + int mapHeight; + QVector tiles; + QString cityMap; + RegionMapHistoryItem(int type, QVector tiles); + RegionMapHistoryItem(int type, QVector tiles, QString cityMap); + RegionMapHistoryItem(int type, QVector tiles, int width, int height); ~RegionMapHistoryItem(); }; diff --git a/include/core/regionmap.h b/include/core/regionmap.h index ae804148..7c4b70a9 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -1,8 +1,8 @@ #ifndef REGIONMAP_H #define REGIONMAP_H -#include "project.h" #include "map.h" +#include "project.h" #include "tilemaptileselector.h" #include "history.h" #include "historyitem.h" @@ -94,6 +94,7 @@ public: QString newAbbr(QString);// makes a *unique* 5 character abbreviation from mapname to add to mapname_abbr + // TODO: did I use these like, at all? // editing functions // if they are booleans, returns true if successful? bool placeTile(char, int, int);// place tile at x, y @@ -111,12 +112,16 @@ public: void resize(int, int); void setWidth(int); void setHeight(int); + void setBackgroundImageData(QVector *); int width(); int height(); QSize imgSize(); unsigned getTileId(int, int); int getMapSquareIndex(int, int); + QVector getTiles(); + void setTiles(QVector); + void resetSquare(int); // TODO: move read / write functions to private (and others) diff --git a/include/ui/citymappixmapitem.h b/include/ui/citymappixmapitem.h index 0fc461b5..bc82e3c1 100644 --- a/include/ui/citymappixmapitem.h +++ b/include/ui/citymappixmapitem.h @@ -5,6 +5,7 @@ #include #include +#include class CityMapPixmapItem : public QObject, public QGraphicsPixmapItem { Q_OBJECT @@ -30,6 +31,9 @@ public: int width(); int height(); + QVector getTiles(); + void setTiles(QVector); + private: int width_; int height_; @@ -41,6 +45,8 @@ signals: protected: void mousePressEvent(QGraphicsSceneMouseEvent*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*); }; #endif // CITYMAPPIXMAPITEM_H diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index f1dd6507..1a44a873 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace Ui { class RegionMapEditor; @@ -53,13 +54,15 @@ private: unsigned selectedCityTile; unsigned selectedImageTile; + bool hasUnsavedChanges = false; + double scaleUpFactor = 2.0; double scaleDownFactor = 1.0 / scaleUpFactor; - double scaleRegionMapTiles = 1.0; - double scaleRegionMapImage = 1.0; - double scaleCityMapTiles = 1.0; - double scaleCityMapImage = 1.0; + int scaleRegionMapTiles = 1; + int scaleRegionMapImage = 1; + int scaleCityMapTiles = 1; + int scaleCityMapImage = 1; QGraphicsScene *scene_region_map_image = nullptr; QGraphicsScene *scene_city_map_image = nullptr; @@ -85,6 +88,8 @@ private: bool createCityMap(QString); + void closeEvent(QCloseEvent*); + private slots: void on_action_RegionMap_Save_triggered(); void on_action_RegionMap_Undo_triggered(); @@ -92,21 +97,17 @@ private slots: void on_action_RegionMap_Resize_triggered(); void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); - void on_pushButton_RM_Options_save_clicked(); void on_pushButton_RM_Options_delete_clicked(); - void on_pushButton_CityMap_save_clicked(); + void on_comboBox_RM_ConnectedMap_activated(const QString &); void on_pushButton_CityMap_add_clicked(); - void on_pushButton_Zoom_In_Image_Tiles_clicked(); - void on_pushButton_Zoom_Out_Image_Tiles_clicked(); - void on_pushButton_Zoom_In_City_Tiles_clicked(); - void on_pushButton_Zoom_Out_City_Tiles_clicked(); - void on_pushButton_Zoom_In_City_Map_clicked(); - void on_pushButton_Zoom_Out_City_Map_clicked(); - void on_pushButton_Zoom_In_Map_Image_clicked(); - void on_pushButton_Zoom_Out_Map_Image_clicked(); + void on_verticalSlider_Zoom_Map_Image_valueChanged(int); + void on_verticalSlider_Zoom_Image_Tiles_valueChanged(int); + void on_verticalSlider_Zoom_City_Map_valueChanged(int); + void on_verticalSlider_Zoom_City_Tiles_valueChanged(int); void on_comboBox_CityMap_picker_currentTextChanged(const QString &); void on_spinBox_RM_Options_x_valueChanged(int); void on_spinBox_RM_Options_y_valueChanged(int); + void on_lineEdit_RM_MapName_textEdited(const QString &); void onHoveredRegionMapTileChanged(int, int); void onHoveredRegionMapTileCleared(); void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); diff --git a/include/ui/tilemaptileselector.h b/include/ui/tilemaptileselector.h index 1bf94984..7bf2b632 100644 --- a/include/ui/tilemaptileselector.h +++ b/include/ui/tilemaptileselector.h @@ -19,7 +19,7 @@ public: int pixelWidth; int pixelHeight; - unsigned selectedTile; + unsigned selectedTile = 0; // TODO: which of these need to be made public? // call this tilemap? or is tilemap the binary file? diff --git a/src/core/historyitem.cpp b/src/core/historyitem.cpp index 1a8c44d2..997de9f6 100644 --- a/src/core/historyitem.cpp +++ b/src/core/historyitem.cpp @@ -10,11 +10,22 @@ HistoryItem::~HistoryItem() { if (this->metatiles) delete this->metatiles; } -RegionMapHistoryItem::RegionMapHistoryItem(int which_, int index_, unsigned prev_, unsigned tile_) { - this->which = which_; - this->index = index_; - this->prev = prev_; - this->tile = tile_; +RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector tiles) { + this->which = which; + this->tiles = tiles; +} + +RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector tiles, QString cityMap) { + this->which = which; + this->tiles = tiles; + this->cityMap = cityMap; +} + +RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector tiles, int width, int height) { + this->which = which; + this->tiles = tiles; + this->mapWidth = width; + this->mapHeight = height; } RegionMapHistoryItem::~RegionMapHistoryItem() {} diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 7dde15dd..678ed2a2 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -79,6 +79,7 @@ void RegionMap::readLayout() { QMap *qmap = new QMap; QTextStream in(&file); + in.setCodec("UTF-8"); while (!in.atEnd()) { line = in.readLine(); if (line.startsWith("static const u8")) { @@ -182,7 +183,29 @@ int RegionMap::height() { } QSize RegionMap::imgSize() { - return QSize(img_width_ * 8 + 2, img_height_ * 8 + 2); + return QSize(img_width_ * 8, img_height_ * 8); +} + +QVector RegionMap::getTiles() { + // + QVector tileIds; + for (auto square : map_squares) { + tileIds.append(square.tile_img_id); + } + return tileIds; +} + +void RegionMap::setTiles(QVector tileIds) { + // + if (tileIds.size() != map_squares.size()) { + qDebug() << "YOU SHOULD RESIZE"; + return; + } + int i = 0; + for (uint8_t tileId : tileIds) { + map_squares[i].tile_img_id = tileId; + i++; + } } // TODO: rename to getTileIdAt()? diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index b1224aa9..da920ca6 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -62,6 +62,39 @@ void CityMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { emit mouseEvent(event, this); } +void CityMapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + if (x < width_ && x >= 0 + && y < height_ && y >= 0) { + emit this->hoveredRegionMapTileChanged(x, y); + emit mouseEvent(event, this); + } +} + +void CityMapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + emit mouseEvent(event, this); +} + +QVector CityMapPixmapItem::getTiles() { + // + QVector tiles; + for (auto tile : data) { + tiles.append(tile); + } + return tiles; +} + +void CityMapPixmapItem::setTiles(QVector tiles) { + // + QByteArray newData; + for (auto tile : tiles) { + newData.append(tile); + } + this->data = newData; +} + int CityMapPixmapItem::getIndexAt(int x, int y) { return 2 * (x + y * this->width_); } diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 226e7bd0..3c9ea146 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -9,15 +9,16 @@ #include #include #include +#include +#include -RegionMapEditor::RegionMapEditor(QWidget *parent, Project *pro) : +RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : QMainWindow(parent), ui(new Ui::RegionMapEditor) { this->ui->setupUi(this); - this->project = pro; + this->project = project_; this->region_map = new RegionMap; - this->setFixedSize(this->size()); } RegionMapEditor::~RegionMapEditor() @@ -39,8 +40,18 @@ RegionMapEditor::~RegionMapEditor() void RegionMapEditor::on_action_RegionMap_Save_triggered() { if (project && region_map) { region_map->save(); + this->city_map_item->save(); + this->region_map->saveOptions( + this->region_map_layout_item->selectedTile, + this->ui->comboBox_RM_ConnectedMap->currentText(), + this->ui->lineEdit_RM_MapName->text(), + this->ui->spinBox_RM_Options_x->value(), + this->ui->spinBox_RM_Options_y->value() + ); + this->region_map_layout_item->highlightedTile = -1; displayRegionMap(); } + this->hasUnsavedChanges = false; } void RegionMapEditor::loadRegionMapData() { @@ -90,7 +101,11 @@ void RegionMapEditor::displayRegionMapImage() { this->scene_region_map_image->setSceneRect(this->scene_region_map_image->itemsBoundingRect()); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize().width() * scaleRegionMapImage + 2, + this->region_map->imgSize().height() * scaleRegionMapImage + 2); + + RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, this->region_map->getTiles()); + history.push(commit); } void RegionMapEditor::displayRegionMapLayout() { @@ -118,7 +133,8 @@ void RegionMapEditor::displayRegionMapLayout() { this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->itemsBoundingRect()); this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize().width() * scaleRegionMapImage + 2, + this->region_map->imgSize().height() * scaleRegionMapImage + 2); } void RegionMapEditor::displayRegionMapLayoutOptions() { @@ -135,17 +151,23 @@ void RegionMapEditor::displayRegionMapLayoutOptions() { ); updateRegionMapLayoutOptions(currIndex); + + // TODO: implement when the code is decompiled + this->ui->label_RM_CityMap->setVisible(false); + this->ui->comboBox_RM_CityMap->setVisible(false); } void RegionMapEditor::updateRegionMapLayoutOptions(int index) { this->ui->spinBox_RM_Options_x->blockSignals(true); this->ui->spinBox_RM_Options_y->blockSignals(true); + this->ui->comboBox_RM_ConnectedMap->blockSignals(true); this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec)); this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); this->ui->spinBox_RM_Options_x->blockSignals(false); this->ui->spinBox_RM_Options_y->blockSignals(false); + this->ui->comboBox_RM_ConnectedMap->blockSignals(false); } void RegionMapEditor::displayRegionMapTileSelector() { @@ -223,6 +245,11 @@ void RegionMapEditor::displayCityMap(QString f) { this->ui->graphicsView_City_Map->setScene(scene_city_map_image); this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, 8 * city_map_item->height() * scaleCityMapImage + 2); + + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::CityMapImage, this->city_map_item->getTiles(), this->city_map_item->file + ); + history.push(commit); } bool RegionMapEditor::createCityMap(QString name) { @@ -313,35 +340,53 @@ void RegionMapEditor::onHoveredRegionMapTileCleared() { } void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *item) { + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = this->region_map->getMapSquareIndex(x, y); + if (index > this->region_map->map_squares.size() - 1) return; + if (event->buttons() & Qt::RightButton) { item->select(event); //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - QPointF pos = event->pos(); - int x = static_cast(pos.x()) / 8; - int y = static_cast(pos.y()) / 8; - int index = this->region_map->getMapSquareIndex(x, y); - if (index > this->region_map->map_squares.size() - 1) return; - RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, index, - this->region_map->map_squares[index].tile_img_id, this->mapsquare_selector_item->getSelectedTile()); - history.push(commit); item->paint(event); + this->hasUnsavedChanges = true; + if (event->type() == QEvent::GraphicsSceneMouseRelease) { + RegionMapHistoryItem *current = history.current(); + bool addToHistory = !(current && current->tiles == this->region_map->getTiles()); + if (addToHistory) { + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::BackgroundImage, this->region_map->getTiles() + ); + history.push(commit); + } + } } } void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityMapPixmapItem *item) { // + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = this->city_map_item->getIndexAt(x, y); + if (event->buttons() & Qt::RightButton) {// TODO //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - QPointF pos = event->pos(); - int x = static_cast(pos.x()) / 8; - int y = static_cast(pos.y()) / 8; - int index = this->city_map_item->getIndexAt(x, y); - RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::CityMapImage, index, - this->city_map_item->data[index], this->city_map_selector_item->getSelectedTile()); - history.push(commit); item->paint(event); + this->hasUnsavedChanges = true; + if (event->type() == QEvent::GraphicsSceneMouseRelease) { + RegionMapHistoryItem *current = history.current(); + bool addToHistory = !(current && current->tiles == this->city_map_item->getTiles()); + if (addToHistory) { + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::CityMapImage, this->city_map_item->getTiles(), this->city_map_item->file + ); + history.push(commit); + } + } } } @@ -350,12 +395,10 @@ void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { switch (index) { case 0: - this->ui->pushButton_Zoom_In_Image_Tiles->setVisible(true); - this->ui->pushButton_Zoom_Out_Image_Tiles->setVisible(true); + this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(true); break; case 1: - this->ui->pushButton_Zoom_In_Image_Tiles->setVisible(false); - this->ui->pushButton_Zoom_Out_Image_Tiles->setVisible(false); + this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(false); break; } } @@ -364,34 +407,30 @@ void RegionMapEditor::on_spinBox_RM_Options_x_valueChanged(int x) { int y = this->ui->spinBox_RM_Options_y->value(); int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); this->region_map_layout_item->highlight(x, y, red); + this->hasUnsavedChanges = true; } void RegionMapEditor::on_spinBox_RM_Options_y_valueChanged(int y) { int x = this->ui->spinBox_RM_Options_x->value(); int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); this->region_map_layout_item->highlight(x, y, red); + this->hasUnsavedChanges = true; } -void RegionMapEditor::on_pushButton_RM_Options_save_clicked() { - this->region_map->saveOptions( - this->region_map_layout_item->selectedTile, - this->ui->comboBox_RM_ConnectedMap->currentText(), - this->ui->lineEdit_RM_MapName->text(), - this->ui->spinBox_RM_Options_x->value(), - this->ui->spinBox_RM_Options_y->value() - ); - this->region_map_layout_item->highlightedTile = -1; - this->region_map_layout_item->draw(); +void RegionMapEditor::on_comboBox_RM_ConnectedMap_activated(const QString &mapsec) { + this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(mapsec)); + //this->hasUnsavedChanges = true;// sometimes this is called for unknown reasons } -void RegionMapEditor::on_pushButton_CityMap_save_clicked() { - this->city_map_item->save(); +void RegionMapEditor::on_lineEdit_RM_MapName_textEdited(const QString &text) { + this->hasUnsavedChanges = true; } void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() { this->region_map->resetSquare(this->region_map_layout_item->selectedTile); this->region_map_layout_item->draw(); this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); + this->hasUnsavedChanges = true; } void RegionMapEditor::on_pushButton_CityMap_add_clicked() { @@ -419,6 +458,8 @@ void RegionMapEditor::on_pushButton_CityMap_add_clicked() { if (popup.exec() == QDialog::Accepted) { createCityMap(name); } + + this->hasUnsavedChanges = true; } void RegionMapEditor::on_action_RegionMap_Resize_triggered() { @@ -448,59 +489,75 @@ void RegionMapEditor::on_action_RegionMap_Resize_triggered() { if (popup.exec() == QDialog::Accepted) { resize(widthSpinBox->value(), heightSpinBox->value()); } + + this->hasUnsavedChanges = true; } void RegionMapEditor::on_action_RegionMap_Undo_triggered() { undo(); + this->hasUnsavedChanges = true; } +// TODO: add resizing void RegionMapEditor::undo() { - RegionMapHistoryItem *commit = history.current(); + RegionMapHistoryItem *commit = history.back(); if (!commit) return; - uint8_t tile = static_cast(commit->prev); - - history.back(); - switch (commit->which) { case RegionMapEditorBox::BackgroundImage: - history.back();// TODO: why do I need to do this? - this->region_map->map_squares[commit->index].tile_img_id = tile; + this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); break; case RegionMapEditorBox::CityMapImage: - this->city_map_item->data[commit->index] = tile; + if (commit->cityMap == this->city_map_item->file) + this->city_map_item->setTiles(commit->tiles); this->city_map_item->draw(); break; + case RegionMapEditorBox::BackroundResize: + this->region_map->resize(commit->mapWidth, commit->mapHeight); + displayRegionMap(); + break; } } void RegionMapEditor::on_action_RegionMap_Redo_triggered() { redo(); + this->hasUnsavedChanges = true; } void RegionMapEditor::redo() { RegionMapHistoryItem *commit = history.next(); if (!commit) return; - uint8_t tile = static_cast(commit->tile); - switch (commit->which) { case RegionMapEditorBox::BackgroundImage: - history.next();// TODO: why do I need to do this? - this->region_map->map_squares[commit->index].tile_img_id = tile; + this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); break; case RegionMapEditorBox::CityMapImage: - this->city_map_item->data[commit->index] = tile; + this->city_map_item->setTiles(commit->tiles); this->city_map_item->draw(); break; + case RegionMapEditorBox::BackroundResize: + this->region_map->resize(commit->mapWidth, commit->mapHeight); + displayRegionMap(); + break; } } void RegionMapEditor::resize(int w, int h) { + RegionMapHistoryItem *commitOld = new RegionMapHistoryItem( + RegionMapEditorBox::BackroundResize, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() + ); + RegionMapHistoryItem *commitNew = new RegionMapHistoryItem( + RegionMapEditorBox::BackroundResize, this->region_map->getTiles(), w, h + ); + history.push(commitOld); + history.push(commitNew); + history.back(); + this->region_map->resize(w, h); this->currIndex = 2 * w + 1; displayRegionMap(); @@ -510,74 +567,93 @@ void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QStrin this->displayCityMap(file); } -void RegionMapEditor::on_pushButton_Zoom_In_Image_Tiles_clicked() { - if (scaleRegionMapTiles >= 8.0) return; - scaleRegionMapTiles *= 2.0; - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, - this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); - this->ui->graphicsView_RegionMap_Tiles->scale(2.0, 2.0); +void RegionMapEditor::closeEvent(QCloseEvent *event) +{ + if (this->hasUnsavedChanges) { + QMessageBox::StandardButton result = QMessageBox::question( + this, + "porymap", + "The region map has been modified, save changes?", + QMessageBox::No | QMessageBox::Yes | QMessageBox::Cancel, + QMessageBox::Yes); + + if (result == QMessageBox::Yes) { + this->on_action_RegionMap_Save_triggered(); + event->accept(); + } else if (result == QMessageBox::No) { + event->accept(); + } else if (result == QMessageBox::Cancel) { + event->ignore(); + } + } else { + event->accept(); + } } -void RegionMapEditor::on_pushButton_Zoom_Out_Image_Tiles_clicked() { - if (scaleRegionMapTiles <= 1.0) return; - scaleRegionMapTiles /= 2.0; - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, - this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); - this->ui->graphicsView_RegionMap_Tiles->scale(0.5, 0.5); +void RegionMapEditor::on_verticalSlider_Zoom_Map_Image_valueChanged(int val) { + bool zoom_in = val > scaleRegionMapImage ? true : false; + scaleRegionMapImage = val; + + this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize().width() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2, + this->region_map->imgSize().height() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2); + this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize().width() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2, + this->region_map->imgSize().height() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2); + + if (zoom_in) { + this->ui->graphicsView_Region_Map_BkgImg->scale(scaleUpFactor, scaleUpFactor); + this->ui->graphicsView_Region_Map_Layout->scale(scaleUpFactor, scaleUpFactor); + } else { + // + this->ui->graphicsView_Region_Map_BkgImg->scale(scaleDownFactor, scaleDownFactor); + this->ui->graphicsView_Region_Map_Layout->scale(scaleDownFactor, scaleDownFactor); + } } -void RegionMapEditor::on_pushButton_Zoom_In_City_Tiles_clicked() { - if (scaleCityMapTiles >= 8.0) return; - scaleCityMapTiles *= 2.0; - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, - this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); - this->ui->graphicsView_City_Map_Tiles->scale(2.0,2.0); +void RegionMapEditor::on_verticalSlider_Zoom_Image_Tiles_valueChanged(int val) { + bool zoom_in = val > scaleRegionMapTiles ? true : false; + scaleRegionMapTiles = val; + + this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * pow(scaleUpFactor, scaleRegionMapTiles - 1) + 2, + this->mapsquare_selector_item->pixelHeight * pow(scaleUpFactor, scaleRegionMapTiles - 1) + 2); + + if (zoom_in) { + this->ui->graphicsView_RegionMap_Tiles->scale(scaleUpFactor, scaleUpFactor); + } else { + this->ui->graphicsView_RegionMap_Tiles->scale(scaleDownFactor, scaleDownFactor); + } } -void RegionMapEditor::on_pushButton_Zoom_Out_City_Tiles_clicked() { - if (scaleCityMapTiles <= 1.0) return; - scaleCityMapTiles /= 2.0; - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, - this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); - this->ui->graphicsView_City_Map_Tiles->scale(0.5,0.5); +void RegionMapEditor::on_verticalSlider_Zoom_City_Map_valueChanged(int val) { + bool zoom_in = val > scaleCityMapImage ? true : false; + scaleCityMapImage = val; + + this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * pow(scaleUpFactor, scaleCityMapImage - 1) + 2, + 8 * city_map_item->height() * pow(scaleUpFactor, scaleCityMapImage - 1) + 2); + + if (zoom_in) { + this->ui->graphicsView_City_Map->scale(scaleUpFactor, scaleUpFactor); + } else { + this->ui->graphicsView_City_Map->scale(scaleDownFactor, scaleDownFactor); + } } -void RegionMapEditor::on_pushButton_Zoom_In_City_Map_clicked() { - if (scaleCityMapImage >= 8.0) return; - scaleCityMapImage *= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, - 8 * city_map_item->height() * scaleCityMapImage + 2); - this->ui->graphicsView_City_Map->scale(2.0,2.0); -} +void RegionMapEditor::on_verticalSlider_Zoom_City_Tiles_valueChanged(int val) { + bool zoom_in = val > scaleCityMapTiles ? true : false; + scaleCityMapTiles = val; -void RegionMapEditor::on_pushButton_Zoom_Out_City_Map_clicked() { - if (scaleCityMapImage <= 1.0) return; - scaleCityMapImage /= 2.0; - this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, - 8 * city_map_item->height() * scaleCityMapImage + 2); - this->ui->graphicsView_City_Map->scale(0.5,0.5); -} + this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * pow(scaleUpFactor, scaleCityMapTiles - 1) + 2, + this->city_map_selector_item->pixelHeight * pow(scaleUpFactor, scaleCityMapTiles - 1) + 2); -void RegionMapEditor::on_pushButton_Zoom_In_Map_Image_clicked() { - if (scaleRegionMapImage >= 8.0) return; - scaleRegionMapImage *= 2.0; - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); - this->ui->graphicsView_Region_Map_BkgImg->scale(2.0,2.0); - this->ui->graphicsView_Region_Map_Layout->scale(2.0,2.0); -} - -void RegionMapEditor::on_pushButton_Zoom_Out_Map_Image_clicked() { - if (scaleRegionMapImage <= 1.0) return; - scaleRegionMapImage /= 2.0; - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize() * scaleRegionMapImage); - this->ui->graphicsView_Region_Map_BkgImg->scale(0.5,0.5); - this->ui->graphicsView_Region_Map_Layout->scale(0.5,0.5); + if (zoom_in) { + this->ui->graphicsView_City_Map_Tiles->scale(scaleUpFactor, scaleUpFactor); + } else { + this->ui->graphicsView_City_Map_Tiles->scale(scaleDownFactor, scaleDownFactor); + } } void RegionMapEditor::on_action_RegionMap_Generate_triggered() { // RegionMapGenerator generator(this->project); generator.generate("LittlerootTown"); + this->hasUnsavedChanges = true; } diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp index 2da3ae5d..8654b66b 100644 --- a/src/ui/regionmaplayoutpixmapitem.cpp +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -73,10 +73,7 @@ void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) } void RegionMapLayoutPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - SelectablePixmapItem::mouseMoveEvent(event); - this->updateSelectedTile(); - emit hoveredTileChanged(this->selectedTile); - emit selectedTileChanged(this->selectedTile); + mousePressEvent(event); } void RegionMapLayoutPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index c75341d7..6f3886c7 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -60,10 +60,14 @@ void RegionMapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { } void RegionMapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - int x = static_cast(event->pos().x()) / 8; - int y = static_cast(event->pos().y()) / 8; - emit this->hoveredRegionMapTileChanged(x, y); - emit mouseEvent(event, this); + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + if (x < this->region_map->width() && x >= 0 + && y < this->region_map->height() && y >= 0) { + emit this->hoveredRegionMapTileChanged(x, y); + emit mouseEvent(event, this); + } } void RegionMapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { From 38cb97793d8b98e382bba0a911746f58b5867231 Mon Sep 17 00:00:00 2001 From: garak Date: Mon, 28 Jan 2019 13:47:20 -0500 Subject: [PATCH 10/17] fix outstanding rme bugs and history --- forms/mainwindow.ui | 1 + forms/regionmapeditor.ui | 28 ++-- include/core/historyitem.h | 7 +- include/core/regionmap.h | 111 ++++++--------- include/ui/regionmapeditor.h | 38 ++--- include/ui/tilemaptileselector.h | 13 +- porymap.pro | 2 - src/core/historyitem.cpp | 5 - src/core/regionmap.cpp | 201 +++++++++++++-------------- src/project.cpp | 1 - src/ui/citymappixmapitem.cpp | 4 - src/ui/regionmapeditor.cpp | 75 +++++----- src/ui/regionmaplayoutpixmapitem.cpp | 6 +- src/ui/regionmappixmapitem.cpp | 3 - src/ui/tilemaptileselector.cpp | 26 ++-- 15 files changed, 235 insertions(+), 286 deletions(-) diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 61aaa45a..4e60a96a 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -2976,6 +2976,7 @@ Cursor Tile Outline + Region Map Editor diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 6e2e06ce..1d521a36 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -61,8 +61,8 @@ 0 0 - 345 - 188 + 348 + 225 @@ -182,8 +182,8 @@ 0 0 - 345 - 188 + 348 + 225 @@ -332,8 +332,8 @@ 8 0 - 281 - 225 + 278 + 262 @@ -652,7 +652,7 @@ 0 0 440 - 267 + 230 @@ -788,7 +788,7 @@ 8 0 254 - 311 + 274 @@ -902,6 +902,9 @@ 160 + + Qt::StrongFocus + 1 @@ -946,6 +949,9 @@ 160 + + Qt::StrongFocus + 1 @@ -981,6 +987,9 @@ 160 + + Qt::StrongFocus + 1 @@ -1025,6 +1034,9 @@ 160 + + Qt::StrongFocus + 1 diff --git a/include/core/historyitem.h b/include/core/historyitem.h index 7d87795d..92675055 100644 --- a/include/core/historyitem.h +++ b/include/core/historyitem.h @@ -2,7 +2,6 @@ #define HISTORYITEM_H #include "blockdata.h" -//#include "regionmap.h" class HistoryItem { public: @@ -16,17 +15,15 @@ public: enum RegionMapEditorBox { BackgroundImage = 1, CityMapImage = 2, - BackroundResize = 3, }; class RegionMapHistoryItem { public: int which; - int mapWidth; - int mapHeight; + int mapWidth = 0; + int mapHeight = 0; QVector tiles; QString cityMap; - RegionMapHistoryItem(int type, QVector tiles); RegionMapHistoryItem(int type, QVector tiles, QString cityMap); RegionMapHistoryItem(int type, QVector tiles, int width, int height); ~RegionMapHistoryItem(); diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 7c4b70a9..7e7733e3 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -15,15 +15,6 @@ #include #include - - -struct CityMapPosition -{ - QString tilemap; - int x; - int y; -}; - struct RegionMapEntry { int x; @@ -38,14 +29,14 @@ class RegionMapSquare public: int x = -1; int y = -1; - uint8_t tile_img_id; + uint8_t tile_img_id = 0x00; + uint8_t secid = 0x00; bool has_map = false; + bool has_city_map = false; + bool duplicated = false; QString map_name; QString mapsec; - uint8_t secid; - bool has_city_map = false; QString city_map_name; - bool duplicated = false; }; class RegionMap : public QObject @@ -57,18 +48,45 @@ public: ~RegionMap() {}; - static QString mapSecToMapConstant(QString); - Project *project; QVector map_squares; + History history; const int padLeft = 1; const int padRight = 3; const int padTop = 2; const int padBottom = 3; - History history; + void init(Project*); + + void readBkgImgBin(); + void readLayout(); + + void save(); + void saveBkgImgBin(); + void saveLayout(); + void saveOptions(int id, QString sec, QString name, int x, int y); + + void resize(int width, int height); + void resetSquare(int index); + + int width(); + int height(); + QSize imgSize(); + unsigned getTileId(int x, int y); + int getMapSquareIndex(int x, int y); + QString pngPath(); + QString cityTilesPath(); + + QVector getTiles(); + void setTiles(QVector tileIds); + +private: + int layout_width_; + int layout_height_; + int img_width_; + int img_height_; QString region_map_png_path; QString region_map_bin_path; @@ -76,65 +94,14 @@ public: QString region_map_layout_bin_path; QString city_map_tiles_path; - QByteArray mapBinData; - - QMap sMapNamesMap;// {"{/sMapName_/}LittlerootTown" : "LITTLEROOT{NAME_END} TOWN"} - QMap mapSecToMapName;// {"MAPSEC_LITTLEROOT_TOWN" : "LITTLEROOT{NAME_END} TOWN"} - QMap mapSecToMapEntry;// TODO: add to this on creation of new map - + QMap sMapNamesMap; + QMap mapSecToMapEntry; QVector sMapNames; - bool hasUnsavedChanges(); - - void init(Project*); - - void readBkgImgBin(); - void readCityMaps(); - void readLayout(); - - QString newAbbr(QString);// makes a *unique* 5 character abbreviation from mapname to add to mapname_abbr - - // TODO: did I use these like, at all? - // editing functions - // if they are booleans, returns true if successful? - bool placeTile(char, int, int);// place tile at x, y - bool removeTile(char, int, int);// replaces with 0x00 byte at x,y - bool placeMap(QString, int, int); - bool removeMap(QString, int, int); - bool removeMap(QString);// remove all instances of map - - void save(); - void saveBkgImgBin(); - void saveLayout(); - void saveOptions(int, QString, QString, int, int); - void saveCityMaps(); - - void resize(int, int); - void setWidth(int); - void setHeight(int); - void setBackgroundImageData(QVector *); - int width(); - int height(); - QSize imgSize(); - unsigned getTileId(int, int); - int getMapSquareIndex(int, int); - - QVector getTiles(); - void setTiles(QVector); - - void resetSquare(int); - -// TODO: move read / write functions to private (and others) -private: - int layout_width_; - int layout_height_; - int img_width_; - int img_height_; - int img_index_(int, int);// returns index int at x,y args (x + y * width_ * 2) // 2 because - int layout_index_(int, int); - void fillMapSquaresFromLayout(); - QString fix_case(QString);// CAPS_WITH_UNDERSCORE to CamelCase + int img_index_(int x, int y); + int layout_index_(int x, int y); + QString fix_case(QString); }; #endif // REGIONMAP_H diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index 1a44a873..b9af6fd4 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -30,19 +30,19 @@ public: void loadRegionMapData(); void loadCityMaps(); - void onRegionMapTileSelectorSelectedTileChanged(unsigned); - void onCityMapTileSelectorSelectedTileChanged(unsigned); - void onRegionMapTileSelectorHoveredTileChanged(unsigned); + void onRegionMapTileSelectorSelectedTileChanged(unsigned id); + void onCityMapTileSelectorSelectedTileChanged(unsigned id); + void onRegionMapTileSelectorHoveredTileChanged(unsigned id); void onRegionMapTileSelectorHoveredTileCleared(); - void onRegionMapLayoutSelectedTileChanged(int); - void onRegionMapLayoutHoveredTileChanged(int); + void onRegionMapLayoutSelectedTileChanged(int index); + void onRegionMapLayoutHoveredTileChanged(int index); void onRegionMapLayoutHoveredTileCleared(); void undo(); void redo(); - void resize(int, int); + void resize(int width, int height); private: Ui::RegionMapEditor *ui; @@ -55,6 +55,8 @@ private: unsigned selectedImageTile; bool hasUnsavedChanges = false; + bool cityMapFirstDraw = true; + bool regionMapFirstDraw = true; double scaleUpFactor = 2.0; double scaleDownFactor = 1.0 / scaleUpFactor; @@ -81,14 +83,14 @@ private: void displayRegionMapImage(); void displayRegionMapLayout(); void displayRegionMapLayoutOptions(); - void updateRegionMapLayoutOptions(int); + void updateRegionMapLayoutOptions(int index); void displayRegionMapTileSelector(); void displayCityMapTileSelector(); - void displayCityMap(QString); + void displayCityMap(QString name); - bool createCityMap(QString); + bool createCityMap(QString name); - void closeEvent(QCloseEvent*); + void closeEvent(QCloseEvent* event); private slots: void on_action_RegionMap_Save_triggered(); @@ -98,20 +100,20 @@ private slots: void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_delete_clicked(); - void on_comboBox_RM_ConnectedMap_activated(const QString &); + void on_comboBox_RM_ConnectedMap_activated(const QString &text); void on_pushButton_CityMap_add_clicked(); void on_verticalSlider_Zoom_Map_Image_valueChanged(int); void on_verticalSlider_Zoom_Image_Tiles_valueChanged(int); void on_verticalSlider_Zoom_City_Map_valueChanged(int); void on_verticalSlider_Zoom_City_Tiles_valueChanged(int); - void on_comboBox_CityMap_picker_currentTextChanged(const QString &); - void on_spinBox_RM_Options_x_valueChanged(int); - void on_spinBox_RM_Options_y_valueChanged(int); - void on_lineEdit_RM_MapName_textEdited(const QString &); - void onHoveredRegionMapTileChanged(int, int); + void on_comboBox_CityMap_picker_currentTextChanged(const QString &text); + void on_spinBox_RM_Options_x_valueChanged(int val); + void on_spinBox_RM_Options_y_valueChanged(int val); + void on_lineEdit_RM_MapName_textEdited(const QString &text); + void onHoveredRegionMapTileChanged(int x, int y); void onHoveredRegionMapTileCleared(); - void mouseEvent_region_map(QGraphicsSceneMouseEvent *, RegionMapPixmapItem *); - void mouseEvent_city_map(QGraphicsSceneMouseEvent *, CityMapPixmapItem *); + void mouseEvent_region_map(QGraphicsSceneMouseEvent *event, RegionMapPixmapItem *item); + void mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityMapPixmapItem *item); }; #endif // REGIONMAPEDITOR_H diff --git a/include/ui/tilemaptileselector.h b/include/ui/tilemaptileselector.h index 7bf2b632..293ad3af 100644 --- a/include/ui/tilemaptileselector.h +++ b/include/ui/tilemaptileselector.h @@ -6,9 +6,10 @@ class TilemapTileSelector: public SelectablePixmapItem { Q_OBJECT public: - TilemapTileSelector(QPixmap pixmap): SelectablePixmapItem(8, 8, 1, 1) { - this->pixmap = pixmap; - this->numTilesWide = pixmap.width() / 8; + TilemapTileSelector(QPixmap pixmap_): SelectablePixmapItem(8, 8, 1, 1) { + this->tilemap = pixmap_; + this->setPixmap(this->tilemap); + this->numTilesWide = tilemap.width() / 8; this->selectedTile = 0x00; setAcceptHoverEvents(true); } @@ -21,10 +22,7 @@ public: unsigned selectedTile = 0; - // TODO: which of these need to be made public? - // call this tilemap? or is tilemap the binary file? - QPixmap pixmap;// pointer? - QImage currTile;// image of just the currently selected tile to draw onto graphicsview + QPixmap tilemap; QImage tileImg(unsigned tileId); protected: @@ -40,7 +38,6 @@ private: void updateSelectedTile(); unsigned getTileId(int x, int y); QPoint getTileIdCoords(unsigned); - unsigned getValidTileId(unsigned);// TODO: implement this to prevent segfaults signals: void hoveredTileChanged(unsigned); diff --git a/porymap.pro b/porymap.pro index 7a72c3d8..754f45f1 100644 --- a/porymap.pro +++ b/porymap.pro @@ -28,7 +28,6 @@ SOURCES += src/core/block.cpp \ src/core/parseutil.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ - src/core/regionmapeditor.cpp \ src/core/regionmap.cpp \ src/core/regionmapgenerator.cpp \ src/ui/aboutporymap.cpp \ @@ -86,7 +85,6 @@ HEADERS += include/core/block.h \ include/core/parseutil.h \ include/core/tile.h \ include/core/tileset.h \ - include/core/regionmapeditor.h \ include/core/regionmap.h \ include/core/regionmapgenerator.h \ include/ui/aboutporymap.h \ diff --git a/src/core/historyitem.cpp b/src/core/historyitem.cpp index 997de9f6..1b0f3888 100644 --- a/src/core/historyitem.cpp +++ b/src/core/historyitem.cpp @@ -10,11 +10,6 @@ HistoryItem::~HistoryItem() { if (this->metatiles) delete this->metatiles; } -RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector tiles) { - this->which = which; - this->tiles = tiles; -} - RegionMapHistoryItem::RegionMapHistoryItem(int which, QVector tiles, QString cityMap) { this->which = which; this->tiles = tiles; diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 678ed2a2..3720e03d 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -7,8 +7,7 @@ #include #include #include - - +#include void RegionMap::init(Project *pro) { QString path = pro->root; @@ -29,10 +28,15 @@ void RegionMap::init(Project *pro) { readBkgImgBin(); readLayout(); - readCityMaps(); } -// TODO: if the tileId is not valid for the provided image, make sure it does not crash +void RegionMap::save() { + logInfo("Saving region map data."); + saveBkgImgBin(); + saveLayout(); + porymapConfig.setRegionMapDimensions(this->img_width_, this->img_height_); +} + void RegionMap::readBkgImgBin() { QFile binFile(region_map_bin_path); if (!binFile.open(QIODevice::ReadOnly)) return; @@ -54,7 +58,7 @@ void RegionMap::readBkgImgBin() { } void RegionMap::saveBkgImgBin() { - QByteArray data(4096,0);// use a constant here? maybe read the original size? + QByteArray data(pow(img_width_ * 2, 2),0); for (int m = 0; m < img_height_; m++) { for (int n = 0; n < img_width_; n++) { @@ -68,9 +72,7 @@ void RegionMap::saveBkgImgBin() { file.close(); } -// TODO: reorganize this into project? the i/o stuff. use regionMapSections void RegionMap::readLayout() { - // QFile file(region_map_entries_path); if (!file.open(QIODevice::ReadOnly)) return; @@ -111,17 +113,16 @@ void RegionMap::readLayout() { QByteArray mapBinData = binFile.readAll(); binFile.close(); - // TODO: improve this? - for (int m = 0; m < layout_height_; m++) { - for (int n = 0; n < layout_width_; n++) { - int i = img_index_(n,m); - map_squares[i].secid = static_cast(mapBinData.at(layout_index_(n,m))); - QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(n,m)))]; + for (int y = 0; y < layout_height_; y++) { + for (int x = 0; x < layout_width_; x++) { + int i = img_index_(x,y); + map_squares[i].secid = static_cast(mapBinData.at(layout_index_(x,y))); + QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(x,y)))]; if (secname != "MAPSEC_NONE") map_squares[i].has_map = true; map_squares[i].mapsec = secname; - map_squares[i].map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name);// TODO: this is atrocious - map_squares[i].x = n; - map_squares[i].y = m; + map_squares[i].map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name); + map_squares[i].x = x; + map_squares[i].y = y; } } } @@ -162,69 +163,11 @@ void RegionMap::saveLayout() { bfile.close(); } -void RegionMap::readCityMaps() {} - -// layout coords to image index -int RegionMap::img_index_(int x, int y) { - return ((x + this->padLeft) + (y + this->padTop) * img_width_); -} - -// layout coords to layout index -int RegionMap::layout_index_(int x, int y) { - return (x + y * layout_width_); -} - -int RegionMap::width() { - return this->img_width_; -} - -int RegionMap::height() { - return this->img_height_; -} - -QSize RegionMap::imgSize() { - return QSize(img_width_ * 8, img_height_ * 8); -} - -QVector RegionMap::getTiles() { - // - QVector tileIds; - for (auto square : map_squares) { - tileIds.append(square.tile_img_id); - } - return tileIds; -} - -void RegionMap::setTiles(QVector tileIds) { - // - if (tileIds.size() != map_squares.size()) { - qDebug() << "YOU SHOULD RESIZE"; - return; - } - int i = 0; - for (uint8_t tileId : tileIds) { - map_squares[i].tile_img_id = tileId; - i++; - } -} - -// TODO: rename to getTileIdAt()? -unsigned RegionMap::getTileId(int x, int y) { - return map_squares[x + y * img_width_].tile_img_id; -} - -void RegionMap::save() { - logInfo("Saving region map info."); - saveBkgImgBin(); - saveLayout(); - porymapConfig.setRegionMapDimensions(this->img_width_, this->img_height_); -} - void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { resetSquare(id); int index = getMapSquareIndex(x + this->padLeft, y + this->padTop); if (!sec.isEmpty()) { - this->map_squares[index].has_map = true; + this->map_squares[index].has_map = sec == "MAPSEC_NONE" ? false : true; this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf(sec)); this->map_squares[index].mapsec = sec; if (!name.isEmpty()) { @@ -234,7 +177,7 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { QString sName = fix_case(sec); sMapNames.append(sName); sMapNamesMap.insert(sName, name); - struct RegionMapEntry entry = {x, y, 1, 1, sName};// TODO: change width, height? + struct RegionMapEntry entry = {x, y, 1, 1, sName}; mapSecToMapEntry.insert(sec, entry); } } @@ -242,32 +185,6 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { this->map_squares[index].y = y; this->map_squares[index].duplicated = false; } - //resetSquare(id); -} - -// from x, y of image -int RegionMap::getMapSquareIndex(int x, int y) { - int index = (x + y * img_width_); - return index < map_squares.length() ? index : 0; -} - -// For turning a MAPSEC_NAME into a unique identifier sMapName-style variable. -QString RegionMap::fix_case(QString caps) { - bool big = true; - QString camel; - - for (auto ch : caps.remove(QRegularExpression("({.*})")).remove("MAPSEC")) { - if (ch == '_' || ch == ' ') { - big = true; - continue; - } - if (big) { - camel += ch.toUpper(); - big = false; - } - else camel += ch.toLower(); - } - return camel; } void RegionMap::resetSquare(int index) { @@ -282,7 +199,6 @@ void RegionMap::resetSquare(int index) { } void RegionMap::resize(int newWidth, int newHeight) { - // QVector new_squares; for (int y = 0; y < newHeight; y++) { @@ -301,10 +217,87 @@ void RegionMap::resize(int newWidth, int newHeight) { new_squares.append(square); } } - this->map_squares = new_squares; this->img_width_ = newWidth; this->img_height_ = newHeight; this->layout_width_ = newWidth - this->padLeft - this->padRight; this->layout_height_ = newHeight - this->padTop - this->padBottom; } + +QVector RegionMap::getTiles() { + QVector tileIds; + for (auto square : map_squares) { + tileIds.append(square.tile_img_id); + } + return tileIds; +} + +void RegionMap::setTiles(QVector tileIds) { + if (tileIds.size() != map_squares.size()) return; + + int i = 0; + for (uint8_t tileId : tileIds) { + map_squares[i].tile_img_id = tileId; + i++; + } +} + +// Layout coords to image index. +int RegionMap::img_index_(int x, int y) { + return ((x + this->padLeft) + (y + this->padTop) * img_width_); +} + +// Layout coords to layout index. +int RegionMap::layout_index_(int x, int y) { + return (x + y * layout_width_); +} + +int RegionMap::width() { + return this->img_width_; +} + +int RegionMap::height() { + return this->img_height_; +} + +QSize RegionMap::imgSize() { + return QSize(img_width_ * 8, img_height_ * 8); +} + +unsigned RegionMap::getTileId(int x, int y) { + return map_squares.at(x + y * img_width_).tile_img_id; +} + +QString RegionMap::pngPath() { + return this->region_map_png_path; +} + +QString RegionMap::cityTilesPath() { + return this->city_map_tiles_path; +} + +// From x, y of image. +int RegionMap::getMapSquareIndex(int x, int y) { + int index = (x + y * img_width_); + return index < map_squares.length() ? index : 0; +} + +// For turning a MAPSEC_NAME into a unique identifier sMapName-style variable. +// CAPS_WITH_UNDERSCORE to CamelCase +QString RegionMap::fix_case(QString caps) { + bool big = true; + QString camel; + + for (auto ch : caps.remove(QRegularExpression("({.*})")).remove("MAPSEC")) { + if (ch == '_' || ch == ' ') { + big = true; + continue; + } + if (big) { + camel += ch.toUpper(); + big = false; + } + else camel += ch.toLower(); + } + return camel; +} diff --git a/src/project.cpp b/src/project.cpp index d7e99ce3..d763efba 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1512,7 +1512,6 @@ void Project::readRegionMapSections() { QString filepath = root + "/include/constants/region_map_sections.h"; QStringList prefixes = (QStringList() << "MAPSEC_"); readCDefinesSorted(filepath, prefixes, regionMapSections); - regionMapSections->removeAll("MAPSEC_SUBTRACT_KANTO");// TODO: fix this (in repos?) } void Project::readItemNames() { diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index da920ca6..bbe31f65 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -6,8 +6,6 @@ #include #include - - void CityMapPixmapItem::init() { width_ = 10; height_ = 10; @@ -78,7 +76,6 @@ void CityMapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } QVector CityMapPixmapItem::getTiles() { - // QVector tiles; for (auto tile : data) { tiles.append(tile); @@ -87,7 +84,6 @@ QVector CityMapPixmapItem::getTiles() { } void CityMapPixmapItem::setTiles(QVector tiles) { - // QByteArray newData; for (auto tile : tiles) { newData.append(tile); diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 3c9ea146..42fff3ef 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -19,6 +19,7 @@ RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : this->ui->setupUi(this); this->project = project_; this->region_map = new RegionMap; + this->ui->action_RegionMap_Generate->setVisible(false); } RegionMapEditor::~RegionMapEditor() @@ -48,6 +49,7 @@ void RegionMapEditor::on_action_RegionMap_Save_triggered() { this->ui->spinBox_RM_Options_x->value(), this->ui->spinBox_RM_Options_y->value() ); + this->currIndex = this->region_map_layout_item->highlightedTile; this->region_map_layout_item->highlightedTile = -1; displayRegionMap(); } @@ -104,8 +106,13 @@ void RegionMapEditor::displayRegionMapImage() { this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize().width() * scaleRegionMapImage + 2, this->region_map->imgSize().height() * scaleRegionMapImage + 2); - RegionMapHistoryItem *commit = new RegionMapHistoryItem(RegionMapEditorBox::BackgroundImage, this->region_map->getTiles()); - history.push(commit); + if (regionMapFirstDraw) { + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() + ); + history.push(commit); + regionMapFirstDraw = false; + } } void RegionMapEditor::displayRegionMapLayout() { @@ -179,7 +186,7 @@ void RegionMapEditor::displayRegionMapTileSelector() { delete mapsquare_selector_item; } - this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->region_map_png_path)); + this->mapsquare_selector_item = new TilemapTileSelector(QPixmap(this->region_map->pngPath())); this->mapsquare_selector_item->draw(); this->scene_region_map_tiles->addItem(this->mapsquare_selector_item); @@ -207,7 +214,7 @@ void RegionMapEditor::displayCityMapTileSelector() { delete city_map_selector_item; } - this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->city_map_tiles_path)); + this->city_map_selector_item = new TilemapTileSelector(QPixmap(this->region_map->cityTilesPath())); this->city_map_selector_item->draw(); this->scene_city_map_tiles->addItem(this->city_map_selector_item); @@ -245,11 +252,6 @@ void RegionMapEditor::displayCityMap(QString f) { this->ui->graphicsView_City_Map->setScene(scene_city_map_image); this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * scaleCityMapImage + 2, 8 * city_map_item->height() * scaleCityMapImage + 2); - - RegionMapHistoryItem *commit = new RegionMapHistoryItem( - RegionMapEditorBox::CityMapImage, this->city_map_item->getTiles(), this->city_map_item->file - ); - history.push(commit); } bool RegionMapEditor::createCityMap(QString name) { @@ -302,9 +304,10 @@ void RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared() { void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { QString message = QString(); this->currIndex = index; + this->region_map_layout_item->highlightedTile = index; if (this->region_map->map_squares[index].has_map) { message = QString("\t %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}");// ruby-specific + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); } this->ui->statusbar->showMessage(message); @@ -319,7 +322,7 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { message = QString("(%1, %2)").arg(x).arg(y); if (this->region_map->map_squares[index].has_map) { message += QString("\t %1").arg(this->project->mapSecToMapHoverName->value( - this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); + this->region_map->map_squares[index].mapsec)).remove("{NAME_END}"); } } this->ui->statusbar->showMessage(message); @@ -330,7 +333,7 @@ void RegionMapEditor::onRegionMapLayoutHoveredTileCleared() { } void RegionMapEditor::onHoveredRegionMapTileChanged(int x, int y) { - QString message = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString message = QString("x: %1, y: %2 Tile: 0x").arg(x).arg(y) + QString("%1").arg(this->region_map->getTileId(x, y), 4, 16, QChar('0')).toUpper(); this->ui->statusbar->showMessage(message); } @@ -357,7 +360,7 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg bool addToHistory = !(current && current->tiles == this->region_map->getTiles()); if (addToHistory) { RegionMapHistoryItem *commit = new RegionMapHistoryItem( - RegionMapEditorBox::BackgroundImage, this->region_map->getTiles() + RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() ); history.push(commit); } @@ -366,12 +369,19 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg } void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityMapPixmapItem *item) { - // QPointF pos = event->pos(); int x = static_cast(pos.x()) / 8; int y = static_cast(pos.y()) / 8; int index = this->city_map_item->getIndexAt(x, y); + if (cityMapFirstDraw) { + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::CityMapImage, this->city_map_item->getTiles(), this->city_map_item->file + ); + history.push(commit); + cityMapFirstDraw = false; + } + if (event->buttons() & Qt::RightButton) {// TODO //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { @@ -473,7 +483,7 @@ void RegionMapEditor::on_action_RegionMap_Resize_triggered() { QSpinBox *heightSpinBox = new QSpinBox(); widthSpinBox->setMinimum(32); heightSpinBox->setMinimum(20); - widthSpinBox->setMaximum(60);// w * h * 2 <= 4960 + widthSpinBox->setMaximum(60);// TODO: find real limits heightSpinBox->setMaximum(40); widthSpinBox->setValue(this->region_map->width()); heightSpinBox->setValue(this->region_map->height()); @@ -488,6 +498,10 @@ void RegionMapEditor::on_action_RegionMap_Resize_triggered() { if (popup.exec() == QDialog::Accepted) { resize(widthSpinBox->value(), heightSpinBox->value()); + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), widthSpinBox->value(), heightSpinBox->value() + ); + history.push(commit); } this->hasUnsavedChanges = true; @@ -498,7 +512,6 @@ void RegionMapEditor::on_action_RegionMap_Undo_triggered() { this->hasUnsavedChanges = true; } -// TODO: add resizing void RegionMapEditor::undo() { RegionMapHistoryItem *commit = history.back(); if (!commit) return; @@ -506,6 +519,8 @@ void RegionMapEditor::undo() { switch (commit->which) { case RegionMapEditorBox::BackgroundImage: + if (commit->mapWidth != this->region_map->width() || commit->mapHeight != this->region_map->height()) + this->resize(commit->mapWidth, commit->mapHeight); this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); break; @@ -514,10 +529,6 @@ void RegionMapEditor::undo() { this->city_map_item->setTiles(commit->tiles); this->city_map_item->draw(); break; - case RegionMapEditorBox::BackroundResize: - this->region_map->resize(commit->mapWidth, commit->mapHeight); - displayRegionMap(); - break; } } @@ -533,6 +544,8 @@ void RegionMapEditor::redo() { switch (commit->which) { case RegionMapEditorBox::BackgroundImage: + if (commit->mapWidth != this->region_map->width() || commit->mapHeight != this->region_map->height()) + this->resize(commit->mapWidth, commit->mapHeight); this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); break; @@ -540,31 +553,20 @@ void RegionMapEditor::redo() { this->city_map_item->setTiles(commit->tiles); this->city_map_item->draw(); break; - case RegionMapEditorBox::BackroundResize: - this->region_map->resize(commit->mapWidth, commit->mapHeight); - displayRegionMap(); - break; } } void RegionMapEditor::resize(int w, int h) { - RegionMapHistoryItem *commitOld = new RegionMapHistoryItem( - RegionMapEditorBox::BackroundResize, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() - ); - RegionMapHistoryItem *commitNew = new RegionMapHistoryItem( - RegionMapEditorBox::BackroundResize, this->region_map->getTiles(), w, h - ); - history.push(commitOld); - history.push(commitNew); - history.back(); - this->region_map->resize(w, h); - this->currIndex = 2 * w + 1; - displayRegionMap(); + this->currIndex = this->region_map->padLeft * w + this->region_map->padTop; + displayRegionMapImage(); + displayRegionMapLayout(); + displayRegionMapLayoutOptions(); } void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); + this->cityMapFirstDraw = true; } void RegionMapEditor::closeEvent(QCloseEvent *event) @@ -652,7 +654,6 @@ void RegionMapEditor::on_verticalSlider_Zoom_City_Tiles_valueChanged(int val) { } void RegionMapEditor::on_action_RegionMap_Generate_triggered() { - // RegionMapGenerator generator(this->project); generator.generate("LittlerootTown"); this->hasUnsavedChanges = true; diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp index 8654b66b..82edc5ba 100644 --- a/src/ui/regionmaplayoutpixmapitem.cpp +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -1,7 +1,5 @@ #include "regionmaplayoutpixmapitem.h" - - void RegionMapLayoutPixmapItem::draw() { if (!region_map) return; @@ -76,9 +74,7 @@ void RegionMapLayoutPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) mousePressEvent(event); } -void RegionMapLayoutPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - // -} +void RegionMapLayoutPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {} void RegionMapLayoutPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPoint pos = this->getCellPos(event->pos()); diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index 6f3886c7..976d1ae9 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -1,7 +1,4 @@ #include "regionmappixmapitem.h" -#include "imageproviders.h" - - void RegionMapPixmapItem::draw() { if (!region_map) return; diff --git a/src/ui/tilemaptileselector.cpp b/src/ui/tilemaptileselector.cpp index af7dfdc5..770b20b5 100644 --- a/src/ui/tilemaptileselector.cpp +++ b/src/ui/tilemaptileselector.cpp @@ -3,16 +3,15 @@ #include void TilemapTileSelector::draw() { - size_t width_ = this->pixmap.width(); + size_t width_ = this->tilemap.width(); this->pixelWidth = width_; - size_t height_ = this->pixmap.height(); + size_t height_ = this->tilemap.height(); this->pixelHeight = height_; size_t ntiles_ = (width_/8) * (height_/8); this->numTilesWide = width_ / 8; this->numTiles = ntiles_; - this->setPixmap(this->pixmap); this->drawSelection(); } @@ -37,6 +36,16 @@ unsigned TilemapTileSelector::getTileId(int x, int y) { return index < this->numTiles ? index : this->numTiles % index; } +QPoint TilemapTileSelector::getTileIdCoords(unsigned tileId) { + int index = tileId < this->numTiles ? tileId : this->numTiles % tileId; + return QPoint(index % this->numTilesWide, index / this->numTilesWide); +} + +QImage TilemapTileSelector::tileImg(unsigned tileId) { + QPoint pos = getTileIdCoords(tileId); + return this->tilemap.copy(pos.x() * 8, pos.y() * 8, 8, 8).toImage(); +} + void TilemapTileSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) { SelectablePixmapItem::mousePressEvent(event); this->updateSelectedTile(); @@ -65,14 +74,3 @@ void TilemapTileSelector::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { void TilemapTileSelector::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) { emit this->hoveredTileCleared(); } - -QPoint TilemapTileSelector::getTileIdCoords(unsigned tileId) { - int index = tileId < this->numTiles ? tileId : this->numTiles % tileId; - return QPoint(index % this->numTilesWide, index / this->numTilesWide); -} - -QImage TilemapTileSelector::tileImg(unsigned tileId) { - // - QPoint pos = getTileIdCoords(tileId); - return pixmap.copy(pos.x() * 8, pos.y() * 8, 8, 8).toImage(); -} From 3cef77a174d84e60d8b93b8919c1070dcecf4d24 Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 16 Feb 2019 17:56:44 -0500 Subject: [PATCH 11/17] add map section swap feature and smooth zooming --- forms/regionmapeditor.ui | 50 +++++++++---- include/core/regionmap.h | 3 +- include/ui/regionmapeditor.h | 8 +- src/core/regionmap.cpp | 13 ++++ src/mainwindow.cpp | 2 - src/ui/regionmapeditor.cpp | 132 ++++++++++++++++++++------------- src/ui/tilemaptileselector.cpp | 1 + 7 files changed, 132 insertions(+), 77 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 1d521a36..3eda7dcd 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -61,7 +61,7 @@ 0 0 - 348 + 350 225 @@ -182,7 +182,7 @@ 0 0 - 348 + 350 225 @@ -651,7 +651,7 @@ 0 0 - 440 + 441 230 @@ -787,7 +787,7 @@ 8 0 - 254 + 255 274 @@ -906,16 +906,19 @@ Qt::StrongFocus - 1 + 10 - 4 + 100 + + + 30 Qt::Vertical - QSlider::TicksAbove + QSlider::NoTicks 1 @@ -953,16 +956,19 @@ Qt::StrongFocus - 1 + 10 - 4 + 100 + + + 30 Qt::Vertical - QSlider::TicksAbove + QSlider::NoTicks 1 @@ -991,16 +997,19 @@ Qt::StrongFocus - 1 + 10 - 4 + 100 + + + 30 Qt::Vertical - QSlider::TicksBelow + QSlider::NoTicks 1 @@ -1038,16 +1047,19 @@ Qt::StrongFocus - 1 + 10 - 4 + 100 + + + 30 Qt::Vertical - QSlider::TicksBelow + QSlider::NoTicks 1 @@ -1080,6 +1092,7 @@ + @@ -1129,6 +1142,11 @@ Generate... + + + Swap... + + diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 7e7733e3..e9bf94bc 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -48,7 +48,7 @@ public: ~RegionMap() {}; - Project *project; + Project *project = nullptr; QVector map_squares; History history; @@ -70,6 +70,7 @@ public: void resize(int width, int height); void resetSquare(int index); + void replaceSectionId(unsigned oldId, unsigned newId); int width(); int height(); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index b9af6fd4..de2a8cfc 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -59,12 +59,7 @@ private: bool regionMapFirstDraw = true; double scaleUpFactor = 2.0; - double scaleDownFactor = 1.0 / scaleUpFactor; - - int scaleRegionMapTiles = 1; - int scaleRegionMapImage = 1; - int scaleCityMapTiles = 1; - int scaleCityMapImage = 1; + double initialScale = 30.0; QGraphicsScene *scene_region_map_image = nullptr; QGraphicsScene *scene_city_map_image = nullptr; @@ -97,6 +92,7 @@ private slots: void on_action_RegionMap_Undo_triggered(); void on_action_RegionMap_Redo_triggered(); void on_action_RegionMap_Resize_triggered(); + void on_action_Swap_triggered(); void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_delete_clicked(); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 3720e03d..ab8609de 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -198,6 +198,19 @@ void RegionMap::resetSquare(int index) { logInfo(QString("Reset map square at (%1, %2).").arg(this->map_squares[index].x).arg(this->map_squares[index].y)); } +void RegionMap::replaceSectionId(unsigned oldId, unsigned newId) { + for (auto &square : map_squares) { + if (square.secid == oldId) { + square.has_map = false; + square.secid = newId; + QString secname = (*(project->regionMapSections))[newId]; + if (secname != "MAPSEC_NONE") square.has_map = true; + square.mapsec = secname; + square.map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name); + } + } +} + void RegionMap::resize(int newWidth, int newHeight) { QVector new_squares; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0c3d3304..382e6399 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1980,9 +1980,7 @@ void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) { } void MainWindow::on_pushButton_RM_Options_save_clicked() { - // this->editor->region_map->saveOptions( - // this->editor->region_map_layout_item->selectedTile, this->ui->comboBox_RM_ConnectedMap->currentText(), this->ui->lineEdit_RM_MapName->text(), diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 42fff3ef..793a00bf 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -11,6 +11,7 @@ #include #include #include +#include RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : QMainWindow(parent), @@ -103,10 +104,9 @@ void RegionMapEditor::displayRegionMapImage() { this->scene_region_map_image->setSceneRect(this->scene_region_map_image->itemsBoundingRect()); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize().width() * scaleRegionMapImage + 2, - this->region_map->imgSize().height() * scaleRegionMapImage + 2); if (regionMapFirstDraw) { + on_verticalSlider_Zoom_Map_Image_valueChanged(initialScale); RegionMapHistoryItem *commit = new RegionMapHistoryItem( RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() ); @@ -140,8 +140,6 @@ void RegionMapEditor::displayRegionMapLayout() { this->scene_region_map_layout->setSceneRect(this->scene_region_map_layout->itemsBoundingRect()); this->ui->graphicsView_Region_Map_Layout->setScene(this->scene_region_map_layout); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize().width() * scaleRegionMapImage + 2, - this->region_map->imgSize().height() * scaleRegionMapImage + 2); } void RegionMapEditor::displayRegionMapLayoutOptions() { @@ -199,8 +197,7 @@ void RegionMapEditor::displayRegionMapTileSelector() { this, &RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared); this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * scaleRegionMapTiles + 2, - this->mapsquare_selector_item->pixelHeight * scaleRegionMapTiles + 2); + on_verticalSlider_Zoom_Image_Tiles_valueChanged(initialScale); this->mapsquare_selector_item->select(this->selectedImageTile); } @@ -223,8 +220,7 @@ void RegionMapEditor::displayCityMapTileSelector() { this, &RegionMapEditor::onCityMapTileSelectorSelectedTileChanged); this->ui->graphicsView_City_Map_Tiles->setScene(this->scene_city_map_tiles); - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * scaleCityMapTiles + 2, - this->city_map_selector_item->pixelHeight * scaleCityMapTiles + 2); + on_verticalSlider_Zoom_City_Tiles_valueChanged(initialScale); this->city_map_selector_item->select(this->selectedCityTile); } @@ -250,8 +246,7 @@ void RegionMapEditor::displayCityMap(QString f) { 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(8 * city_map_item->width() * scaleCityMapImage + 2, - 8 * city_map_item->height() * scaleCityMapImage + 2); + on_verticalSlider_Zoom_City_Map_valueChanged(initialScale); } bool RegionMapEditor::createCityMap(QString name) { @@ -564,6 +559,45 @@ void RegionMapEditor::resize(int w, int h) { displayRegionMapLayoutOptions(); } +void RegionMapEditor::on_action_Swap_triggered() { + QDialog popup(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + popup.setWindowTitle("New City Map"); + popup.setWindowModality(Qt::NonModal); + + QFormLayout form(&popup); + + QComboBox *oldSecBox = new QComboBox(); + oldSecBox->addItems(*(this->project->regionMapSections)); + form.addRow(new QLabel("Old Map Section:"), oldSecBox); + QComboBox *newSecBox = new QComboBox(); + newSecBox->addItems(*(this->project->regionMapSections)); + form.addRow(new QLabel("New Map Section:"), newSecBox); + + QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup); + form.addRow(&buttonBox); + + QString beforeSection, afterSection; + uint8_t oldId, newId; + connect(&buttonBox, SIGNAL(rejected()), &popup, SLOT(reject())); + connect(&buttonBox, &QDialogButtonBox::accepted, [this, &popup, &oldSecBox, &newSecBox, + &beforeSection, &afterSection, &oldId, &newId](){ + beforeSection = oldSecBox->currentText(); + afterSection = newSecBox->currentText(); + if (!beforeSection.isEmpty() && !afterSection.isEmpty()) { + oldId = static_cast(project->regionMapSections->indexOf(beforeSection)); + newId = static_cast(project->regionMapSections->indexOf(afterSection)); + popup.accept(); + } + }); + + if (popup.exec() == QDialog::Accepted) { + this->region_map->replaceSectionId(oldId, newId); + this->region_map_layout_item->draw(); + this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); + this->hasUnsavedChanges = true; + } +} + void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); this->cityMapFirstDraw = true; @@ -593,64 +627,58 @@ void RegionMapEditor::closeEvent(QCloseEvent *event) } void RegionMapEditor::on_verticalSlider_Zoom_Map_Image_valueChanged(int val) { - bool zoom_in = val > scaleRegionMapImage ? true : false; - scaleRegionMapImage = val; + double scale = pow(scaleUpFactor, static_cast(val - initialScale) / initialScale); - this->ui->graphicsView_Region_Map_BkgImg->setFixedSize(this->region_map->imgSize().width() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2, - this->region_map->imgSize().height() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2); - this->ui->graphicsView_Region_Map_Layout->setFixedSize(this->region_map->imgSize().width() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2, - this->region_map->imgSize().height() * pow(scaleUpFactor, scaleRegionMapImage - 1) + 2); + QMatrix matrix; + matrix.scale(scale, scale); + int width = ceil(static_cast(this->region_map->imgSize().width()) * scale); + int height = ceil(static_cast(this->region_map->imgSize().height()) * scale); - if (zoom_in) { - this->ui->graphicsView_Region_Map_BkgImg->scale(scaleUpFactor, scaleUpFactor); - this->ui->graphicsView_Region_Map_Layout->scale(scaleUpFactor, scaleUpFactor); - } else { - // - this->ui->graphicsView_Region_Map_BkgImg->scale(scaleDownFactor, scaleDownFactor); - this->ui->graphicsView_Region_Map_Layout->scale(scaleDownFactor, scaleDownFactor); - } + ui->graphicsView_Region_Map_BkgImg->setResizeAnchor(QGraphicsView::NoAnchor); + ui->graphicsView_Region_Map_BkgImg->setMatrix(matrix); + ui->graphicsView_Region_Map_BkgImg->setFixedSize(width + 2, height + 2); + ui->graphicsView_Region_Map_Layout->setResizeAnchor(QGraphicsView::NoAnchor); + ui->graphicsView_Region_Map_Layout->setMatrix(matrix); + ui->graphicsView_Region_Map_Layout->setFixedSize(width + 2, height + 2); } void RegionMapEditor::on_verticalSlider_Zoom_Image_Tiles_valueChanged(int val) { - bool zoom_in = val > scaleRegionMapTiles ? true : false; - scaleRegionMapTiles = val; + double scale = pow(scaleUpFactor, static_cast(val - initialScale) / initialScale); - this->ui->graphicsView_RegionMap_Tiles->setFixedSize(this->mapsquare_selector_item->pixelWidth * pow(scaleUpFactor, scaleRegionMapTiles - 1) + 2, - this->mapsquare_selector_item->pixelHeight * pow(scaleUpFactor, scaleRegionMapTiles - 1) + 2); - - if (zoom_in) { - this->ui->graphicsView_RegionMap_Tiles->scale(scaleUpFactor, scaleUpFactor); - } else { - this->ui->graphicsView_RegionMap_Tiles->scale(scaleDownFactor, scaleDownFactor); - } + QMatrix matrix; + matrix.scale(scale, scale); + int width = ceil(static_cast(this->mapsquare_selector_item->pixelWidth) * scale); + int height = ceil(static_cast(this->mapsquare_selector_item->pixelHeight) * scale); + + ui->graphicsView_RegionMap_Tiles->setResizeAnchor(QGraphicsView::NoAnchor); + ui->graphicsView_RegionMap_Tiles->setMatrix(matrix); + ui->graphicsView_RegionMap_Tiles->setFixedSize(width + 2, height + 2); } void RegionMapEditor::on_verticalSlider_Zoom_City_Map_valueChanged(int val) { - bool zoom_in = val > scaleCityMapImage ? true : false; - scaleCityMapImage = val; + double scale = pow(scaleUpFactor, static_cast(val - initialScale) / initialScale); - this->ui->graphicsView_City_Map->setFixedSize(8 * city_map_item->width() * pow(scaleUpFactor, scaleCityMapImage - 1) + 2, - 8 * city_map_item->height() * pow(scaleUpFactor, scaleCityMapImage - 1) + 2); + QMatrix matrix; + matrix.scale(scale, scale); + int width = ceil(static_cast(8 * city_map_item->width()) * scale); + int height = ceil(static_cast(8 * city_map_item->height()) * scale); - if (zoom_in) { - this->ui->graphicsView_City_Map->scale(scaleUpFactor, scaleUpFactor); - } else { - this->ui->graphicsView_City_Map->scale(scaleDownFactor, scaleDownFactor); - } + ui->graphicsView_City_Map->setResizeAnchor(QGraphicsView::NoAnchor); + ui->graphicsView_City_Map->setMatrix(matrix); + ui->graphicsView_City_Map->setFixedSize(width + 2, height + 2); } void RegionMapEditor::on_verticalSlider_Zoom_City_Tiles_valueChanged(int val) { - bool zoom_in = val > scaleCityMapTiles ? true : false; - scaleCityMapTiles = val; + double scale = pow(scaleUpFactor, static_cast(val - initialScale) / initialScale); - this->ui->graphicsView_City_Map_Tiles->setFixedSize(this->city_map_selector_item->pixelWidth * pow(scaleUpFactor, scaleCityMapTiles - 1) + 2, - this->city_map_selector_item->pixelHeight * pow(scaleUpFactor, scaleCityMapTiles - 1) + 2); + QMatrix matrix; + matrix.scale(scale, scale); + int width = ceil(static_cast(this->city_map_selector_item->pixelWidth) * scale); + int height = ceil(static_cast(this->city_map_selector_item->pixelHeight) * scale); - if (zoom_in) { - this->ui->graphicsView_City_Map_Tiles->scale(scaleUpFactor, scaleUpFactor); - } else { - this->ui->graphicsView_City_Map_Tiles->scale(scaleDownFactor, scaleDownFactor); - } + ui->graphicsView_City_Map_Tiles->setResizeAnchor(QGraphicsView::NoAnchor); + ui->graphicsView_City_Map_Tiles->setMatrix(matrix); + ui->graphicsView_City_Map_Tiles->setFixedSize(width + 2, height + 2); } void RegionMapEditor::on_action_RegionMap_Generate_triggered() { diff --git a/src/ui/tilemaptileselector.cpp b/src/ui/tilemaptileselector.cpp index 770b20b5..a18185ab 100644 --- a/src/ui/tilemaptileselector.cpp +++ b/src/ui/tilemaptileselector.cpp @@ -12,6 +12,7 @@ void TilemapTileSelector::draw() { this->numTilesWide = width_ / 8; this->numTiles = ntiles_; + this->setPixmap(tilemap); this->drawSelection(); } From 5b6f658e293328e27a29ae7c46174574b70e8428 Mon Sep 17 00:00:00 2001 From: garak Date: Mon, 25 Feb 2019 13:31:34 -0500 Subject: [PATCH 12/17] add new convenience features to rme and fix typos --- forms/regionmapeditor.ui | 13 +++++++++ include/core/regionmap.h | 2 ++ include/project.h | 6 ++-- include/ui/regionmapeditor.h | 2 ++ src/config.cpp | 2 +- src/core/regionmap.cpp | 24 +++++++++++----- src/mainwindow.cpp | 21 ++------------ src/project.cpp | 15 ++++++++-- src/ui/citymappixmapitem.cpp | 3 -- src/ui/newmappopup.cpp | 2 +- src/ui/regionmapeditor.cpp | 50 ++++++++++++++++++++++++++++------ src/ui/regionmappixmapitem.cpp | 3 -- 12 files changed, 96 insertions(+), 47 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 3eda7dcd..0cba8783 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -1092,7 +1092,10 @@ + + + @@ -1147,6 +1150,16 @@ Swap... + + + Reset Image + + + + + Reset Layout + + diff --git a/include/core/regionmap.h b/include/core/regionmap.h index e9bf94bc..96838fc7 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -70,6 +70,8 @@ public: void resize(int width, int height); void resetSquare(int index); + void resetLayout(); + void resetImage(); void replaceSectionId(unsigned oldId, unsigned newId); int width(); diff --git a/include/project.h b/include/project.h index b4270bff..c1d55ff0 100644 --- a/include/project.h +++ b/include/project.h @@ -29,7 +29,9 @@ public: QString layoutsLabel; QMap mapLayouts; QMap mapLayoutsMaster; - QStringList *regionMapSections = nullptr; + QMap *mapSecToMapHoverName; + QMap mapSectionNameToValue; + QMap mapSectionValueToName; QStringList *itemNames = nullptr; QStringList *flagNames = nullptr; QStringList *varNames = nullptr; @@ -47,8 +49,6 @@ public: Map* loadMap(QString); Map* getMap(QString); - QMap *mapSecToMapHoverName; - QMap *tileset_cache = nullptr; Tileset* loadTileset(QString, Tileset *tileset = nullptr); Tileset* getTileset(QString, bool forceLoad = false); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index de2a8cfc..1d8301e1 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -92,6 +92,8 @@ private slots: void on_action_RegionMap_Undo_triggered(); void on_action_RegionMap_Redo_triggered(); void on_action_RegionMap_Resize_triggered(); + void on_action_RegionMap_ResetImage_triggered(); + void on_action_RegionMap_ResetLayout_triggered(); void on_action_Swap_triggered(); void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); diff --git a/src/config.cpp b/src/config.cpp index 32ad12fe..e689b91e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -260,7 +260,7 @@ void PorymapConfig::setShowCursorTile(bool enabled) { } void PorymapConfig::setRegionMapDimensions(int width, int height) { - this->regionMapDimensions = QSize(width, height);//QString("%1x%2").arg(width).arg(height); + this->regionMapDimensions = QSize(width, height); } QString PorymapConfig::getRecentProject() { diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index ab8609de..41212fa1 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -116,8 +116,9 @@ void RegionMap::readLayout() { for (int y = 0; y < layout_height_; y++) { for (int x = 0; x < layout_width_; x++) { int i = img_index_(x,y); - map_squares[i].secid = static_cast(mapBinData.at(layout_index_(x,y))); - QString secname = (*(project->regionMapSections))[static_cast(mapBinData.at(layout_index_(x,y)))]; + uint8_t id = static_cast(mapBinData.at(layout_index_(x,y))); + map_squares[i].secid = id; + QString secname = project->mapSectionValueToName.value(id); if (secname != "MAPSEC_NONE") map_squares[i].has_map = true; map_squares[i].mapsec = secname; map_squares[i].map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name); @@ -140,7 +141,7 @@ void RegionMap::saveLayout() { entries_text += "\nconst struct RegionMapLocation gRegionMapEntries[] = {\n"; - for (auto sec : *(project->regionMapSections)) { + for (auto sec : project->mapSectionNameToValue.keys()) { if (!mapSecToMapEntry.contains(sec)) continue; struct RegionMapEntry entry = mapSecToMapEntry.value(sec); entries_text += " [" + sec + "] = {" + QString::number(entry.x) + ", " + QString::number(entry.y) + ", " @@ -168,7 +169,7 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { int index = getMapSquareIndex(x + this->padLeft, y + this->padTop); if (!sec.isEmpty()) { this->map_squares[index].has_map = sec == "MAPSEC_NONE" ? false : true; - this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf(sec)); + this->map_squares[index].secid = static_cast(project->mapSectionNameToValue.value(sec)); this->map_squares[index].mapsec = sec; if (!name.isEmpty()) { this->map_squares[index].map_name = name; @@ -191,11 +192,20 @@ void RegionMap::resetSquare(int index) { this->map_squares[index].mapsec = "MAPSEC_NONE"; this->map_squares[index].map_name = QString(); this->map_squares[index].has_map = false; - this->map_squares[index].secid = static_cast(project->regionMapSections->indexOf("MAPSEC_NONE")); + this->map_squares[index].secid = static_cast(project->mapSectionNameToValue.value("MAPSEC_NONE")); this->map_squares[index].has_city_map = false; this->map_squares[index].city_map_name = QString(); this->map_squares[index].duplicated = false; - logInfo(QString("Reset map square at (%1, %2).").arg(this->map_squares[index].x).arg(this->map_squares[index].y)); +} + +void RegionMap::resetLayout() { + for (int i = 0; i < map_squares.size(); i++) + resetSquare(i); +} + +void RegionMap::resetImage() { + for (int i = 0; i < map_squares.size(); i++) + this->map_squares[i].tile_img_id = 0x00; } void RegionMap::replaceSectionId(unsigned oldId, unsigned newId) { @@ -203,7 +213,7 @@ void RegionMap::replaceSectionId(unsigned oldId, unsigned newId) { if (square.secid == oldId) { square.has_map = false; square.secid = newId; - QString secname = (*(project->regionMapSections))[newId]; + QString secname = project->mapSectionValueToName.value(newId); if (secname != "MAPSEC_NONE") square.has_map = true; square.mapsec = secname; square.map_name = sMapNamesMap.value(mapSecToMapEntry.value(secname).name); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 382e6399..07c62fdc 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -469,7 +469,7 @@ void MainWindow::displayMapProperties() { ui->comboBox_Song->addItems(songs); ui->comboBox_Song->setCurrentText(map->song); - ui->comboBox_Location->addItems(*project->regionMapSections); + ui->comboBox_Location->addItems(project->mapSectionValueToName.values()); ui->comboBox_Location->setCurrentText(map->location); QMap tilesets = project->getTilesets(); @@ -661,8 +661,8 @@ void MainWindow::sortMapList() { case MapSortOrder::Area: { QMap mapsecToGroupNum; - for (int i = 0; i < project->regionMapSections->length(); i++) { - QString mapsec_name = project->regionMapSections->value(i); + for (int i = 0; i < project->mapSectionNameToValue.size(); i++) { + QString mapsec_name = project->mapSectionValueToName.value(i); QStandardItem *mapsec = new QStandardItem; mapsec->setText(mapsec_name); mapsec->setIcon(folderIcon); @@ -1979,21 +1979,6 @@ void MainWindow::on_horizontalSlider_MetatileZoom_valueChanged(int value) { ui->graphicsView_Metatiles->setFixedSize(size.width() + 2, size.height() + 2); } -void MainWindow::on_pushButton_RM_Options_save_clicked() { - this->editor->region_map->saveOptions( - this->editor->region_map_layout_item->selectedTile, - this->ui->comboBox_RM_ConnectedMap->currentText(), - this->ui->lineEdit_RM_MapName->text(), - this->ui->spinBox_RM_Options_x->value(), - this->ui->spinBox_RM_Options_y->value() - ); - this->editor->region_map_layout_item->draw(); -} - -void MainWindow::on_pushButton_CityMap_save_clicked() { - this->editor->city_map_item->save(); -} - void MainWindow::on_actionRegion_Map_Editor_triggered() { if (!this->regionMapEditor) { this->regionMapEditor = new RegionMapEditor(this, this->editor->project); diff --git a/src/project.cpp b/src/project.cpp index d763efba..79c15d60 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -31,7 +31,6 @@ Project::Project() groupNames = new QStringList; map_groups = new QMap; mapNames = new QStringList; - regionMapSections = new QStringList; itemNames = new QStringList; flagNames = new QStringList; varNames = new QStringList; @@ -1510,8 +1509,18 @@ void Project::readTilesetProperties() { void Project::readRegionMapSections() { QString filepath = root + "/include/constants/region_map_sections.h"; - QStringList prefixes = (QStringList() << "MAPSEC_"); - readCDefinesSorted(filepath, prefixes, regionMapSections); + this->mapSectionNameToValue.clear(); + this->mapSectionValueToName.clear(); + QString text = readTextFile(filepath); + if (!text.isNull()) { + QStringList prefixes = (QStringList() << "MAPSEC_"); + this->mapSectionNameToValue = readCDefines(text, prefixes); + for (QString defineName : this->mapSectionNameToValue.keys()) { + this->mapSectionValueToName.insert(this->mapSectionNameToValue[defineName], defineName); + } + } else { + logError(QString("Failed to read C defines file: '%1'").arg(filepath)); + } } void Project::readItemNames() { diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index bbe31f65..3d0eb2ca 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -54,9 +54,6 @@ void CityMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { } 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); } diff --git a/src/ui/newmappopup.cpp b/src/ui/newmappopup.cpp index 6978932b..941ddd44 100644 --- a/src/ui/newmappopup.cpp +++ b/src/ui/newmappopup.cpp @@ -70,7 +70,7 @@ void NewMapPopup::setDefaultValues(int groupNum, QString mapSec) { } ui->comboBox_NewMap_Type->addItems(*project->mapTypes); - ui->comboBox_NewMap_Location->addItems(*project->regionMapSections); + ui->comboBox_NewMap_Location->addItems(project->mapSectionValueToName.values()); if (!mapSec.isEmpty()) ui->comboBox_NewMap_Location->setCurrentText(mapSec); ui->frame_NewMap_Options->setEnabled(true); diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 793a00bf..fa2ef5c0 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : @@ -41,8 +40,6 @@ RegionMapEditor::~RegionMapEditor() void RegionMapEditor::on_action_RegionMap_Save_triggered() { if (project && region_map) { - region_map->save(); - this->city_map_item->save(); this->region_map->saveOptions( this->region_map_layout_item->selectedTile, this->ui->comboBox_RM_ConnectedMap->currentText(), @@ -50,6 +47,8 @@ void RegionMapEditor::on_action_RegionMap_Save_triggered() { this->ui->spinBox_RM_Options_x->value(), this->ui->spinBox_RM_Options_y->value() ); + this->region_map->save(); + this->city_map_item->save(); this->currIndex = this->region_map_layout_item->highlightedTile; this->region_map_layout_item->highlightedTile = -1; displayRegionMap(); @@ -144,7 +143,7 @@ void RegionMapEditor::displayRegionMapLayout() { void RegionMapEditor::displayRegionMapLayoutOptions() { this->ui->comboBox_RM_ConnectedMap->clear(); - this->ui->comboBox_RM_ConnectedMap->addItems(*(this->project->regionMapSections)); + this->ui->comboBox_RM_ConnectedMap->addItems(this->project->mapSectionValueToName.values()); this->ui->frame_RM_Options->setEnabled(true); @@ -567,10 +566,10 @@ void RegionMapEditor::on_action_Swap_triggered() { QFormLayout form(&popup); QComboBox *oldSecBox = new QComboBox(); - oldSecBox->addItems(*(this->project->regionMapSections)); + oldSecBox->addItems(this->project->mapSectionValueToName.values()); form.addRow(new QLabel("Old Map Section:"), oldSecBox); QComboBox *newSecBox = new QComboBox(); - newSecBox->addItems(*(this->project->regionMapSections)); + newSecBox->addItems(this->project->mapSectionValueToName.values()); form.addRow(new QLabel("New Map Section:"), newSecBox); QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &popup); @@ -584,8 +583,8 @@ void RegionMapEditor::on_action_Swap_triggered() { beforeSection = oldSecBox->currentText(); afterSection = newSecBox->currentText(); if (!beforeSection.isEmpty() && !afterSection.isEmpty()) { - oldId = static_cast(project->regionMapSections->indexOf(beforeSection)); - newId = static_cast(project->regionMapSections->indexOf(afterSection)); + oldId = static_cast(this->project->mapSectionNameToValue.value(beforeSection)); + newId = static_cast(this->project->mapSectionNameToValue.value(afterSection)); popup.accept(); } }); @@ -598,6 +597,41 @@ void RegionMapEditor::on_action_Swap_triggered() { } } +void RegionMapEditor::on_action_RegionMap_ResetImage_triggered() { + QMessageBox::StandardButton result = QMessageBox::question( + this, + "WARNING", + "This action will reset the entire map image to metatile 0x00, continue?", + QMessageBox::Yes | QMessageBox::Cancel, + QMessageBox::Yes + ); + + if (result == QMessageBox::Yes) { + this->region_map->resetImage(); + displayRegionMapImage(); + displayRegionMapLayout(); + } else { + return; + } +} + +void RegionMapEditor::on_action_RegionMap_ResetLayout_triggered() { + QMessageBox::StandardButton result = QMessageBox::question( + this, + "WARNING", + "This action will reset the entire map layout to MAPSEC_NONE, continue?", + QMessageBox::Yes | QMessageBox::Cancel, + QMessageBox::Yes + ); + + if (result == QMessageBox::Yes) { + this->region_map->resetLayout(); + displayRegionMapLayout(); + } else { + return; + } +} + void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); this->cityMapFirstDraw = true; diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index 976d1ae9..5488ca4c 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -50,9 +50,6 @@ void RegionMapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { } void RegionMapPixmapItem::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); } From 8af714a6816e738c8e17dc1bec63cdce05b47f1a Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 17 Mar 2019 11:37:13 -0500 Subject: [PATCH 13/17] Rename 'Reset' actions to 'Clear', and add 'Clear Background Map' to edit history --- forms/regionmapeditor.ui | 38 ++++++++++++++++++------------------ include/core/regionmap.h | 4 ++-- include/ui/regionmapeditor.h | 4 ++-- src/core/regionmap.cpp | 4 ++-- src/ui/regionmapeditor.cpp | 25 +++++++++--------------- 5 files changed, 34 insertions(+), 41 deletions(-) diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index 0cba8783..acd7ceb9 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -61,8 +61,8 @@ 0 0 - 350 - 225 + 384 + 249 @@ -182,8 +182,8 @@ 0 0 - 350 - 225 + 384 + 249 @@ -330,10 +330,10 @@ - 8 + 0 0 - 278 - 262 + 283 + 275 @@ -651,8 +651,8 @@ 0 0 - 441 - 230 + 412 + 249 @@ -785,10 +785,10 @@ - 8 + 0 0 - 255 - 274 + 295 + 283 @@ -1076,7 +1076,7 @@ 0 0 829 - 22 + 21 @@ -1094,8 +1094,8 @@ - - + + @@ -1150,14 +1150,14 @@ Swap... - + - Reset Image + Clear Background Image - + - Reset Layout + Clear Map Layout diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 96838fc7..7300cc60 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -70,8 +70,8 @@ public: void resize(int width, int height); void resetSquare(int index); - void resetLayout(); - void resetImage(); + void clearLayout(); + void clearImage(); void replaceSectionId(unsigned oldId, unsigned newId); int width(); diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index 1d8301e1..b057adaf 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -92,8 +92,8 @@ private slots: void on_action_RegionMap_Undo_triggered(); void on_action_RegionMap_Redo_triggered(); void on_action_RegionMap_Resize_triggered(); - void on_action_RegionMap_ResetImage_triggered(); - void on_action_RegionMap_ResetLayout_triggered(); + void on_action_RegionMap_ClearImage_triggered(); + void on_action_RegionMap_ClearLayout_triggered(); void on_action_Swap_triggered(); void on_action_RegionMap_Generate_triggered(); void on_tabWidget_Region_Map_currentChanged(int); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 41212fa1..3f376102 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -198,12 +198,12 @@ void RegionMap::resetSquare(int index) { this->map_squares[index].duplicated = false; } -void RegionMap::resetLayout() { +void RegionMap::clearLayout() { for (int i = 0; i < map_squares.size(); i++) resetSquare(i); } -void RegionMap::resetImage() { +void RegionMap::clearImage() { for (int i = 0; i < map_squares.size(); i++) this->map_squares[i].tile_img_id = 0x00; } diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index fa2ef5c0..b3e7de19 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -597,25 +597,18 @@ void RegionMapEditor::on_action_Swap_triggered() { } } -void RegionMapEditor::on_action_RegionMap_ResetImage_triggered() { - QMessageBox::StandardButton result = QMessageBox::question( - this, - "WARNING", - "This action will reset the entire map image to metatile 0x00, continue?", - QMessageBox::Yes | QMessageBox::Cancel, - QMessageBox::Yes +void RegionMapEditor::on_action_RegionMap_ClearImage_triggered() { + this->region_map->clearImage(); + RegionMapHistoryItem *commit = new RegionMapHistoryItem( + RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->width(), this->region_map->height() ); + history.push(commit); - if (result == QMessageBox::Yes) { - this->region_map->resetImage(); - displayRegionMapImage(); - displayRegionMapLayout(); - } else { - return; - } + displayRegionMapImage(); + displayRegionMapLayout(); } -void RegionMapEditor::on_action_RegionMap_ResetLayout_triggered() { +void RegionMapEditor::on_action_RegionMap_ClearLayout_triggered() { QMessageBox::StandardButton result = QMessageBox::question( this, "WARNING", @@ -625,7 +618,7 @@ void RegionMapEditor::on_action_RegionMap_ResetLayout_triggered() { ); if (result == QMessageBox::Yes) { - this->region_map->resetLayout(); + this->region_map->clearLayout(); displayRegionMapLayout(); } else { return; From 545e2134d86ce5227d095a5b61868b6bcb6d8093 Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 23 Mar 2019 16:56:30 -0400 Subject: [PATCH 14/17] remember changes to region map layout between saves --- include/ui/regionmapeditor.h | 1 + src/core/regionmap.cpp | 4 ++-- src/ui/regionmapeditor.cpp | 25 +++++++++++++++++-------- src/ui/regionmaplayoutpixmapitem.cpp | 8 ++------ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index b057adaf..ee364b83 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -29,6 +29,7 @@ public: void loadRegionMapData(); void loadCityMaps(); + void setCurrentSquareOptions(); void onRegionMapTileSelectorSelectedTileChanged(unsigned id); void onCityMapTileSelectorSelectedTileChanged(unsigned id); diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 3f376102..4368c9b3 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -174,10 +174,10 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { if (!name.isEmpty()) { this->map_squares[index].map_name = name; this->project->mapSecToMapHoverName->insert(sec, name); + QString sName = fix_case(sec); + sMapNamesMap.insert(sName, name); if (!mapSecToMapEntry.keys().contains(sec)) { - QString sName = fix_case(sec); sMapNames.append(sName); - sMapNamesMap.insert(sName, name); struct RegionMapEntry entry = {x, y, 1, 1, sName}; mapSecToMapEntry.insert(sec, entry); } diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index b3e7de19..9efbf014 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -39,14 +39,8 @@ RegionMapEditor::~RegionMapEditor() } void RegionMapEditor::on_action_RegionMap_Save_triggered() { + setCurrentSquareOptions(); if (project && region_map) { - this->region_map->saveOptions( - this->region_map_layout_item->selectedTile, - this->ui->comboBox_RM_ConnectedMap->currentText(), - this->ui->lineEdit_RM_MapName->text(), - this->ui->spinBox_RM_Options_x->value(), - this->ui->spinBox_RM_Options_y->value() - ); this->region_map->save(); this->city_map_item->save(); this->currIndex = this->region_map_layout_item->highlightedTile; @@ -56,6 +50,18 @@ void RegionMapEditor::on_action_RegionMap_Save_triggered() { this->hasUnsavedChanges = false; } +void RegionMapEditor::setCurrentSquareOptions() { + if (project && region_map) { + this->region_map->saveOptions( + this->currIndex, + this->ui->comboBox_RM_ConnectedMap->currentText(), + this->ui->lineEdit_RM_MapName->text(), + this->ui->spinBox_RM_Options_x->value(), + this->ui->spinBox_RM_Options_y->value() + ); + } +} + void RegionMapEditor::loadRegionMapData() { this->region_map->init(project); this->currIndex = this->region_map->width() * this->region_map->padTop + this->region_map->padLeft; @@ -154,7 +160,7 @@ void RegionMapEditor::displayRegionMapLayoutOptions() { this->region_map->height() - this->region_map->padTop - this->region_map->padBottom - 1 ); - updateRegionMapLayoutOptions(currIndex); + updateRegionMapLayoutOptions(this->currIndex); // TODO: implement when the code is decompiled this->ui->label_RM_CityMap->setVisible(false); @@ -296,6 +302,7 @@ void RegionMapEditor::onRegionMapTileSelectorHoveredTileCleared() { } void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { + setCurrentSquareOptions(); QString message = QString(); this->currIndex = index; this->region_map_layout_item->highlightedTile = index; @@ -306,6 +313,7 @@ void RegionMapEditor::onRegionMapLayoutSelectedTileChanged(int index) { this->ui->statusbar->showMessage(message); updateRegionMapLayoutOptions(index); + this->region_map_layout_item->draw(); } void RegionMapEditor::onRegionMapLayoutHoveredTileChanged(int index) { @@ -348,6 +356,7 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { item->paint(event); + this->region_map_layout_item->draw(); this->hasUnsavedChanges = true; if (event->type() == QEvent::GraphicsSceneMouseRelease) { RegionMapHistoryItem *current = history.current(); diff --git a/src/ui/regionmaplayoutpixmapitem.cpp b/src/ui/regionmaplayoutpixmapitem.cpp index 82edc5ba..f9f6c2fc 100644 --- a/src/ui/regionmaplayoutpixmapitem.cpp +++ b/src/ui/regionmaplayoutpixmapitem.cpp @@ -10,11 +10,7 @@ void RegionMapLayoutPixmapItem::draw() { QImage bottom_img = this->tile_selector->tileImg(region_map->map_squares[i].tile_img_id); QImage top_img(8, 8, QImage::Format_RGBA8888); if (region_map->map_squares[i].has_map) { - if (i == highlightedTile) { - top_img.fill(Qt::red); - } else { - top_img.fill(Qt::gray); - } + top_img.fill(Qt::gray); } else { top_img.fill(Qt::black); } @@ -55,8 +51,8 @@ void RegionMapLayoutPixmapItem::select(int index) { void RegionMapLayoutPixmapItem::highlight(int x, int y, int red) { this->highlightedTile = red; - draw(); SelectablePixmapItem::select(x + this->region_map->padLeft, y + this->region_map->padTop, 0, 0); + draw(); } void RegionMapLayoutPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { From a475823fee214f0bef2749c39f56a5ab8b15bc47 Mon Sep 17 00:00:00 2001 From: garak Date: Mon, 25 Mar 2019 00:10:57 -0400 Subject: [PATCH 15/17] further refine region map editor --- forms/regionmapeditor.ui | 76 +++++--------- include/core/regionmap.h | 3 + include/core/regionmapgenerator.h | 77 -------------- include/ui/regionmapeditor.h | 6 +- porymap.pro | 2 - src/core/regionmap.cpp | 31 ++++++ src/core/regionmapgenerator.cpp | 148 -------------------------- src/ui/regionmapeditor.cpp | 169 ++++++++++++++++++++++-------- src/ui/regionmappixmapitem.cpp | 13 +-- 9 files changed, 197 insertions(+), 328 deletions(-) delete mode 100644 include/core/regionmapgenerator.h delete mode 100644 src/core/regionmapgenerator.cpp diff --git a/forms/regionmapeditor.ui b/forms/regionmapeditor.ui index acd7ceb9..96cd6e10 100644 --- a/forms/regionmapeditor.ui +++ b/forms/regionmapeditor.ui @@ -61,8 +61,8 @@ 0 0 - 384 - 249 + 350 + 225 @@ -182,8 +182,8 @@ 0 0 - 384 - 249 + 350 + 225 @@ -330,10 +330,10 @@ - 0 + 8 0 - 283 - 275 + 278 + 262 @@ -487,6 +487,9 @@ true + + QComboBox::NoInsert + @@ -518,43 +521,11 @@ - - - - 10 - 100 - 138 - 26 - - - - - - - x - - - - - - - - - - y - - - - - - - - 10 - 190 + 100 113 32 @@ -651,8 +622,8 @@ 0 0 - 412 - 249 + 441 + 230 @@ -785,10 +756,10 @@ - 0 + 8 0 - 295 - 283 + 255 + 274 @@ -1076,7 +1047,7 @@ 0 0 829 - 21 + 22 @@ -1101,7 +1072,8 @@ Tools - + + @@ -1160,6 +1132,16 @@ Clear Map Layout + + + Import Region Map Image Tiles... + + + + + Import City Map Image Tiles... + + diff --git a/include/core/regionmap.h b/include/core/regionmap.h index 7300cc60..f2556c78 100644 --- a/include/core/regionmap.h +++ b/include/core/regionmap.h @@ -64,6 +64,7 @@ public: void readLayout(); void save(); + void saveTileImages(); void saveBkgImgBin(); void saveLayout(); void saveOptions(int id, QString sec, QString name, int x, int y); @@ -80,7 +81,9 @@ public: unsigned getTileId(int x, int y); int getMapSquareIndex(int x, int y); QString pngPath(); + void setTemporaryPngPath(QString); QString cityTilesPath(); + void setTemporaryCityTilesPath(QString); QVector getTiles(); void setTiles(QVector tileIds); diff --git a/include/core/regionmapgenerator.h b/include/core/regionmapgenerator.h deleted file mode 100644 index 54539009..00000000 --- a/include/core/regionmapgenerator.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef GUARD_REGIONMAPGENERATOR_H -#define GUARD_REGIONMAPGENERATOR_H - -#include "project.h" -#include "regionmap.h" -#include "map.h" - -#include -#include -#include -#include -#include -#include - -class GeneratorEntry -{ - GeneratorEntry(QString name_, int x_, int y_, int width_, int height_) { - this->name = name_; - this->x = x_; - this->y = y_; - this->width = width_; - this->height = height_; - } - int x; - int y; - int width; - int height; - QString name; - friend class RegionMapGenerator; -public: - friend QDebug operator<<(QDebug, const GeneratorEntry &); -}; - -class RegionMapGenerator -{ - // -public: - // - RegionMapGenerator() = default; - RegionMapGenerator(Project *); - ~RegionMapGenerator() {}; - - Project *project = nullptr; - - QVector map_squares; - - QStack> forks;//? - QSet connections;// - // - - void generate(QString); - - void center();// center the map squares so they arent hanging over - -private: - // - int square_block_width_ = 20; - int square_block_height_ = 20; - - int width_; - int height_; - - //QPoint - - QList entries_; - QMap entries; - - void bfs(int);// breadth first search of a graph - void search(int); - void dfs(int, QVector &);// depth first search - void sort(); - void ts();// topological sort - void populateSquares(); - -}; - -#endif // GUARD_REGIONMAPGENERATOR_H diff --git a/include/ui/regionmapeditor.h b/include/ui/regionmapeditor.h index ee364b83..a684095c 100644 --- a/include/ui/regionmapeditor.h +++ b/include/ui/regionmapeditor.h @@ -83,6 +83,7 @@ private: void displayRegionMapTileSelector(); void displayCityMapTileSelector(); void displayCityMap(QString name); + void importTileImage(bool city = false);//QString path);// what is this path tho? bool createCityMap(QString name); @@ -96,7 +97,8 @@ private slots: void on_action_RegionMap_ClearImage_triggered(); void on_action_RegionMap_ClearLayout_triggered(); void on_action_Swap_triggered(); - void on_action_RegionMap_Generate_triggered(); + void on_action_Import_RegionMap_ImageTiles_triggered(); + void on_action_Import_CityMap_ImageTiles_triggered(); void on_tabWidget_Region_Map_currentChanged(int); void on_pushButton_RM_Options_delete_clicked(); void on_comboBox_RM_ConnectedMap_activated(const QString &text); @@ -106,8 +108,6 @@ private slots: void on_verticalSlider_Zoom_City_Map_valueChanged(int); void on_verticalSlider_Zoom_City_Tiles_valueChanged(int); void on_comboBox_CityMap_picker_currentTextChanged(const QString &text); - void on_spinBox_RM_Options_x_valueChanged(int val); - void on_spinBox_RM_Options_y_valueChanged(int val); void on_lineEdit_RM_MapName_textEdited(const QString &text); void onHoveredRegionMapTileChanged(int x, int y); void onHoveredRegionMapTileCleared(); diff --git a/porymap.pro b/porymap.pro index 754f45f1..b3a19b8d 100644 --- a/porymap.pro +++ b/porymap.pro @@ -29,7 +29,6 @@ SOURCES += src/core/block.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ src/core/regionmap.cpp \ - src/core/regionmapgenerator.cpp \ src/ui/aboutporymap.cpp \ src/ui/bordermetatilespixmapitem.cpp \ src/ui/collisionpixmapitem.cpp \ @@ -86,7 +85,6 @@ HEADERS += include/core/block.h \ include/core/tile.h \ include/core/tileset.h \ include/core/regionmap.h \ - include/core/regionmapgenerator.h \ include/ui/aboutporymap.h \ include/ui/bordermetatilespixmapitem.h \ include/ui/collisionpixmapitem.h \ diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 4368c9b3..3968a77c 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -32,11 +32,29 @@ void RegionMap::init(Project *pro) { void RegionMap::save() { logInfo("Saving region map data."); + saveTileImages(); saveBkgImgBin(); saveLayout(); porymapConfig.setRegionMapDimensions(this->img_width_, this->img_height_); } +void RegionMap::saveTileImages() { + QFile backgroundTileFile(pngPath()); + if (backgroundTileFile.open(QIODevice::ReadOnly)) { + QByteArray imageData = backgroundTileFile.readAll(); + QImage pngImage = QImage::fromData(imageData); + this->region_map_png_path = project->root + "/graphics/pokenav/region_map.png"; + pngImage.save(pngPath()); + } + QFile cityTileFile(cityTilesPath()); + if (cityTileFile.open(QIODevice::ReadOnly)) { + QByteArray imageData = cityTileFile.readAll(); + QImage cityTilesImage = QImage::fromData(imageData); + this->city_map_tiles_path = project->root + "/graphics/pokenav/zoom_tiles.png"; + cityTilesImage.save(cityTilesPath()); + } +} + void RegionMap::readBkgImgBin() { QFile binFile(region_map_bin_path); if (!binFile.open(QIODevice::ReadOnly)) return; @@ -168,6 +186,11 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) { resetSquare(id); int index = getMapSquareIndex(x + this->padLeft, y + this->padTop); if (!sec.isEmpty()) { + // Validate the input section name. + if (!project->mapSectionNameToValue.contains(sec)) { + sec = "MAPSEC_NONE"; + name = QString(); + } this->map_squares[index].has_map = sec == "MAPSEC_NONE" ? false : true; this->map_squares[index].secid = static_cast(project->mapSectionNameToValue.value(sec)); this->map_squares[index].mapsec = sec; @@ -295,10 +318,18 @@ QString RegionMap::pngPath() { return this->region_map_png_path; } +void RegionMap::setTemporaryPngPath(QString path) { + this->region_map_png_path = path; +} + QString RegionMap::cityTilesPath() { return this->city_map_tiles_path; } +void RegionMap::setTemporaryCityTilesPath(QString path) { + this->city_map_tiles_path = path; +} + // From x, y of image. int RegionMap::getMapSquareIndex(int x, int y) { int index = (x + y * img_width_); diff --git a/src/core/regionmapgenerator.cpp b/src/core/regionmapgenerator.cpp deleted file mode 100644 index 5cbabc8c..00000000 --- a/src/core/regionmapgenerator.cpp +++ /dev/null @@ -1,148 +0,0 @@ -#include "regionmapgenerator.h" - - - -RegionMapGenerator::RegionMapGenerator(Project *project_) { - this->project = project_; -} - -QDebug operator<<(QDebug debug, const GeneratorEntry &entry) -{ - debug.nospace() << "Entry_" << entry.name - << " (" << entry.x << ", " << entry.y << ")" - << " [" << entry.width << " x " << entry.height << "]" - ; - return debug; -} - -// -void RegionMapGenerator::generate(QString mapName) { - // - int i = project->mapNames->indexOf(mapName); - //Map *map = project->loadMap(mapName); - qDebug() << "generating region map from:" << mapName; - search(i); - - populateSquares(); -} - -// use a progress bar because this is rather slow (because loadMap) -// TODO: use custom functions to load only necessary data from maps? -// need connections and dimensions -// maybe use gMapGroup0.numMaps as hint for size of progress -void RegionMapGenerator::bfs(int start) { - // - int size = project->mapNames->size(); - - //* - - QVector visited(size, false); - QList queue; - - visited[start] = true; - queue.append(start); - - while (!queue.isEmpty()) { - start = queue.first(); - - qDebug() << project->mapNames->at(start); - Map *map = project->loadMap(project->mapNames->at(start)); - - if (!map) break; - - this->entries.insert(map->location, { - map->name, 0, 0, map->getWidth() / square_block_width_, map->getHeight() / square_block_height_ - }); - - queue.removeFirst(); - - // get all connected map indexes - // if not visited, mark it visited and insert into queue - for (auto c : map->connections) { - int i = project->mapNames->indexOf(c->map_name); - if (!visited[i] && c->direction != "dive") { - visited[i] = true; - queue.append(i); - } - } - //delete map; - } - //*/ - qDebug() << "search complete";// << entries.keys(); - //return; -} - -// -void RegionMapGenerator::dfs(int start, QVector &visited) { - // - visited[start] = true; - - Map *map = project->loadMap(project->mapNames->at(start)); - //qDebug() << map->name; - this->entries.insert(map->location, { - map->name, 0, 0, map->getWidth() / square_block_width_, map->getHeight() / square_block_height_ - }); - - // place map on the grid? - - for (auto c : map->connections) { - int i = project->mapNames->indexOf(c->map_name); - if (!visited[i] && c->direction != "dive") { - dfs(i, visited); - } - } -} - -void RegionMapGenerator::search(int start) { - // - int size = project->mapNames->size(); - QVector visited(size, false); - - dfs(start, visited); -} - -void RegionMapGenerator::populateSquares() { - // - for (auto entry : entries.values()) { - qDebug() << entry;//.name << entry.width << "x" << entry.height; - } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index 9efbf014..858abc82 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -1,13 +1,16 @@ #include "regionmapeditor.h" #include "ui_regionmapeditor.h" -#include "regionmapgenerator.h" +#include "imageexport.h" #include "config.h" +#include "log.h" #include #include #include +#include #include #include +#include #include #include #include @@ -19,7 +22,7 @@ RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : this->ui->setupUi(this); this->project = project_; this->region_map = new RegionMap; - this->ui->action_RegionMap_Generate->setVisible(false); + this->ui->action_RegionMap_Resize->setVisible(false); } RegionMapEditor::~RegionMapEditor() @@ -56,8 +59,8 @@ void RegionMapEditor::setCurrentSquareOptions() { this->currIndex, this->ui->comboBox_RM_ConnectedMap->currentText(), this->ui->lineEdit_RM_MapName->text(), - this->ui->spinBox_RM_Options_x->value(), - this->ui->spinBox_RM_Options_y->value() + this->region_map->map_squares[this->currIndex].x, + this->region_map->map_squares[this->currIndex].y ); } } @@ -153,13 +156,6 @@ void RegionMapEditor::displayRegionMapLayoutOptions() { this->ui->frame_RM_Options->setEnabled(true); - this->ui->spinBox_RM_Options_x->setMaximum( - this->region_map->width() - this->region_map->padLeft - this->region_map->padRight - 1 - ); - this->ui->spinBox_RM_Options_y->setMaximum( - this->region_map->height() - this->region_map->padTop - this->region_map->padBottom - 1 - ); - updateRegionMapLayoutOptions(this->currIndex); // TODO: implement when the code is decompiled @@ -168,15 +164,9 @@ void RegionMapEditor::displayRegionMapLayoutOptions() { } void RegionMapEditor::updateRegionMapLayoutOptions(int index) { - this->ui->spinBox_RM_Options_x->blockSignals(true); - this->ui->spinBox_RM_Options_y->blockSignals(true); this->ui->comboBox_RM_ConnectedMap->blockSignals(true); this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(this->region_map->map_squares[index].mapsec)); this->ui->comboBox_RM_ConnectedMap->setCurrentText(this->region_map->map_squares[index].mapsec); - this->ui->spinBox_RM_Options_x->setValue(this->region_map->map_squares[index].x); - this->ui->spinBox_RM_Options_y->setValue(this->region_map->map_squares[index].y); - this->ui->spinBox_RM_Options_x->blockSignals(false); - this->ui->spinBox_RM_Options_y->blockSignals(false); this->ui->comboBox_RM_ConnectedMap->blockSignals(false); } @@ -355,9 +345,6 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg item->select(event); //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - item->paint(event); - this->region_map_layout_item->draw(); - this->hasUnsavedChanges = true; if (event->type() == QEvent::GraphicsSceneMouseRelease) { RegionMapHistoryItem *current = history.current(); bool addToHistory = !(current && current->tiles == this->region_map->getTiles()); @@ -367,6 +354,10 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg ); history.push(commit); } + } else { + item->paint(event); + this->region_map_layout_item->draw(); + this->hasUnsavedChanges = true; } } } @@ -388,8 +379,6 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM if (event->buttons() & Qt::RightButton) {// TODO //} else if (event->buttons() & Qt::MiddleButton) {// TODO } else { - item->paint(event); - this->hasUnsavedChanges = true; if (event->type() == QEvent::GraphicsSceneMouseRelease) { RegionMapHistoryItem *current = history.current(); bool addToHistory = !(current && current->tiles == this->city_map_item->getTiles()); @@ -399,6 +388,9 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM ); history.push(commit); } + } else { + item->paint(event); + this->hasUnsavedChanges = true; } } } @@ -416,23 +408,9 @@ void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { } } -void RegionMapEditor::on_spinBox_RM_Options_x_valueChanged(int x) { - int y = this->ui->spinBox_RM_Options_y->value(); - int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); - this->region_map_layout_item->highlight(x, y, red); - this->hasUnsavedChanges = true; -} - -void RegionMapEditor::on_spinBox_RM_Options_y_valueChanged(int y) { - int x = this->ui->spinBox_RM_Options_x->value(); - int red = this->region_map->getMapSquareIndex(x + this->region_map->padLeft, y + this->region_map->padTop); - this->region_map_layout_item->highlight(x, y, red); - this->hasUnsavedChanges = true; -} - void RegionMapEditor::on_comboBox_RM_ConnectedMap_activated(const QString &mapsec) { this->ui->lineEdit_RM_MapName->setText(this->project->mapSecToMapHoverName->value(mapsec)); - //this->hasUnsavedChanges = true;// sometimes this is called for unknown reasons + this->hasUnsavedChanges = true;// sometimes this is called for unknown reasons } void RegionMapEditor::on_lineEdit_RM_MapName_textEdited(const QString &text) { @@ -441,6 +419,7 @@ void RegionMapEditor::on_lineEdit_RM_MapName_textEdited(const QString &text) { void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() { this->region_map->resetSquare(this->region_map_layout_item->selectedTile); + updateRegionMapLayoutOptions(this->region_map_layout_item->selectedTile); this->region_map_layout_item->draw(); this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); this->hasUnsavedChanges = true; @@ -526,6 +505,7 @@ void RegionMapEditor::undo() { this->resize(commit->mapWidth, commit->mapHeight); this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); + this->region_map_layout_item->draw(); break; case RegionMapEditorBox::CityMapImage: if (commit->cityMap == this->city_map_item->file) @@ -551,6 +531,7 @@ void RegionMapEditor::redo() { this->resize(commit->mapWidth, commit->mapHeight); this->region_map->setTiles(commit->tiles); this->region_map_item->draw(); + this->region_map_layout_item->draw(); break; case RegionMapEditorBox::CityMapImage: this->city_map_item->setTiles(commit->tiles); @@ -634,6 +615,114 @@ void RegionMapEditor::on_action_RegionMap_ClearLayout_triggered() { } } +void RegionMapEditor::on_action_Import_RegionMap_ImageTiles_triggered() { + importTileImage(false); +} + +void RegionMapEditor::on_action_Import_CityMap_ImageTiles_triggered() { + importTileImage(true); +} + +void RegionMapEditor::importTileImage(bool city) { + QString descriptor = city ? "City Map" : "Region Map"; + + QString infilepath = QFileDialog::getOpenFileName(this, QString("Import %1 Tiles Image").arg(descriptor), + this->project->root, "Image Files (*.png *.bmp *.jpg *.dib)"); + if (infilepath.isEmpty()) { + return; + } + + logInfo(QString("Importing %1 Tiles from '%2'").arg(descriptor).arg(infilepath)); + + // Read image data from buffer so that the built-in QImage doesn't try to detect file format + // purely from the extension name. + QFile file(infilepath); + QImage image; + if (file.open(QIODevice::ReadOnly)) { + QByteArray imageData = file.readAll(); + image = QImage::fromData(imageData); + } else { + QString errorMessage = QString("Failed to open image file: '%1'").arg(infilepath); + logError(errorMessage); + QMessageBox msgBox(this); + msgBox.setText("Failed to import tiles."); + msgBox.setInformativeText(errorMessage); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Icon::Critical); + msgBox.exec(); + return; + } + if (image.width() == 0 || image.height() == 0 || image.width() % 8 != 0 || image.height() % 8 != 0) { + QString errorMessage = QString("The image dimensions (%1 x %2) are invalid. Width and height must be multiples of 8 pixels.") + .arg(image.width()) + .arg(image.height()); + logError(errorMessage); + QMessageBox msgBox(this); + msgBox.setText("Failed to import tiles."); + msgBox.setInformativeText(errorMessage); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Icon::Critical); + msgBox.exec(); + return; + } + + // Validate total number of tiles in image. + int numTilesWide = image.width() / 8; + int numTilesHigh = image.height() / 8; + int totalTiles = numTilesHigh * numTilesWide; + int maxAllowedTiles = 0x100; + if (totalTiles > maxAllowedTiles) { + QString errorMessage = QString("The total number of tiles in the provided image (%1) is greater than the allowed number (%2).") + .arg(maxAllowedTiles) + .arg(totalTiles); + logError(errorMessage); + QMessageBox msgBox(this); + msgBox.setText("Failed to import tiles."); + msgBox.setInformativeText(errorMessage); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Icon::Critical); + msgBox.exec(); + return; + } + + // Validate the image's palette. + QString palMessage = QString(); + bool palError = false; + if (image.colorCount() == 0) { + palMessage = QString("The provided image is not indexed."); + palError = true; + } else if (!city && image.colorCount() != 256) { + palMessage = QString("The provided image has a palette with %1 colors. You must provide an indexed imaged with a 256 color palette.").arg(image.colorCount()); + palError = true; + } else if (city && image.colorCount() != 16) { + palMessage = QString("The provided image has a palette with %1 colors. You must provide an indexed imaged with a 16 color palette.").arg(image.colorCount()); + palError = true; + } + + if (palError) { + logError(palMessage); + QMessageBox msgBox(this); + msgBox.setText("Failed to import tiles."); + msgBox.setInformativeText(palMessage); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.setIcon(QMessageBox::Icon::Critical); + msgBox.exec(); + return; + } + + // Use the image from the correct path. + if (city) { + this->region_map->setTemporaryCityTilesPath(infilepath); + } else { + this->region_map->setTemporaryPngPath(infilepath); + } + this->hasUnsavedChanges = true; + + // Redload and redraw images. + displayRegionMap(); + displayCityMap(this->ui->comboBox_CityMap_picker->currentText()); +} + void RegionMapEditor::on_comboBox_CityMap_picker_currentTextChanged(const QString &file) { this->displayCityMap(file); this->cityMapFirstDraw = true; @@ -716,9 +805,3 @@ void RegionMapEditor::on_verticalSlider_Zoom_City_Tiles_valueChanged(int val) { ui->graphicsView_City_Map_Tiles->setMatrix(matrix); ui->graphicsView_City_Map_Tiles->setFixedSize(width + 2, height + 2); } - -void RegionMapEditor::on_action_RegionMap_Generate_triggered() { - RegionMapGenerator generator(this->project); - generator.generate("LittlerootTown"); - this->hasUnsavedChanges = true; -} diff --git a/src/ui/regionmappixmapitem.cpp b/src/ui/regionmappixmapitem.cpp index 5488ca4c..3ca04b06 100644 --- a/src/ui/regionmappixmapitem.cpp +++ b/src/ui/regionmappixmapitem.cpp @@ -20,14 +20,11 @@ void RegionMapPixmapItem::draw() { void RegionMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { if (region_map) { - if (event->type() == QEvent::GraphicsSceneMousePress) { - } else { - QPointF pos = event->pos(); - int x = static_cast(pos.x()) / 8; - int y = static_cast(pos.y()) / 8; - int index = x + y * region_map->width(); - this->region_map->map_squares[index].tile_img_id = this->tile_selector->selectedTile; - } + QPointF pos = event->pos(); + int x = static_cast(pos.x()) / 8; + int y = static_cast(pos.y()) / 8; + int index = x + y * region_map->width(); + this->region_map->map_squares[index].tile_img_id = this->tile_selector->selectedTile; draw(); } } From 5bfc32fb7f876985e3cd4a897dddd38c661ccb3a Mon Sep 17 00:00:00 2001 From: garak Date: Sat, 6 Apr 2019 18:11:56 -0400 Subject: [PATCH 16/17] write palette of region map image, fix ruby city map render --- .../core/{paletteparser.h => paletteutil.h} | 11 +++-- include/ui/citymappixmapitem.h | 2 +- porymap.pro | 4 +- .../{paletteparser.cpp => paletteutil.cpp} | 48 +++++++++++++++---- src/core/regionmap.cpp | 4 ++ src/ui/citymappixmapitem.cpp | 10 ++++ src/ui/paletteeditor.cpp | 4 +- src/ui/tileseteditor.cpp | 4 +- 8 files changed, 65 insertions(+), 22 deletions(-) rename include/core/{paletteparser.h => paletteutil.h} (74%) rename src/core/{paletteparser.cpp => paletteutil.cpp} (85%) diff --git a/include/core/paletteparser.h b/include/core/paletteutil.h similarity index 74% rename from include/core/paletteparser.h rename to include/core/paletteutil.h index 336393cf..a067dea3 100644 --- a/include/core/paletteparser.h +++ b/include/core/paletteutil.h @@ -1,15 +1,16 @@ -#ifndef PALETTEPARSER_H -#define PALETTEPARSER_H +#ifndef PALETTEUTIL_H +#define PALETTEUTIL_H #include #include #include -class PaletteParser +class PaletteUtil { public: - PaletteParser(); + PaletteUtil(); QList parse(QString filepath, bool *error); + void writeJASC(QString filepath, QVector colors, int offset, int nColors); private: QList parsePal(QString filepath, bool *error); QList parseJASC(QString filepath, bool *error); @@ -20,4 +21,4 @@ private: int clampColorValue(int value); }; -#endif // PALETTEPARSER_H +#endif // PALETTEUTIL_H diff --git a/include/ui/citymappixmapitem.h b/include/ui/citymappixmapitem.h index bc82e3c1..a3c48faa 100644 --- a/include/ui/citymappixmapitem.h +++ b/include/ui/citymappixmapitem.h @@ -21,7 +21,7 @@ public: QString file; QByteArray data; - + void init(); void save(); void create(QString); diff --git a/porymap.pro b/porymap.pro index b3a19b8d..177f488b 100644 --- a/porymap.pro +++ b/porymap.pro @@ -24,7 +24,7 @@ SOURCES += src/core/block.cpp \ src/core/maplayout.cpp \ src/core/metatile.cpp \ src/core/metatileparser.cpp \ - src/core/paletteparser.cpp \ + src/core/paletteutil.cpp \ src/core/parseutil.cpp \ src/core/tile.cpp \ src/core/tileset.cpp \ @@ -80,7 +80,7 @@ HEADERS += include/core/block.h \ include/core/maplayout.h \ include/core/metatile.h \ include/core/metatileparser.h \ - include/core/paletteparser.h \ + include/core/paletteutil.h \ include/core/parseutil.h \ include/core/tile.h \ include/core/tileset.h \ diff --git a/src/core/paletteparser.cpp b/src/core/paletteutil.cpp similarity index 85% rename from src/core/paletteparser.cpp rename to src/core/paletteutil.cpp index 5a024628..4080e40c 100644 --- a/src/core/paletteparser.cpp +++ b/src/core/paletteutil.cpp @@ -1,14 +1,14 @@ -#include "paletteparser.h" +#include "paletteutil.h" #include "log.h" #include #include -PaletteParser::PaletteParser() +PaletteUtil::PaletteUtil() { } -QList PaletteParser::parse(QString filepath, bool *error) { +QList PaletteUtil::parse(QString filepath, bool *error) { QFileInfo info(filepath); QString extension = info.completeSuffix(); if (extension.isNull()) { @@ -34,7 +34,7 @@ QList PaletteParser::parse(QString filepath, bool *error) { return QList(); } -QList PaletteParser::parsePal(QString filepath, bool *error) { +QList PaletteUtil::parsePal(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -53,7 +53,7 @@ QList PaletteParser::parsePal(QString filepath, bool *error) { } } -QList PaletteParser::parseJASC(QString filepath, bool *error) { +QList PaletteUtil::parseJASC(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -120,7 +120,7 @@ QList PaletteParser::parseJASC(QString filepath, bool *error) { return palette; } -QList PaletteParser::parseAdvanceMapPal(QString filepath, bool *error) { +QList PaletteUtil::parseAdvanceMapPal(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -152,7 +152,7 @@ QList PaletteParser::parseAdvanceMapPal(QString filepath, bool *error) { return palette; } -QList PaletteParser::parseAdobeColorTable(QString filepath, bool *error) { +QList PaletteUtil::parseAdobeColorTable(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -184,7 +184,7 @@ QList PaletteParser::parseAdobeColorTable(QString filepath, bool *error) { return palette; } -QList PaletteParser::parseTileLayerPro(QString filepath, bool *error) { +QList PaletteUtil::parseTileLayerPro(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -222,7 +222,7 @@ QList PaletteParser::parseTileLayerPro(QString filepath, bool *error) { return palette; } -QList PaletteParser::parseAdvancePaletteEditor(QString filepath, bool *error) { +QList PaletteUtil::parseAdvancePaletteEditor(QString filepath, bool *error) { QFile file(filepath); if (!file.open(QIODevice::ReadOnly)) { *error = true; @@ -267,7 +267,35 @@ QList PaletteParser::parseAdvancePaletteEditor(QString filepath, bool *err return palette; } -int PaletteParser::clampColorValue(int value) { +void PaletteUtil::writeJASC(QString filepath, QVector palette, int offset, int nColors) { + if (!nColors) { + logWarn(QString("Cannot save a palette with no colors.")); + return; + } + if (offset > palette.size() || offset + nColors > palette.size()) { + logWarn("Palette offset out of range for color table."); + return; + } + + QString text = "JASC-PAL\n0100\n"; + text += QString::number(nColors) + "\n"; + + for (int i = offset; i < offset + nColors; i++) { + QRgb color = palette.at(i); + text += QString::number(qRed(color)) + " " + + QString::number(qGreen(color)) + " " + + QString::number(qBlue(color)) + "\n"; + } + + QFile file(filepath); + if (file.open(QIODevice::WriteOnly)) { + file.write(text.toUtf8()); + } else { + logWarn(QString("Could not write to file '%1': ").arg(filepath) + file.errorString()); + } +} + +int PaletteUtil::clampColorValue(int value) { if (value < 0) { value = 0; } diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 3968a77c..ec052e3b 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -1,4 +1,5 @@ #include "regionmap.h" +#include "paletteutil.h" #include "log.h" #include "config.h" @@ -45,6 +46,9 @@ void RegionMap::saveTileImages() { QImage pngImage = QImage::fromData(imageData); this->region_map_png_path = project->root + "/graphics/pokenav/region_map.png"; pngImage.save(pngPath()); + + PaletteUtil parser; + parser.writeJASC(project->root + "/graphics/pokenav/region_map.pal", pngImage.colorTable(), 0x70, 0x20); } QFile cityTileFile(cityTilesPath()); if (cityTileFile.open(QIODevice::ReadOnly)) { diff --git a/src/ui/citymappixmapitem.cpp b/src/ui/citymappixmapitem.cpp index 3d0eb2ca..5f270eb4 100644 --- a/src/ui/citymappixmapitem.cpp +++ b/src/ui/citymappixmapitem.cpp @@ -1,5 +1,6 @@ #include "citymappixmapitem.h" #include "imageproviders.h" +#include "config.h" #include "log.h" #include @@ -14,6 +15,11 @@ void CityMapPixmapItem::init() { if (!binFile.open(QIODevice::ReadOnly)) return; data = binFile.readAll(); + if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) { + for (int i = 0; i < data.size(); i++) + data[i] = data[i] ^ 0x80; + } + binFile.close(); } @@ -39,6 +45,10 @@ void CityMapPixmapItem::save() { logError(QString("Cannot save city map tilemap to %1.").arg(file)); return; } + if (projectConfig.getBaseGameVersion() == BaseGameVersion::pokeruby) { + for (int i = 0; i < data.size(); i++) + data[i] = data[i] ^ 0x80; + } binFile.write(data); binFile.close(); } diff --git a/src/ui/paletteeditor.cpp b/src/ui/paletteeditor.cpp index fa195d7c..c573fa79 100644 --- a/src/ui/paletteeditor.cpp +++ b/src/ui/paletteeditor.cpp @@ -1,6 +1,6 @@ #include "paletteeditor.h" #include "ui_paletteeditor.h" -#include "paletteparser.h" +#include "paletteutil.h" #include #include #include "log.h" @@ -268,7 +268,7 @@ void PaletteEditor::on_actionImport_Palette_triggered() return; } - PaletteParser parser; + PaletteUtil parser; bool error = false; QList palette = parser.parse(filepath, &error); if (error) { diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 58d07217..1fa8602c 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -3,7 +3,7 @@ #include "log.h" #include "imageproviders.h" #include "metatileparser.h" -#include "paletteparser.h" +#include "paletteutil.h" #include "imageexport.h" #include #include @@ -408,7 +408,7 @@ void TilesetEditor::importTilesetTiles(Tileset *tileset, bool primary) { return; } - PaletteParser parser; + PaletteUtil parser; bool error = false; QList palette = parser.parse(filepath, &error); if (error) { From a61dd168cf631ba24c0e7692a1393b4d39f0154b Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 6 Apr 2019 19:58:38 -0500 Subject: [PATCH 17/17] Write JASC files with \r\n line endings --- src/core/paletteutil.cpp | 6 +++--- src/project.cpp | 15 +++------------ 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/core/paletteutil.cpp b/src/core/paletteutil.cpp index 4080e40c..3a6f7ff7 100644 --- a/src/core/paletteutil.cpp +++ b/src/core/paletteutil.cpp @@ -277,14 +277,14 @@ void PaletteUtil::writeJASC(QString filepath, QVector palette, int offset, return; } - QString text = "JASC-PAL\n0100\n"; - text += QString::number(nColors) + "\n"; + QString text = "JASC-PAL\r\n0100\r\n"; + text += QString::number(nColors) + "\r\n"; for (int i = offset; i < offset + nColors; i++) { QRgb color = palette.at(i); text += QString::number(qRed(color)) + " " + QString::number(qGreen(color)) + " " - + QString::number(qBlue(color)) + "\n"; + + QString::number(qBlue(color)) + "\r\n"; } QFile file(filepath); diff --git a/src/project.cpp b/src/project.cpp index ab01eb21..133cc807 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -4,6 +4,7 @@ #include "historyitem.h" #include "log.h" #include "parseutil.h" +#include "paletteutil.h" #include "tile.h" #include "tileset.h" #include "event.h" @@ -720,20 +721,10 @@ void Project::saveTilesetTilesImage(Tileset *tileset) { } void Project::saveTilesetPalettes(Tileset *tileset, bool primary) { + PaletteUtil parser; for (int i = 0; i < Project::getNumPalettesTotal(); i++) { QString filepath = tileset->palettePaths.at(i); - QString content = "JASC-PAL\r\n"; - content += "0100\r\n"; - content += "16\r\n"; - for (int j = 0; j < 16; j++) { - QRgb color = tileset->palettes->at(i).at(j); - content += QString("%1 %2 %3\r\n") - .arg(qRed(color)) - .arg(qGreen(color)) - .arg(qBlue(color)); - } - - saveTextFile(filepath, content); + parser.writeJASC(filepath, tileset->palettes->at(i).toVector(), 0, 16); } }