Skip to content

Commit

Permalink
Merge pull request #231 from wario-land/ssp
Browse files Browse the repository at this point in the history
More script APIs, support Export & Import palette file in Tileset Editor, and several bugfixes.
  • Loading branch information
shinespeciall authored Jul 7, 2020
2 parents 601c915 + 177be92 commit 51abb8b
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 65 deletions.
2 changes: 2 additions & 0 deletions Dialog/RoomConfigDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ private slots:
LevelComponents::Tileset *currentTileset = nullptr;

// Enumeration of the available tilesets
// This part is modified from xTibor/steaks
// Although he uses MIT license in that repository, but we think we had better point this out
// clang-format off
static constexpr const char *TilesetNamesSetData[0x5C] =
{
Expand Down
152 changes: 151 additions & 1 deletion Dialog/TilesetEditDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ void TilesetEditDialog::on_horizontalSlider_valueChanged(int value)
ResetPaletteBarGraphicView(value);
SetSelectedTile8x8(0, true);
SetSelectedColorId(0);
ReRenderTile16Map();
}

void TilesetEditDialog::TLTile8x8Reset()
Expand Down Expand Up @@ -982,10 +983,10 @@ void TilesetEditDialog::on_pushButton_ExportTile16sCombinationData_clicked()
if (file.isOpen())
{
file.write(reinterpret_cast<const char*>(map16tilePtr), filesize);
file.close();
} else {
QMessageBox::critical(this, QString("Error"), QString("Cannot save file!"));
}
file.close();
}
}

Expand Down Expand Up @@ -1039,3 +1040,152 @@ void TilesetEditDialog::on_pushButton_ImportTile16sCombinationData_clicked()
// Update UI and select the first Tile16
SetSelectedTile16(0, true);
}

/// <summary>
/// Export current palette data to a file.
/// </summary>
void TilesetEditDialog::on_pushButton_ExportPalette_clicked()
{
QString romFileDir = QFileInfo(ROMUtils::ROMFilePath).dir().path();
QString selectedfilter;
QString qFilePath =
QFileDialog::getSaveFileName(this,
tr("Save palette file"),
romFileDir,
tr("usenti pal file (*.pal);;YY-CHR pal file (*.pal)"),
&selectedfilter);
if(qFilePath.isEmpty()) return;
QVector<QRgb> tmppalette = tilesetEditParams->newTileset->GetPalettes()[SelectedPaletteId];
if(selectedfilter.compare("usenti pal file (*.pal)") == 0)
{
QFile palfile(qFilePath);
if(palfile.open(QIODevice::WriteOnly | QIODevice::Text))
{
// Stream text to the file
QTextStream out(&palfile);
out << QString("CLRX 8 16\n");
for(int j = 0; j < 4; ++j)
{
for(int i = 0; i < 4; ++i)
{
// RGB888(QRgb) -> BGR888(usenti pal file)
int color = ((tmppalette[i + 4 * j] & 0xFF0000) >> 16) |
(tmppalette[i + 4 * j] & 0xFF00) |
((tmppalette[i + 4 * j] & 0xFF) << 16);
out << QString("0x") + QString("%1").arg(color, 8, 16, QChar('0')) + QString(" ");
}
out << QString("\n");
}
palfile.close();
}
}
else if (selectedfilter.compare("YY-CHR pal file (*.pal)") == 0)
{
unsigned char *palettedata = new unsigned char[3 * 16];
for(int j = 0; j < 16; ++j)
{
palettedata[3 * j] = (tmppalette[j] & 0xFF0000) >> 16; // R
palettedata[3 * j + 1] = (tmppalette[j] & 0xFF00) >> 8; // G
palettedata[3 * j + 2] = tmppalette[j] & 0xFF; // B
}
QFile palfile(qFilePath);
palfile.open(QIODevice::WriteOnly);
if (palfile.isOpen())
{
palfile.write(reinterpret_cast<const char*>(palettedata), 3 * 16);
palfile.close();
} else {
QMessageBox::critical(this, QString("Error"), QString("Cannot save file!"));
}
delete[] palettedata;
}
}

