[nostalgia/core/studio] Add ability for TileSheetEditor to switch palettes

This commit is contained in:
Gary Talent 2020-03-19 02:09:26 -05:00
parent c45ec45457
commit 2f4868f59e
4 changed files with 79 additions and 30 deletions

View File

@ -11,6 +11,8 @@
namespace nostalgia::core { namespace nostalgia::core {
constexpr auto PluginName = "NostalgiaCore"; constexpr auto PluginName = "NostalgiaCore";
constexpr auto TileSheetDir = "/TileSheets/";
constexpr auto PaletteDir = "/Palettes/";
// Command IDs to use with QUndoCommand::id() // Command IDs to use with QUndoCommand::id()
enum class CommandId { enum class CommandId {

View File

@ -10,10 +10,10 @@
#include <nostalgia/studio/studio.hpp> #include <nostalgia/studio/studio.hpp>
#include "consts.hpp"
namespace nostalgia::core { namespace nostalgia::core {
constexpr auto TileSheetDir = "/TileSheets/";
constexpr auto PaletteDir = "/Palettes/";
constexpr auto TileSheetName = "tilesheetName"; constexpr auto TileSheetName = "tilesheetName";
constexpr auto ImportPath = "importPath"; constexpr auto ImportPath = "importPath";
constexpr auto Palette = "palette"; constexpr auto Palette = "palette";

View File

@ -22,6 +22,8 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <memory> #include <memory>
#include <nostalgia/core/consts.hpp>
#include "consts.hpp" #include "consts.hpp"
#include "tilesheeteditor.hpp" #include "tilesheeteditor.hpp"
@ -193,22 +195,26 @@ void SheetData::updatePixel(QVariant pixelItem) {
} }
} }
int SheetData::columns() { int SheetData::columns() const {
return m_columns; return m_columns;
} }
int SheetData::rows() { int SheetData::rows() const {
return m_rows; return m_rows;
} }
const QVector<int> &SheetData::pixels() { const QVector<int> &SheetData::pixels() const {
return m_pixels; return m_pixels;
} }
QStringList SheetData::palette() { QStringList SheetData::palette() const {
return m_palette; return m_palette;
} }
QString SheetData::palettePath() const {
return m_currentPalettePath;
}
void SheetData::load(const studio::Context *ctx, QString ngPath, QString palPath) { void SheetData::load(const studio::Context *ctx, QString ngPath, QString palPath) {
auto ng = ctx->project->loadObj<NostalgiaGraphic>(ngPath); auto ng = ctx->project->loadObj<NostalgiaGraphic>(ngPath);
if (palPath == "" && ng->defaultPalette.type() == ox::FileAddressType::Path) { if (palPath == "" && ng->defaultPalette.type() == ox::FileAddressType::Path) {
@ -226,7 +232,7 @@ void SheetData::load(const studio::Context *ctx, QString ngPath, QString palPath
updatePixels(ng.get()); updatePixels(ng.get());
} }
void SheetData::save(const studio::Context *ctx, QString ngPath) { void SheetData::save(const studio::Context *ctx, QString ngPath) const {
auto ng = toNostalgiaGraphic(); auto ng = toNostalgiaGraphic();
ctx->project->writeObj(ngPath, ng.get()); ctx->project->writeObj(ngPath, ng.get());
} }
@ -239,13 +245,13 @@ void SheetData::setPalette(const NostalgiaPalette *npal) {
const auto color = c.name(QColor::HexArgb); const auto color = c.name(QColor::HexArgb);
m_palette.append(color); m_palette.append(color);
} }
emit paletteChanged();
} }
void SheetData::setPalette(const studio::Context *ctx, QString palPath) { void SheetData::setPalette(const studio::Context *ctx, QString palPath) {
std::unique_ptr<NostalgiaPalette> npal; std::unique_ptr<NostalgiaPalette> npal;
try { try {
npal = ctx->project->loadObj<NostalgiaPalette>(palPath); npal = ctx->project->loadObj<NostalgiaPalette>(palPath);
qInfo() << "Opened palette" << palPath;
} catch (ox::Error) { } catch (ox::Error) {
qWarning() << "Could not open palette" << palPath; qWarning() << "Could not open palette" << palPath;
} }
@ -299,10 +305,9 @@ void SheetData::updatePixels(const NostalgiaGraphic *ng) {
} }
} }
emit pixelsChanged(); emit pixelsChanged();
emit paletteChanged();
} }
std::unique_ptr<NostalgiaGraphic> SheetData::toNostalgiaGraphic() { std::unique_ptr<NostalgiaGraphic> SheetData::toNostalgiaGraphic() const {
auto ng = std::make_unique<NostalgiaGraphic>(); auto ng = std::make_unique<NostalgiaGraphic>();
const auto highestColorIdx = static_cast<uint8_t>(*std::max_element(m_pixels.begin(), m_pixels.end())); const auto highestColorIdx = static_cast<uint8_t>(*std::max_element(m_pixels.begin(), m_pixels.end()));
ng->defaultPalette = m_currentPalettePath.toUtf8().data(); ng->defaultPalette = m_currentPalettePath.toUtf8().data();
@ -372,9 +377,11 @@ TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidg
m_canvas->rootContext()->setContextProperty("sheetData", &m_sheetData); m_canvas->rootContext()->setContextProperty("sheetData", &m_sheetData);
m_canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml")); m_canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml"));
m_canvas->setResizeMode(QQuickWidget::SizeRootObjectToView); m_canvas->setResizeMode(QQuickWidget::SizeRootObjectToView);
setColorTable(m_sheetData.palette()); setPalette();
setColorTable();
restoreState(); restoreState();
connect(&m_sheetData, &SheetData::changeOccurred, [this] { setUnsavedChanges(true); }); connect(&m_sheetData, &SheetData::changeOccurred, [this] { setUnsavedChanges(true); });
connect(&m_sheetData, &SheetData::paletteChanged, this, &TileSheetEditor::setColorTable);
} }
TileSheetEditor::~TileSheetEditor() { TileSheetEditor::~TileSheetEditor() {
@ -405,23 +412,30 @@ QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) {
m_colorPicker.colorTable->horizontalHeader()->setStretchLastSection(true); m_colorPicker.colorTable->horizontalHeader()->setStretchLastSection(true);
m_colorPicker.colorTable->verticalHeader()->hide(); m_colorPicker.colorTable->verticalHeader()->hide();
connect(m_colorPicker.colorTable, &QTableWidget::itemSelectionChanged, this, &TileSheetEditor::colorSelected); connect(m_colorPicker.colorTable, &QTableWidget::itemSelectionChanged, this, &TileSheetEditor::colorSelected);
connect(m_colorPicker.palette, &QComboBox::currentTextChanged, this, [this](QString name) {
m_sheetData.setPalette(m_ctx, palettePath(name));
});
lyt->addWidget(m_colorPicker.palette); lyt->addWidget(m_colorPicker.palette);
lyt->addWidget(m_colorPicker.colorTable); lyt->addWidget(m_colorPicker.colorTable);
m_ctx->project->subscribe(studio::ProjectEvent::FileRecognized, m_colorPicker.palette, [this](QString path) {
if (path.startsWith(PaletteDir) && path.endsWith(FileExt_npal)) {
auto name = paletteName(path);
m_colorPicker.palette->addItem(name);
}
});
m_ctx->project->subscribe(studio::ProjectEvent::FileDeleted, m_colorPicker.palette, [this](QString path) {
if (path.startsWith(PaletteDir) && path.endsWith(FileExt_npal)) {
auto name = paletteName(path);
auto idx = m_colorPicker.palette->findText(name);
m_colorPicker.palette->removeItem(idx);
}
});
return colorPicker; return colorPicker;
} }
void TileSheetEditor::setColorTable(QStringList hexColors) { void TileSheetEditor::setPalette() {
auto tbl = m_colorPicker.colorTable; auto name = paletteName(m_sheetData.palettePath());
tbl->setRowCount(hexColors.size()); m_colorPicker.palette->setCurrentText(name);
for (int i = 0; i < hexColors.size(); i++) {
auto hexCode = new QTableWidgetItem;
hexCode->setText(hexColors[i]);
hexCode->setFont(QFont("monospace"));
tbl->setItem(i, 0, hexCode);
auto color = new QTableWidgetItem;
color->setBackground(QColor(hexColors[i]));
tbl->setItem(i, 1, color);
}
} }
void TileSheetEditor::saveState() { void TileSheetEditor::saveState() {
@ -442,8 +456,33 @@ void TileSheetEditor::restoreState() {
settings.endGroup(); settings.endGroup();
} }
QString TileSheetEditor::paletteName(QString palettePath) const {
const auto begin = ox_strlen(PaletteDir);
const auto end = palettePath.size() - (ox_strlen(FileExt_npal) + ox_strlen(PaletteDir));
return palettePath.mid(begin, end);
}
QString TileSheetEditor::palettePath(QString paletteName) const {
return PaletteDir + paletteName + FileExt_npal;
}
void TileSheetEditor::colorSelected() { void TileSheetEditor::colorSelected() {
m_sheetData.setSelectedColor(m_colorPicker.colorTable->currentRow()); m_sheetData.setSelectedColor(m_colorPicker.colorTable->currentRow());
} }
void TileSheetEditor::setColorTable() {
auto hexColors = m_sheetData.palette();
auto tbl = m_colorPicker.colorTable;
tbl->setRowCount(hexColors.size());
for (int i = 0; i < hexColors.size(); i++) {
auto hexCode = new QTableWidgetItem;
hexCode->setText(hexColors[i]);
hexCode->setFont(QFont("monospace"));
tbl->setItem(i, 0, hexCode);
auto color = new QTableWidgetItem;
color->setBackground(QColor(hexColors[i]));
tbl->setItem(i, 1, color);
}
}
} }

