Compare commits

..

No commits in common. "bfd4bc3c4164a7c618c0be4d542bb660005541c5" and "c021e5e7fbdaa21e4a527d4fb8d6cbfee7362fe3" have entirely different histories.

15 changed files with 171 additions and 500 deletions

View File

@ -112,12 +112,12 @@ int tileRows(Context&) noexcept;
ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
CompactPalette const&palette,
Palette const&palette,
size_t page = 0) noexcept;
ox::Error loadSpritePalette(
Context &ctx,
CompactPalette const&palette,
Palette const&palette,
size_t page = 0) noexcept;
ox::Error loadBgPalette(

View File

@ -36,43 +36,7 @@ struct PaletteV2 {
ox::Vector<ox::Vector<Color16>> pages;
};
struct PaletteV3 {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.Palette";
static constexpr auto TypeVersion = 3;
static constexpr auto Preloadable = true;
struct ColorInfo {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.Palette.ColorInfo";
static constexpr auto TypeVersion = 3;
ox::String name;
constexpr ColorInfo() noexcept = default;
constexpr explicit ColorInfo(ox::StringView pName) noexcept:
name(pName) {}
constexpr explicit ColorInfo(ox::String &&pName) noexcept:
name(std::move(pName)) {}
};
ox::Vector<ColorInfo> colorInfo;
ox::Vector<ox::Vector<Color16>> pages;
};
using Palette = PaletteV3;
struct CompactPaletteV1 {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.Palette";
static constexpr auto TypeVersion = 2;
static constexpr auto Preloadable = true;
ox::Vector<ox::Vector<Color16>> pages{};
};
using CompactPalette = CompactPaletteV1;
[[nodiscard]]
constexpr bool valid(Palette const&p) noexcept {
auto const colors = p.colorInfo.size();
return ox::all_of(p.pages.begin(), p.pages.end(), [colors](auto const&page) {
return page.size() == colors;
});
}
using Palette = PaletteV2;
[[nodiscard]]
constexpr Color16 color(Palette const&pal, size_t page, size_t idx) noexcept {
@ -82,54 +46,14 @@ constexpr Color16 color(Palette const&pal, size_t page, size_t idx) noexcept {
return 0;
}
[[nodiscard]]
constexpr Color16 color(CompactPalette const&pal, size_t page, size_t idx) noexcept {
if (page < pal.pages.size() && idx < pal.pages[page].size()) [[likely]] {
return pal.pages[page][idx];
}
return 0;
}
[[nodiscard]]
constexpr Color16 color(Palette const&pal, size_t idx) noexcept {
return color(pal, 0, idx);
auto constexpr page = 0;
return color(pal, page, idx);
}
[[nodiscard]]
constexpr Color16 color(CompactPalette const&pal, size_t idx) noexcept {
return color(pal, 0, idx);
}
[[nodiscard]]
constexpr auto &colors(Palette &pal, size_t page = 0) noexcept {
return pal.pages[page];
}
[[nodiscard]]
constexpr auto &colors(CompactPalette &pal, size_t page = 0) noexcept {
return pal.pages[page];
}
[[nodiscard]]
constexpr auto &colors(Palette const&pal, size_t page = 0) noexcept {
return pal.pages[page];
}
[[nodiscard]]
constexpr auto &colors(CompactPalette const&pal, size_t page = 0) noexcept {
return pal.pages[page];
}
[[nodiscard]]
constexpr size_t colorCnt(Palette const&pal, size_t page = 0) noexcept {
if (page < pal.pages.size()) [[likely]] {
return pal.pages[page].size();
}
return 0;
}
[[nodiscard]]
constexpr size_t colorCnt(CompactPalette const&pal, size_t page = 0) noexcept {
constexpr size_t colors(Palette const&pal, size_t page = 0) noexcept {
if (page < pal.pages.size()) [[likely]] {
return pal.pages[page].size();
}
@ -145,15 +69,6 @@ constexpr size_t largestPage(Palette const&pal) noexcept {
return out;
}
[[nodiscard]]
constexpr size_t largestPage(CompactPalette const&pal) noexcept {
size_t out{};
for (auto const&page : pal.pages) {
out = ox::max(out, page.size());
}
return out;
}
oxModelBegin(NostalgiaPalette)
oxModelField(colors)
oxModelEnd()
@ -166,17 +81,4 @@ oxModelBegin(PaletteV2)
oxModelField(pages)
oxModelEnd()
oxModelBegin(PaletteV3::ColorInfo)
oxModelField(name)
oxModelEnd()
oxModelBegin(PaletteV3)
oxModelField(colorInfo)
oxModelField(pages)
oxModelEnd()
oxModelBegin(CompactPaletteV1)
oxModelField(pages)
oxModelEnd()
}

View File

@ -136,13 +136,13 @@ ox::Error initGfx(Context&, InitParams const&) noexcept {
ox::Error loadBgPalette(
Context&,
size_t palBank,
CompactPalette const&palette,
Palette const&palette,
size_t page) noexcept {
if (palette.pages.empty()) {
return {};
}
auto const paletteMem = MEM_BG_PALETTE + palBank * 16;
for (auto i = 0u; i < colorCnt(palette, page); ++i) {
for (auto i = 0u; i < colors(palette, page); ++i) {
paletteMem[i] = color(palette, page, i);
}
return {};
@ -150,13 +150,13 @@ ox::Error loadBgPalette(
ox::Error loadSpritePalette(
Context&,
CompactPalette const&palette,
Palette const&palette,
size_t page) noexcept {
if (palette.pages.empty()) {
return {};
}
auto const paletteMem = MEM_SPRITE_PALETTE;
for (auto i = 0u; i < colorCnt(palette, page); ++i) {
for (auto i = 0u; i < colors(palette, page); ++i) {
paletteMem[i] = color(palette, page, i);
}
return {};
@ -166,14 +166,14 @@ ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
ox::FileAddress const&paletteAddr) noexcept {
oxRequire(pal, keel::readObj<CompactPalette>(keelCtx(ctx), paletteAddr));
oxRequire(pal, keel::readObj<Palette>(keelCtx(ctx), paletteAddr));
return loadBgPalette(ctx, palBank, *pal, 0);
}
ox::Error loadSpritePalette(
Context &ctx,
ox::FileAddress const&paletteAddr) noexcept {
oxRequire(pal, keel::readObj<CompactPalette>(keelCtx(ctx), paletteAddr));
oxRequire(pal, keel::readObj<Palette>(keelCtx(ctx), paletteAddr));
return loadSpritePalette(ctx, *pal, 0);
}

View File

@ -18,8 +18,6 @@ static class: public keel::Module {
private:
NostalgiaPaletteToPaletteV1Converter m_nostalgiaPaletteToPaletteV1Converter;
PaletteV1ToPaletteV2Converter m_paletteV1ToPaletteV2Converter;
PaletteV2ToPaletteV3Converter m_paletteV2ToPaletteV3Converter;
PaletteToCompactPaletteConverter m_paletteToCompactPaletteConverter;
TileSheetV1ToTileSheetV2Converter m_tileSheetV1ToTileSheetV2Converter;
TileSheetV2ToTileSheetV3Converter m_tileSheetV2ToTileSheetV3Converter;
TileSheetV3ToTileSheetV4Converter m_tileSheetV3ToTileSheetV4Converter;
@ -49,8 +47,6 @@ static class: public keel::Module {
return {
&m_nostalgiaPaletteToPaletteV1Converter,
&m_paletteV1ToPaletteV2Converter,
&m_paletteV2ToPaletteV3Converter,
&m_paletteToCompactPaletteConverter,
&m_tileSheetV1ToTileSheetV2Converter,
&m_tileSheetV2ToTileSheetV3Converter,
&m_tileSheetV3ToTileSheetV4Converter,
@ -75,10 +71,8 @@ static class: public keel::Module {
},
[](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Result<bool> {
if (typeId == ox::ModelTypeId_v<NostalgiaPalette> ||
typeId == ox::ModelTypeId_v<PaletteV1> ||
typeId == ox::ModelTypeId_v<PaletteV2> ||
typeId == ox::ModelTypeId_v<PaletteV3>) {
oxReturnError(keel::convertBuffToBuff<CompactPalette>(
typeId == ox::ModelTypeId_v<PaletteV1>) {
oxReturnError(keel::convertBuffToBuff<Palette>(
ctx, buff, ox::ClawFormat::Metal).moveTo(buff));
return true;
}

View File

@ -22,27 +22,6 @@ ox::Error PaletteV1ToPaletteV2Converter::convert(
return {};
}
ox::Error PaletteV2ToPaletteV3Converter::convert(
keel::Context&,
PaletteV2 &src,
PaletteV3 &dst) const noexcept {
dst.pages = std::move(src.pages);
if (!dst.pages.empty()) {
for (size_t i = 0; i < dst.pages[0].size(); ++i) {
dst.colorInfo.emplace_back(ox::sfmt("Color {}", i));
}
}
return {};
}
ox::Error PaletteToCompactPaletteConverter::convert(
keel::Context&,
Palette &src,
CompactPalette &dst) const noexcept {
dst.pages = std::move(src.pages);
return {};
}
ox::Error TileSheetV1ToTileSheetV2Converter::convert(
keel::Context&,
TileSheetV1 &src,

View File

@ -24,14 +24,6 @@ class PaletteV1ToPaletteV2Converter: public keel::Converter<PaletteV1, PaletteV2
ox::Error convert(keel::Context&, PaletteV1 &src, PaletteV2 &dst) const noexcept final;
};
class PaletteV2ToPaletteV3Converter: public keel::Converter<PaletteV2, PaletteV3> {
ox::Error convert(keel::Context&, PaletteV2 &src, PaletteV3 &dst) const noexcept final;
};
class PaletteToCompactPaletteConverter: public keel::Converter<Palette, CompactPalette> {
ox::Error convert(keel::Context&, Palette &src, CompactPalette &dst) const noexcept final;
};
class TileSheetV1ToTileSheetV2Converter: public keel::Converter<TileSheetV1, TileSheetV2> {
ox::Error convert(keel::Context&, TileSheetV1 &src, TileSheetV2 &dst) const noexcept final;
};

View File

@ -364,7 +364,7 @@ static void loadPalette(
ox::Array<GLfloat, 1024> &palette,
size_t palOffset,
GLuint shaderPgrm,
CompactPalette const&pal,
Palette const&pal,
size_t page = 0) noexcept {
static constexpr std::size_t ColorCnt = 256;
for (auto i = palOffset; auto const c : pal.pages[page]) {
@ -516,7 +516,7 @@ static ox::Result<TileSheetData> normalizeTileSheet(
ox::Error loadBgPalette(
Context &ctx,
size_t palBank,
CompactPalette const&palette,
Palette const&palette,
size_t page) noexcept {
renderer::loadPalette(ctx.bgPalette, palBank * 16 * 4, ctx.bgShader, palette, page);
return {};
@ -524,7 +524,7 @@ ox::Error loadBgPalette(
ox::Error loadSpritePalette(
Context &ctx,
CompactPalette const&palette,
Palette const&palette,
size_t page) noexcept {
ox::Array<GLfloat, 1024> pal;
renderer::loadPalette(pal, 0, ctx.spriteShader, palette, page);
@ -536,7 +536,7 @@ ox::Error loadBgPalette(
size_t palBank,
ox::FileAddress const&paletteAddr) noexcept {
auto &kctx = keelCtx(ctx.turbineCtx);
oxRequire(palette, readObj<CompactPalette>(kctx, paletteAddr));
oxRequire(palette, readObj<Palette>(kctx, paletteAddr));
renderer::loadPalette(ctx.bgPalette, palBank * 16 * 4, ctx.bgShader, *palette);
return {};
}
@ -545,7 +545,7 @@ ox::Error loadSpritePalette(
Context &ctx,
ox::FileAddress const&paletteAddr) noexcept {
auto &kctx = keelCtx(ctx.turbineCtx);
oxRequire(palette, readObj<CompactPalette>(kctx, paletteAddr));
oxRequire(palette, readObj<Palette>(kctx, paletteAddr));
ox::Array<GLfloat, 1024> pal;
renderer::loadPalette(pal, 0, ctx.spriteShader, *palette);
return {};

View File

@ -15,16 +15,11 @@
namespace nostalgia::core {
namespace ig = studio::ig;
PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::StringView const path):
PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::CRStringView path):
Editor(path),
m_sctx(sctx),
m_tctx(sctx.tctx),
m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), itemPath()).unwrapThrow()) {
if (!valid(m_pal)) {
throw OxException(1, "PaletteEditorImGui: invalid Palette object");
}
m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), ox::FileAddress(itemPath())).unwrapThrow()) {
undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand);
}
@ -44,6 +39,7 @@ void PaletteEditorImGui::keyStateChanged(turbine::Key key, bool down) {
static_cast<uint32_t>(key - turbine::Key::Num_1 + 9), m_pal.pages.size() - 1);
}
}
}
void PaletteEditorImGui::draw(studio::StudioContext&) noexcept {
@ -62,7 +58,7 @@ void PaletteEditorImGui::draw(studio::StudioContext&) noexcept {
}
ox::Error PaletteEditorImGui::saveItem() noexcept {
return m_sctx.project->writeObj(itemPath(), m_pal, ox::ClawFormat::Organic);
return m_sctx.project->writeObj(itemPath(), m_pal);
}
void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept {
@ -75,39 +71,42 @@ void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept {
void PaletteEditorImGui::drawColorsEditor() noexcept {
constexpr auto tableFlags = ImGuiTableFlags_RowBg;
auto const colorsSz = ImGui::GetContentRegionAvail();
auto const colorEditor = m_selectedColorRow < colorCnt(m_pal, m_page);
auto const colorEditor = m_selectedColorRow < colors(m_pal, m_page);
auto const colorEditorWidth = 220;
static constexpr auto toolbarHeight = 40;
{
auto const sz = ImVec2(70, 24);
if (ImGui::Button("Add", sz)) {
auto const colorSz = colorCnt(m_pal, m_page);
auto const colorSz = static_cast<int>(colors(m_pal, m_page));
constexpr Color16 c = 0;
std::ignore = pushCommand<AddColorCommand>(m_pal, c, colorSz);
std::ignore = undoStack()->push(ox::make_unique<AddColorCommand>(&m_pal, c, m_page, colorSz));
}
ImGui::SameLine();
ImGui::BeginDisabled(m_selectedColorRow >= colorCnt(m_pal, m_page));
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page));
{
if (ImGui::Button("Remove", sz)) {
std::ignore = pushCommand<RemoveColorCommand>(m_pal, m_selectedColorRow);
m_selectedColorRow = ox::min(colorCnt(m_pal, m_page) - 1, m_selectedColorRow);
std::ignore = undoStack()->push(
ox::make_unique<RemoveColorCommand>(
&m_pal,
color(m_pal, m_page, static_cast<std::size_t>(m_selectedColorRow)),
m_page,
static_cast<int>(m_selectedColorRow)));
m_selectedColorRow = ox::min(colors(m_pal, m_page) - 1, m_selectedColorRow);
}
ImGui::SameLine();
ImGui::BeginDisabled(m_selectedColorRow <= 0);
{
if (ImGui::Button("Move Up", sz)) {
std::ignore = pushCommand<MoveColorCommand>(
m_pal, m_page, m_selectedColorRow, m_selectedColorRow - 1);
std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, -1));
--m_selectedColorRow;
}
}
ImGui::EndDisabled();
ImGui::SameLine();
ImGui::BeginDisabled(m_selectedColorRow >= colorCnt(m_pal, m_page) - 1);
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page) - 1);
{
if (ImGui::Button("Move Down", sz)) {
std::ignore = pushCommand<MoveColorCommand>(
m_pal, m_page, m_selectedColorRow, m_selectedColorRow + 1);
std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, 1));
++m_selectedColorRow;
}
}
@ -115,13 +114,8 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
}
ImGui::EndDisabled();
}
auto const tblWidth = (colorsSz.x - static_cast<float>(colorEditorWidth) - 8.f)
* static_cast<float>(colorEditor);
ImGui::BeginTable(
"Colors",
5,
tableFlags,
ImVec2(tblWidth, colorsSz.y - (toolbarHeight + 5)));
auto const tblWidth = (colorsSz.x - colorEditorWidth - 8) * colorEditor;
ImGui::BeginTable("Colors", 5, tableFlags, ImVec2(tblWidth, colorsSz.y - (toolbarHeight + 5)));
{
ImGui::TableSetupColumn("Idx", ImGuiTableColumnFlags_WidthFixed, 25);
ImGui::TableSetupColumn("Red", ImGuiTableColumnFlags_WidthFixed, 50);
@ -129,19 +123,17 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
ImGui::TableSetupColumn("Blue", ImGuiTableColumnFlags_WidthFixed, 50);
ImGui::TableSetupColumn("Color Preview", ImGuiTableColumnFlags_NoHide);
ImGui::TableHeadersRow();
for (auto i = 0u; auto const&c : m_pal.pages[m_page]) {
for (auto i = 0u; auto const c : m_pal.pages[m_page]) {
ImGui::PushID(static_cast<int>(i));
ImGui::TableNextRow();
drawColumn(i + 1);
drawColumn(i);
drawColumn(red16(c));
drawColumn(green16(c));
drawColumn(blue16(c));
ImGui::TableNextColumn();
auto const ic = ImGui::GetColorU32(
ImVec4(redf(c), greenf(c), bluef(c), 1));
auto const ic = ImGui::GetColorU32(ImVec4(redf(c), greenf(c), bluef(c), 1));
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ic);
if (ImGui::Selectable(
"##ColorRow", i == m_selectedColorRow, ImGuiSelectableFlags_SpanAllColumns)) {
if (ImGui::Selectable("##ColorRow", i == m_selectedColorRow, ImGuiSelectableFlags_SpanAllColumns)) {
m_selectedColorRow = i;
}
ImGui::PopID();
@ -163,30 +155,28 @@ void PaletteEditorImGui::drawPagesEditor() noexcept {
constexpr auto toolbarHeight = 40;
auto const btnSz = ImVec2(paneSz.x / 3 - 5.5f, 24);
if (ImGui::Button("Add", btnSz)) {
std::ignore = pushCommand<DuplicatePageCommand>(m_pal, 0u, m_pal.pages.size());
std::ignore = undoStack()->push(ox::make_unique<AddPageCommand>(m_pal));
m_page = m_pal.pages.size() - 1;
}
ImGui::SameLine();
if (ImGui::Button("Remove", btnSz)) {
std::ignore = pushCommand<RemovePageCommand>(m_pal, m_page);
std::ignore = undoStack()->push(ox::make_unique<RemovePageCommand>(m_pal, m_page));
m_page = std::min(m_page, m_pal.pages.size() - 1);
}
ImGui::SameLine();
if (ImGui::Button("Duplicate", btnSz)) {
std::ignore = pushCommand<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size());
std::ignore = undoStack()->push(ox::make_unique<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size()));
}
ImGui::BeginTable(
"PageSelect",
2,
tableFlags,
ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5)));
ImGui::BeginTable("PageSelect", 2, tableFlags, ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5)));
{
ImGui::TableSetupColumn("Page", ImGuiTableColumnFlags_WidthFixed, 60);
ImGui::TableSetupColumn("Colors", ImGuiTableColumnFlags_NoHide);
ImGui::TableHeadersRow();
for (auto i = 0u; i < m_pal.pages.size(); ++i) {
ImGui::PushID(static_cast<int>(i));
ImGui::TableNextRow();
drawColumn(i + 1);
drawColumn(colors(m_pal, i));
ImGui::SameLine();
if (ImGui::Selectable("##PageRow", i == m_page, ImGuiSelectableFlags_SpanAllColumns)) {
m_page = i;
@ -203,33 +193,20 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
int g = green16(c);
int b = blue16(c);
int const a = alpha16(c);
auto const&currentName = m_pal.colorInfo[m_selectedColorRow].name;
ox::IString<50> name;
name = currentName;
ImGui::InputText("Name", name.data(), name.cap() + 1);
ImGui::Separator();
ImGui::InputInt("Red", &r, 1, 5);
ImGui::InputInt("Green", &g, 1, 5);
ImGui::InputInt("Blue", &b, 1, 5);
if (ig::PushButton("Apply to all pages", {-1, ig::BtnSz.y})) {
std::ignore = pushCommand<ApplyColorAllPagesCommand>(
m_pal, m_page, m_selectedColorRow);
}
r = ox::max(r, 0);
g = ox::max(g, 0);
b = ox::max(b, 0);
auto const newColor = color16(r, g, b, a);
if (c != newColor) {
std::ignore = pushCommand<UpdateColorCommand>(m_pal, m_page, m_selectedColorRow, newColor);
}
if (currentName != name.data()) {
std::ignore = pushCommand<UpdateColorInfoCommand>(
m_pal, m_selectedColorRow, Palette::ColorInfo{name.data()});
std::ignore = undoStack()->push(ox::make_unique<UpdateColorCommand>(
&m_pal, m_page, static_cast<int>(m_selectedColorRow), c, newColor));
}
}
ox::Error PaletteEditorImGui::handleCommand(studio::UndoCommand const*cmd) noexcept {
if (dynamic_cast<RemovePageCommand const*>(cmd)) {
if (dynamic_cast<AddPageCommand const*>(cmd)) {
m_page = m_pal.pages.size() - 1;
} else if (dynamic_cast<RemovePageCommand const*>(cmd)) {
m_page = ox::min(m_page, m_pal.pages.size() - 1);
} else if (auto const dupPageCmd = dynamic_cast<DuplicatePageCommand const*>(cmd)) {
m_page = ox::clamp<size_t>(dupPageCmd->insertIdx(), 0, m_pal.pages.size() - 1);

View File

@ -21,7 +21,7 @@ class PaletteEditorImGui: public studio::Editor {
size_t m_page = 0;
public:
PaletteEditorImGui(studio::StudioContext &sctx, ox::StringView path);
PaletteEditorImGui(studio::StudioContext &sctx, ox::CRStringView path);
void keyStateChanged(turbine::Key key, bool down) override;

View File

@ -6,44 +6,21 @@
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[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](ox::SpanView<Color16> const page) {
return page[m_idx] == c;
})) {
throw studio::NoChangesException();
}
AddPageCommand::AddPageCommand(Palette &pal) noexcept:
m_pal(pal) {
}
int ApplyColorAllPagesCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::ApplyColorAllPages);
int AddPageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddPage);
}
ox::Error ApplyColorAllPagesCommand::redo() noexcept {
auto const c = color(m_pal, m_page, m_idx);
for (auto &page : m_pal.pages) {
page[m_idx] = c;
}
ox::Error AddPageCommand::redo() noexcept {
m_pal.pages.emplace_back();
return {};
}
ox::Error ApplyColorAllPagesCommand::undo() noexcept {
for (size_t p = 0u; auto &page : m_pal.pages) {
page[m_idx] = m_origColors[p];
++p;
}
return {};
ox::Error AddPageCommand::undo() noexcept {
return m_pal.pages.erase(static_cast<std::size_t>(m_pal.pages.size() - 1)).error;
}
@ -78,134 +55,78 @@ size_t DuplicatePageCommand::insertIdx() const noexcept {
RemovePageCommand::RemovePageCommand(Palette &pal, size_t idx) noexcept:
m_pal(pal),
m_idx(idx) {}
m_idx(idx) {
}
int RemovePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemovePage);
}
ox::Error RemovePageCommand::redo() noexcept {
m_page = std::move(colors(m_pal, m_idx));
m_page = std::move(m_pal.pages[m_idx]);
return m_pal.pages.erase(static_cast<std::size_t>(m_idx)).error;
}
ox::Error RemovePageCommand::undo() noexcept {
m_pal.pages.emplace(m_idx, std::move(m_page));
m_pal.pages.insert(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) {}
AddColorCommand::AddColorCommand(Palette *pal, Color16 color, size_t page, int idx) noexcept {
m_pal = pal;
m_color = color;
m_page = page;
m_idx = idx;
}
int AddColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddColor);
}
ox::Error AddColorCommand::redo() noexcept {
for (auto &page : m_pal.pages) {
page.emplace(static_cast<size_t>(m_idx), m_color);
}
m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color);
return {};
}
ox::Error AddColorCommand::undo() noexcept {
for (auto &page : m_pal.pages) {
oxReturnError(page.erase(static_cast<std::size_t>(m_idx)));
}
return {};
return m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)).error;
}
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[m_idx]);
RemoveColorCommand::RemoveColorCommand(Palette *pal, Color16 color, size_t page, int idx) noexcept {
m_pal = pal;
m_color = color;
m_page = page;
m_idx = idx;
}
return colors;
}()) {}
int RemoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemoveColor);
}
ox::Error RemoveColorCommand::redo() noexcept {
for (auto &page : m_pal.pages) {
oxReturnError(page.erase(m_idx));
}
return {};
return m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)).error;
}
ox::Error RemoveColorCommand::undo() noexcept {
for (size_t p = 0; auto &page : m_pal.pages) {
page.emplace(m_idx, m_colors[p]);
++p;
}
m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color);
return {};
}
UpdateColorInfoCommand::UpdateColorInfoCommand(
Palette &pal,
size_t idx,
Palette::ColorInfo newColorInfo):
m_pal(pal),
m_idx(idx),
m_altColorInfo(std::move(newColorInfo)) {
if (m_pal.colorInfo[m_idx].name == m_altColorInfo.name) {
throw studio::NoChangesException();
}
}
bool UpdateColorInfoCommand::mergeWith(UndoCommand const&cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
return false;
}
auto ucCmd = static_cast<UpdateColorInfoCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}
return true;
}
[[nodiscard]]
int UpdateColorInfoCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColor);
}
ox::Error UpdateColorInfoCommand::redo() noexcept {
swap();
return {};
}
ox::Error UpdateColorInfoCommand::undo() noexcept {
swap();
return {};
}
void UpdateColorInfoCommand::swap() noexcept {
std::swap(m_pal.colorInfo[m_idx], m_altColorInfo);
}
UpdateColorCommand::UpdateColorCommand(
Palette &pal,
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();
}
int idx,
Color16 oldColor,
Color16 newColor) noexcept {
m_pal = pal;
m_page = page;
m_idx = idx;
m_oldColor = oldColor;
m_newColor = newColor;
//setObsolete(m_oldColor == m_newColor);
}
bool UpdateColorCommand::mergeWith(UndoCommand const&cmd) noexcept {
@ -216,6 +137,7 @@ bool UpdateColorCommand::mergeWith(UndoCommand const&cmd) noexcept {
if (m_idx != ucCmd->m_idx) {
return false;
}
m_newColor = ucCmd->m_newColor;
return true;
}
@ -225,46 +147,41 @@ int UpdateColorCommand::commandId() const noexcept {
}
ox::Error UpdateColorCommand::redo() noexcept {
swap();
m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_newColor;
return {};
}
ox::Error UpdateColorCommand::undo() noexcept {
swap();
m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_oldColor;
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, std::size_t idx, int offset) noexcept {
m_pal = pal;
m_page = page;
m_idx = idx;
m_offset = offset;
}
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);
moveColor(static_cast<int>(m_idx), m_offset);
return {};
}
ox::Error MoveColorCommand::undo() noexcept {
moveColor(m_dstIdx, m_srcIdx);
moveColor(static_cast<int>(m_idx) + m_offset, -m_offset);
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);
void MoveColorCommand::moveColor(int idx, int offset) noexcept {
const auto c = m_pal->pages[m_page][static_cast<std::size_t>(idx)];
std::ignore = m_pal->pages[m_page].erase(static_cast<std::size_t>(idx));
m_pal->pages[m_page].insert(static_cast<std::size_t>(idx + offset), c);
}
}