/// <summary>
/// Import current palette data from a file.
/// </summary>
void TilesetEditDialog::on_pushButton_ImportPalette_clicked()
{
QString romFileDir = QFileInfo(ROMUtils::ROMFilePath).dir().path();
QString selectedfilter;
QString qFilePath = QFileDialog::getOpenFileName(
this,
tr("Open palette file"),
romFileDir,
tr("usenti pal file (*.pal);;YY-CHR pal file (*.pal)"),
&selectedfilter
);
if(qFilePath.isEmpty()) return;

// Check the file extension
if(!qFilePath.endsWith(".pal", Qt::CaseInsensitive))
{
QMessageBox::critical(this, QString("Error"), QString("Wrong file extension!"));
return;
}

// Set palette
if(selectedfilter.compare("usenti pal file (*.pal)") == 0)
{
QFile palfile(qFilePath);
if(palfile.open(QIODevice::ReadOnly | QIODevice::Text))
{
// Stream text from the file
QTextStream in(&palfile);
QString header = in.readLine();
if(header.compare("CLRX 8 16"))
{
QMessageBox::critical(this, QString("Error"), QString("Wrong file format!"));
return;
}
for(int j = 0; j < 4; ++j)
{
QString line = in.readLine();
QStringList fields = line.split(" ");
for(int i = 0; i < 4; ++i)
{
if(i == 0 && j == 0) continue; // Skip the first color
// BGR888(usenti pal file) -> RGB888(QRgb)
int fileformatcolor = fields[i].toInt(nullptr, 16);
int color = ((fileformatcolor & 0xFF0000) >> 16) |
(fileformatcolor & 0xFF00) |
((fileformatcolor & 0xFF) << 16);
QColor newcolor = QColor::fromRgb(color);
newcolor.setAlpha(0xFF);
tilesetEditParams->newTileset->SetColor(SelectedPaletteId, i + 4 * j, newcolor.rgba());
}
}
palfile.close();
}
}
else if (selectedfilter.compare("YY-CHR pal file (*.pal)") == 0)
{
QFile file(qFilePath);
file.open(QIODevice::ReadOnly);
int length;
if (!file.isOpen() || (length = (int) file.size()) < (3 * 16))
{
file.close();
QMessageBox::critical(this, QString("Error"), QString("File size too small! It should be >= 48 bytes."));
return;
}

// Read data
unsigned char *paldata = new unsigned char[length];
file.read((char *) paldata, length);
file.close();
for(int j = 1; j < 16; ++j) // Skip the first color
{
int color = (paldata[3 * j] << 16) |
(paldata[3 * j + 1] << 8) |
paldata[3 * j + 2];
QColor newcolor = QColor::fromRgb(color);
newcolor.setAlpha(0xFF);
tilesetEditParams->newTileset->SetColor(SelectedPaletteId, j, newcolor.rgba());
}
}
ResetPaletteBarGraphicView(SelectedPaletteId);
SetSelectedColorId(0);
ReRenderTile8x8Map(SelectedPaletteId);
ReRenderTile16Map();
}
3 changes: 3 additions & 0 deletions Dialog/TilesetEditDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QCheckBox>
#include <QScrollBar>
#include <QColorDialog>
#include <QTextStream>
#include "LevelComponents/Layer.h"
#include "LevelComponents/Room.h"
#include "LevelComponents/Tileset.h"
Expand Down Expand Up @@ -90,6 +91,8 @@ private slots:
void on_pushButton_ImportTile8x8Graphic_clicked();
void on_pushButton_ExportTile16sCombinationData_clicked();
void on_pushButton_ImportTile16sCombinationData_clicked();
void on_pushButton_ExportPalette_clicked();
void on_pushButton_ImportPalette_clicked();

private:
Ui::TilesetEditDialog *ui;
Expand Down
21 changes: 21 additions & 0 deletions Dialog/TilesetEditDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_ExportPalette">
<property name="text">
<string>Export Palette</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_ImportPalette">
<property name="text">
<string>Import Palette</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_RGB888Value">
<property name="text">
Expand Down
8 changes: 4 additions & 4 deletions DockWidget/EditModeDockWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<set>Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea</set>
</property>
<property name="windowTitle">
<string>Visibility and Editing Controls</string>
<string>View and Edit</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -100,7 +100,7 @@
</size>
</property>
<property name="title">
<string>Visible Layers</string>
<string>View Layers</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
Expand Down Expand Up @@ -228,7 +228,7 @@
</size>
</property>
<property name="title">
<string>Extra</string>
<string>Extra View</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
Expand Down Expand Up @@ -268,7 +268,7 @@
</size>
</property>
<property name="title">
<string>Selected Layer</string>
<string>Edit Layer</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
Expand Down
6 changes: 4 additions & 2 deletions EditorWindow/MainGraphicsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <QMouseEvent>
#include <QScrollBar>

#include <cassert>
#include <iostream>

extern WL4EditorWindow *singleton;
Expand Down Expand Up @@ -180,7 +179,10 @@ void MainGraphicsView::mousePressEvent(QMouseEvent *event)
// Add the new entity
bool success =
room->AddEntity(tileX, tileY, singleton->GetEntitySetDockWidgetPtr()->GetCurrentEntityLocalId());
assert(success /* Failure to add entity */); // TODO: Show information if failure
if(!success)
{
singleton->GetOutputWidgetPtr()->PrintString("Cannot add more entity under the current difficulty in this room");
}
int difficulty = singleton->GetEditModeWidgetPtr()->GetEditModeParams().seleteddifficulty;
room->SetEntityListDirty(difficulty, true);
singleton->SetUnsavedChanges(true);
Expand Down
Loading

0 comments on commit 51abb8b

Please sign in to comment.