diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp index bf6a557e..fa1a573f 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp @@ -63,9 +63,9 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept { const auto fbSizei = geo::Size(static_cast(fbSize.x), static_cast(fbSize.y)); if (m_framebuffer.width != fbSizei.width || m_framebuffer.height != fbSizei.height) { m_framebuffer = glutils::generateFrameBuffer(fbSizei.width, fbSizei.height); - m_tileSheetEditor.resize(fbSize); + m_tileSheetEditor.resizeView(fbSize); } else if (m_tileSheetEditor.updated()) { - m_tileSheetEditor.resize(fbSize); + m_tileSheetEditor.resizeView(fbSize); m_tileSheetEditor.ackUpdate(); } glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer); @@ -93,7 +93,7 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept { const auto &winPos = ImGui::GetWindowPos(); clickPos.x -= winPos.x + 10; clickPos.y -= winPos.y + 10; - m_tileSheetEditor.hitPixel(fbSize, clickPos); + m_tileSheetEditor.click(fbSize, clickPos, m_palIdx); } } if (io.MouseReleased[0]) { @@ -102,20 +102,30 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept { } } -void TileSheetEditorImGui::drawPalettePicker() const noexcept { +void TileSheetEditorImGui::drawPalettePicker() noexcept { // header - ImGui::BeginTable("PaletteTable", 2); - ImGui::TableSetupColumn("No.", 0, 0.35); - ImGui::TableSetupColumn("Color16", 0, 3); - ImGui::TableHeadersRow(); - for (auto i = 1; auto c : m_tileSheetEditor.pal().colors) { - ImGui::TableNextColumn(); - ImGui::Text("%d", i); - ImGui::TableNextColumn(); - ImGui::Text("(%d, %d, %d)", red16(c), green16(c), blue16(c)); - ++i; + if (ImGui::BeginTable("PaletteTable", 2, ImGuiTableFlags_RowBg)) { + ImGui::TableSetupColumn("No.", 0, 0.35); + ImGui::TableSetupColumn("Color16", 0, 3); + ImGui::TableHeadersRow(); + for (auto i = 0u; auto c: m_tileSheetEditor.pal().colors) { + ImGui::PushID(static_cast(i)); + // Column: color idx + ImGui::TableNextColumn(); + const auto label = ox::BString<8>() + (i + 1); + const auto rowSelected = i == m_palIdx; + if (ImGui::Selectable(label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) { + m_palIdx = i; + } + // Column: color RGB + ImGui::TableNextColumn(); + ImGui::Text("(%d, %d, %d)", red16(c), green16(c), blue16(c)); + ImGui::TableNextRow(); + ImGui::PopID(); + ++i; + } + ImGui::EndTable(); } - ImGui::EndTable(); } } diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp index cf18f835..3680a8a3 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.hpp @@ -28,6 +28,9 @@ class TileSheetEditorImGui: public studio::Editor { float m_palViewWidth = 200; geo::Vec2 m_prevMouseDownPos; + // palette view vars + std::size_t m_palIdx = 0; + public: TileSheetEditorImGui(Context *ctx, const ox::String &path); @@ -65,7 +68,7 @@ class TileSheetEditorImGui: public studio::Editor { void drawTileSheet(const geo::Vec2 &fbSize) noexcept; - void drawPalettePicker() const noexcept; + void drawPalettePicker() noexcept; // slots public: diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index cd535954..04a42712 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -19,12 +19,15 @@ TileSheetEditor::TileSheetEditor(Context *ctx, const ox::String &path): m_model( } void TileSheetEditor::cut() { + m_model.cut(); } void TileSheetEditor::copy() { + m_model.copy(); } void TileSheetEditor::paste() { + m_model.paste(); } void TileSheetEditor::draw() noexcept { @@ -60,7 +63,7 @@ void TileSheetEditor::scrollH(const geo::Vec2 &paneSz, float wheelh) noexcept { m_scrollOffset.x = ox::clamp(m_scrollOffset.x, -(sheetSize.x / 2), 0.f); } -void TileSheetEditor::hitPixel(const geo::Vec2 &paneSize, const geo::Vec2 &clickPos) noexcept { +void TileSheetEditor::click(const geo::Vec2 &paneSize, const geo::Vec2 &clickPos, size_t palIdx) noexcept { auto [x, y] = clickPos; const auto pixDrawSz = m_pixelsDrawer.pixelSize(paneSize); x /= paneSize.x; @@ -70,15 +73,14 @@ void TileSheetEditor::hitPixel(const geo::Vec2 &paneSize, const geo::Vec2 &click x /= pixDrawSz.x; y /= pixDrawSz.y; const auto pt = geo::Point(static_cast(x * 2), static_cast(y * 2)); - const uint8_t palIdx = 0; - m_model.draw(pt, palIdx); + m_model.drawCommand(pt, palIdx); } void TileSheetEditor::releaseMouseButton() noexcept { - m_model.endDraw(); + m_model.endDrawCommand(); } -void TileSheetEditor::resize(const geo::Vec2 &sz) noexcept { +void TileSheetEditor::resizeView(const geo::Vec2 &sz) noexcept { m_pixelsDrawer.initBufferSet(sz, img(), pal()); m_pixelGridDrawer.initBufferSet(sz, img()); } diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 2126e6f0..46d14ebb 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -59,7 +59,7 @@ class TileSheetEditor { void draw() noexcept; - void hitPixel(const geo::Vec2 &paneSize, const geo::Vec2 &clickPos) noexcept; + void click(const geo::Vec2 &paneSize, const geo::Vec2 &clickPos, size_t palIdx) noexcept; void releaseMouseButton() noexcept; @@ -67,7 +67,7 @@ class TileSheetEditor { void scrollH(const geo::Vec2 &paneSz, float wheel) noexcept; - void resize(const geo::Vec2 &sz) noexcept; + void resizeView(const geo::Vec2 &sz) noexcept; [[nodiscard]] constexpr const NostalgiaGraphic &img() const noexcept; diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.cpp b/src/nostalgia/core/studio/tilesheeteditormodel.cpp index b692a972..b7abf1d6 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.cpp @@ -14,15 +14,27 @@ TileSheetEditorModel::TileSheetEditorModel(Context *ctx, const ox::String &path) oxThrowError(readObj(ctx, m_img.defaultPalette).moveTo(&m_pal)); } -void TileSheetEditorModel::draw(const geo::Point &pt, std::size_t palIdx) noexcept { +void TileSheetEditorModel::cut() { +} + +void TileSheetEditorModel::copy() { +} + +void TileSheetEditorModel::paste() { +} + +void TileSheetEditorModel::drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept { if (m_ongoingDrawCommand) { m_updated = m_ongoingDrawCommand->append(ptToIdx(pt, m_img.columns)); } else { - pushCommand(new DrawCommand(&m_img, ptToIdx(pt, m_img.columns), palIdx)); + const auto idx = ptToIdx(pt, m_img.columns); + if (m_img.getPixel(idx) != palIdx) { + pushCommand(new DrawCommand(&m_img, idx, palIdx)); + } } } -void TileSheetEditorModel::endDraw() noexcept { +void TileSheetEditorModel::endDrawCommand() noexcept { m_ongoingDrawCommand = nullptr; } diff --git a/src/nostalgia/core/studio/tilesheeteditormodel.hpp b/src/nostalgia/core/studio/tilesheeteditormodel.hpp index 3efb93cc..d6b24f06 100644 --- a/src/nostalgia/core/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/core/studio/tilesheeteditormodel.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include namespace nostalgia::core { @@ -55,13 +54,14 @@ struct DrawCommand: public studio::UndoCommand { return false; } - void redo() noexcept override { + void redo() noexcept final { for (const auto &c : m_changes) { + oxDebugf("{} to {}", c.idx, m_palIdx); m_img->setPixel(c.idx, m_palIdx); } } - void undo() noexcept override { + void undo() noexcept final { for (const auto &c : m_changes) { m_img->setPixel(c.idx, c.oldPalIdx); } @@ -137,9 +137,9 @@ class TileSheetEditorModel { [[nodiscard]] constexpr const NostalgiaPalette &pal() const noexcept; - void draw(const geo::Point &pt, std::size_t palIdx) noexcept; + void drawCommand(const geo::Point &pt, std::size_t palIdx) noexcept; - void endDraw() noexcept; + void endDrawCommand() noexcept; [[nodiscard]] bool updated() const noexcept;