[nostalgia] Add external palettes to tilesheet system

This commit is contained in:
2019-10-22 19:23:14 -05:00
parent fbdb48a1ee
commit 92103bfc41
31 changed files with 802 additions and 105 deletions
+2
View File
@@ -3,6 +3,7 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "Native")
add_library(
NostalgiaCore-Studio SHARED
import_tilesheet_wizard.cpp
new_tilesheet_wizard.cpp
plugin.cpp
)
endif()
@@ -12,6 +13,7 @@ target_link_libraries(
Qt5::Core
Qt5::Widgets
NostalgiaStudio
NostalgiaPack
OxFS
OxStd
)
@@ -7,13 +7,21 @@
*/
#include <QBuffer>
#include <QDebug>
#include <QFile>
#include <nostalgia/core/consts.hpp>
#include <nostalgia/tools/pack/imgconv.hpp>
#include "import_tilesheet_wizard.hpp"
namespace nostalgia::core {
ImportTilesheetWizardPage::ImportTilesheetWizardPage(const studio::Context *ctx) {
static const auto PaletteOption_Bundle = QObject::tr("Bundle");
static const auto PaletteOption_New = QObject::tr("New");
static const auto PaletteOptions = QStringList{PaletteOption_Bundle, PaletteOption_New};
ImportTilesheetWizardMainPage::ImportTilesheetWizardMainPage(const studio::Context *ctx) {
m_ctx = ctx;
addLineEdit(tr("&Tile Sheet Name:"), QString(TileSheetName) + "*", "", [this](QString) {
auto importPath = field(ImportPath).toString();
@@ -28,44 +36,52 @@ ImportTilesheetWizardPage::ImportTilesheetWizardPage(const studio::Context *ctx)
auto fileTypes = "(*.png);;(*.bmp);;(*.jpg);;(*.jpeg)";
addPathBrowse(tr("Tile Sheet &Path:"), QString(ImportPath) + "*", "",
QFileDialog::ExistingFile, fileTypes);
//addComboBox(tr("Bits Per Pixe&l:"), BPP, {tr("Auto"), "4", "8"});
addLineEdit(tr("Til&es:"), QString(TileCount), "");
}
int ImportTilesheetWizardPage::accept() {
auto tilesheetName = field(TileSheetName).toString();
auto importPath = field(ImportPath).toString();
QFile importFile(importPath);
if (importFile.exists()) {
return importImage(importFile, field(TileSheetName).toString());
} else {
return 1;
}
}
ImportTilesheetWizardPalettePage::ImportTilesheetWizardPalettePage(const studio::Context *ctx) {
m_ctx = ctx;
auto cb = addComboBox(tr("P&alette:"), Palette, PaletteOptions);
auto name = addLineEdit(tr("Palette &Name:"), PaletteName);
name->setDisabled(true);
int ImportTilesheetWizardPage::importImage(QFile &srcFile, QString tilesheetName) {
if (srcFile.exists()) {
srcFile.open(QIODevice::ReadOnly);
auto buff = srcFile.readAll();
QImage srcImg;
if (srcImg.loadFromData(buff)) {
int err = 0;
// ensure image is PNG
QByteArray out;
QBuffer outBuffer(&out);
outBuffer.open(QIODevice::WriteOnly);
srcImg.save(&outBuffer, "PNG");
// make sure tile sheet directory exists
m_ctx->project->mkdir(TileSheetDir);
// write image
err |= m_ctx->project->write(TileSheetDir + tilesheetName + ".png", reinterpret_cast<uint8_t*>(out.data()), out.size());
err |= m_ctx->project->saveRomFs();
return err;
} else {
return 1;
connect(cb, QOverload<int>::of(&QComboBox::currentIndexChanged), [name](int idx) {
if (idx == 1) {
name->setDisabled(false);
} else {
name->setDisabled(true);
}
}
} else {
return 2;
);
cb->setCurrentIndex(0);
}
int ImportTilesheetWizardPalettePage::accept() {
const auto tilesheetName = field(TileSheetName).toString();
const auto importPath = field(ImportPath).toString();
const auto tileCount = field(TileCount).toInt();
const auto palette = field(Palette).toInt();
const auto paletteName = field(PaletteName).toString();
const auto outPath = TileSheetDir + tilesheetName + FileExt_ng;
if (!QFile(importPath).exists()) {
return OxError(1);
}
auto ng = imgToNg(importPath, tileCount, 0);
if (!ng) {
return OxError(1);
}
if (palette != PaletteOptions.indexOf(PaletteOption_Bundle)) {
const auto outPath = PaletteDir + paletteName + FileExt_npal;
core::NostalgiaPalette pal;
pal.colors = std::move(ng->pal);
auto [buff, err] = toBuffer(&pal);
oxReturnError(err);
oxReturnError(m_ctx->project->write(outPath, buff.data(), buff.size()));
}
auto [buff, err] = toBuffer(ng.get());
oxReturnError(err);
oxReturnError(m_ctx->project->write(outPath, buff.data(), buff.size()));
return m_ctx->project->saveRomFs();
}
}
@@ -12,21 +12,32 @@
namespace nostalgia::core {
class ImportTilesheetWizardPage: public studio::WizardFormPage {
constexpr auto TileSheetDir = "/TileSheets/";
constexpr auto PaletteDir = "/Palettes/";
constexpr auto TileSheetName = "tilesheetName";
constexpr auto ImportPath = "importPath";
constexpr auto Palette = "palette";
constexpr auto PaletteName = "paletteName";
constexpr auto TileCount = "tileCount";
class ImportTilesheetWizardMainPage: public studio::WizardFormPage {
private:
static constexpr auto TileSheetDir = "/TileSheets/";
static constexpr auto TileSheetName = "projectName";
static constexpr auto ImportPath = "projectPath";
//static constexpr auto BPP = "bpp";
const studio::Context *m_ctx = nullptr;
public:
ImportTilesheetWizardPage(const studio::Context *args);
ImportTilesheetWizardMainPage(const studio::Context *args);
};
class ImportTilesheetWizardPalettePage: public studio::WizardFormPage {
private:
//static constexpr auto BPP = "bpp";
const studio::Context *m_ctx = nullptr;
public:
ImportTilesheetWizardPalettePage(const studio::Context *args);
int accept();
private:
int importImage(QFile &srcFile, QString dest);
};
}
@@ -0,0 +1,29 @@
/*
* Copyright 2016 - 2019 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QBuffer>
#include <QFile>
#include "new_tilesheet_wizard.hpp"
namespace nostalgia::core {
NewTilesheetWizardPage::NewTilesheetWizardPage(const studio::Context *ctx) {
m_ctx = ctx;
addLineEdit(tr("&Tile Sheet Name:"), QString(TileSheetName) + "*", "", [](QString) {
return 0;
}
);
}
int NewTilesheetWizardPage::accept() {
auto tilesheetName = field(TileSheetName).toString();
return 0;
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2016 - 2019 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <nostalgia/studio/studio.hpp>
namespace nostalgia::core {
class NewTilesheetWizardPage: public studio::WizardFormPage {
private:
static constexpr auto TileSheetDir = "/TileSheets/";
static constexpr auto TileSheetName = "projectName";
const studio::Context *m_ctx = nullptr;
public:
NewTilesheetWizardPage(const studio::Context *args);
int accept();
private:
};
}
+18 -3
View File
@@ -6,6 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "new_tilesheet_wizard.hpp"
#include "import_tilesheet_wizard.hpp"
#include "plugin.hpp"
@@ -17,13 +18,27 @@ namespace nostalgia::core {
Plugin::Plugin() {
}
QVector<studio::WizardMaker> Plugin::importWizards(const studio::Context *args) {
QVector<studio::WizardMaker> Plugin::newWizards(const Context *ctx) {
return {
{
tr("Tile Sheet"),
[args]() {
[ctx]() {
QVector<QWizardPage*> pgs;
pgs.push_back(new ImportTilesheetWizardPage(args));
pgs.push_back(new NewTilesheetWizardPage(ctx));
return pgs;
}
}
};
}
QVector<studio::WizardMaker> Plugin::importWizards(const studio::Context *ctx) {
return {
{
tr("Tile Sheet"),
[ctx]() {
QVector<QWizardPage*> pgs;
pgs.push_back(new ImportTilesheetWizardMainPage(ctx));
pgs.push_back(new ImportTilesheetWizardPalettePage(ctx));
return pgs;
}
}
+2
View File
@@ -22,6 +22,8 @@ class Plugin: public QObject, studio::Plugin {
public:
Plugin();
QVector<studio::WizardMaker> newWizards(const studio::Context *ctx) override;
QVector<studio::WizardMaker> importWizards(const studio::Context *args) override;
};