[nostalgia/core/studio] Cleanup

This commit is contained in:
Gary Talent 2024-12-10 23:29:21 -06:00
parent ff05d860c4
commit d85a10af84
26 changed files with 769 additions and 568 deletions

View File

@ -1,5 +1,13 @@
target_sources( target_sources(
NostalgiaCore-Studio PRIVATE NostalgiaCore-Studio PRIVATE
paletteeditor.cpp commands/addcolorcommand.cpp
commands/applycolorallpagescommand.cpp
commands/duplicatepagecommand.cpp
commands/movecolorcommand.cpp
commands/removecolorcommand.cpp
commands/removepagecommand.cpp
commands/renamepagecommand.cpp
commands/updatecolorcommand.cpp
commands/updatecolorinfocommand.cpp
paletteeditor-imgui.cpp paletteeditor-imgui.cpp
) )

View File

@ -0,0 +1,35 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "addcolorcommand.hpp"
namespace nostalgia::core {
AddColorCommand::AddColorCommand(Palette &pal, Color16 const color, size_t const idx) noexcept:
m_pal(pal),
m_color(color),
m_idx(idx) {}
int AddColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddColor);
}
ox::Error AddColorCommand::redo() noexcept {
m_pal.colorNames.emplace(m_idx, ox::sfmt("Color {}", m_pal.colorNames.size() + 1));
for (auto &page : m_pal.pages) {
page.colors.emplace(m_idx, m_color);
}
return {};
}
ox::Error AddColorCommand::undo() noexcept {
oxReturnError(m_pal.colorNames.erase(m_idx));
for (auto &page : m_pal.pages) {
oxReturnError(page.colors.erase(m_idx));
}
return {};
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class AddColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
Color16 m_color = 0;
size_t const m_idx = 0;
public:
AddColorCommand(Palette &pal, Color16 color, size_t idx) noexcept;
~AddColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
};
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "applycolorallpagescommand.hpp"
namespace nostalgia::core {
ApplyColorAllPagesCommand::ApplyColorAllPagesCommand(Palette &pal, size_t const page, size_t const idx):
m_pal(pal),
m_page(page),
m_idx(idx),
m_origColors([this] {
ox::Vector<Color16> colors;
colors.reserve(m_pal.pages.size());
for (auto const&p : m_pal.pages) {
colors.emplace_back(p.colors[m_idx]);
}
return colors;
}()) {
auto const c = color(m_pal, m_page, m_idx);
if (ox::all_of(m_pal.pages.begin(), m_pal.pages.end(), [this, c](PalettePage const&page) {
return page.colors[m_idx] == c;
})) {
throw studio::NoChangesException();
}
}
int ApplyColorAllPagesCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::ApplyColorAllPages);
}
ox::Error ApplyColorAllPagesCommand::redo() noexcept {
auto const c = color(m_pal, m_page, m_idx);
for (auto &page : m_pal.pages) {
page.colors[m_idx] = c;
}
return {};
}
ox::Error ApplyColorAllPagesCommand::undo() noexcept {
for (size_t p = 0u; auto &page : m_pal.pages) {
page.colors[m_idx] = m_origColors[p];
++p;
}
return {};
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class ApplyColorAllPagesCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page{};
size_t const m_idx{};
ox::Vector<Color16> const m_origColors;
public:
ApplyColorAllPagesCommand(Palette &pal, size_t page, size_t idx);
~ApplyColorAllPagesCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
namespace nostalgia::core {
enum class PaletteEditorCommandId {
ApplyColorAllPages,
RenamePage,
DuplicatePage,
RemovePage,
AddColor,
RemoveColor,
UpdateColorInfo,
UpdateColor,
MoveColor,
};
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "duplicatepagecommand.hpp"
namespace nostalgia::core {
DuplicatePageCommand::DuplicatePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept:
m_pal(pal),
m_dstIdx(dstIdx) {
auto const&src = m_pal.pages[srcIdx];
m_page.reserve(src.colors.size());
for (auto const&s : src.colors) {
m_page.emplace_back(s);
}
}
int DuplicatePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::DuplicatePage);
}
ox::Error DuplicatePageCommand::redo() noexcept {
m_pal.pages.emplace(m_dstIdx, "", std::move(m_page));
return {};
}
ox::Error DuplicatePageCommand::undo() noexcept {
m_page = std::move(m_pal.pages[m_dstIdx].colors);
return m_pal.pages.erase(m_dstIdx).error;
}
size_t DuplicatePageCommand::insertIdx() const noexcept {
return m_dstIdx;
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class DuplicatePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_dstIdx = 0;
ox::Vector<PaletteColor> m_page;
public:
DuplicatePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept;
~DuplicatePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
[[nodiscard]]
size_t insertIdx() const noexcept;
};
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "movecolorcommand.hpp"
namespace nostalgia::core {
MoveColorCommand::MoveColorCommand(
Palette &pal, size_t page, size_t srcIdx, size_t dstIdx) noexcept:
m_pal(pal),
m_page(page),
m_srcIdx(srcIdx),
m_dstIdx(dstIdx) {}
int MoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::MoveColor);
}
ox::Error MoveColorCommand::redo() noexcept {
moveColor(m_srcIdx, m_dstIdx);
return {};
}
ox::Error MoveColorCommand::undo() noexcept {
moveColor(m_dstIdx, m_srcIdx);
return {};
}
void MoveColorCommand::moveColor(size_t srcIdx, size_t dstIdx) noexcept {
auto const c = color(m_pal, m_page, srcIdx);
std::ignore = colors(m_pal, m_page).erase(srcIdx);
colors(m_pal, m_page).emplace(dstIdx, c);
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class MoveColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page = 0;
std::size_t const m_srcIdx = 0;
std::size_t const m_dstIdx = 0;
public:
MoveColorCommand(Palette &pal, size_t page, size_t srcIdx, size_t dstIdx) noexcept;
~MoveColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
private:
void moveColor(size_t srcIdx, size_t dstIdx) noexcept;
};
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "removecolorcommand.hpp"
namespace nostalgia::core {
RemoveColorCommand::RemoveColorCommand(Palette &pal, size_t const idx) noexcept:
m_pal(pal),
m_idx(idx),
m_colors([this] {
ox::Vector<Color16> colors;
colors.reserve(m_pal.pages.size());
for (auto const&p : m_pal.pages) {
colors.emplace_back(p.colors[m_idx]);
}
return colors;
}()) {}
int RemoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemoveColor);
}
ox::Error RemoveColorCommand::redo() noexcept {
m_colorInfo = std::move(m_pal.colorNames[m_idx]);
oxReturnError(m_pal.colorNames.erase(m_idx));
for (auto &page : m_pal.pages) {
oxReturnError(page.colors.erase(m_idx));
}
return {};
}
ox::Error RemoveColorCommand::undo() noexcept {
m_pal.colorNames.emplace(m_idx, std::move(m_colorInfo));
for (size_t p = 0; auto &page : m_pal.pages) {
page.colors.emplace(m_idx, m_colors[p]);
++p;
}
return {};
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class RemoveColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx = 0;
ox::String m_colorInfo;
ox::Vector<Color16> const m_colors;
public:
RemoveColorCommand(Palette &pal, size_t idx) noexcept;
~RemoveColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
};
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "removepagecommand.hpp"
namespace nostalgia::core {
RemovePageCommand::RemovePageCommand(Palette &pal, size_t idx) noexcept:
m_pal(pal),
m_idx(idx) {}
int RemovePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemovePage);
}
ox::Error RemovePageCommand::redo() noexcept {
m_page = std::move(m_pal.pages[m_idx]);
return m_pal.pages.erase(m_idx).error;
}
ox::Error RemovePageCommand::undo() noexcept {
m_pal.pages.emplace(m_idx, std::move(m_page));
return {};
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class RemovePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_idx = 0;
PalettePage m_page;
public:
RemovePageCommand(Palette &pal, size_t idx) noexcept;
~RemovePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "renamepagecommand.hpp"
namespace nostalgia::core {
RenamePageCommand::RenamePageCommand(Palette &pal, size_t const page, ox::StringParam name) noexcept:
m_pal(pal),
m_page(page),
m_name{std::move(name)} {}
int RenamePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RenamePage);
}
ox::Error RenamePageCommand::redo() noexcept {
std::swap(m_pal.pages[m_page].name, m_name);
return {};
}
ox::Error RenamePageCommand::undo() noexcept {
std::swap(m_pal.pages[m_page].name, m_name);
return {};
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
#include "commands.hpp"
namespace nostalgia::core {
class RenamePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_page = 0;
ox::String m_name;
public:
RenamePageCommand(Palette &pal, size_t page, ox::StringParam name) noexcept;
~RenamePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "updatecolorcommand.hpp"
namespace nostalgia::core {
UpdateColorCommand::UpdateColorCommand(
Palette &pal,
size_t page,
size_t idx,
Color16 newColor):
m_pal(pal),
m_page(page),
m_idx(idx),
m_altColor(newColor) {
if (color(m_pal, m_page, m_idx) == newColor) {
throw studio::NoChangesException();
}
}
bool UpdateColorCommand::mergeWith(UndoCommand &cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
return false;
}
auto ucCmd = dynamic_cast<UpdateColorCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}
return true;
}
int UpdateColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColor);
}
ox::Error UpdateColorCommand::redo() noexcept {
swap();
return {};
}
ox::Error UpdateColorCommand::undo() noexcept {
swap();
return {};
}
void UpdateColorCommand::swap() noexcept {
auto &dst = colors(m_pal, m_page)[m_idx];
std::swap(dst, m_altColor);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class UpdateColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page = 0;
size_t const m_idx{};
PaletteColor m_altColor{};
public:
UpdateColorCommand(
Palette &pal,
size_t page,
size_t idx,
Color16 newColor);
~UpdateColorCommand() noexcept override = default;
[[nodiscard]]
bool mergeWith(UndoCommand &cmd) noexcept final;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
private:
void swap() noexcept;
};
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "commands.hpp"
#include "updatecolorinfocommand.hpp"
namespace nostalgia::core {
UpdateColorInfoCommand::UpdateColorInfoCommand(
Palette &pal,
size_t idx,
ox::StringParam newColorInfo):
m_pal(pal),
m_idx(idx),
m_altColorInfo(std::move(newColorInfo)) {
if (m_pal.colorNames[m_idx] == m_altColorInfo) {
throw studio::NoChangesException();
}
}
bool UpdateColorInfoCommand::mergeWith(UndoCommand &cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColorInfo)) {
return false;
}
auto ucCmd = dynamic_cast<UpdateColorInfoCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}
m_pal.colorNames[m_idx] = std::move(ucCmd->m_pal.colorNames[m_idx]);
setObsolete(m_altColorInfo == m_pal.colorNames[m_idx]);
return true;
}
int UpdateColorInfoCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColorInfo);
}
ox::Error UpdateColorInfoCommand::redo() noexcept {
swap();
return {};
}
ox::Error UpdateColorInfoCommand::undo() noexcept {
swap();
return {};
}
void UpdateColorInfoCommand::swap() noexcept {
std::swap(m_pal.colorNames[m_idx], m_altColorInfo);
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
class UpdateColorInfoCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx{};
ox::String m_altColorInfo;
public:
UpdateColorInfoCommand(
Palette &pal,
size_t idx,
ox::StringParam newColorInfo);
~UpdateColorInfoCommand() noexcept override = default;
[[nodiscard]]
bool mergeWith(UndoCommand &cmd) noexcept final;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
private:
void swap() noexcept;
};
}