View File

@ -13,28 +13,24 @@
namespace nostalgia::core {
enum class PaletteEditorCommandId {
ApplyColorAllPages,
AddPage,
DuplicatePage,
RemovePage,
AddColor,
RemoveColor,
UpdateColorInfo,
UpdateColor,
MoveColor,
};
class ApplyColorAllPagesCommand: public studio::UndoCommand {
class AddPageCommand: 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);
AddPageCommand(Palette &pal) noexcept;
~ApplyColorAllPagesCommand() noexcept override = default;
~AddPageCommand() noexcept override = default;
[[nodiscard]]
int commandId() const noexcept final;
@ -42,6 +38,7 @@ class ApplyColorAllPagesCommand: public studio::UndoCommand {
ox::Error redo() noexcept final;
ox::Error undo() noexcept final;
};
class DuplicatePageCommand: public studio::UndoCommand {
@ -89,12 +86,13 @@ class RemovePageCommand: public studio::UndoCommand {
class AddColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
Palette *m_pal = nullptr;
Color16 m_color = 0;
size_t const m_idx = 0;
int m_idx = -1;
size_t m_page = 0;
public:
AddColorCommand(Palette &pal, Color16 color, size_t idx) noexcept;
AddColorCommand(Palette *pal, Color16 color, size_t page, int idx) noexcept;
~AddColorCommand() noexcept override = default;
@ -109,12 +107,13 @@ class AddColorCommand: public studio::UndoCommand {
class RemoveColorCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx = 0;
ox::Vector<Color16> const m_colors;
Palette *m_pal = nullptr;
Color16 m_color = 0;
size_t m_page = 0;
int m_idx = -1;
public:
RemoveColorCommand(Palette &pal, size_t idx) noexcept;
RemoveColorCommand(Palette *pal, Color16 color, size_t page, int idx) noexcept;
~RemoveColorCommand() noexcept override = default;
@ -127,48 +126,16 @@ class RemoveColorCommand: public studio::UndoCommand {
};
class UpdateColorInfoCommand: public studio::UndoCommand {
private:
Palette &m_pal;
size_t const m_idx{};
Palette::ColorInfo m_altColorInfo;
public:
UpdateColorInfoCommand(
Palette &pal,
size_t idx,
Palette::ColorInfo newColorInfo);
~UpdateColorInfoCommand() noexcept override = default;
[[nodiscard]]
bool mergeWith(const 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{};
Color16 m_altColor{};
Palette *m_pal = nullptr;
Color16 m_oldColor = 0;
Color16 m_newColor = 0;
size_t m_page = 0;
int m_idx = -1;
public:
UpdateColorCommand(
Palette &pal,
size_t page,
size_t idx,
Color16 newColor);
UpdateColorCommand(Palette *pal, size_t page, int idx, Color16 oldColor, Color16 newColor) noexcept;
~UpdateColorCommand() noexcept override = default;
@ -182,20 +149,17 @@ class UpdateColorCommand: public studio::UndoCommand {
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;
Palette *m_pal = nullptr;
size_t m_page = 0;
std::size_t m_idx = 0;
int m_offset = 0;
public:
MoveColorCommand(Palette &pal, size_t page, size_t srcIdx, size_t dstIdx) noexcept;
MoveColorCommand(Palette *pal, size_t page, std::size_t idx, int offset) noexcept;
~MoveColorCommand() noexcept override = default;
@ -208,7 +172,7 @@ class MoveColorCommand: public studio::UndoCommand {
ox::Error undo() noexcept override;
private:
void moveColor(size_t srcIdx, size_t dstIdx) noexcept;
void moveColor(int idx, int offset) noexcept;
};
}

View File

@ -7,7 +7,6 @@
#include <ox/std/point.hpp>
#include <keel/media.hpp>
#include <studio/studio.hpp>
#include "tilesheeteditor-imgui.hpp"
@ -15,36 +14,6 @@ namespace nostalgia::core {
namespace ig = studio::ig;
static ox::String configName(ox::StringView str) noexcept {
auto out = ox::String{str};
for (auto &c : out) {
if (c == '/' || c == '\\') {
c = '%';
}
}
return out;
}
struct TileSheetEditorConfig {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.TileSheetEditorConfig";
static constexpr auto TypeVersion = 1;
TileSheet::SubSheetIdx activeSubsheet{};
};
oxModelBegin(TileSheetEditorConfig)
oxModelFieldRename(activeSubsheet, active_subsheet)
oxModelEnd()
struct TileSheetEditorConfigs {
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.TileSheetEditorConfigs";
static constexpr auto TypeVersion = 1;
ox::HashMap<ox::String, TileSheetEditorConfig> configs;
};
oxModelBegin(TileSheetEditorConfigs)
oxModelField(configs)
oxModelEnd()
static ox::Vector<uint32_t> normalizePixelSizes(
ox::Vector<uint8_t> const&inPixels,
int const bpp) noexcept {
@ -106,7 +75,7 @@ static ox::Error toPngFile(
8)));
}
TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::StringView const path):
TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::CRStringView path):
Editor(path),
m_sctx(sctx),
m_tctx(m_sctx.tctx),
@ -118,12 +87,6 @@ TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::Stri
m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet);
m_exportMenu.inputSubmitted.connect(this, &TileSheetEditorImGui::exportSubhseetToPng);
m_model.paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection);
// load config
auto const&config = studio::readConfig<TileSheetEditorConfig>(
keelCtx(m_sctx), configName(itemPath()));
if (config.ok()) {
m_model.setActiveSubsheet(validateSubSheetIdx(m_model.img(), config.value.activeSubsheet));
}
}
void TileSheetEditorImGui::exportFile() {
@ -157,7 +120,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) {
auto const popupOpen = m_subsheetEditor.isOpen() && m_exportMenu.isOpen();
auto const pal = m_model.pal();
if (!popupOpen) {
auto const colorCnt = core::colorCnt(pal, m_model.palettePage());
auto const colorCnt = pal.pages[m_model.palettePage()].size();
if (key == turbine::Key::Alpha_D) {
m_tool = TileSheetTool::Draw;
setCopyEnabled(false);
@ -181,8 +144,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) {
static_cast<uint32_t>(key - turbine::Key::Num_1), m_model.pal().pages.size() - 1);
m_model.setPalettePage(idx);
} else if (key <= turbine::Key::Num_0 + colorCnt) {
auto const idx = ox::min<std::size_t>(
static_cast<uint32_t>(key - turbine::Key::Num_1), colorCnt - 1);
auto const idx = ox::min<std::size_t>(static_cast<uint32_t>(key - turbine::Key::Num_1), colorCnt - 1);
m_view.setPalIdx(idx);
}
} else if (key == turbine::Key::Num_0) {
@ -246,7 +208,7 @@ void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept {
auto const&parent = m_model.activeSubSheet();
m_model.addSubsheet(insertOnIdx);
insertOnIdx.emplace_back(parent.subsheets.size() - 1);
setActiveSubsheet(insertOnIdx);
m_model.setActiveSubsheet(insertOnIdx);
}
ImGui::SameLine();
if (ig::PushButton("-", btnSize)) {
@ -279,7 +241,7 @@ void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept {
}
ImGui::EndChild();
m_subsheetEditor.draw(m_tctx);
m_exportMenu.draw(m_tctx);
m_exportMenu.draw(m_sctx);
}
void TileSheetEditorImGui::drawSubsheetSelector(
@ -299,7 +261,7 @@ void TileSheetEditorImGui::drawSubsheetSelector(
auto const open = ImGui::TreeNodeEx(lbl.c_str(), flags);
ImGui::SameLine();
if (ImGui::IsItemClicked()) {
setActiveSubsheet(path);
m_model.setActiveSubsheet(path);
}
if (ImGui::IsMouseDoubleClicked(0) && ImGui::IsItemHovered()) {
showSubsheetEditor();
@ -470,23 +432,20 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
ImGui::Indent(-20);
}
// header
if (ImGui::BeginTable(
"PaletteTable", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) {
if (ImGui::BeginTable("PaletteTable", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) {
ImGui::TableSetupColumn("No.", 0, 0.45f);
ImGui::TableSetupColumn("", 0, 0.22f);
ImGui::TableSetupColumn("Color16", 0, 3);
ImGui::TableHeadersRow();
{
auto const&pal = m_model.pal();
if (pal.pages.size() > m_model.palettePage()) {
for (auto i = 0u; auto const&c: pal.pages[m_model.palettePage()]) {
for (auto i = 0u; auto c: pal.pages[m_model.palettePage()]) {
ImGui::PushID(static_cast<int>(i));
// Column: color idx
ImGui::TableNextColumn();
auto const label = ox::itoa(i + 1);
auto const rowSelected = i == m_view.palIdx();
if (ImGui::Selectable(
label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
if (ImGui::Selectable(label.c_str(), rowSelected, ImGuiSelectableFlags_SpanAllColumns)) {
m_view.setPalIdx(i);
}
// Column: color RGB
@ -500,7 +459,6 @@ void TileSheetEditorImGui::drawPaletteSelector() noexcept {
++i;
}
}
}
ImGui::EndTable();
}
}
@ -522,20 +480,12 @@ ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept {
return {};
}
void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept {
m_model.setActiveSubsheet(path);
studio::editConfig<TileSheetEditorConfig>(keelCtx(m_sctx), configName(itemPath()),
[&path](TileSheetEditorConfig *config) {
config->activeSubsheet = std::move(path);
});
}
ox::Error TileSheetEditorImGui::markUnsavedChanges(studio::UndoCommand const*) noexcept {
setUnsavedChanges(true);
return {};
}
void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &tctx) noexcept {
void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &sctx) noexcept {
constexpr auto popupName = "Edit Subsheet";
if (!m_show) {
return;
@ -544,7 +494,7 @@ void TileSheetEditorImGui::SubSheetEditor::draw(turbine::Context &tctx) noexcept
auto constexpr popupWidth = 235.f;
auto const popupHeight = modSize ? 130.f : 85.f;
auto const popupSz = ImVec2(popupWidth, popupHeight);
if (ig::BeginPopup(tctx, popupName, m_show, popupSz)) {
if (ig::BeginPopup(sctx, popupName, m_show, popupSz)) {
ImGui::InputText("Name", m_name.data(), m_name.cap());
if (modSize) {
ImGui::InputInt("Columns", &m_cols);
@ -561,7 +511,7 @@ void TileSheetEditorImGui::SubSheetEditor::close() noexcept {
m_show = false;
}
void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &tctx) noexcept {
void TileSheetEditorImGui::ExportMenu::draw(studio::StudioContext &sctx) noexcept {
constexpr auto popupName = "Export Tile Sheet";
if (!m_show) {
return;
@ -569,7 +519,7 @@ void TileSheetEditorImGui::ExportMenu::draw(turbine::Context &tctx) noexcept {
constexpr auto popupWidth = 235.f;
constexpr auto popupHeight = 85.f;
constexpr auto popupSz = ImVec2(popupWidth, popupHeight);
if (ig::BeginPopup(tctx, popupName, m_show, popupSz)) {
if (ig::BeginPopup(sctx.tctx, popupName, m_show, popupSz)) {
ImGui::InputInt("Scale", &m_scale);
m_scale = ox::clamp(m_scale, 1, 50);
if (ig::PopupControlsOkCancel(popupWidth, m_show) == ig::PopupResponse::OK) {

View File

@ -48,7 +48,7 @@ class TileSheetEditorImGui: public studio::Editor {
m_show = true;
m_scale = 5;
}
void draw(turbine::Context &sctx) noexcept;
void draw(studio::StudioContext &sctx) noexcept;
void close() noexcept;
[[nodiscard]]
inline bool isOpen() const noexcept { return m_show; }
@ -67,7 +67,7 @@ class TileSheetEditorImGui: public studio::Editor {
TileSheetTool m_tool = TileSheetTool::Draw;
public:
TileSheetEditorImGui(studio::StudioContext &sctx, ox::StringView path);
TileSheetEditorImGui(studio::StudioContext &sctx, ox::CRStringView path);
~TileSheetEditorImGui() override = default;
@ -111,8 +111,6 @@ class TileSheetEditorImGui: public studio::Editor {
private:
ox::Error markUnsavedChanges(studio::UndoCommand const*) noexcept;
void setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept;
};
}

View File

@ -27,8 +27,7 @@
namespace nostalgia::core {
Palette const TileSheetEditorModel::s_defaultPalette = {
.colorInfo = {ox::Vector<Palette::ColorInfo>{{}}},
.pages = {{ox::Vector<Color16>(128)}},
.pages = {ox::Vector<Color16>(128)},
};
// delete pixels of all non-leaf nodes

View File

@ -4,7 +4,6 @@
#pragma once
#include <studio/configio.hpp>
#include <studio/context.hpp>
#include <studio/editor.hpp>
#include <studio/filedialog.hpp>