From f78d35b12b7f87707370fdf15e33b0273352623d Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 22 Nov 2026 19:12:32 -0500 Subject: [PATCH] Save a spritesheet's image from one Document method The spritesheets window and the close-document prompt both use it. --- src/document.cpp | 16 ++++++++++++++++ src/document.h | 1 + src/ui/documents.cpp | 21 ++------------------- src/ui/spritesheets.cpp | 15 +-------------- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index 68ab016..50d7a07 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -1291,3 +1291,19 @@ bool Document::is_able_to_redo() { return !snapshots.redoStack.is_empty(); } + +// Writes a spritesheet's image to its file, beside the document, and says how it went. +void Document::spritesheet_save(int id) +{ + auto spritesheet = item_get(model.content.spritesheets, id); + auto texture = texture_get(id); + if (!spritesheet || !texture) + return; + auto path = directory_get() / spritesheet->path; + path_ensure_directory(path.parent_path()); + auto isSaved = texture->write(path); + if (isSaved) + spritesheet_hash_set_saved(id); + toast_log(isSaved ? Level::INFO : Level::ERROR, isSaved ? TOAST_SAVE_SPRITESHEET : TOAST_SAVE_SPRITESHEET_FAILED, + id, path_to_utf8(spritesheet->path)); +} diff --git a/src/document.h b/src/document.h index 7160197..4136e69 100644 --- a/src/document.h +++ b/src/document.h @@ -187,6 +187,7 @@ public: void command_run(Manager&, Command&); void spritesheet_hash_update(int); void spritesheet_hash_set_saved(int); + void spritesheet_save(int); bool spritesheet_is_dirty(int); bool spritesheet_any_dirty(); void spritesheet_hashes_sync(); diff --git a/src/ui/documents.cpp b/src/ui/documents.cpp index 95fc0d3..ef4bb06 100644 --- a/src/ui/documents.cpp +++ b/src/ui/documents.cpp @@ -296,25 +296,8 @@ void Documents::update(Taskbar& taskbar, Manager& manager, Settings& settings, R { if (isSpritesheetDirty) for (auto& spritesheet : closeDocument.model.content.spritesheets) - { - auto id = spritesheet.id; - auto texture = closeDocument.texture_get(id); - if (texture && closeDocument.spritesheet_is_dirty(id)) - { - auto pathString = path_to_utf8(spritesheet.path); - auto savePath = closeDocument.directory_get() / spritesheet.path; - path_ensure_directory(savePath.parent_path()); - if (texture->write(savePath)) - { - closeDocument.spritesheet_hash_set_saved(id); - toast_log(Level::INFO, TOAST_SAVE_SPRITESHEET, id, pathString); - } - else - { - toast_log(Level::ERROR, TOAST_SAVE_SPRITESHEET_FAILED, id, pathString); - } - } - } + if (closeDocument.spritesheet_is_dirty(spritesheet.id)) + closeDocument.spritesheet_save(spritesheet.id); auto index = closeDocumentIndex; manager.command_push({.runManager = [index](Manager& manager) { manager.close(index); }}); close(); diff --git a/src/ui/spritesheets.cpp b/src/ui/spritesheets.cpp index e1d688d..3d90e87 100644 --- a/src/ui/spritesheets.cpp +++ b/src/ui/spritesheets.cpp @@ -27,20 +27,7 @@ static bool is_spritesheet_regions(Document& document, int id) static void spritesheets_save(Document& document, const std::set& ids) { for (auto id : ids) - { - auto spritesheet = spritesheet_get(document, id); - auto texture = document.texture_get(id); - if (!spritesheet || !texture) - continue; - auto pathString = path_to_utf8(spritesheet->path); - WorkingDirectory workingDirectory(document.directory_get()); - path_ensure_directory(spritesheet->path.parent_path()); - auto isSaved = texture->write(spritesheet->path); - if (isSaved) - document.spritesheet_hash_set_saved(id); - toast_log(isSaved ? Level::INFO : Level::ERROR, - isSaved ? TOAST_SAVE_SPRITESHEET : TOAST_SAVE_SPRITESHEET_FAILED, id, pathString); - } + document.spritesheet_save(id); } // Points a spritesheet at a new file: replacing loads it, setting the path writes the current texture there.