From 64ee637b74c068e5a269b0ce189a86e11186a150 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 31 Mar 2020 21:11:41 -0500 Subject: [PATCH] [nostalgia/core/studio] Add NewPaletteWizard and implement NewTileSheetWizard --- src/nostalgia/core/studio/CMakeLists.txt | 1 + .../core/studio/new_tilesheet_wizard.cpp | 28 +++++++++++-- .../core/studio/new_tilesheet_wizard.hpp | 3 +- .../core/studio/newpalettewizard.cpp | 40 +++++++++++++++++++ .../core/studio/newpalettewizard.hpp | 28 +++++++++++++ src/nostalgia/core/studio/plugin.cpp | 11 ++++- src/nostalgia/core/studio/tilesheeteditor.cpp | 4 +- 7 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/nostalgia/core/studio/newpalettewizard.cpp create mode 100644 src/nostalgia/core/studio/newpalettewizard.hpp diff --git a/src/nostalgia/core/studio/CMakeLists.txt b/src/nostalgia/core/studio/CMakeLists.txt index 7cd7354f..71920918 100644 --- a/src/nostalgia/core/studio/CMakeLists.txt +++ b/src/nostalgia/core/studio/CMakeLists.txt @@ -3,6 +3,7 @@ add_library( imgconv.cpp import_tilesheet_wizard.cpp new_tilesheet_wizard.cpp + newpalettewizard.cpp paletteeditor.cpp plugin.cpp tilesheeteditor.cpp diff --git a/src/nostalgia/core/studio/new_tilesheet_wizard.cpp b/src/nostalgia/core/studio/new_tilesheet_wizard.cpp index a361c67a..c70de4d9 100644 --- a/src/nostalgia/core/studio/new_tilesheet_wizard.cpp +++ b/src/nostalgia/core/studio/new_tilesheet_wizard.cpp @@ -9,20 +9,42 @@ #include #include +#include +#include + +#include "consts.hpp" #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; + addLineEdit(tr("&Tile Sheet Name:"), QString(TileSheetName) + "*", "", [this](QString name) { + auto path = QString(TileSheetDir) + name + FileExt_ng; + auto err = m_ctx->project->exists(path); + if (err) { + showValidationError(tr("A tile sheet with this name already exists.")); + } + return err; } ); + m_palettePicker = addComboBox(tr("&Palette:"), QString(Palette) + "*", {""}); + m_ctx->project->subscribe(studio::ProjectEvent::FileRecognized, m_palettePicker, [this](QString path) { + if (path.startsWith(PaletteDir) && path.endsWith(FileExt_npal)) { + m_palettePicker->addItem(studio::filePathToName(path, PaletteDir, FileExt_npal), path); + } + }); } int NewTilesheetWizardPage::accept() { - auto tilesheetName = field(TileSheetName).toString(); + const auto tilesheetName = field(TileSheetName).toString(); + const auto palette = m_palettePicker->itemData(field(Palette).toInt()).toString(); + const auto outPath = TileSheetDir + tilesheetName + FileExt_ng; + NostalgiaGraphic ng; + ng.columns = 1; + ng.rows = 1; + ng.defaultPalette = palette.toUtf8().data(); + m_ctx->project->writeObj(outPath, &ng); return 0; } diff --git a/src/nostalgia/core/studio/new_tilesheet_wizard.hpp b/src/nostalgia/core/studio/new_tilesheet_wizard.hpp index c3a0c21d..caafe196 100644 --- a/src/nostalgia/core/studio/new_tilesheet_wizard.hpp +++ b/src/nostalgia/core/studio/new_tilesheet_wizard.hpp @@ -14,8 +14,9 @@ namespace nostalgia::core { class NewTilesheetWizardPage: public studio::WizardFormPage { private: - static constexpr auto TileSheetDir = "/TileSheets/"; static constexpr auto TileSheetName = "projectName"; + static constexpr auto Palette = "palette"; + class QComboBox *m_palettePicker = nullptr; const studio::Context *m_ctx = nullptr; public: diff --git a/src/nostalgia/core/studio/newpalettewizard.cpp b/src/nostalgia/core/studio/newpalettewizard.cpp new file mode 100644 index 00000000..b2475847 --- /dev/null +++ b/src/nostalgia/core/studio/newpalettewizard.cpp @@ -0,0 +1,40 @@ +/* + * Copyright 2016 - 2020 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 +#include +#include + +#include +#include + +#include "consts.hpp" +#include "newpalettewizard.hpp" + +namespace nostalgia::core { + +NewPaletteWizardPage::NewPaletteWizardPage(const studio::Context *ctx) { + m_ctx = ctx; + addLineEdit(tr("&Palette Name:"), QString(PaletteName) + "*", "", [this](QString) { + return 0; + }); +} + +int NewPaletteWizardPage::accept() { + const auto paletteName = field(PaletteName).toString(); + const auto path = PaletteDir + paletteName + FileExt_npal; + if (m_ctx->project->exists(path)) { + showValidationError(tr("A palette with this name already exists.")); + return 1; + } + NostalgiaPalette pal; + m_ctx->project->writeObj(path, &pal); + return 0; +} + +} diff --git a/src/nostalgia/core/studio/newpalettewizard.hpp b/src/nostalgia/core/studio/newpalettewizard.hpp new file mode 100644 index 00000000..a55d819c --- /dev/null +++ b/src/nostalgia/core/studio/newpalettewizard.hpp @@ -0,0 +1,28 @@ +/* + * Copyright 2016 - 2020 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 + +namespace nostalgia::core { + +class NewPaletteWizardPage: public studio::WizardFormPage { + private: + static constexpr auto PaletteName = "paletteName"; + const studio::Context *m_ctx = nullptr; + + public: + NewPaletteWizardPage(const studio::Context *args); + + int accept(); + + private: +}; + +} diff --git a/src/nostalgia/core/studio/plugin.cpp b/src/nostalgia/core/studio/plugin.cpp index e496a267..b58f34b4 100644 --- a/src/nostalgia/core/studio/plugin.cpp +++ b/src/nostalgia/core/studio/plugin.cpp @@ -7,6 +7,7 @@ */ #include "new_tilesheet_wizard.hpp" +#include "newpalettewizard.hpp" #include "import_tilesheet_wizard.hpp" #include "paletteeditor.hpp" #include "tilesheeteditor.hpp" @@ -19,6 +20,14 @@ namespace nostalgia::core { QVector Plugin::newWizards(const studio::Context *ctx) { return { + { + tr("Palette"), + [ctx]() { + QVector pgs; + pgs.push_back(new NewPaletteWizardPage(ctx)); + return pgs; + } + }, { tr("Tile Sheet"), [ctx]() { @@ -26,7 +35,7 @@ QVector Plugin::newWizards(const studio::Context *ctx) { pgs.push_back(new NewTilesheetWizardPage(ctx)); return pgs; } - } + }, }; } diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index 64abab24..54160fd0 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -564,9 +564,7 @@ void TileSheetEditor::restoreState() { } 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); + return studio::filePathToName(palettePath, PaletteDir, FileExt_npal); } QString TileSheetEditor::palettePath(QString paletteName) const {