add/remove map section entry in region map editor

This commit is contained in:
garak 2022-04-26 22:19:36 -04:00 committed by garakmon
parent fa01768962
commit e82ff92b2b
7 changed files with 400 additions and 0 deletions

View file

@ -1028,6 +1028,13 @@
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="pushButton_entryActivate">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">

View file

@ -56,6 +56,7 @@ public:
void setEntries(tsl::ordered_map<QString, MapSectionEntry> *entries) { this->region_map_entries = entries; }
MapSectionEntry getEntry(QString section);
void setEntry(QString section, MapSectionEntry entry);
void removeEntry(QString section);
void save();
void saveTilemap();

View file

@ -0,0 +1,129 @@
#pragma once
#ifndef REGIONMAPEDITCOMMANDS_H
#define REGIONMAPEDITCOMMANDS_H
#include "regionmap.h"
#include <QUndoCommand>
#include <QList>
class RegionMap;
enum RMCommandId {
ID_EditTilemap = 0,
ID_EditLayout,
ID_ResizeLayout,
ID_EditEntry,
};
/// Implements a command to commit tilemap paint actions
class EditTilemap : public QUndoCommand {
public:
EditTilemap(RegionMap *map, QByteArray oldTilemap, QByteArray newTilemap, unsigned actionId, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
bool mergeWith(const QUndoCommand *command) override;
int id() const override { return RMCommandId::ID_EditTilemap; }
private:
RegionMap *map;
QByteArray oldTilemap;
QByteArray newTilemap;
unsigned actionId;
};
/// Edit region map section layout
class EditLayout : public QUndoCommand {
public:
EditLayout(RegionMap *map, QString layer, int index, QList<LayoutSquare> oldLayout, QList<LayoutSquare> newLayout, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
bool mergeWith(const QUndoCommand *command) override;
int id() const override { return RMCommandId::ID_EditLayout; }
private:
RegionMap *map;
int index;
QString layer;
QList<LayoutSquare> oldLayout;
QList<LayoutSquare> newLayout;
};
/// Edit Layout Dimensions
class ResizeLayout : public QUndoCommand {
public:
ResizeLayout(RegionMap *map, int oldWidth, int oldHeight, int newWidth, int newHeight,
QMap<QString, QList<LayoutSquare>> oldLayouts, QMap<QString, QList<LayoutSquare>> newLayouts, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
bool mergeWith(const QUndoCommand *command) override;
int id() const override { return RMCommandId::ID_ResizeLayout; }
private:
RegionMap *map;
int oldWidth;
int oldHeight;
int newWidth;
int newHeight;
QMap<QString, QList<LayoutSquare>> oldLayouts;
QMap<QString, QList<LayoutSquare>> newLayouts;
};
/// Edit Entry Value
class EditEntry : public QUndoCommand {
public:
EditEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
bool mergeWith(const QUndoCommand *command) override;
int id() const override { return RMCommandId::ID_EditEntry; }
protected:
RegionMap *map;
QString section;
MapSectionEntry oldEntry;
MapSectionEntry newEntry;
};
/// Remove Entry
class RemoveEntry : public EditEntry {
public:
RemoveEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
};
/// Add Entry
class AddEntry : public EditEntry {
public:
AddEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
};
// ResizeTilemap
// ResizeMap
#endif // REGIONMAPEDITCOMMANDS_H

View file

@ -136,6 +136,7 @@ private slots:
void on_spinBox_RM_Entry_y_valueChanged(int);
void on_spinBox_RM_Entry_width_valueChanged(int);
void on_spinBox_RM_Entry_height_valueChanged(int);
void on_pushButton_entryActivate_clicked();
void on_spinBox_RM_LayoutWidth_valueChanged(int);
void on_spinBox_RM_LayoutHeight_valueChanged(int);
void on_spinBox_tilePalette_valueChanged(int);

View file

@ -587,6 +587,10 @@ void RegionMap::setEntry(QString section, MapSectionEntry entry) {
this->region_map_entries->operator[](section) = entry;
}
void RegionMap::removeEntry(QString section) {
this->region_map_entries->erase(section);
}
QString RegionMap::palPath() {
return this->palette_path.isEmpty() ? QString() : this->project->root + "/" + this->palette_path;
}

View file

@ -0,0 +1,228 @@
#include "regionmapeditcommands.h"
EditTilemap::EditTilemap(RegionMap *map, QByteArray oldTilemap, QByteArray newTilemap, unsigned actionId, QUndoCommand *parent)
: QUndoCommand(parent) {
setText("Edit Tilemap");
this->map = map;
this->oldTilemap = oldTilemap;
this->newTilemap = newTilemap;
this->actionId = actionId;
}
void EditTilemap::redo() {
QUndoCommand::redo();
if (!map) return;
map->setTilemap(this->newTilemap);
}
void EditTilemap::undo() {
if (!map) return;
map->setTilemap(this->oldTilemap);
QUndoCommand::undo();
}
bool EditTilemap::mergeWith(const QUndoCommand *command) {
const EditTilemap *other = static_cast<const EditTilemap *>(command);
if (this->map != other->map)
return false;
if (this->actionId != other->actionId)
return false;
newTilemap = other->newTilemap;
return true;
}
///
EditLayout::EditLayout(RegionMap *map, QString layer, int index, QList<LayoutSquare> oldLayout, QList<LayoutSquare> newLayout, QUndoCommand *parent)
: QUndoCommand(parent) {
setText("Edit Layout");
this->map = map;
this->layer = layer;
this->index = index;
this->oldLayout = oldLayout;
this->newLayout = newLayout;
}
void EditLayout::redo() {
QUndoCommand::redo();
if (!map) return;
map->setLayout(this->layer, this->newLayout);
}
void EditLayout::undo() {
if (!map) return;
map->setLayout(this->layer, this->oldLayout);
QUndoCommand::undo();
}
bool EditLayout::mergeWith(const QUndoCommand *command) {
const EditLayout *other = static_cast<const EditLayout *>(command);
if (this->map != other->map)
return false;
if (this->text() != other->text())
return false;
if (this->index != other->index)
return false;
this->newLayout = other->newLayout;
return true;
}
///
ResizeLayout::ResizeLayout(RegionMap *map, int oldWidth, int oldHeight, int newWidth, int newHeight,
QMap<QString, QList<LayoutSquare>> oldLayouts, QMap<QString, QList<LayoutSquare>> newLayouts, QUndoCommand *parent)
: QUndoCommand(parent) {
setText("Change Layout Dimensions");
this->map = map;
this->oldWidth = oldWidth;
this->oldHeight = oldHeight;
this->newWidth = newWidth;
this->newHeight = newHeight;
this->oldLayouts = oldLayouts;
this->newLayouts = newLayouts;
}
void ResizeLayout::redo() {
QUndoCommand::redo();
if (!map) return;
map->setLayoutDimensions(newWidth, newHeight, false);
map->setAllLayouts(this->newLayouts);
}
void ResizeLayout::undo() {
if (!map) return;
map->setLayoutDimensions(oldWidth, oldHeight, false);
map->setAllLayouts(this->oldLayouts);
QUndoCommand::undo();
}
bool ResizeLayout::mergeWith(const QUndoCommand *command) {
const ResizeLayout *other = static_cast<const ResizeLayout *>(command);
if (this->map != other->map)
return false;
// always allow consecutive dimension changes to merge
this->newWidth = other->newWidth;
this->newHeight = other->newHeight;
this->newLayouts = other->newLayouts;
return true;
}
///
EditEntry::EditEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent)
: QUndoCommand(parent) {
setText("Change Entry for " + section);
this->map = map;
this->section = section;
this->oldEntry = oldEntry;
this->newEntry = newEntry;
}
void EditEntry::redo() {
QUndoCommand::redo();
if (!map) return;
map->setEntry(section, newEntry);
}
void EditEntry::undo() {
if (!map) return;
map->setEntry(section, oldEntry);
QUndoCommand::undo();
}
bool EditEntry::mergeWith(const QUndoCommand *command) {
const EditEntry *other = static_cast<const EditEntry *>(command);
if (this->map != other->map)
return false;
if (this->section != other->section)
return false;
this->newEntry = other->newEntry;
return true;
}
///
AddEntry::AddEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent)
: EditEntry(map, section, oldEntry, newEntry, parent) {
setText("Add Entry for " + section);
}
void AddEntry::redo() {
QUndoCommand::redo();
if (!map) return;
map->setEntry(section, newEntry);
}
void AddEntry::undo() {
if (!map) return;
map->removeEntry(section);
QUndoCommand::undo();
}
///
RemoveEntry::RemoveEntry(RegionMap *map, QString section, MapSectionEntry oldEntry, MapSectionEntry newEntry, QUndoCommand *parent)
: EditEntry(map, section, oldEntry, newEntry, parent) {
setText("Remove Entry for " + section);
}
void RemoveEntry::redo() {
QUndoCommand::redo();
if (!map) return;
map->removeEntry(section);
}
void RemoveEntry::undo() {
if (!map) return;
map->setEntry(section, oldEntry);
QUndoCommand::undo();
}

View file

@ -646,6 +646,7 @@ void RegionMapEditor::updateRegionMapEntryOptions(QString section) {
this->ui->spinBox_RM_Entry_y->setEnabled(enabled);
this->ui->spinBox_RM_Entry_width->setEnabled(enabled);
this->ui->spinBox_RM_Entry_height->setEnabled(enabled);
this->ui->pushButton_entryActivate->setText(enabled ? "Remove" : "Add");
this->ui->lineEdit_RM_MapName->blockSignals(true);
this->ui->spinBox_RM_Entry_x->blockSignals(true);
@ -672,6 +673,35 @@ void RegionMapEditor::updateRegionMapEntryOptions(QString section) {
this->ui->spinBox_RM_Entry_height->blockSignals(false);
}
void RegionMapEditor::on_pushButton_entryActivate_clicked() {
QString section = this->ui->comboBox_RM_Entry_MapSection->currentText();
if (section == "MAPSEC_NONE") return;
if (this->region_map_entries.contains(section)) {
// disable
MapSectionEntry oldEntry = this->region_map->getEntry(section);
this->region_map->removeEntry(section);
MapSectionEntry newEntry = this->region_map->getEntry(section);
RemoveEntry *commit = new RemoveEntry(this->region_map, section, oldEntry, newEntry);
this->region_map->editHistory.push(commit);
updateRegionMapEntryOptions(section);
this->ui->pushButton_entryActivate->setText("Add");
} else {
// enable
MapSectionEntry oldEntry = this->region_map->getEntry(section);
MapSectionEntry entry = MapSectionEntry();
entry.valid = true;
this->region_map->setEntry(section, entry);
MapSectionEntry newEntry = this->region_map->getEntry(section);
AddEntry *commit = new AddEntry(this->region_map, section, oldEntry, newEntry);
this->region_map->editHistory.push(commit);
updateRegionMapEntryOptions(section);
this->ui->pushButton_entryActivate->setText("Remove");
}
}
void RegionMapEditor::displayRegionMapTileSelector() {
if (!scene_region_map_tiles) {
this->scene_region_map_tiles = new QGraphicsScene;