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) {