diff --git a/src/nostalgia/core/studio/consts.hpp b/src/nostalgia/core/studio/consts.hpp index 4361307b..a3b6b450 100644 --- a/src/nostalgia/core/studio/consts.hpp +++ b/src/nostalgia/core/studio/consts.hpp @@ -11,6 +11,8 @@ namespace nostalgia::core { constexpr auto PluginName = "NostalgiaCore"; +constexpr auto TileSheetDir = "/TileSheets/"; +constexpr auto PaletteDir = "/Palettes/"; // Command IDs to use with QUndoCommand::id() enum class CommandId { diff --git a/src/nostalgia/core/studio/import_tilesheet_wizard.hpp b/src/nostalgia/core/studio/import_tilesheet_wizard.hpp index 8f06d47d..02c4b7a4 100644 --- a/src/nostalgia/core/studio/import_tilesheet_wizard.hpp +++ b/src/nostalgia/core/studio/import_tilesheet_wizard.hpp @@ -10,10 +10,10 @@ #include +#include "consts.hpp" + namespace nostalgia::core { -constexpr auto TileSheetDir = "/TileSheets/"; -constexpr auto PaletteDir = "/Palettes/"; constexpr auto TileSheetName = "tilesheetName"; constexpr auto ImportPath = "importPath"; constexpr auto Palette = "palette"; diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index c234b625..889672f4 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include "consts.hpp" #include "tilesheeteditor.hpp" @@ -193,22 +195,26 @@ void SheetData::updatePixel(QVariant pixelItem) { } } -int SheetData::columns() { +int SheetData::columns() const { return m_columns; } -int SheetData::rows() { +int SheetData::rows() const { return m_rows; } -const QVector &SheetData::pixels() { +const QVector &SheetData::pixels() const { return m_pixels; } -QStringList SheetData::palette() { +QStringList SheetData::palette() const { return m_palette; } +QString SheetData::palettePath() const { + return m_currentPalettePath; +} + void SheetData::load(const studio::Context *ctx, QString ngPath, QString palPath) { auto ng = ctx->project->loadObj(ngPath); 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()); } -void SheetData::save(const studio::Context *ctx, QString ngPath) { +void SheetData::save(const studio::Context *ctx, QString ngPath) const { auto ng = toNostalgiaGraphic(); ctx->project->writeObj(ngPath, ng.get()); } @@ -239,13 +245,13 @@ void SheetData::setPalette(const NostalgiaPalette *npal) { const auto color = c.name(QColor::HexArgb); m_palette.append(color); } + emit paletteChanged(); } void SheetData::setPalette(const studio::Context *ctx, QString palPath) { std::unique_ptr npal; try { npal = ctx->project->loadObj(palPath); - qInfo() << "Opened palette" << palPath; } catch (ox::Error) { qWarning() << "Could not open palette" << palPath; } @@ -299,10 +305,9 @@ void SheetData::updatePixels(const NostalgiaGraphic *ng) { } } emit pixelsChanged(); - emit paletteChanged(); } -std::unique_ptr SheetData::toNostalgiaGraphic() { +std::unique_ptr SheetData::toNostalgiaGraphic() const { auto ng = std::make_unique(); const auto highestColorIdx = static_cast(*std::max_element(m_pixels.begin(), m_pixels.end())); 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->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml")); m_canvas->setResizeMode(QQuickWidget::SizeRootObjectToView); - setColorTable(m_sheetData.palette()); + setPalette(); + setColorTable(); restoreState(); connect(&m_sheetData, &SheetData::changeOccurred, [this] { setUnsavedChanges(true); }); + connect(&m_sheetData, &SheetData::paletteChanged, this, &TileSheetEditor::setColorTable); } TileSheetEditor::~TileSheetEditor() { @@ -405,23 +412,30 @@ QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) { m_colorPicker.colorTable->horizontalHeader()->setStretchLastSection(true); m_colorPicker.colorTable->verticalHeader()->hide(); 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.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; } -void TileSheetEditor::setColorTable(QStringList hexColors) { - 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); - } +void TileSheetEditor::setPalette() { + auto name = paletteName(m_sheetData.palettePath()); + m_colorPicker.palette->setCurrentText(name); } void TileSheetEditor::saveState() { @@ -442,8 +456,33 @@ void TileSheetEditor::restoreState() { 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() { 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); + } +} + } diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 4666c88c..3609ddcc 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -43,17 +43,19 @@ class SheetData: public QObject { Q_INVOKABLE void endCmd(); - int columns(); + [[nodiscard]] int columns() const; - int rows(); + [[nodiscard]] int rows() const; - const QVector &pixels(); + const QVector &pixels() const; - QStringList palette(); + [[nodiscard]] QStringList palette() const; + + [[nodiscard]] QString palettePath() const; 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); @@ -81,7 +83,7 @@ class SheetData: public QObject { private: void updatePixels(const NostalgiaGraphic *ng); - [[nodiscard]] std::unique_ptr toNostalgiaGraphic(); + [[nodiscard]] std::unique_ptr toNostalgiaGraphic() const; signals: void changeOccurred(); @@ -129,15 +131,21 @@ class TileSheetEditor: public studio::Editor { private: QWidget *setupColorPicker(QWidget *widget); - void setColorTable(QStringList hexColors); + void setPalette(); void saveState(); void restoreState(); + [[nodiscard]] QString paletteName(QString palettePath) const; + + [[nodiscard]] QString palettePath(QString palettePath) const; + public slots: void colorSelected(); + void setColorTable(); + }; }