diff --git a/data/gui.xml b/data/gui.xml index 8c53f21eb..9b71f1329 100644 --- a/data/gui.xml +++ b/data/gui.xml @@ -392,6 +392,12 @@ + + + + + + diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index a302b205d..5d3f1a5c1 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -162,6 +162,7 @@ add_library(app-lib color_picker.cpp color_utils.cpp commands/cmd_about.cpp + commands/cmd_add_color.cpp commands/cmd_advanced_mode.cpp commands/cmd_background_from_layer.cpp commands/cmd_cancel.cpp diff --git a/src/app/commands/cmd_add_color.cpp b/src/app/commands/cmd_add_color.cpp new file mode 100644 index 000000000..17c4f37d1 --- /dev/null +++ b/src/app/commands/cmd_add_color.cpp @@ -0,0 +1,156 @@ +// Aseprite +// Copyright (C) 2016 David Capello +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/app.h" +#include "app/cmd/set_palette.h" +#include "app/commands/command.h" +#include "app/commands/params.h" +#include "app/console.h" +#include "app/context.h" +#include "app/context_access.h" +#include "app/modules/palettes.h" +#include "app/transaction.h" +#include "app/ui/color_bar.h" +#include "app/ui/context_bar.h" +#include "app/ui/editor/editor.h" +#include "app/ui/main_window.h" +#include "doc/palette.h" + +namespace app { + +class AddColorCommand : public Command { +public: + enum class Source { Fg, Bg, Color }; + + AddColorCommand(); + +protected: + void onLoadParams(const Params& params) override; + bool onEnabled(Context* ctx) override; + void onExecute(Context* ctx) override; + std::string onGetFriendlyName() const override; + + Source m_source; + app::Color m_color; +}; + +AddColorCommand::AddColorCommand() + : Command("AddColor", + "Add Color", + CmdUIOnlyFlag) + , m_source(Source::Fg) +{ +} + +void AddColorCommand::onLoadParams(const Params& params) +{ + std::string source = params.get("source"); + if (source == "fg") + m_source = Source::Fg; + else if (source == "bg") + m_source = Source::Bg; + else + m_source = Source::Color; + + if (m_source == Source::Color) + m_color = app::Color::fromString(params.get("color")); + else + m_color = app::Color::fromMask(); +} + +bool AddColorCommand::onEnabled(Context* ctx) +{ + return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable); +} + +void AddColorCommand::onExecute(Context* ctx) +{ + app::Color appColor; + + switch (m_source) { + case Source::Fg: + appColor = ColorBar::instance()->getFgColor(); + break; + case Source::Bg: + appColor = ColorBar::instance()->getBgColor(); + break; + case Source::Color: + appColor = m_color; + break; + } + + try { + Palette* newPalette = get_current_palette(); // System current pal + color_t color = doc::rgba( + appColor.getRed(), + appColor.getGreen(), + appColor.getBlue(), + appColor.getAlpha()); + int index = newPalette->findExactMatch( + appColor.getRed(), + appColor.getGreen(), + appColor.getBlue(), + appColor.getAlpha(), -1); + + // It should be -1, because the user has pressed the warning + // button that is available only when the color isn't in the + // palette. + ASSERT(index < 0); + if (index >= 0) + return; + + ContextWriter writer(ctx, 500); + Document* document(writer.document()); + Sprite* sprite = writer.sprite(); + if (!document || !sprite) { + ASSERT(false); + return; + } + + newPalette->addEntry(color); + index = newPalette->size()-1; + + if (document) { + frame_t frame = writer.frame(); + + Transaction transaction(writer.context(), "Add Color", ModifyDocument); + transaction.execute(new cmd::SetPalette(sprite, frame, newPalette)); + transaction.commit(); + } + + set_current_palette(newPalette, true); + ui::Manager::getDefault()->invalidate(); + } + catch (base::Exception& e) { + Console::showException(e); + } +} + +std::string AddColorCommand::onGetFriendlyName() const +{ + std::string text = "Add "; + + switch (m_source) { + case Source::Fg: text += "Foreground"; break; + case Source::Bg: text += "Background"; break; + case Source::Color: text += "Specific"; break; + } + + text += " Color to Palette"; + return text; +} + +Command* CommandFactory::createAddColorCommand() +{ + return new AddColorCommand; +} + +} // namespace app diff --git a/src/app/commands/commands_list.h b/src/app/commands/commands_list.h index 7a685b13f..684802e16 100644 --- a/src/app/commands/commands_list.h +++ b/src/app/commands/commands_list.h @@ -1,11 +1,12 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. FOR_EACH_COMMAND(About) +FOR_EACH_COMMAND(AddColor) FOR_EACH_COMMAND(AdvancedMode) FOR_EACH_COMMAND(AutocropSprite) FOR_EACH_COMMAND(BackgroundFromLayer) diff --git a/src/app/ui/color_bar.cpp b/src/app/ui/color_bar.cpp index 8fbf0ff0d..c353ff8eb 100644 --- a/src/app/ui/color_bar.cpp +++ b/src/app/ui/color_bar.cpp @@ -997,55 +997,12 @@ void ColorBar::onCancel(Context* ctx) void ColorBar::onFixWarningClick(ColorButton* colorButton, ui::Button* warningIcon) { - try { - Palette* newPalette = get_current_palette(); // System current pal - app::Color appColor = colorButton->getColor(); - color_t color = doc::rgba( - appColor.getRed(), - appColor.getGreen(), - appColor.getBlue(), - appColor.getAlpha()); - int index = newPalette->findExactMatch( - appColor.getRed(), - appColor.getGreen(), - appColor.getBlue(), - appColor.getAlpha(), -1); + Command* command = CommandsModule::instance()->getCommandByName(CommandId::AddColor); + Params params; + params.set("source", "color"); + params.set("color", colorButton->getColor().toString().c_str()); - // It should be -1, because the user has pressed the warning - // button that is available only when the color isn't in the - // palette. - ASSERT(index < 0); - if (index >= 0) - return; - - ContextWriter writer(UIContext::instance(), 500); - Document* document(writer.document()); - Sprite* sprite = writer.sprite(); - if (!document || !sprite) { - ASSERT(false); - return; - } - - newPalette->addEntry(color); - index = newPalette->size()-1; - - if (document) { - frame_t frame = writer.frame(); - - Transaction transaction(writer.context(), "Add palette entry", ModifyDocument); - transaction.execute(new cmd::SetPalette(sprite, frame, newPalette)); - transaction.commit(); - } - - set_current_palette(newPalette, true); - ui::Manager::getDefault()->invalidate(); - - warningIcon->setVisible(false); - warningIcon->parent()->layout(); - } - catch (base::Exception& e) { - Console::showException(e); - } + UIContext::instance()->executeCommand(command, params); } void ColorBar::updateWarningIcon(const app::Color& color, ui::Button* warningIcon)