add dialog to add tilesets

This commit is contained in:
Karathan 2019-03-22 00:50:50 +01:00 committed by huderlem
parent d456847f43
commit 1b62c15ba4
13 changed files with 516 additions and 49 deletions

View file

@ -561,8 +561,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>545</width>
<height>587</height>
<width>522</width>
<height>601</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_8">
@ -876,8 +876,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>256</width>
<height>74</height>
<width>259</width>
<height>70</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
@ -1064,10 +1064,10 @@
</property>
<property name="geometry">
<rect>
<x>8</x>
<x>0</x>
<y>0</y>
<width>221</width>
<height>328</height>
<width>263</width>
<height>338</height>
</rect>
</property>
<property name="sizePolicy">
@ -1344,8 +1344,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>371</width>
<height>643</height>
<width>385</width>
<height>652</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_7">
@ -1617,8 +1617,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>430</width>
<height>534</height>
<width>432</width>
<height>554</height>
</rect>
</property>
<property name="sizePolicy">
@ -2531,8 +2531,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>818</width>
<height>539</height>
<width>829</width>
<height>543</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_14">
@ -2748,6 +2748,7 @@
<addaction name="actionMap_Shift"/>
<addaction name="separator"/>
<addaction name="action_NewMap"/>
<addaction name="actionNew_Tileset"/>
<addaction name="actionTileset_Editor"/>
</widget>
<widget class="QMenu" name="menuHelp">
@ -2976,6 +2977,11 @@
<string>Cursor Tile Outline</string>
</property>
</action>
<action name="actionNew_Tileset">
<property name="text">
<string>New Tileset</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>

View file

@ -7,25 +7,22 @@
<x>0</x>
<y>0</y>
<width>410</width>
<height>508</height>
<height>515</height>
</rect>
</property>
<property name="windowTitle">
<string>New Map Options</string>
</property>
<widget class="QWidget" name="centralwidget">
<property name="enabled">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QFrame" name="frame_NewMap_Options">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
@ -39,13 +36,6 @@
<property name="verticalSpacing">
<number>12</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_newMap_Name">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_NewMap_Name">
<property name="toolTip">
@ -196,20 +186,6 @@
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_NewMap_Allow_Biking">
<property name="text">
<string>Allow Biking</string>
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QLabel" name="label_NewMap_Allow_Escape_Rope">
<property name="text">
<string>Allow Escape Rope</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QCheckBox" name="checkBox_NewMap_Allow_Running">
<property name="text">
@ -217,6 +193,13 @@
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_NewMap_Allow_Biking">
<property name="text">
<string>Allow Biking</string>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QCheckBox" name="checkBox_NewMap_Allow_Biking">
<property name="text">
@ -224,6 +207,13 @@
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QLabel" name="label_NewMap_Allow_Escape_Rope">
<property name="text">
<string>Allow Escape Rope</string>
</property>
</widget>
</item>
<item row="11" column="1">
<widget class="QCheckBox" name="checkBox_NewMap_Allow_Escape_Rope">
<property name="text">
@ -231,6 +221,13 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_newMap_Name">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -249,7 +246,7 @@
<x>0</x>
<y>0</y>
<width>410</width>
<height>21</height>
<height>22</height>
</rect>
</property>
</widget>

222
forms/newtilesetdialog.ui Normal file
View file

@ -0,0 +1,222 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>NewTilesetDialog</class>
<widget class="QDialog" name="NewTilesetDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>190</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Add new Tileset</string>
</property>
<widget class="QWidget" name="gridWidget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>190</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>380</width>
<height>135</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>380</width>
<height>129</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="horizontalSpacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameLineEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="typeLabel">
<property name="text">
<string>Type</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="typeComboBox">
<item>
<property name="text">
<string>Primary</string>
</property>
</item>
<item>
<property name="text">
<string>Secondary</string>
</property>
</item>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="pathLabel">
<property name="text">
<string>Path</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="pathLineEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="symbolNameLabel">
<property name="text">
<string>Symbol Name</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="symbolNameLineEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="4" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>NewTilesetDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>NewTilesetDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View file

@ -35,6 +35,10 @@ public:
static Metatile* getMetatile(int, Tileset*, Tileset*);
static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*);
static QList<QRgb> getPalette(int, Tileset*, Tileset*);
bool appendToHeaders(QString headerFile, QString friendlyName);
bool appendToGraphics(QString graphicsFile, QString friendlyName, bool primary);
bool appendToMetatiles(QString metatileFile, QString friendlyName, bool primary);
};
#endif // TILESET_H

View file

