From a22aafaf96460bb8984fbd0bef34544077667150 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 26 Jan 2025 22:12:57 -0600 Subject: [PATCH] [nostalgia/gfx/studio/palette] Add ability to reorder Palette pages --- .../src/studio/paletteeditor/CMakeLists.txt | 1 + .../paletteeditor/commands/commands.hpp | 1 + .../commands/movepagecommand.cpp | 40 +++++++++++++++++++ .../commands/movepagecommand.hpp | 35 ++++++++++++++++ .../paletteeditor/paletteeditor-imgui.cpp | 22 ++++++++++ 5 files changed, 99 insertions(+) create mode 100644 src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.cpp create mode 100644 src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.hpp diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/CMakeLists.txt b/src/nostalgia/modules/gfx/src/studio/paletteeditor/CMakeLists.txt index cdaf9d3e..9ec397bf 100644 --- a/src/nostalgia/modules/gfx/src/studio/paletteeditor/CMakeLists.txt +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources( commands/applycolorallpagescommand.cpp commands/duplicatepagecommand.cpp commands/movecolorcommand.cpp + commands/movepagecommand.cpp commands/removecolorcommand.cpp commands/removepagecommand.cpp commands/renamepagecommand.cpp diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/commands.hpp b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/commands.hpp index 7ca6ee69..666a0f20 100644 --- a/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/commands.hpp +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/commands.hpp @@ -12,6 +12,7 @@ enum class PaletteEditorCommandId { AddPage, DuplicatePage, RemovePage, + MovePage, AddColor, RemoveColor, UpdateColorInfo, diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.cpp b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.cpp new file mode 100644 index 00000000..86a9c80d --- /dev/null +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.cpp @@ -0,0 +1,40 @@ +/* + * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#include "commands.hpp" + +#include "movepagecommand.hpp" + +namespace nostalgia::gfx { + +MovePageCommand::MovePageCommand( + Palette &pal, + size_t const srcIdx, + size_t const dstIdx) noexcept: + m_pal(pal), + m_srcIdx(srcIdx), + m_dstIdx(dstIdx) {} + +int MovePageCommand::commandId() const noexcept { + return static_cast(PaletteEditorCommandId::MovePage); +} + +ox::Error MovePageCommand::redo() noexcept { + movePage(m_srcIdx, m_dstIdx); + return {}; +} + +ox::Error MovePageCommand::undo() noexcept { + movePage(m_dstIdx, m_srcIdx); + return {}; +} + +void MovePageCommand::movePage( + size_t const srcIdx, size_t const dstIdx) noexcept { + auto page = std::move(m_pal.pages[srcIdx]); + std::ignore = m_pal.pages.erase(srcIdx); + m_pal.pages.emplace(dstIdx, std::move(page)); +} + +} diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.hpp b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.hpp new file mode 100644 index 00000000..0b42161d --- /dev/null +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/commands/movepagecommand.hpp @@ -0,0 +1,35 @@ +/* + * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +#include + +#include + +namespace nostalgia::gfx { + +class MovePageCommand: public studio::UndoCommand { + private: + Palette &m_pal; + std::size_t const m_srcIdx = 0; + std::size_t const m_dstIdx = 0; + + public: + MovePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept; + + ~MovePageCommand() noexcept override = default; + + [[nodiscard]] + int commandId() const noexcept override; + + ox::Error redo() noexcept override; + + ox::Error undo() noexcept override; + + private: + void movePage(size_t srcIdx, size_t dstIdx) noexcept; +}; + +} diff --git a/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp index 28d1275a..8a433078 100644 --- a/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/gfx/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -11,6 +11,7 @@ #include "commands/applycolorallpagescommand.hpp" #include "commands/duplicatepagecommand.hpp" #include "commands/movecolorcommand.hpp" +#include "commands/movepagecommand.hpp" #include "commands/removecolorcommand.hpp" #include "commands/removepagecommand.hpp" #include "commands/renamepagecommand.hpp" @@ -19,6 +20,7 @@ #include "paletteeditor-imgui.hpp" + namespace nostalgia::gfx { namespace ig = studio::ig; @@ -33,6 +35,16 @@ OX_MODEL_BEGIN(ColorDragDrop) OX_MODEL_FIELD(i) OX_MODEL_END() +struct PageDragDrop { + static constexpr auto TypeName = "nostalgia.gfx.PageDragDrop"; + static constexpr auto TypeVersion = 1; + uint32_t page{}; +}; + +OX_MODEL_BEGIN(PageDragDrop) + OX_MODEL_FIELD(page) +OX_MODEL_END() + void PaletteEditorImGui::PageRenameDialog::draw(turbine::Context &tctx) noexcept { if (!m_show) { return; @@ -235,6 +247,16 @@ void PaletteEditorImGui::drawPagesEditor() noexcept { if (ImGui::Selectable("##PageRow", i == m_page, ImGuiSelectableFlags_SpanAllColumns)) { m_page = i; } + std::ignore = ig::dragDropSource([this, i] { + ImGui::Text("%s", m_pal.pages[i].name.c_str()); + return ig::setDragDropPayload(PageDragDrop{i}); + }, ImGuiDragDropFlags_SourceAllowNullID); + if (ig::DragDropTarget const d; d) { + auto const [src, err] = ig::getDragDropPayload(); + if (!err) { + std::ignore = pushCommand(m_pal, src.page, i); + } + } } } ImGui::EndTable();