create edit history for region map tilemap and layout

This commit is contained in:
garak 2022-04-26 17:02:03 -04:00 committed by garakmon
parent 43ebeb1662
commit 2b152ce64e
9 changed files with 152 additions and 176 deletions

View file

@ -1124,8 +1124,6 @@
<property name="title"> <property name="title">
<string>Edit</string> <string>Edit</string>
</property> </property>
<addaction name="action_RegionMap_Undo"/>
<addaction name="action_RegionMap_Redo"/>
<addaction name="action_RegionMap_Resize"/> <addaction name="action_RegionMap_Resize"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="action_Swap"/> <addaction name="action_Swap"/>
@ -1150,22 +1148,6 @@
<string>Ctrl+S</string> <string>Ctrl+S</string>
</property> </property>
</action> </action>
<action name="action_RegionMap_Undo">
<property name="text">
<string>Undo</string>
</property>
<property name="shortcut">
<string>Ctrl+Z</string>
</property>
</action>
<action name="action_RegionMap_Redo">
<property name="text">
<string>Redo</string>
</property>
<property name="shortcut">
<string>Ctrl+Y</string>
</property>
</action>
<action name="action_RegionMap_Resize"> <action name="action_RegionMap_Resize">
<property name="text"> <property name="text">
<string>Resize</string> <string>Resize</string>

View file