@ -17,6 +17,7 @@
#include "tileseteditor.h"
#include "filterchildrenproxymodel.h"
#include "newmappopup.h"
#include "newtilesetdialog.h"
namespace Ui {
class MainWindow;
@ -52,6 +53,7 @@ private slots:
void onNewMapCreated();
void on_action_NewMap_triggered();
void on_actionNew_Tileset_triggered();
void on_action_Save_triggered();
void on_tabWidget_2_currentChanged(int index);
void on_action_Exit_triggered();

View file

@ -90,6 +90,10 @@ public:
void saveMapConstantsHeader();
void saveHealLocationStruct(Map*);
void saveTilesets(Tileset*, Tileset*);
void saveTilesetMetatileAttributes(Tileset*);
void saveTilesetMetatiles(Tileset*);
void saveTilesetTilesImage(Tileset*);
void saveTilesetPalettes(Tileset*, bool);
QList<QStringList>* parseAsm(QString text);
QStringList getSongNames();
@ -129,10 +133,6 @@ public:
static int getNumPalettesPrimary();
static int getNumPalettesTotal();
private:
void saveTilesetMetatileAttributes(Tileset*);
void saveTilesetMetatiles(Tileset*);
void saveTilesetTilesImage(Tileset*);
void saveTilesetPalettes(Tileset*, bool);
void updateMapLayout(Map*);
void readCDefinesSorted(QString, QStringList, QStringList*);
void readCDefinesSorted(QString, QStringList, QStringList*, QString, int);

View file

@ -0,0 +1,32 @@
#ifndef NEWTILESETDIALOG_H
#define NEWTILESETDIALOG_H
#include <QDialog>
#include "project.h"
namespace Ui {
class NewTilesetDialog;
}
class NewTilesetDialog : public QDialog
{
Q_OBJECT
public:
explicit NewTilesetDialog(Project *project, QWidget *parent = nullptr);
~NewTilesetDialog();
QString path;
QString fullSymbolName;
QString friendlyName;
bool isSecondary;
private slots:
void NameOrSecondaryChanged();
void SecondaryChanged();
private:
Ui::NewTilesetDialog *ui;
Project *project = nullptr;
};
#endif // NEWTILESETDIALOG_H

View file

@ -60,7 +60,8 @@ SOURCES += src/core/block.cpp \
src/mainwindow.cpp \
src/project.cpp \
src/settings.cpp \
src/log.cpp
src/log.cpp \
src/ui/newtilesetdialog.cpp
HEADERS += include/core/block.h \
include/core/blockdata.h \
@ -109,14 +110,16 @@ HEADERS += include/core/block.h \
include/mainwindow.h \
include/project.h \
include/settings.h \
include/log.h
include/log.h \
include/ui/newtilesetdialog.h
FORMS += forms/mainwindow.ui \
forms/eventpropertiesframe.ui \
forms/tileseteditor.ui \
forms/paletteeditor.ui \
forms/newmappopup.ui \
forms/aboutporymap.ui
forms/aboutporymap.ui \
forms/newtilesetdialog.ui
RESOURCES += \
resources/images.qrc

View file

@ -30,5 +30,6 @@
<file>icons/sort_number.ico</file>
<file>icons/collapse_all.ico</file>
<file>icons/expand_all.ico</file>
<file>images/blank_tileset.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

View file

@ -1,6 +1,7 @@
#include "tileset.h"
#include "metatile.h"
#include "project.h"
#include "log.h"
#include <QPainter>
#include <QImage>
@ -86,3 +87,70 @@ QList<QRgb> Tileset::getPalette(int paletteId, Tileset *primaryTileset, Tileset
}
return paletteTable;
}
bool Tileset::appendToHeaders(QString headerFile, QString friendlyName){
QFile file(headerFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
logError(QString("Could not write to file \"%1\"").arg(headerFile));
return false;
}
QString dataString = "\r\n\t.align 2\r\n";
dataString.append(QString("%1::\r\n").arg(this->name));
dataString.append(QString("\t.byte %1 @ is compressed\r\n").arg(this->is_compressed));
dataString.append(QString("\t.byte %1 @ is secondary\r\n").arg(this->is_secondary));
dataString.append(QString("\t.byte %1\r\n").arg(this->padding));
dataString.append(QString("\t.4byte gTilesetTiles_%1\r\n").arg(friendlyName));
dataString.append(QString("\t.4byte gTilesetPalettes_%1\r\n").arg(friendlyName));
dataString.append(QString("\t.4byte gMetatiles_%1\r\n").arg(friendlyName));
dataString.append(QString("\t.4byte gMetatileAttributes_%1\r\n").arg(friendlyName));
dataString.append("\t.4byte NULL\r\n");
file.write(dataString.toUtf8());
file.flush();
file.close();
return true;
}
bool Tileset::appendToGraphics(QString graphicsFile, QString friendlyName, bool primary) {
int startPaletteId = primary ? 0 : Project::getNumPalettesPrimary();
int endPaletteId = primary ? Project::getNumPalettesPrimary() : Project::getNumPalettesTotal();
QString primaryString = primary ? "primary" : "secondary";
QFile file(graphicsFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
logError(QString("Could not write to file \"%1\"").arg(graphicsFile));
return false;
}
QString dataString = "\r\n\t.align 2\r\n";
dataString.append(QString("gTilesetPalettes_%1::\r\n").arg(friendlyName));
for(int i = startPaletteId; i < endPaletteId; ++i) {
QString paletteString;
paletteString.sprintf("%02d.gbapal", i);
dataString.append(QString("\t.incbin \"data/tilesets/%1/%2/palettes/%3\"\r\n").arg(primaryString, friendlyName.toLower(), paletteString));
}
dataString.append("\r\n\t.align 2\r\n");
dataString.append(QString("gTilesetTiles_%1::\r\n").arg(friendlyName));
dataString.append(QString("\t.incbin \"data/tilesets/%1/%2/tiles.4bpp.lz\"\r\n").arg(primaryString, friendlyName.toLower()));
file.write(dataString.toUtf8());
file.flush();
file.close();
return true;
}
bool Tileset::appendToMetatiles(QString metatileFile, QString friendlyName, bool primary) {
QString primaryString = primary ? "primary" : "secondary";
QFile file(metatileFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
logError(QString("Could not write to file \"%1\"").arg(metatileFile));
return false;
}
QString dataString = "\r\n\t.align 1\r\n";
dataString.append(QString("gMetatiles_%1::\r\n").arg(friendlyName));
dataString.append(QString("\t.incbin \"data/tilesets/%1/%2/metatiles.bin\"\r\n").arg(primaryString, friendlyName.toLower()));
dataString.append(QString("\r\n\t.align 1\r\n"));
dataString.append(QString("gMetatileAttributes_%1::\r\n").arg(friendlyName));
dataString.append(QString("\t.incbin \"data/tilesets/%1/%2/metatile_attributes.bin\"").arg(primaryString, friendlyName.toLower()));
file.write(dataString.toUtf8());
file.flush();
file.close();
return true;
}