View File

@ -43,17 +43,19 @@ class SheetData: public QObject {
Q_INVOKABLE void endCmd(); Q_INVOKABLE void endCmd();
int columns(); [[nodiscard]] int columns() const;
int rows(); [[nodiscard]] int rows() const;
const QVector<int> &pixels(); const QVector<int> &pixels() const;
QStringList palette(); [[nodiscard]] QStringList palette() const;
[[nodiscard]] QString palettePath() const;
void load(const studio::Context *ctx, QString ngPath, QString palPath = ""); void load(const studio::Context *ctx, QString ngPath, QString palPath = "");
void save(const studio::Context *ctx, QString ngPath); void save(const studio::Context *ctx, QString ngPath) const;
void setPalette(const NostalgiaPalette *pal); void setPalette(const NostalgiaPalette *pal);
@ -81,7 +83,7 @@ class SheetData: public QObject {
private: private:
void updatePixels(const NostalgiaGraphic *ng); void updatePixels(const NostalgiaGraphic *ng);
[[nodiscard]] std::unique_ptr<NostalgiaGraphic> toNostalgiaGraphic(); [[nodiscard]] std::unique_ptr<NostalgiaGraphic> toNostalgiaGraphic() const;
signals: signals:
void changeOccurred(); void changeOccurred();
@ -129,15 +131,21 @@ class TileSheetEditor: public studio::Editor {
private: private:
QWidget *setupColorPicker(QWidget *widget); QWidget *setupColorPicker(QWidget *widget);
void setColorTable(QStringList hexColors); void setPalette();
void saveState(); void saveState();
void restoreState(); void restoreState();
[[nodiscard]] QString paletteName(QString palettePath) const;
[[nodiscard]] QString palettePath(QString palettePath) const;
public slots: public slots:
void colorSelected(); void colorSelected();
void setColorTable();
}; };
} }