View File

@ -6,9 +6,16 @@
#include <keel/media.hpp> #include <keel/media.hpp>
#include <nostalgia/core/gfx.hpp> #include "commands/addcolorcommand.hpp"
#include "commands/applycolorallpagescommand.hpp"
#include "commands/duplicatepagecommand.hpp"
#include "commands/movecolorcommand.hpp"
#include "commands/removecolorcommand.hpp"
#include "commands/removepagecommand.hpp"
#include "commands/renamepagecommand.hpp"
#include "commands/updatecolorcommand.hpp"
#include "commands/updatecolorinfocommand.hpp"
#include "paletteeditor.hpp"
#include "paletteeditor-imgui.hpp" #include "paletteeditor-imgui.hpp"
namespace nostalgia::core { namespace nostalgia::core {
@ -16,7 +23,7 @@ namespace nostalgia::core {
namespace ig = studio::ig; namespace ig = studio::ig;
void PaletteEditorImGui::PageRename::draw(turbine::Context &tctx) noexcept { void PaletteEditorImGui::PageRenameDialog::draw(turbine::Context &tctx) noexcept {
if (!m_show) { if (!m_show) {
return; return;
} }
@ -42,7 +49,7 @@ PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::StringPa
m_tctx(sctx.tctx), m_tctx(sctx.tctx),
m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), itemPath()).unwrapThrow()) { m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), itemPath()).unwrapThrow()) {
undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand); undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand);
m_pageRename.inputSubmitted.connect(this, &PaletteEditorImGui::renamePage); m_pageRenameDlg.inputSubmitted.connect(this, &PaletteEditorImGui::renamePage);
} }
void PaletteEditorImGui::draw(studio::StudioContext&) noexcept { void PaletteEditorImGui::draw(studio::StudioContext&) noexcept {
@ -58,7 +65,7 @@ void PaletteEditorImGui::draw(studio::StudioContext&) noexcept {
drawColorsEditor(); drawColorsEditor();
ImGui::EndChild(); ImGui::EndChild();
} }
m_pageRename.draw(m_tctx); m_pageRenameDlg.draw(m_tctx);
} }
ox::Error PaletteEditorImGui::saveItem() noexcept { ox::Error PaletteEditorImGui::saveItem() noexcept {
@ -203,7 +210,7 @@ void PaletteEditorImGui::drawPagesEditor() noexcept {
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Rename", btnSz)) { if (ImGui::Button("Rename", btnSz)) {
m_pageRename.show(m_pal.pages[m_page].name); m_pageRenameDlg.show(m_pal.pages[m_page].name);
} }
ImGui::BeginTable( ImGui::BeginTable(
"PageSelect", "PageSelect",
@ -245,7 +252,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
std::ignore = pushCommand<ApplyColorAllPagesCommand>( std::ignore = pushCommand<ApplyColorAllPagesCommand>(
m_pal, m_page, m_selectedColorRow); m_pal, m_page, m_selectedColorRow);
} }
if (!inputFocused && !m_pageRename.isOpen()) { if (!inputFocused && !m_pageRenameDlg.isOpen()) {
if (!ImGui::IsKeyDown(ImGuiKey_ModAlt)) { if (!ImGui::IsKeyDown(ImGuiKey_ModAlt)) {
numShortcuts(m_selectedColorRow, largestPage(m_pal)); numShortcuts(m_selectedColorRow, largestPage(m_pal));
} else { } else {

View File

@ -14,7 +14,7 @@ namespace nostalgia::core {
class PaletteEditorImGui: public studio::Editor { class PaletteEditorImGui: public studio::Editor {
private: private:
class PageRename { class PageRenameDialog {
private: private:
ox::IString<50> m_name; ox::IString<50> m_name;
bool m_show = false; bool m_show = false;
@ -30,7 +30,7 @@ class PaletteEditorImGui: public studio::Editor {
[[nodiscard]] [[nodiscard]]
constexpr bool isOpen() const noexcept { return m_show; } constexpr bool isOpen() const noexcept { return m_show; }
void draw(turbine::Context &tctx) noexcept; void draw(turbine::Context &tctx) noexcept;
} m_pageRename; } m_pageRenameDlg;
studio::StudioContext &m_sctx; studio::StudioContext &m_sctx;
turbine::Context &m_tctx; turbine::Context &m_tctx;
Palette m_pal; Palette m_pal;

View File

@ -1,295 +0,0 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "paletteeditor.hpp"
namespace nostalgia::core {
ApplyColorAllPagesCommand::ApplyColorAllPagesCommand(Palette &pal, size_t const page, size_t const idx):
m_pal(pal),
m_page(page),
m_idx(idx),
m_origColors([this] {
ox::Vector<Color16> colors;
colors.reserve(m_pal.pages.size());
for (auto const&p : m_pal.pages) {
colors.emplace_back(p.colors[m_idx]);
}
return colors;
}()) {
auto const c = color(m_pal, m_page, m_idx);
if (ox::all_of(m_pal.pages.begin(), m_pal.pages.end(), [this, c](PalettePage const&page) {
return page.colors[m_idx] == c;
})) {
throw studio::NoChangesException();
}
}
int ApplyColorAllPagesCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::ApplyColorAllPages);
}
ox::Error ApplyColorAllPagesCommand::redo() noexcept {
auto const c = color(m_pal, m_page, m_idx);
for (auto &page : m_pal.pages) {
page.colors[m_idx] = c;
}
return {};
}
ox::Error ApplyColorAllPagesCommand::undo() noexcept {
for (size_t p = 0u; auto &page : m_pal.pages) {
page.colors[m_idx] = m_origColors[p];
++p;
}
return {};
}
RenamePageCommand::RenamePageCommand(Palette &pal, size_t const page, ox::StringParam name) noexcept:
m_pal(pal),
m_page(page),
m_name{std::move(name)} {}
int RenamePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RenamePage);
}
ox::Error RenamePageCommand::redo() noexcept {
std::swap(m_pal.pages[m_page].name, m_name);
return {};
}
ox::Error RenamePageCommand::undo() noexcept {
std::swap(m_pal.pages[m_page].name, m_name);
return {};
}
DuplicatePageCommand::DuplicatePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept:
m_pal(pal),
m_dstIdx(dstIdx) {
auto const&src = m_pal.pages[srcIdx];
m_page.reserve(src.colors.size());
for (auto const&s : src.colors) {
m_page.emplace_back(s);
}
}
int DuplicatePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::DuplicatePage);
}
ox::Error DuplicatePageCommand::redo() noexcept {
m_pal.pages.emplace(m_dstIdx, "", std::move(m_page));
return {};
}
ox::Error DuplicatePageCommand::undo() noexcept {
m_page = std::move(m_pal.pages[m_dstIdx].colors);
return m_pal.pages.erase(m_dstIdx).error;
}
size_t DuplicatePageCommand::insertIdx() const noexcept {
return m_dstIdx;
}
RemovePageCommand::RemovePageCommand(Palette &pal, size_t idx) noexcept:
m_pal(pal),
m_idx(idx) {}
int RemovePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemovePage);
}
ox::Error RemovePageCommand::redo() noexcept {
m_page = std::move(m_pal.pages[m_idx]);
return m_pal.pages.erase(m_idx).error;
}
ox::Error RemovePageCommand::undo() noexcept {
m_pal.pages.emplace(m_idx, std::move(m_page));
return {};
}
AddColorCommand::AddColorCommand(Palette &pal, Color16 const color, size_t const idx) noexcept:
m_pal(pal),
m_color(color),
m_idx(idx) {}
int AddColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddColor);
}
ox::Error AddColorCommand::redo() noexcept {
m_pal.colorNames.emplace(m_idx, ox::sfmt("Color {}", m_pal.colorNames.size() + 1));
for (auto &page : m_pal.pages) {
page.colors.emplace(m_idx, m_color);
}
return {};
}
ox::Error AddColorCommand::undo() noexcept {
oxReturnError(m_pal.colorNames.erase(m_idx));
for (auto &page : m_pal.pages) {
oxReturnError(page.colors.erase(m_idx));
}
return {};
}
RemoveColorCommand::RemoveColorCommand(Palette &pal, size_t const idx) noexcept:
m_pal(pal),
m_idx(idx),
m_colors([this] {
ox::Vector<Color16> colors;
colors.reserve(m_pal.pages.size());
for (auto const&p : m_pal.pages) {
colors.emplace_back(p.colors[m_idx]);
}
return colors;
}()) {}
int RemoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemoveColor);
}
ox::Error RemoveColorCommand::redo() noexcept {
m_colorInfo = std::move(m_pal.colorNames[m_idx]);
oxReturnError(m_pal.colorNames.erase(m_idx));
for (auto &page : m_pal.pages) {
oxReturnError(page.colors.erase(m_idx));
}
return {};
}
ox::Error RemoveColorCommand::undo() noexcept {
m_pal.colorNames.emplace(m_idx, std::move(m_colorInfo));
for (size_t p = 0; auto &page : m_pal.pages) {
page.colors.emplace(m_idx, m_colors[p]);
++p;
}
return {};
}
UpdateColorInfoCommand::UpdateColorInfoCommand(
Palette &pal,
size_t idx,
ox::StringParam newColorInfo):
m_pal(pal),
m_idx(idx),
m_altColorInfo(std::move(newColorInfo)) {
if (m_pal.colorNames[m_idx] == m_altColorInfo) {
throw studio::NoChangesException();
}
}
bool UpdateColorInfoCommand::mergeWith(UndoCommand &cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColorInfo)) {
return false;
}
auto ucCmd = dynamic_cast<UpdateColorInfoCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}
m_pal.colorNames[m_idx] = std::move(ucCmd->m_pal.colorNames[m_idx]);
setObsolete(m_altColorInfo == m_pal.colorNames[m_idx]);
return true;
}
int UpdateColorInfoCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColorInfo);
}
ox::Error UpdateColorInfoCommand::redo() noexcept {
swap();
return {};
}
ox::Error UpdateColorInfoCommand::undo() noexcept {
swap();
return {};
}
void UpdateColorInfoCommand::swap() noexcept {
std::swap(m_pal.colorNames[m_idx], m_altColorInfo);
}
UpdateColorCommand::UpdateColorCommand(
Palette &pal,
size_t page,
size_t idx,
Color16 newColor):
m_pal(pal),
m_page(page),
m_idx(idx),
m_altColor(newColor) {
if (color(m_pal, m_page, m_idx) == newColor) {
throw studio::NoChangesException();
}
}
bool UpdateColorCommand::mergeWith(UndoCommand &cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
return false;
}
auto ucCmd = dynamic_cast<UpdateColorCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}
return true;
}
int UpdateColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColor);
}
ox::Error UpdateColorCommand::redo() noexcept {
swap();
return {};
}
ox::Error UpdateColorCommand::undo() noexcept {
swap();
return {};
}
void UpdateColorCommand::swap() noexcept {
auto &dst = colors(m_pal, m_page)[m_idx];
std::swap(dst, m_altColor);
}
MoveColorCommand::MoveColorCommand(
Palette &pal, size_t page, size_t srcIdx, size_t dstIdx) noexcept:
m_pal(pal),
m_page(page),
m_srcIdx(srcIdx),
m_dstIdx(dstIdx) {}
int MoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::MoveColor);
}
ox::Error MoveColorCommand::redo() noexcept {
moveColor(m_srcIdx, m_dstIdx);
return {};
}
ox::Error MoveColorCommand::undo() noexcept {
moveColor(m_dstIdx, m_srcIdx);
return {};
}
void MoveColorCommand::moveColor(size_t srcIdx, size_t dstIdx) noexcept {
auto const c = color(m_pal, m_page, srcIdx);
std::ignore = colors(m_pal, m_page).erase(srcIdx);
colors(m_pal, m_page).emplace(dstIdx, c);
}
}