View file

@ -10,6 +10,7 @@
#include "currentselectedmetatilespixmapitem.h"
#include "customattributestable.h"
#include <QFileDialog>
#include <QStandardItemModel>
#include <QShortcut>
@ -852,6 +853,97 @@ void MainWindow::on_action_NewMap_triggered() {
openNewMapPopupWindow(MapSortOrder::Group, 0);
}
void MainWindow::on_actionNew_Tileset_triggered() {
NewTilesetDialog *createTilesetDialog = new NewTilesetDialog(editor->project, this);
if(createTilesetDialog->exec() == QDialog::Accepted){
if(createTilesetDialog->friendlyName.isEmpty()) {
logError(QString("Tried to create a directory with an empty name."));
QMessageBox msgBox(this);
msgBox.setText("Failed to add new tileset.");
QString message = QString("The given name was empty.");
msgBox.setInformativeText(message);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Icon::Critical);
msgBox.exec();
return;
}
QString fullDirectoryPath = editor->project->root + createTilesetDialog->path;
QDir directory;
if(directory.exists(fullDirectoryPath)) {
logError(QString("Could not create tileset, could not create directory \"%1\", it already exists.").arg(fullDirectoryPath));
QMessageBox msgBox(this);
msgBox.setText("Failed to add new tileset.");
QString message = QString("The tileset already exists, view porymap.log for a complete description of the error.");
msgBox.setInformativeText(message);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Icon::Critical);
msgBox.exec();
return;
}
directory.mkdir(fullDirectoryPath);
directory.mkdir(fullDirectoryPath + "/palettes");
Tileset *newSet = new Tileset();
newSet->name = createTilesetDialog->fullSymbolName;
newSet->tilesImagePath = fullDirectoryPath + "/tiles.png";
newSet->metatiles_path = fullDirectoryPath + "/metatiles.bin";
newSet->metatile_attrs_path = fullDirectoryPath + "/metatile_attributes.bin";
newSet->is_secondary = createTilesetDialog->isSecondary ? "TRUE" : "FALSE";
int numMetaTiles = createTilesetDialog->isSecondary ? (Project::getNumTilesTotal() - Project::getNumTilesPrimary()) : Project::getNumTilesPrimary();
QImage *tilesImage = new QImage(":/images/blank_tileset.png");
editor->project->loadTilesetTiles(newSet, *tilesImage);
newSet->metatiles = new QList<Metatile*>();
for(int i = 0; i < numMetaTiles; ++i) {
Metatile *mt = new Metatile();
for(int j = 0; j < 8; ++j){
Tile *tile = new Tile();
//Create a checkerboard-style dummy tileset
if(((i / 8) % 2) == 0)
tile->tile = ((i % 2) == 0) ? 1 : 2;
else
tile->tile = ((i % 2) == 1) ? 1 : 2;
tile->xflip = false;
tile->yflip = false;
tile->palette = 0;
mt->tiles->append(*tile);
}
mt->behavior = 0;
mt->layerType = 0;
newSet->metatiles->append(mt);
}
newSet->palettes = new QList<QList<QRgb>>();
newSet->palettePaths = *new QList<QString>();
for(int i = 0; i < 16; ++i) {
QList<QRgb> *currentPal = new QList<QRgb>();
for(int i = 0; i < 16;++i) {
currentPal->append(qRgb(0,0,0));
}
newSet->palettes->append(*currentPal);
QString fileName;
fileName.sprintf("%02d.pal", i);
newSet->palettePaths.append(fullDirectoryPath+"/palettes/" + fileName);
}
(*newSet->palettes)[0][1] = qRgb(255,0,255);
newSet->is_compressed = "TRUE";
newSet->padding = "0";
editor->project->saveTilesetTilesImage(newSet);
editor->project->saveTilesetMetatiles(newSet);
editor->project->saveTilesetMetatileAttributes(newSet);
editor->project->saveTilesetPalettes(newSet, !createTilesetDialog->isSecondary);
//append to tileset specific files
newSet->appendToHeaders(editor->project->root + "/data/tilesets/headers.inc", createTilesetDialog->friendlyName);
newSet->appendToGraphics(editor->project->root + "/data/tilesets/graphics.inc", createTilesetDialog->friendlyName, !createTilesetDialog->isSecondary);
newSet->appendToMetatiles(editor->project->root + "/data/tilesets/metatiles.inc", createTilesetDialog->friendlyName, !createTilesetDialog->isSecondary);
if(!createTilesetDialog->isSecondary) {
this->ui->comboBox_PrimaryTileset->addItem(createTilesetDialog->fullSymbolName);
} else {
this->ui->comboBox_SecondaryTileset->addItem(createTilesetDialog->fullSymbolName);
}
}
}
void MainWindow::onTilesetChanged(QString mapName)
{
setMap(mapName);

View file

@ -0,0 +1,40 @@
#include "newtilesetdialog.h"
#include "ui_newtilesetdialog.h"
#include <QFileDialog>
#include "project.h"
NewTilesetDialog::NewTilesetDialog(Project* project, QWidget *parent) :
QDialog(parent),
ui(new Ui::NewTilesetDialog)
{
ui->setupUi(this);
this->setFixedSize(this->width(), this->height());
this->project = project;
//only allow characters valid for a symbol
QRegExp expression("[-_.A-Za-z0-9]+$");
QRegExpValidator *validator = new QRegExpValidator(expression);
this->ui->nameLineEdit->setValidator(validator);
connect(this->ui->nameLineEdit, &QLineEdit::textChanged, this, &NewTilesetDialog::NameOrSecondaryChanged);
connect(this->ui->typeComboBox, &QComboBox::currentTextChanged, this, &NewTilesetDialog::SecondaryChanged);
//connect(this->ui->toolButton, &QToolButton::clicked, this, &NewTilesetDialog::ChangeFilePath);
this->SecondaryChanged();
}
NewTilesetDialog::~NewTilesetDialog()
{
delete ui;
}
void NewTilesetDialog::SecondaryChanged(){
this->isSecondary = (this->ui->typeComboBox->currentIndex() == 1);
NameOrSecondaryChanged();
}
void NewTilesetDialog::NameOrSecondaryChanged() {
this->friendlyName = this->ui->nameLineEdit->text();
this->fullSymbolName = "gTileset_" + this->friendlyName;
this->ui->symbolNameLineEdit->setText(this->fullSymbolName);
this->path = QString("/data/tilesets/") + (this->isSecondary ? "secondary/" : "primary/") + this->friendlyName.toLower();
this->ui->pathLineEdit->setText(this->path);
}