Now --palette changes the palette of the last sprite (fix #1245)

This commit is contained in:
David Capello 2016-09-05 11:59:25 -03:00
parent 20b726f73b
commit 4749001473
13 changed files with 129 additions and 86 deletions

View File

@ -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()) {

View File

@ -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("<filename>").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("<filename>").description("Save the last given document with other format"))
, m_scale(m_po.add("scale").requiresValue("<factor>").description("Resize all previous opened documents"))
, m_saveAs(m_po.add("save-as").requiresValue("<filename>").description("Save the last given sprite with other format"))
, m_palette(m_po.add("palette").requiresValue("<filename>").description("Change the palette of the last given sprite"))
, m_scale(m_po.add("scale").requiresValue("<factor>").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("<filename.json>").description("File to store the sprite sheet metadata"))
, m_format(m_po.add("format").requiresValue("<format>").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);

View File

@ -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;

View File

@ -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) { }
};

View File

@ -222,6 +222,18 @@ void CliProcessor::process()
else
console.printf("A document is needed before --save-as argument\n");
}
// --palette <filename>
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 <factor>
else if (opt == &m_options.scale()) {
Command* command = CommandsModule::instance()->getCommandByName(CommandId::SpriteSize);

View File

@ -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<doc::Palette> 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");

View File

@ -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;
};

View File

@ -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";

View File

@ -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;
};

View File

@ -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<doc::Palette> palette(load_palette(filename.c_str()));
if (!palette) {
// Do nothing
if (filename.empty())
return;
base::UniquePtr<doc::Palette> palette(load_palette(filename.c_str()));
if (!palette) {
if (context->isUIAvailable())
Alert::show("Error<<Loading palette file||&Close");
}
else {
SetPaletteCommand* cmd = static_cast<SetPaletteCommand*>(
CommandsModule::instance()->getCommandByName(CommandId::SetPalette));
cmd->setPalette(palette);
context->executeCommand(cmd);
}
return;
}
SetPaletteCommand* cmd = static_cast<SetPaletteCommand*>(
CommandsModule::instance()->getCommandByName(CommandId::SetPalette));
cmd->setPalette(palette);
context->executeCommand(cmd);
}
Command* CommandFactory::createLoadPaletteCommand()

View File

@ -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()

View File

@ -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<Palette> 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; j<i; ++j) {
if (pal->getEntry(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; j<i; ++j) {
if (pal->getEntry(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)

View File

@ -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();