View File

@ -1,234 +0,0 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <studio/studio.hpp>
#include <nostalgia/core/color.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core {
enum class PaletteEditorCommandId {
ApplyColorAllPages,
RenamePage,
DuplicatePage,
RemovePage,
AddColor,
RemoveColor,
UpdateColorInfo,
UpdateColor,
MoveColor,
};
class ApplyColorAllPagesCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page{};
size_t const m_idx{};
ox::Vector<Color16> const m_origColors;
public:
ApplyColorAllPagesCommand(Palette &pal, size_t page, size_t idx);
~ApplyColorAllPagesCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
class RenamePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_page = 0;
ox::String m_name;
public:
RenamePageCommand(Palette &pal, size_t page, ox::StringParam name) noexcept;
~RenamePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
class DuplicatePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_dstIdx = 0;
ox::Vector<PaletteColor> m_page;
public:
DuplicatePageCommand(Palette &pal, size_t srcIdx, size_t dstIdx) noexcept;
~DuplicatePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
[[nodiscard]]
size_t insertIdx() const noexcept;
};
class RemovePageCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t m_idx = 0;
PalettePage m_page;
public:
RemovePageCommand(Palette &pal, size_t idx) noexcept;
~RemovePageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
class AddColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
Color16 m_color = 0;
size_t const m_idx = 0;
public:
AddColorCommand(Palette &pal, Color16 color, size_t idx) noexcept;
~AddColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
};
class RemoveColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx = 0;
ox::String m_colorInfo;
ox::Vector<Color16> const m_colors;
public:
RemoveColorCommand(Palette &pal, size_t idx) noexcept;
~RemoveColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
};
class UpdateColorInfoCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx{};
ox::String m_altColorInfo;
public:
UpdateColorInfoCommand(
Palette &pal,
size_t idx,
ox::StringParam newColorInfo);
~UpdateColorInfoCommand() noexcept override = default;
[[nodiscard]]
bool mergeWith(UndoCommand &cmd) noexcept final;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
private:
void swap() noexcept;
};
class UpdateColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page = 0;
size_t const m_idx{};
PaletteColor m_altColor{};
public:
UpdateColorCommand(
Palette &pal,
size_t page,
size_t idx,
Color16 newColor);
~UpdateColorCommand() noexcept override = default;
[[nodiscard]]
bool mergeWith(UndoCommand &cmd) noexcept final;
[[nodiscard]]
int commandId() const noexcept final;
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
private:
void swap() noexcept;
};
class MoveColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_page = 0;
std::size_t const m_srcIdx = 0;
std::size_t const m_dstIdx = 0;
public:
MoveColorCommand(Palette &pal, size_t page, size_t srcIdx, size_t dstIdx) noexcept;
~MoveColorCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept override;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
private:
void moveColor(size_t srcIdx, size_t dstIdx) noexcept;
};
}

