From 474900147329291293a83fa36ec876dbec1710f6 Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 5 Sep 2016 11:59:25 -0300 Subject: [PATCH] Now --palette changes the palette of the last sprite (fix #1245) --- src/app/app.cpp | 2 +- src/app/cli/app_options.cpp | 8 +- src/app/cli/app_options.h | 6 +- src/app/cli/cli_delegate.h | 1 + src/app/cli/cli_processor.cpp | 12 +++ src/app/cli/default_cli_delegate.cpp | 21 +++++ src/app/cli/default_cli_delegate.h | 1 + src/app/cli/preview_cli_delegate.cpp | 11 +++ src/app/cli/preview_cli_delegate.h | 2 + src/app/commands/cmd_load_palette.cpp | 30 ++++--- src/app/commands/cmd_set_palette.cpp | 5 +- src/app/modules/palettes.cpp | 108 ++++++++++++-------------- src/app/modules/palettes.h | 8 +- 13 files changed, 129 insertions(+), 86 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index f4c7504d5..337d1f4e4 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -170,7 +170,7 @@ void App::initialize(const AppOptions& options) // Load or create the default palette, or migrate the default // palette from an old format palette to the new one, etc. - load_default_palette(options.paletteFileName()); + load_default_palette(); // Initialize GUI interface if (isGui()) { diff --git a/src/app/cli/app_options.cpp b/src/app/cli/app_options.cpp index 93d039f28..86ee74c67 100644 --- a/src/app/cli/app_options.cpp +++ b/src/app/cli/app_options.cpp @@ -26,12 +26,12 @@ AppOptions::AppOptions(int argc, const char* argv[]) , m_showHelp(false) , m_showVersion(false) , m_verboseLevel(kNoVerbose) - , m_palette(m_po.add("palette").requiresValue("").description("Use a specific palette by default")) , m_shell(m_po.add("shell").description("Start an interactive console to execute scripts")) , m_batch(m_po.add("batch").mnemonic('b').description("Do not start the UI")) , m_preview(m_po.add("preview").mnemonic('p').description("Do not execute actions, just print what will be\ndone")) - , m_saveAs(m_po.add("save-as").requiresValue("").description("Save the last given document with other format")) - , m_scale(m_po.add("scale").requiresValue("").description("Resize all previous opened documents")) + , m_saveAs(m_po.add("save-as").requiresValue("").description("Save the last given sprite with other format")) + , m_palette(m_po.add("palette").requiresValue("").description("Change the palette of the last given sprite")) + , m_scale(m_po.add("scale").requiresValue("").description("Resize all previously opened sprites")) , m_shrinkTo(m_po.add("shrink-to").requiresValue("width,height").description("Shrink each sprite if it is\nlarger than width or height")) , m_data(m_po.add("data").requiresValue("").description("File to store the sprite sheet metadata")) , m_format(m_po.add("format").requiresValue("").description("Format to export the data file\n(json-hash, json-array)")) @@ -69,8 +69,6 @@ AppOptions::AppOptions(int argc, const char* argv[]) else if (m_po.enabled(m_verbose)) m_verboseLevel = kVerbose; - m_paletteFileName = m_po.value_of(m_palette); - m_startShell = m_po.enabled(m_shell); m_previewCLI = m_po.enabled(m_preview); m_showHelp = m_po.enabled(m_help); diff --git a/src/app/cli/app_options.h b/src/app/cli/app_options.h index b825bb75c..82c2e4b16 100644 --- a/src/app/cli/app_options.h +++ b/src/app/cli/app_options.h @@ -40,14 +40,13 @@ public: bool showVersion() const { return m_showVersion; } VerboseLevel verboseLevel() const { return m_verboseLevel; } - const std::string& paletteFileName() const { return m_paletteFileName; } - const ValueList& values() const { return m_po.values(); } // Export options const Option& saveAs() const { return m_saveAs; } + const Option& palette() const { return m_palette; } const Option& scale() const { return m_scale; } const Option& shrinkTo() const { return m_shrinkTo; } const Option& data() const { return m_data; } @@ -85,13 +84,12 @@ private: bool m_showHelp; bool m_showVersion; VerboseLevel m_verboseLevel; - std::string m_paletteFileName; - Option& m_palette; Option& m_shell; Option& m_batch; Option& m_preview; Option& m_saveAs; + Option& m_palette; Option& m_scale; Option& m_shrinkTo; Option& m_data; diff --git a/src/app/cli/cli_delegate.h b/src/app/cli/cli_delegate.h index 48c1471f5..5c6e70124 100644 --- a/src/app/cli/cli_delegate.h +++ b/src/app/cli/cli_delegate.h @@ -27,6 +27,7 @@ namespace app { virtual void beforeOpenFile(const CliOpenFile& cof) { } virtual void afterOpenFile(const CliOpenFile& cof) { } virtual void saveFile(const CliOpenFile& cof) { } + virtual void loadPalette(const CliOpenFile& cof, const std::string& filename) { } virtual void exportFiles(DocumentExporter& exporter) { } virtual void execScript(const std::string& filename) { } }; diff --git a/src/app/cli/cli_processor.cpp b/src/app/cli/cli_processor.cpp index ddb69fb4c..e91eb849f 100644 --- a/src/app/cli/cli_processor.cpp +++ b/src/app/cli/cli_processor.cpp @@ -222,6 +222,18 @@ void CliProcessor::process() else console.printf("A document is needed before --save-as argument\n"); } + // --palette + else if (opt == &m_options.palette()) { + if (lastDoc) { + ASSERT(cof.document == lastDoc); + + std::string filename = value.value(); + m_delegate->loadPalette(cof, filename); + } + else { + console.printf("You need to load a document to change its palette with --palette\n"); + } + } // --scale else if (opt == &m_options.scale()) { Command* command = CommandsModule::instance()->getCommandByName(CommandId::SpriteSize); diff --git a/src/app/cli/default_cli_delegate.cpp b/src/app/cli/default_cli_delegate.cpp index a4dff43b8..fdef561a6 100644 --- a/src/app/cli/default_cli_delegate.cpp +++ b/src/app/cli/default_cli_delegate.cpp @@ -14,13 +14,16 @@ #include "app/cli/cli_open_file.h" #include "app/commands/commands.h" #include "app/commands/params.h" +#include "app/console.h" #include "app/document.h" #include "app/document_exporter.h" +#include "app/file/palette_file.h" #include "app/script/app_scripting.h" #include "app/ui_context.h" #include "base/convert_to.h" #include "doc/frame_tag.h" #include "doc/layer.h" +#include "doc/palette.h" #include "doc/sprite.h" #include "script/engine_delegate.h" @@ -80,6 +83,24 @@ void DefaultCliDelegate::saveFile(const CliOpenFile& cof) ctx->executeCommand(saveAsCommand, params); } +void DefaultCliDelegate::loadPalette(const CliOpenFile& cof, + const std::string& filename) +{ + base::UniquePtr palette(load_palette(filename.c_str())); + if (palette) { + Context* ctx = UIContext::instance(); + Command* loadPalCommand = CommandsModule::instance()->getCommandByName(CommandId::LoadPalette); + Params params; + params.set("filename", filename.c_str()); + + ctx->executeCommand(loadPalCommand, params); + } + else { + Console().printf("Error loading palette in --palette '%s'\n", + filename.c_str()); + } +} + void DefaultCliDelegate::exportFiles(DocumentExporter& exporter) { LOG("Exporting sheet...\n"); diff --git a/src/app/cli/default_cli_delegate.h b/src/app/cli/default_cli_delegate.h index e3d70dd04..494025835 100644 --- a/src/app/cli/default_cli_delegate.h +++ b/src/app/cli/default_cli_delegate.h @@ -18,6 +18,7 @@ namespace app { void showVersion() override; void afterOpenFile(const CliOpenFile& cof) override; void saveFile(const CliOpenFile& cof) override; + void loadPalette(const CliOpenFile& cof, const std::string& filename) override; void exportFiles(DocumentExporter& exporter) override; void execScript(const std::string& filename) override; }; diff --git a/src/app/cli/preview_cli_delegate.cpp b/src/app/cli/preview_cli_delegate.cpp index ed7f56e4a..5cf413000 100644 --- a/src/app/cli/preview_cli_delegate.cpp +++ b/src/app/cli/preview_cli_delegate.cpp @@ -135,6 +135,17 @@ void PreviewCliDelegate::saveFile(const CliOpenFile& cof) std::cout << " - No output\n"; } +void PreviewCliDelegate::loadPalette(const CliOpenFile& cof, + const std::string& filename) +{ + ASSERT(cof.document); + ASSERT(cof.document->sprite()); + + std::cout << "- Load palette:\n" + << " - Sprite: '" << cof.filename << "'\n" + << " - Palette: '" << filename << "'\n"; +} + void PreviewCliDelegate::exportFiles(DocumentExporter& exporter) { std::string type = "None"; diff --git a/src/app/cli/preview_cli_delegate.h b/src/app/cli/preview_cli_delegate.h index 6e22679c6..cf4a7e052 100644 --- a/src/app/cli/preview_cli_delegate.h +++ b/src/app/cli/preview_cli_delegate.h @@ -22,6 +22,8 @@ namespace app { void beforeOpenFile(const CliOpenFile& cof) override; void afterOpenFile(const CliOpenFile& cof) override; void saveFile(const CliOpenFile& cof) override; + void loadPalette(const CliOpenFile& cof, + const std::string& filename) override; void exportFiles(DocumentExporter& exporter) override; void execScript(const std::string& filename) override; }; diff --git a/src/app/commands/cmd_load_palette.cpp b/src/app/commands/cmd_load_palette.cpp index 7f0fb98d8..df89b1b15 100644 --- a/src/app/commands/cmd_load_palette.cpp +++ b/src/app/commands/cmd_load_palette.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -35,6 +35,7 @@ protected: private: std::string m_preset; + std::string m_filename; }; LoadPaletteCommand::LoadPaletteCommand() @@ -47,6 +48,7 @@ LoadPaletteCommand::LoadPaletteCommand() void LoadPaletteCommand::onLoadParams(const Params& params) { m_preset = params.get("preset"); + m_filename = params.get("filename"); } void LoadPaletteCommand::onExecute(Context* context) @@ -58,24 +60,30 @@ void LoadPaletteCommand::onExecute(Context* context) if (!base::is_file(filename)) filename = get_preset_palette_filename(m_preset, ".gpl"); } + else if (!m_filename.empty()) { + filename = m_filename; + } else { std::string exts = get_readable_palette_extensions(); filename = app::show_file_selector("Load Palette", "", exts, FileSelectorType::Open); } - if (!filename.empty()) { - base::UniquePtr palette(load_palette(filename.c_str())); - if (!palette) { + // Do nothing + if (filename.empty()) + return; + + base::UniquePtr palette(load_palette(filename.c_str())); + if (!palette) { + if (context->isUIAvailable()) Alert::show("Error<( - CommandsModule::instance()->getCommandByName(CommandId::SetPalette)); - cmd->setPalette(palette); - context->executeCommand(cmd); - } + return; } + + SetPaletteCommand* cmd = static_cast( + CommandsModule::instance()->getCommandByName(CommandId::SetPalette)); + cmd->setPalette(palette); + context->executeCommand(cmd); } Command* CommandFactory::createLoadPaletteCommand() diff --git a/src/app/commands/cmd_set_palette.cpp b/src/app/commands/cmd_set_palette.cpp index c313b2e09..b86dc4032 100644 --- a/src/app/commands/cmd_set_palette.cpp +++ b/src/app/commands/cmd_set_palette.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -48,7 +48,8 @@ void SetPaletteCommand::onExecute(Context* context) set_current_palette(m_palette, false); // Redraw the entire screen - ui::Manager::getDefault()->invalidate(); + if (context->isUIAvailable()) + ui::Manager::getDefault()->invalidate(); } Command* CommandFactory::createSetPaletteCommand() diff --git a/src/app/modules/palettes.cpp b/src/app/modules/palettes.cpp index f51eef8a0..650f80bd7 100644 --- a/src/app/modules/palettes.cpp +++ b/src/app/modules/palettes.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -42,83 +42,75 @@ void exit_module_palette() delete ase_current_palette; } -void load_default_palette(const std::string& userDefined) +void load_default_palette() { base::UniquePtr pal; + std::string defaultPalName = get_preset_palette_filename( + get_default_palette_preset_name(), ".ase"); - // Load specific palette file defined by the user in the command line. - std::string palFile = userDefined; - if (!palFile.empty()) + // If there is no palette in command line, we use the default one. + std::string palFile = defaultPalName; + if (base::is_file(palFile)) { pal.reset(load_palette(palFile.c_str())); - // Load default palette file + } else { - std::string defaultPalName = get_preset_palette_filename( - get_default_palette_preset_name(), ".ase"); + // Migrate old default.gpl to default.ase format + palFile = get_preset_palette_filename( + get_default_palette_preset_name(), ".gpl"); - // If there is no palette in command line, we use the default one. - palFile = defaultPalName; if (base::is_file(palFile)) { pal.reset(load_palette(palFile.c_str())); - } - else { - // Migrate old default.gpl to default.ase format - palFile = get_preset_palette_filename( - get_default_palette_preset_name(), ".gpl"); - if (base::is_file(palFile)) { - pal.reset(load_palette(palFile.c_str())); + // Remove duplicate black entries at the end (as old palettes + // contains 256 colors) + if (pal && pal->size() == 256) { + doc::color_t black = rgba(0, 0, 0, 255); - // Remove duplicate black entries at the end (as old palettes - // contains 256 colors) - if (pal && pal->size() == 256) { - doc::color_t black = rgba(0, 0, 0, 255); + // Get the last non-black entry + int i = 0; + for (i=pal->size()-1; i>0; --i) { + if (pal->getEntry(i) != black) + break; + } - // Get the last non-black entry - int i = 0; - for (i=pal->size()-1; i>0; --i) { - if (pal->getEntry(i) != black) + if (i < pal->size()-1) { + // Check if there is a black entry in the first entries. + bool hasBlack = false; + for (int j=0; jgetEntry(j) == black) { + hasBlack = true; break; - } - - if (i < pal->size()-1) { - // Check if there is a black entry in the first entries. - bool hasBlack = false; - for (int j=0; jgetEntry(j) == black) { - hasBlack = true; - break; - } } - if (!hasBlack) - ++i; // Leave one black entry - - // Resize the palette - if (i < pal->size()-1) - pal->resize(i+1); } - } + if (!hasBlack) + ++i; // Leave one black entry - // We could remove the old .gpl file (palFile), but as the - // user could be using multiple versions of Aseprite, it's a - // good idea to keep both formats (.gpl for Aseprite <= - // v1.1-beta5, and .ase for future versions). - } - // If the default palette file doesn't exist, we copy db32.gpl - // as the default one (default.ase). - else { - ResourceFinder rf; - rf.includeDataDir("palettes/db32.gpl"); - if (rf.findFirst()) { - pal.reset(load_palette(rf.filename().c_str())); + // Resize the palette + if (i < pal->size()-1) + pal->resize(i+1); } } - // Save default.ase file - if (pal) { - palFile = defaultPalName; - save_palette(palFile.c_str(), pal.get(), 0); + // We could remove the old .gpl file (palFile), but as the + // user could be using multiple versions of Aseprite, it's a + // good idea to keep both formats (.gpl for Aseprite <= + // v1.1-beta5, and .ase for future versions). + } + // If the default palette file doesn't exist, we copy db32.gpl + // as the default one (default.ase). + else { + ResourceFinder rf; + rf.includeDataDir("palettes/db32.gpl"); + if (rf.findFirst()) { + pal.reset(load_palette(rf.filename().c_str())); } } + + // Save default.ase file + if (pal) { + palFile = defaultPalName; + save_palette(palFile.c_str(), pal.get(), 0); + } } if (pal) diff --git a/src/app/modules/palettes.h b/src/app/modules/palettes.h index e771a003b..551b4bedf 100644 --- a/src/app/modules/palettes.h +++ b/src/app/modules/palettes.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -21,10 +21,8 @@ namespace app { void exit_module_palette(); // Loads the default palette or creates it. Also it migrates the - // palette if the palette format changes, etc. The "userDefined" - // parameter can be a default palette name specified in the command - // line. - void load_default_palette(const std::string& userDefined); + // palette if the palette format changes, etc. + void load_default_palette(); Palette* get_default_palette(); Palette* get_current_palette();