diff --git a/src/nostalgia/core/studio/charset.png b/src/nostalgia/core/studio/charset.png deleted file mode 100644 index e37d792c..00000000 Binary files a/src/nostalgia/core/studio/charset.png and /dev/null differ diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp index 9e3af629..2befb634 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp @@ -12,9 +12,22 @@ namespace nostalgia::core { TileSheetEditorImGui::TileSheetEditorImGui(Context *ctx, const ox::String &path): m_tileSheetEditor(ctx, path) { + m_ctx = ctx; m_itemPath = path; const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset(); m_itemName = m_itemPath.substr(lastSlash + 1); + // init palette idx + const auto palPath = model()->palPath(); + auto sctx = applicationData(m_ctx); + const auto &palList = sctx->project->fileList(core::FileExt_npal + 1); + for (std::size_t i = 0; const auto &pal : palList) { + if (palPath == pal) { + m_selectedPaletteIdx = i; + break; + } + ++i; + } + // connect signal/slots undoStack()->changeTriggered.connect(this, &TileSheetEditorImGui::markUnsavedChanges); m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet); } @@ -76,7 +89,7 @@ void TileSheetEditorImGui::draw(core::Context*) noexcept { // draw palette/color picker ImGui::BeginChild("Palette", ImVec2(m_palViewWidth - 24, ySize / 2.07f), true); { - drawPalettePicker(); + drawPaletteSelector(); } ImGui::EndChild(); ImGui::BeginChild("SubSheets", ImVec2(m_palViewWidth - 24, ySize / 2.07f), true); @@ -200,7 +213,7 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept { const auto wheel = io.MouseWheel; const auto wheelh = io.MouseWheelH; if (wheel != 0) { - const auto zoomMod = ox::defines::OS == ox::OS::Darwin ? io.KeySuper : io.KeyCtrl; + const auto zoomMod = ox::defines::OS == ox::OS::Darwin ? io.KeySuper : core::buttonDown(m_ctx, core::Key::Mod_Ctrl); m_tileSheetEditor.scrollV(fbSize, wheel, zoomMod); } if (wheelh != 0) { @@ -233,7 +246,23 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept { } } -void TileSheetEditorImGui::drawPalettePicker() noexcept { +void TileSheetEditorImGui::drawPaletteSelector() noexcept { + auto sctx = applicationData(m_ctx); + const auto &files = sctx->project->fileList(core::FileExt_npal + 1); + const auto first = m_selectedPaletteIdx < files.size() ? files[m_selectedPaletteIdx].c_str() : ""; + if (ImGui::BeginCombo("Palette", first, 0)) { + for (auto n = 0u; n < files.size(); n++) { + const auto selected = (m_selectedPaletteIdx == n); + if (ImGui::Selectable(files[n].c_str(), selected) && m_selectedPaletteIdx != n) { + m_selectedPaletteIdx = n; + oxLogError(model()->setPalette(files[n])); + } + if (selected) { + ImGui::SetItemDefaultFocus(); + } + } + ImGui::EndCombo(); + } // header if (ImGui::BeginTable("PaletteTable", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) { ImGui::TableSetupColumn("No.", 0, 0.45); diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp index 93013af8..50e8c1d8 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp @@ -6,9 +6,7 @@ #include -#include #include -#include #include #include @@ -43,6 +41,9 @@ class TileSheetEditorImGui: public studio::Editor { } void draw() noexcept; }; + std::size_t m_selectedPaletteIdx = 0; + Context *m_ctx = nullptr; + ox::Vector m_paletteList; SubSheetEditor m_subsheetEditor; ox::String m_itemPath; ox::String m_itemName; @@ -103,7 +104,7 @@ class TileSheetEditorImGui: public studio::Editor { void drawTileSheet(const geo::Vec2 &fbSize) noexcept; - void drawPalettePicker() noexcept; + void drawPaletteSelector() noexcept; ox::Error updateActiveSubsheet(const ox::String &name, int cols, int rows) noexcept; diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index e769d4a2..cf4e31a0 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -57,6 +57,7 @@ enum class CommandId { RmSubSheet = 3, UpdateSubSheet = 4, Paste = 5, + PaletteChange = 6, }; constexpr bool operator==(CommandId c, int i) noexcept { @@ -322,6 +323,34 @@ class UpdateSubSheetCommand: public studio::UndoCommand { }; +class PaletteChangeCommand: public studio::UndoCommand { + private: + TileSheet *m_img = nullptr; + ox::FileAddress m_oldPalette; + ox::FileAddress m_newPalette; + + public: + constexpr PaletteChangeCommand(TileSheet *img, const ox::String &newPalette) noexcept { + m_img = img; + m_oldPalette = m_img->defaultPalette; + m_newPalette = newPalette; + } + + void redo() noexcept final { + m_img->defaultPalette = m_newPalette; + } + + void undo() noexcept final { + m_img->defaultPalette = m_oldPalette; + } + + [[nodiscard]] + int commandId() const noexcept final { + return static_cast(CommandId::PaletteChange); + } + +}; + TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) { m_ctx = ctx; @@ -378,6 +407,15 @@ void TileSheetEditorModel::paste() { pushCommand(new PasteCommand(&m_img, m_activeSubsSheetIdx, pt1, pt2, *cb)); } +const ox::FileAddress &TileSheetEditorModel::palPath() const noexcept { + return m_img.defaultPalette; +} + +ox::Error TileSheetEditorModel::setPalette(const ox::String &path) noexcept { + pushCommand(new PaletteChangeCommand(&m_img, path)); + return OxError(0); +} + void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept { const auto &activeSubSheet = m_img.getSubSheet(m_activeSubsSheetIdx); if (pt.x >= activeSubSheet.columns * TileWidth || pt.y >= activeSubSheet.rows * TileHeight) { @@ -480,6 +518,9 @@ ox::Error TileSheetEditorModel::markUpdated(int cmdId) noexcept { case CommandId::Paste: case CommandId::UpdateSubSheet: break; + case CommandId::PaletteChange: + oxReturnError(readObj(m_ctx, m_img.defaultPalette.getPath().value).moveTo(&m_pal)); + break; } return OxError(0); } diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.hpp b/src/nostalgia/core/studio/tilesheeteditormodel.hpp index 4fa732c0..25adc8cd 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include namespace nostalgia::core { @@ -53,6 +52,11 @@ class TileSheetEditorModel: public ox::SignalHandler { [[nodiscard]] constexpr const Palette &pal() const noexcept; + [[nodiscard]] + const ox::FileAddress &palPath() const noexcept; + + ox::Error setPalette(const ox::String &path) noexcept; + void drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept; void endDrawCommand() noexcept;