[nostalgia/gfx/studio/tilesheeteditor] Rework system for tracking current palette path
All checks were successful
Build / build (push) Successful in 3m29s

This commit is contained in:
Gary Talent 2025-01-25 22:59:51 -06:00
parent cfa91d3d39
commit f840240aac
4 changed files with 47 additions and 35 deletions

View File

@ -70,8 +70,8 @@ static ox::Error toPngFile(
ox::Vector<uint32_t> &&pixels,
Palette const&pal,
size_t page,
unsigned width,
unsigned height) noexcept {
unsigned const width,
unsigned const height) noexcept {
for (auto &c : pixels) {
c = color32(color(pal, page, c)) | static_cast<Color32>(0XFF << 24);
}
@ -96,7 +96,6 @@ TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::Stri
// connect signal/slots
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), itemPath());
@ -125,7 +124,7 @@ bool TileSheetEditorImGui::acceptsClipboardPayload() const noexcept {
return m_model.acceptsClipboardPayload();
}
void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) {
void TileSheetEditorImGui::keyStateChanged(turbine::Key const key, bool const down) {
if (!down) {
return;
}
@ -526,14 +525,11 @@ void TileSheetEditorImGui::drawPaletteMenu() noexcept {
}
}
ox::Error TileSheetEditorImGui::updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept {
ox::Error TileSheetEditorImGui::updateActiveSubsheet(
ox::StringView const&name, int const cols, int const rows) noexcept {
return m_model.updateSubsheet(m_model.activeSubSheetIdx(), name, cols, rows);
}
ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept {
return {};
}
void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept {
m_model.setActiveSubsheet(path);
studio::editConfig<TileSheetEditorConfig>(keelCtx(m_sctx), itemPath(),

View File

@ -4,7 +4,6 @@
#pragma once
#include <ox/model/def.hpp>
#include <ox/std/vec.hpp>
#include <glutils/glutils.hpp>
@ -29,7 +28,7 @@ class TileSheetEditorImGui: public studio::Editor {
public:
ox::Signal<ox::Error(ox::StringViewCR name, int cols, int rows)> inputSubmitted;
void show(ox::StringViewCR name, int cols, int rows) noexcept;
void draw(turbine::Context &sctx) noexcept;
void draw(turbine::Context &tctx) noexcept;
void close() noexcept;
[[nodiscard]]
constexpr bool isOpen() const noexcept { return m_show; }
@ -41,7 +40,7 @@ class TileSheetEditorImGui: public studio::Editor {
public:
ox::Signal<ox::Error(int scale)> inputSubmitted;
void show() noexcept;
void draw(turbine::Context &sctx) noexcept;
void draw(turbine::Context &tctx) noexcept;
void close() noexcept;
[[nodiscard]]
constexpr bool isOpen() const noexcept { return m_show; }
@ -99,8 +98,6 @@ class TileSheetEditorImGui: public studio::Editor {
ox::Error updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept;
ox::Error setPaletteSelection() noexcept;
// slots
private:
void setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept;

View File

@ -42,17 +42,19 @@ Palette const TileSheetEditorModel::s_defaultPalette = {
};
TileSheetEditorModel::TileSheetEditorModel(
studio::StudioContext &sctx, ox::StringViewCR path, studio::UndoStack &undoStack):
m_sctx(sctx),
m_tctx(m_sctx.tctx),
m_path(path),
m_img(*readObj<TileSheet>(keelCtx(m_tctx), m_path).unwrapThrow()),
// ignore failure to load palette
m_pal(readObj<Palette>(keelCtx(m_tctx), m_img.defaultPalette).value),
m_undoStack(undoStack) {
studio::StudioContext &sctx, ox::StringParam path, studio::UndoStack &undoStack):
m_sctx(sctx),
m_tctx(m_sctx.tctx),
m_path(std::move(path)),
m_img(*readObj<TileSheet>(keelCtx(m_tctx), m_path).unwrapThrow()),
// ignore failure to load palette
m_pal(readObj<Palette>(keelCtx(m_tctx), m_img.defaultPalette).value),
m_undoStack(undoStack) {
normalizeSubsheets(m_img.subsheet);
m_pal.updated.connect(this, &TileSheetEditorModel::markUpdated);
m_undoStack.changeTriggered.connect(this, &TileSheetEditorModel::markUpdatedCmdId);
setPalPath();
m_sctx.project->fileMoved.connect(this, &TileSheetEditorModel::handleFileRename);
}
void TileSheetEditorModel::cut() {
@ -116,18 +118,7 @@ bool TileSheetEditorModel::acceptsClipboardPayload() const noexcept {
}
ox::StringView TileSheetEditorModel::palPath() const noexcept {
auto &path = m_img.defaultPalette;
constexpr ox::StringView uuidPrefix = "uuid://";
if (ox::beginsWith(path, uuidPrefix)) {
auto const uuid = substr(path, uuidPrefix.bytes());
auto const out = keelCtx(m_tctx).uuidToPath.at(uuid);
if (out.error) {
return {};
}
return *out.value;
} else {
return path;
}
return m_palPath;
}
ox::Error TileSheetEditorModel::setPalette(ox::StringViewCR path) noexcept {
@ -318,10 +309,33 @@ void TileSheetEditorModel::getFillPixels(
}
}
void TileSheetEditorModel::setPalPath() noexcept {
auto &path = m_img.defaultPalette;
constexpr ox::StringView uuidPrefix = "uuid://";
if (ox::beginsWith(path, uuidPrefix)) {
auto const uuid = substr(path, uuidPrefix.bytes());
auto const out = keelCtx(m_tctx).uuidToPath.at(uuid);
if (!out.error) {
m_palPath = *out.value;
}
} else {
m_palPath = path;
}
}
void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept {
std::ignore = m_undoStack.push(ox::UPtr<studio::UndoCommand>{cmd});
m_ongoingDrawCommand = dynamic_cast<DrawCommand*>(cmd);
m_updated = true;
}
ox::Error TileSheetEditorModel::handleFileRename(ox::StringViewCR newPath, ox::UUID const&id) noexcept {
if ((beginsWith(m_img.defaultPalette, "uuid://") &&
substr(m_img.defaultPalette, 7) == id.toString()) ||
m_img.defaultPalette == newPath) {
m_palPath = newPath;
}
return {};
}
}

View File

@ -25,6 +25,7 @@ class TileSheetEditorModel: public ox::SignalHandler {
studio::StudioContext &m_sctx;
turbine::Context &m_tctx;
ox::String m_path;
ox::String m_palPath;
TileSheet m_img;
TileSheet::SubSheetIdx m_activeSubsSheetIdx;
keel::AssetRef<Palette> m_pal;
@ -36,7 +37,7 @@ class TileSheetEditorModel: public ox::SignalHandler {
bool m_updated = false;
public:
TileSheetEditorModel(studio::StudioContext &sctx, ox::StringViewCR path, studio::UndoStack &undoStack);
TileSheetEditorModel(studio::StudioContext &sctx, ox::StringParam path, studio::UndoStack &undoStack);
~TileSheetEditorModel() override = default;
@ -132,8 +133,12 @@ class TileSheetEditorModel: public ox::SignalHandler {
ox::Point const&pt,
int oldColor) const noexcept;
void setPalPath() noexcept;
void pushCommand(studio::UndoCommand *cmd) noexcept;
ox::Error handleFileRename(ox::StringViewCR newPath, ox::UUID const&id) noexcept;
};
constexpr TileSheet const&TileSheetEditorModel::img() const noexcept {