@ -19,32 +19,6 @@ using std::shared_ptr;
class Project; class Project;
enum RegionMapEditorBox {
BackgroundImage = 1,
CityMapImage = 2,
};
class RegionMapHistoryItem {
public:
int which;
int mapWidth = 0;
int mapHeight = 0;
QVector<uint8_t> tiles;
QString cityMap;
RegionMapHistoryItem(int which, QVector<uint8_t> tiles, QString cityMap) {
this->which = which;
this->tiles = tiles;
this->cityMap = cityMap;
}
RegionMapHistoryItem(int which, QVector<uint8_t> tiles, int width, int height) {
this->which = which;
this->tiles = tiles;
this->mapWidth = width;
this->mapHeight = height;
}
~RegionMapHistoryItem() {}
};
struct LayoutSquare struct LayoutSquare
{ {
QString map_section; QString map_section;
@ -69,9 +43,9 @@ public:
RegionMap() = delete; RegionMap() = delete;
RegionMap(Project *); RegionMap(Project *);
Project *project = nullptr; ~RegionMap() {}
History<RegionMapHistoryItem*> history; // TODO Project *project = nullptr;
bool loadMapData(poryjson::Json); bool loadMapData(poryjson::Json);
bool loadTilemap(poryjson::Json); bool loadTilemap(poryjson::Json);
@ -121,6 +95,9 @@ public:
QByteArray getTilemap(); QByteArray getTilemap();
void setTilemap(QByteArray newTilemap); void setTilemap(QByteArray newTilemap);
QList<LayoutSquare> getLayout(QString layer);
void setLayout(QString layer, QList<LayoutSquare> layout);
QStringList getLayers() { return this->layout_layers; } QStringList getLayers() { return this->layout_layers; }
void setLayer(QString layer) { this->current_layer = layer; } void setLayer(QString layer) { this->current_layer = layer; }
QString getLayer() { return this->current_layer; } QString getLayer() { return this->current_layer; }
@ -146,6 +123,12 @@ public:
QString fullPath(QString local); QString fullPath(QString local);
void commit(QUndoCommand *command);
QUndoStack editHistory;
void undo();
void redo();
private: private:
// TODO: defaults needed? // TODO: defaults needed?
tsl::ordered_map<QString, MapSectionEntry> *region_map_entries = nullptr; tsl::ordered_map<QString, MapSectionEntry> *region_map_entries = nullptr;

View file

@ -45,9 +45,6 @@ public:
void onRegionMapEntriesSelectedTileChanged(QString) {}; void onRegionMapEntriesSelectedTileChanged(QString) {};
void onRegionMapEntryDragged(int, int); void onRegionMapEntryDragged(int, int);
void undo();
void redo();
void resize(int width, int height); void resize(int width, int height);
QObjectList shortcutableObjects() const; QObjectList shortcutableObjects() const;
@ -61,7 +58,7 @@ private:
poryjson::Json rmConfigJson; poryjson::Json rmConfigJson;
History<RegionMapHistoryItem*> history; QUndoGroup history;
int currIndex = 0; int currIndex = 0;
unsigned selectedCityTile; unsigned selectedCityTile;
@ -124,8 +121,6 @@ private:
private slots: private slots:
void on_action_RegionMap_Save_triggered(); 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_action_RegionMap_Resize_triggered();
void on_action_RegionMap_ClearImage_triggered(); void on_action_RegionMap_ClearImage_triggered();
void on_action_RegionMap_ClearLayout_triggered(); void on_action_RegionMap_ClearLayout_triggered();

View file

@ -123,7 +123,9 @@ public:
this->tileset = QImage(tilesetFilepath); this->tileset = QImage(tilesetFilepath);
this->format = format; this->format = format;
bool err; bool err;
this->palette = PaletteUtil::parse(palFilepath, &err); if (!palFilepath.isEmpty()) {
this->palette = PaletteUtil::parse(palFilepath, &err);
}
this->setPixmap(QPixmap::fromImage(this->tileset)); this->setPixmap(QPixmap::fromImage(this->tileset));
this->numTilesWide = this->tileset.width() / 8; this->numTilesWide = this->tileset.width() / 8;
this->selectedTile = 0x00; this->selectedTile = 0x00;

View file

@ -12,7 +12,7 @@ TARGET = porymap
TEMPLATE = app TEMPLATE = app
RC_ICONS = resources/icons/porymap-icon-2.ico RC_ICONS = resources/icons/porymap-icon-2.ico
ICON = resources/icons/porymap.icns ICON = resources/icons/porymap.icns
QMAKE_CXXFLAGS += -std=c++11 -Wall QMAKE_CXXFLAGS += -std=c++17 -Wall
QMAKE_TARGET_BUNDLE_PREFIX = com.pret QMAKE_TARGET_BUNDLE_PREFIX = com.pret
SOURCES += src/core/block.cpp \ SOURCES += src/core/block.cpp \
@ -33,6 +33,7 @@ SOURCES += src/core/block.cpp \
src/core/wildmoninfo.cpp \ src/core/wildmoninfo.cpp \
src/core/editcommands.cpp \ src/core/editcommands.cpp \
src/lib/orderedjson.cpp \ src/lib/orderedjson.cpp \
src/core/regionmapeditcommands.cpp \
src/mainwindow_scriptapi.cpp \ src/mainwindow_scriptapi.cpp \
src/ui/aboutporymap.cpp \ src/ui/aboutporymap.cpp \
src/ui/draggablepixmapitem.cpp \ src/ui/draggablepixmapitem.cpp \
@ -106,6 +107,7 @@ HEADERS += include/core/block.h \
include/core/regionmap.h \ include/core/regionmap.h \
include/core/wildmoninfo.h \ include/core/wildmoninfo.h \
include/core/editcommands.h \ include/core/editcommands.h \
include/core/regionmapeditcommands.h \
include/lib/orderedmap.h \ include/lib/orderedmap.h \
include/lib/orderedjson.h \ include/lib/orderedjson.h \
include/ui/aboutporymap.h \ include/ui/aboutporymap.h \

View file

@ -4,6 +4,7 @@
#include "project.h" #include "project.h"
#include "log.h" #include "log.h"
#include "config.h" #include "config.h"
#include "regionmapeditcommands.h"
#include <QByteArray> #include <QByteArray>
#include <QFile> #include <QFile>
@ -163,7 +164,7 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) {
layout.append(square); layout.append(square);
} }
} }
this->layouts["main"] = layout; setLayout("main", layout);
break; break;
} }
case LayoutFormat::CArray: case LayoutFormat::CArray:
@ -226,7 +227,7 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) {
} }
y++; y++;
} }
this->layouts[layerName] = layout; setLayout(layerName, layout);
} }
} else { } else {
@ -241,13 +242,23 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) {
return !errored; return !errored;
} }
void RegionMap::save() { void RegionMap::commit(QUndoCommand *command) {
logInfo("Saving region map data."); editHistory.push(command);
}
void RegionMap::undo() {
//
editHistory.undo();
}
void RegionMap::redo() {
//
editHistory.redo();
}
void RegionMap::save() {
saveTilemap(); saveTilemap();
saveLayout(); saveLayout();
// TODO
} }
void RegionMap::saveTilemap() { void RegionMap::saveTilemap() {
@ -320,18 +331,19 @@ void RegionMap::saveOptions(int id, QString sec, QString name, int x, int y) {
} }
void RegionMap::resetSquare(int index) { void RegionMap::resetSquare(int index) {
// TODO this->layouts[this->current_layer][index].map_section = "MAPSEC_NONE";
this->layouts[this->current_layer][index].has_map = false;
} }
void RegionMap::clearLayout() { void RegionMap::clearLayout() {
// TODO for (int i = 0; i < this->layout_width * this->layout_height; i++) {
resetSquare(i);
}
} }
void RegionMap::clearImage() { void RegionMap::clearImage() {
// TODO QByteArray zeros(this->tilemapSize(), 0);
this->setTilemap(zeros);
} }
void RegionMap::replaceSectionId(unsigned oldId, unsigned newId) { void RegionMap::replaceSectionId(unsigned oldId, unsigned newId) {
@ -404,8 +416,17 @@ void RegionMap::setTilemap(QByteArray newTilemap) {
} }
} }
QList<LayoutSquare> RegionMap::getLayout(QString layer) {
return this->layouts[layer];
}
void RegionMap::setLayout(QString layer, QList<LayoutSquare> layout) {
this->layouts[layer] = layout;
}
QVector<uint8_t> RegionMap::getTiles() { QVector<uint8_t> RegionMap::getTiles() {
QVector<uint8_t> tileIds; QVector<uint8_t> tileIds;
// unused? remove when redo history is fully transitioned
// TODO: change this to use TilemapTile instead of uint8_t // TODO: change this to use TilemapTile instead of uint8_t
return tileIds; return tileIds;
@ -423,7 +444,7 @@ int RegionMap::get_tilemap_index(int x, int y) {
// Layout coords to layout index. // Layout coords to layout index.
int RegionMap::get_layout_index(int x, int y) { int RegionMap::get_layout_index(int x, int y) {
return x + y * this->tilemap_width; return x + y * this->layout_width;
} }
unsigned RegionMap::getTileId(int index) { unsigned RegionMap::getTileId(int index) {
@ -526,7 +547,7 @@ MapSectionEntry RegionMap::getEntry(QString section) {
} }
QString RegionMap::palPath() { QString RegionMap::palPath() {
return this->project->root + "/" + this->palette_path; return this->palette_path.isEmpty() ? QString() : this->project->root + "/" + this->palette_path;
} }
QString RegionMap::pngPath() { QString RegionMap::pngPath() {

View file

@ -1,6 +1,7 @@
#include "regionmapeditor.h" #include "regionmapeditor.h"
#include "ui_regionmapeditor.h" #include "ui_regionmapeditor.h"
#include "regionmappropertiesdialog.h" #include "regionmappropertiesdialog.h"
#include "regionmapeditcommands.h"
#include "imageexport.h" #include "imageexport.h"
#include "shortcut.h" #include "shortcut.h"
#include "config.h" #include "config.h"
@ -36,6 +37,11 @@ RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) :
RegionMapEditor::~RegionMapEditor() RegionMapEditor::~RegionMapEditor()
{ {
delete ui; delete ui;
// deletion must be done in this order else crashes
auto stacks = this->history.stacks();
for (auto *stack : stacks) {
this->history.removeStack(stack);
}
for (auto p : this->region_maps) { for (auto p : this->region_maps) {
delete p.second; delete p.second;
} }
@ -64,6 +70,21 @@ void RegionMapEditor::initShortcuts() {
shortcut_RM_Options_delete->setObjectName("shortcut_RM_Options_delete"); shortcut_RM_Options_delete->setObjectName("shortcut_RM_Options_delete");
shortcut_RM_Options_delete->setWhatsThis("Map Layout: Delete Square"); shortcut_RM_Options_delete->setWhatsThis("Map Layout: Delete Square");
QAction *undoAction = this->history.createUndoAction(this, tr("&Undo"));
undoAction->setObjectName("action_RegionMap_Undo");
undoAction->setShortcut(QKeySequence("Ctrl+Z"));
QAction *redoAction = this->history.createRedoAction(this, tr("&Redo"));
redoAction->setObjectName("action_RegionMap_Redo");
redoAction->setShortcuts({QKeySequence("Ctrl+Y"), QKeySequence("Ctrl+Shift+Z")});
ui->menuEdit->addAction(undoAction);
ui->menuEdit->addAction(redoAction);
connect(&(this->history), &QUndoGroup::indexChanged, [this](int) {
on_tabWidget_Region_Map_currentChanged(this->ui->tabWidget_Region_Map->currentIndex());
});
shortcutsConfig.load(); shortcutsConfig.load();
shortcutsConfig.setDefaultShortcuts(shortcutableObjects()); shortcutsConfig.setDefaultShortcuts(shortcutableObjects());
applyUserShortcuts(); applyUserShortcuts();
@ -409,6 +430,8 @@ bool RegionMapEditor::load() {
newMap->loadMapData(o); newMap->loadMapData(o);
region_maps[alias] = newMap; region_maps[alias] = newMap;
this->history.addStack(&(newMap->editHistory));
} }
// add to ui // add to ui
@ -420,6 +443,7 @@ bool RegionMapEditor::load() {
if (!region_maps.empty()) { if (!region_maps.empty()) {
this->region_map = region_maps.begin()->second; this->region_map = region_maps.begin()->second;
this->currIndex = this->region_map->firstLayoutIndex(); this->currIndex = this->region_map->firstLayoutIndex();
this->region_map->editHistory.setActive();
displayRegionMap(); displayRegionMap();
} }
@ -477,11 +501,14 @@ void RegionMapEditor::on_comboBox_regionSelector_textActivated(const QString &re
// //
if (this->region_maps.contains(region)) { if (this->region_maps.contains(region)) {
this->region_map = region_maps.at(region); this->region_map = region_maps.at(region);
this->region_map->editHistory.setActive();
this->currIndex = this->region_map->firstLayoutIndex(); this->currIndex = this->region_map->firstLayoutIndex();
// TODO: make the above into a function that takes an alias string? in case there is more to it // TODO: make the above into a function that takes an alias string? in case there is more to it
// TODO: anything else needed here? // TODO: anything else needed here?
displayRegionMap(); displayRegionMap();
//this->editGroup.setActiveStack(&(this->region_map->editHistory));
} }
} }
@ -510,15 +537,6 @@ void RegionMapEditor::displayRegionMapImage() {
this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image); this->ui->graphicsView_Region_Map_BkgImg->setScene(this->scene_region_map_image);
on_verticalSlider_Zoom_Map_Image_valueChanged(this->ui->verticalSlider_Zoom_Map_Image->value()); on_verticalSlider_Zoom_Map_Image_valueChanged(this->ui->verticalSlider_Zoom_Map_Image->value());
// if (regionMapFirstDraw) {
// on_verticalSlider_Zoom_Map_Image_valueChanged(this->ui->verticalSlider_Zoom_Map_Image->value());
// RegionMapHistoryItem *commit = new RegionMapHistoryItem(
// RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->tilemapWidth(), this->region_map->height()
// );
// history.push(commit);
// regionMapFirstDraw = false;
// }
} }
void RegionMapEditor::displayRegionMapLayout() { void RegionMapEditor::displayRegionMapLayout() {
@ -670,6 +688,8 @@ void RegionMapEditor::displayRegionMapTileSelector() {
this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles); this->ui->graphicsView_RegionMap_Tiles->setScene(this->scene_region_map_tiles);
on_verticalSlider_Zoom_Image_Tiles_valueChanged(this->ui->verticalSlider_Zoom_Image_Tiles->value()); on_verticalSlider_Zoom_Image_Tiles_valueChanged(this->ui->verticalSlider_Zoom_Image_Tiles->value());
this->ui->frame_tileOptions->setEnabled(this->region_map->tilemapFormat() != TilemapFormat::Plain);
this->mapsquare_selector_item->select(this->selectedImageTile); this->mapsquare_selector_item->select(this->selectedImageTile);
} }
@ -842,21 +862,9 @@ void RegionMapEditor::mouseEvent_region_map(QGraphicsSceneMouseEvent *event, Reg
item->select(event); item->select(event);
//} else if (event->buttons() & Qt::MiddleButton) {// TODO //} else if (event->buttons() & Qt::MiddleButton) {// TODO
} else { } else {
if (event->type() == QEvent::GraphicsSceneMouseRelease) { item->paint(event);
//< TODO: history this->region_map_layout_item->draw();
// RegionMapHistoryItem *current = history.current(); this->hasUnsavedChanges = true;
// bool addToHistory = !(current && current->tiles == this->region_map->getTiles());
// if (addToHistory) {
// RegionMapHistoryItem *commit = new RegionMapHistoryItem(
// RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->width(), this->region_map->height()
// );
// history.push(commit);
// }
} else {
item->paint(event);
this->region_map_layout_item->draw();
this->hasUnsavedChanges = true;
}
} }
} }
@ -894,15 +902,20 @@ void RegionMapEditor::mouseEvent_city_map(QGraphicsSceneMouseEvent *event, CityM
void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) { void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) {
this->ui->stackedWidget_RM_Options->setCurrentIndex(index); this->ui->stackedWidget_RM_Options->setCurrentIndex(index);
if (!region_map) return;
switch (index) switch (index)
{ {
case 0: case 0:
this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(true); this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(true);
this->region_map_item->draw(); if (this->region_map_item)
this->region_map_item->draw();
break; break;
case 1: case 1:
this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(false); this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(false);
this->region_map_layout_item->draw(); if (this->region_map_layout_item) {
this->region_map_layout_item->draw();
updateRegionMapLayoutOptions(this->currIndex);
}
break; break;
case 2: case 2:
this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(false); this->ui->verticalSlider_Zoom_Image_Tiles->setVisible(false);
@ -912,9 +925,17 @@ void RegionMapEditor::on_tabWidget_Region_Map_currentChanged(int index) {
} }
void RegionMapEditor::on_comboBox_RM_ConnectedMap_textActivated(const QString &mapsec) { void RegionMapEditor::on_comboBox_RM_ConnectedMap_textActivated(const QString &mapsec) {
QString layer = this->region_map->getLayer();
QList<LayoutSquare> oldLayout = this->region_map->getLayout(layer);
this->region_map->setSquareMapSection(this->currIndex, mapsec); this->region_map->setSquareMapSection(this->currIndex, mapsec);
onRegionMapLayoutSelectedTileChanged(this->currIndex);// re-draw layout image QList<LayoutSquare> newLayout = this->region_map->getLayout(layer);
EditLayout *command = new EditLayout(this->region_map, layer, this->currIndex, oldLayout, newLayout);
this->region_map->commit(command);
this->hasUnsavedChanges = true;// TODO: sometimes this is called for unknown reasons this->hasUnsavedChanges = true;// TODO: sometimes this is called for unknown reasons
onRegionMapLayoutSelectedTileChanged(this->currIndex);// re-draw layout image
} }
void RegionMapEditor::on_comboBox_RM_Entry_MapSection_textActivated(const QString &text) { void RegionMapEditor::on_comboBox_RM_Entry_MapSection_textActivated(const QString &text) {
@ -988,10 +1009,21 @@ void RegionMapEditor::on_lineEdit_RM_MapName_textEdited(const QString &text) {
} }
void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() { void RegionMapEditor::on_pushButton_RM_Options_delete_clicked() {
this->region_map->resetSquare(this->region_map_layout_item->selectedTile); // TODO: crashing
updateRegionMapLayoutOptions(this->region_map_layout_item->selectedTile); int index = this->region_map->tilemapToLayoutIndex(this->currIndex);
QList<LayoutSquare> oldLayout = this->region_map->getLayout(this->region_map->getLayer());
//this->region_map->resetSquare(this->region_map_layout_item->selectedTile);
this->region_map->resetSquare(index);
QList<LayoutSquare> newLayout = this->region_map->getLayout(this->region_map->getLayer());
EditLayout *commit = new EditLayout(this->region_map, this->region_map->getLayer(), this->currIndex, oldLayout, newLayout);
commit->setText("Reset Layout Square");
this->region_map->editHistory.push(commit);
//updateRegionMapLayoutOptions(this->region_map_layout_item->selectedTile);
updateRegionMapLayoutOptions(this->currIndex);
this->region_map_layout_item->draw(); this->region_map_layout_item->draw();
this->region_map_layout_item->select(this->region_map_layout_item->selectedTile); //this->region_map_layout_item->select(this->region_map_layout_item->selectedTile);
this->region_map_layout_item->select(this->currIndex);
// ^ this line necessary?
this->hasUnsavedChanges = true; this->hasUnsavedChanges = true;
} }
@ -1082,63 +1114,6 @@ void RegionMapEditor::on_action_RegionMap_Resize_triggered() {
return; return;
} }
void RegionMapEditor::on_action_RegionMap_Undo_triggered() {
//< TODO: edit history
undo();
this->hasUnsavedChanges = true;
}
void RegionMapEditor::undo() {
//< TODO: edit history
RegionMapHistoryItem *commit = history.back();
if (!commit) return;
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();
this->region_map_layout_item->draw();
this->region_map_entries_item->draw();
break;
case RegionMapEditorBox::CityMapImage:
//< if (commit->cityMap == this->city_map_item->file)
//< this->city_map_item->setTiles(commit->tiles);
//< this->city_map_item->draw();
break;
}
}
void RegionMapEditor::on_action_RegionMap_Redo_triggered() {
//< TODO: edit history
redo();
this->hasUnsavedChanges = true;
}
void RegionMapEditor::redo() {
//< TODO: edit history
RegionMapHistoryItem *commit = history.next();
if (!commit) return;
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();
this->region_map_layout_item->draw();
this->region_map_entries_item->draw();
break;
case RegionMapEditorBox::CityMapImage:
//< this->city_map_item->setTiles(commit->tiles);
//< this->city_map_item->draw();
break;
}
}
void RegionMapEditor::resize(int w, int h) { void RegionMapEditor::resize(int w, int h) {
this->region_map->resize(w, h); this->region_map->resize(w, h);
this->currIndex = this->region_map->padLeft() * w + this->region_map->padTop(); this->currIndex = this->region_map->padLeft() * w + this->region_map->padTop();
@ -1189,11 +1164,13 @@ void RegionMapEditor::on_action_Swap_triggered() {
} }
void RegionMapEditor::on_action_RegionMap_ClearImage_triggered() { void RegionMapEditor::on_action_RegionMap_ClearImage_triggered() {
QByteArray oldTilemap = this->region_map->getTilemap();
this->region_map->clearImage(); this->region_map->clearImage();
RegionMapHistoryItem *commit = new RegionMapHistoryItem( QByteArray newTilemap = this->region_map->getTilemap();
RegionMapEditorBox::BackgroundImage, this->region_map->getTiles(), this->region_map->tilemapWidth(), this->region_map->tilemapHeight()
); EditTilemap *commit = new EditTilemap(this->region_map, oldTilemap, newTilemap, -1);
history.push(commit); commit->setText("Clear Tilemap");
this->region_map->editHistory.push(commit);
displayRegionMapImage(); displayRegionMapImage();
displayRegionMapLayout(); displayRegionMapLayout();
@ -1209,7 +1186,12 @@ void RegionMapEditor::on_action_RegionMap_ClearLayout_triggered() {
); );
if (result == QMessageBox::Yes) { if (result == QMessageBox::Yes) {
QList<LayoutSquare> oldLayout = this->region_map->getLayout(this->region_map->getLayer());
this->region_map->clearLayout(); this->region_map->clearLayout();
QList<LayoutSquare> newLayout = this->region_map->getLayout(this->region_map->getLayer());
EditLayout *commit = new EditLayout(this->region_map, this->region_map->getLayer(), -1, oldLayout, newLayout);
commit->setText("Clear Layout");
this->region_map->editHistory.push(commit);
displayRegionMapLayout(); displayRegionMapLayout();
} else { } else {
return; return;

View file

@ -1,4 +1,7 @@
#include "regionmappixmapitem.h" #include "regionmappixmapitem.h"
#include "regionmapeditcommands.h"
static unsigned actionId_ = 0;
void RegionMapPixmapItem::draw() { void RegionMapPixmapItem::draw() {
if (!region_map) return; if (!region_map) return;
@ -20,17 +23,25 @@ void RegionMapPixmapItem::draw() {
void RegionMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) { void RegionMapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
if (region_map) { if (region_map) {
QPointF pos = event->pos(); if (event->type() == QEvent::GraphicsSceneMouseRelease) {
int x = static_cast<int>(pos.x()) / 8; actionId_++;
int y = static_cast<int>(pos.y()) / 8; } else {
int index = x + y * region_map->tilemapWidth(); QPointF pos = event->pos();
this->region_map->setTileData(index, int x = static_cast<int>(pos.x()) / 8;
this->tile_selector->selectedTile, int y = static_cast<int>(pos.y()) / 8;
this->tile_selector->tile_hFlip, int index = x + y * region_map->tilemapWidth();
this->tile_selector->tile_vFlip, QByteArray oldTilemap = this->region_map->getTilemap();
this->tile_selector->tile_palette this->region_map->setTileData(index,
); this->tile_selector->selectedTile,
draw(); this->tile_selector->tile_hFlip,
this->tile_selector->tile_vFlip,
this->tile_selector->tile_palette
);
QByteArray newTilemap = this->region_map->getTilemap();
EditTilemap *command = new EditTilemap(this->region_map, oldTilemap, newTilemap, actionId_);
this->region_map->commit(command);
draw();
}
} }
} }

View file

@ -20,6 +20,7 @@ void TilemapTileSelector::select(unsigned tileId) {
QPoint coords = this->getTileIdCoords(tileId); QPoint coords = this->getTileIdCoords(tileId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->selectedTile = tileId; this->selectedTile = tileId;
this->drawSelection();
emit selectedTileChanged(tileId); emit selectedTileChanged(tileId);
} }
@ -50,11 +51,7 @@ QImage TilemapTileSelector::tileImg(shared_ptr<TilemapTile> tile) {
switch(this->format) { switch(this->format) {
case TilemapFormat::Plain: case TilemapFormat::Plain:
{
// TODO: even allow palettes for Plain tiles?
// 1 x palette x any colors
break; break;
}
case TilemapFormat::BPP_4: case TilemapFormat::BPP_4:
{ {
// before Qt 6, the color table is a QVector which is deprecated now, and this method does not exits // before Qt 6, the color table is a QVector which is deprecated now, and this method does not exits
@ -67,10 +64,11 @@ QImage TilemapTileSelector::tileImg(shared_ptr<TilemapTile> tile) {
} }
case TilemapFormat::BPP_8: case TilemapFormat::BPP_8:
{ {
// TODO:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
tilesetImage.setColorTable(this->palette.toVector()); //tilesetImage.setColorTable(this->palette.toVector());
#else #else
tilesetImage.setColorTable(this->palette); //tilesetImage.setColorTable(this->palette);
#endif #endif
break; break;
} }