View File

@ -94,7 +94,6 @@ TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::Stri
m_model(m_view.model()) { m_model(m_view.model()) {
std::ignore = setPaletteSelection(); std::ignore = setPaletteSelection();
// connect signal/slots // connect signal/slots
undoStack()->changeTriggered.connect(this, &TileSheetEditorImGui::markUnsavedChanges);
m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet); m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet);
m_exportMenu.inputSubmitted.connect(this, &TileSheetEditorImGui::exportSubhseetToPng); m_exportMenu.inputSubmitted.connect(this, &TileSheetEditorImGui::exportSubhseetToPng);
m_model.paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection); m_model.paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection);
@ -196,7 +195,7 @@ void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept {
} }
} }
auto const paneSize = ImGui::GetContentRegionAvail(); auto const paneSize = ImGui::GetContentRegionAvail();
auto const tileSheetParentSize = ImVec2{paneSize.x - m_palViewWidth, paneSize.y}; auto const tileSheetParentSize = ImVec2{paneSize.x - s_palViewWidth, paneSize.y};
auto const fbSize = ox::Vec2{tileSheetParentSize.x - 16, tileSheetParentSize.y - 16}; auto const fbSize = ox::Vec2{tileSheetParentSize.x - 16, tileSheetParentSize.y - 16};
ImGui::BeginChild("TileSheetView", tileSheetParentSize, true); ImGui::BeginChild("TileSheetView", tileSheetParentSize, true);
{ {
@ -204,10 +203,10 @@ void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept {
} }
ImGui::EndChild(); ImGui::EndChild();
ImGui::SameLine(); ImGui::SameLine();
ImGui::BeginChild("Controls", {m_palViewWidth - 8, paneSize.y}, true); ImGui::BeginChild("Controls", {s_palViewWidth - 8, paneSize.y}, true);
{ {
auto const controlsSize = ImGui::GetContentRegionAvail(); auto const controlsSize = ImGui::GetContentRegionAvail();
ImGui::BeginChild("ToolBox", {m_palViewWidth - 24, 30}, true); ImGui::BeginChild("ToolBox", {s_palViewWidth - 24, 30}, true);
{ {
auto const btnSz = ImVec2{45, 14}; auto const btnSz = ImVec2{45, 14};
if (ImGui::Selectable("Select", m_tool == TileSheetTool::Select, 0, btnSz)) { if (ImGui::Selectable("Select", m_tool == TileSheetTool::Select, 0, btnSz)) {
@ -227,12 +226,12 @@ void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept {
ImGui::EndChild(); ImGui::EndChild();
auto const ySize = controlsSize.y - 38; auto const ySize = controlsSize.y - 38;
// draw palette/color picker // draw palette/color picker
ImGui::BeginChild("Palette", {m_palViewWidth - 24, ySize / 2.f}, true); ImGui::BeginChild("Palette", {s_palViewWidth - 24, ySize / 2.f}, true);
{ {
drawPaletteSelector(); drawPaletteMenu();
} }
ImGui::EndChild(); ImGui::EndChild();
ImGui::BeginChild("SubSheets", {m_palViewWidth - 24, ySize / 2.f}, true); ImGui::BeginChild("SubSheets", {s_palViewWidth - 24, ySize / 2.f}, true);
{ {
static constexpr auto btnHeight = ig::BtnSz.y; static constexpr auto btnHeight = ig::BtnSz.y;
auto const btnSize = ImVec2{btnHeight, btnHeight}; auto const btnSize = ImVec2{btnHeight, btnHeight};
@ -434,7 +433,7 @@ void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const&fbSize) noexcept {
} }
} }
void TileSheetEditorImGui::drawPaletteSelector() noexcept { void TileSheetEditorImGui::drawPaletteMenu() noexcept {
auto const&files = m_sctx.project->fileList(core::FileExt_npal); auto const&files = m_sctx.project->fileList(core::FileExt_npal);
auto const comboWidthSub = 62; auto const comboWidthSub = 62;
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub); ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - comboWidthSub);
@ -474,6 +473,7 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
auto const&pal = m_model.pal(); auto const&pal = m_model.pal();
if (pal.pages.size() > m_model.palettePage()) { if (pal.pages.size() > m_model.palettePage()) {
for (auto i = 0u; auto const&c: pal.pages[m_model.palettePage()].colors) { for (auto i = 0u; auto const&c: pal.pages[m_model.palettePage()].colors) {
ImGui::TableNextRow();
ImGui::PushID(static_cast<int>(i)); ImGui::PushID(static_cast<int>(i));
// Column: color idx // Column: color idx
ImGui::TableNextColumn(); ImGui::TableNextColumn();
@ -492,7 +492,6 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
ImGui::Text("%s", name); ImGui::Text("%s", name);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("(%02d, %02d, %02d)", red16(c), green16(c), blue16(c)); ImGui::Text("(%02d, %02d, %02d)", red16(c), green16(c), blue16(c));
ImGui::TableNextRow();
ImGui::PopID(); ImGui::PopID();
++i; ++i;
} }
@ -527,9 +526,12 @@ void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexce
}); });
} }
ox::Error TileSheetEditorImGui::markUnsavedChanges(studio::UndoCommand const*) noexcept {
setUnsavedChanges(true); void TileSheetEditorImGui::SubSheetEditor::show(ox::StringViewCR name, int const cols, int const rows) noexcept {
return {}; m_show = true;
m_name = name;
m_cols = cols;
m_rows = rows;
} }
void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &tctx) noexcept { void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &tctx) noexcept {
@ -558,6 +560,12 @@ void TileSheetEditorImGui::SubSheetEditor::close() noexcept {
m_show = false; m_show = false;
} }
void TileSheetEditorImGui::ExportMenu::show() noexcept {
m_show = true;
m_scale = 5;
}
void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &tctx) noexcept { void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &tctx) noexcept {
constexpr auto popupName = "Export Tile Sheet"; constexpr auto popupName = "Export Tile Sheet";
if (!m_show) { if (!m_show) {

View File

@ -26,17 +26,12 @@ class TileSheetEditorImGui: public studio::Editor {
int m_rows = 0; int m_rows = 0;
bool m_show = false; bool m_show = false;
public: public:
ox::Signal<ox::Error(ox::StringView const&name, int cols, int rows)> inputSubmitted; ox::Signal<ox::Error(ox::StringViewCR name, int cols, int rows)> inputSubmitted;
constexpr void show(ox::StringView const&name, int cols, int rows) noexcept { void show(ox::StringViewCR name, int cols, int rows) noexcept;
m_show = true;
m_name = name;
m_cols = cols;
m_rows = rows;
}
void draw(turbine::Context &sctx) noexcept; void draw(turbine::Context &sctx) noexcept;
void close() noexcept; void close() noexcept;
[[nodiscard]] [[nodiscard]]
inline bool isOpen() const noexcept { return m_show; } constexpr bool isOpen() const noexcept { return m_show; }
}; };
class ExportMenu { class ExportMenu {
private: private:
@ -44,15 +39,13 @@ class TileSheetEditorImGui: public studio::Editor {
bool m_show = false; bool m_show = false;
public: public:
ox::Signal<ox::Error(int scale)> inputSubmitted; ox::Signal<ox::Error(int scale)> inputSubmitted;
constexpr void show() noexcept { void show() noexcept;
m_show = true;
m_scale = 5;
}
void draw(turbine::Context &sctx) noexcept; void draw(turbine::Context &sctx) noexcept;
void close() noexcept; void close() noexcept;
[[nodiscard]] [[nodiscard]]
inline bool isOpen() const noexcept { return m_show; } constexpr bool isOpen() const noexcept { return m_show; }
}; };
static constexpr float s_palViewWidth = 300;
std::size_t m_selectedPaletteIdx = 0; std::size_t m_selectedPaletteIdx = 0;
studio::StudioContext &m_sctx; studio::StudioContext &m_sctx;
turbine::Context &m_tctx; turbine::Context &m_tctx;
@ -62,7 +55,6 @@ class TileSheetEditorImGui: public studio::Editor {
glutils::FrameBuffer m_framebuffer; glutils::FrameBuffer m_framebuffer;
TileSheetEditorView m_view; TileSheetEditorView m_view;
TileSheetEditorModel &m_model; TileSheetEditorModel &m_model;
float m_palViewWidth = 300;
ox::Vec2 m_prevMouseDownPos; ox::Vec2 m_prevMouseDownPos;
TileSheetTool m_tool = TileSheetTool::Draw; TileSheetTool m_tool = TileSheetTool::Draw;
@ -101,7 +93,7 @@ class TileSheetEditorImGui: public studio::Editor {
void drawTileSheet(ox::Vec2 const&fbSize) noexcept; void drawTileSheet(ox::Vec2 const&fbSize) noexcept;
void drawPaletteSelector() noexcept; void drawPaletteMenu() noexcept;
ox::Error updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept; ox::Error updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept;
@ -109,8 +101,6 @@ class TileSheetEditorImGui: public studio::Editor {
// slots // slots
private: private:
ox::Error markUnsavedChanges(studio::UndoCommand const*) noexcept;
void setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept; void setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept;
}; };