Add params to ReplaceColor command (now this filter can be used from a script without UI)

This commit is contained in:
David Capello 2019-07-15 19:12:44 -03:00
parent 66442ee575
commit 9143523827
3 changed files with 66 additions and 11 deletions

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -17,6 +18,8 @@
#include "app/commands/commands.h"
#include "app/commands/filters/filter_manager_impl.h"
#include "app/commands/filters/filter_window.h"
#include "app/commands/filters/filter_worker.h"
#include "app/commands/new_params.h"
#include "app/context.h"
#include "app/find_widget.h"
#include "app/ini_file.h"
@ -37,6 +40,14 @@ namespace app {
static const char* ConfigSection = "ReplaceColor";
struct ReplaceColorParams : public NewParams {
Param<bool> ui { this, true, "ui" };
Param<filters::Target> target { this, 0, "target" };
Param<app::Color> from { this, app::Color(), "from" };
Param<app::Color> to { this, app::Color(), "to" };
Param<int> tolerance { this, 0, "tolerance" };
};
// Wrapper for ReplaceColorFilter to handle colors in an easy way
class ReplaceColorFilterWrapper : public ReplaceColorFilter {
public:
@ -127,7 +138,7 @@ private:
ui::Slider* m_toleranceSlider;
};
class ReplaceColorCommand : public Command {
class ReplaceColorCommand : public CommandWithNewParams<ReplaceColorParams> {
public:
ReplaceColorCommand();
@ -137,7 +148,7 @@ protected:
};
ReplaceColorCommand::ReplaceColorCommand()
: Command(CommandId::ReplaceColor(), CmdRecordableFlag)
: CommandWithNewParams<ReplaceColorParams>(CommandId::ReplaceColor(), CmdRecordableFlag)
{
}
@ -149,13 +160,10 @@ bool ReplaceColorCommand::onEnabled(Context* context)
void ReplaceColorCommand::onExecute(Context* context)
{
const bool ui = (params().ui() && context->isUIAvailable());
Site site = context->activeSite();
ReplaceColorFilterWrapper filter(site.layer());
filter.setFrom(get_config_color(ConfigSection, "Color1", ColorBar::instance()->getFgColor()));
filter.setTo(get_config_color(ConfigSection, "Color2", ColorBar::instance()->getBgColor()));
filter.setTolerance(get_config_int(ConfigSection, "Tolerance", 0));
FilterManagerImpl filterMgr(context, &filter);
filterMgr.setTarget(
site.sprite()->pixelFormat() == IMAGE_INDEXED ?
@ -166,11 +174,32 @@ void ReplaceColorCommand::onExecute(Context* context)
TARGET_GRAY_CHANNEL |
TARGET_ALPHA_CHANNEL);
ReplaceColorWindow window(filter, filterMgr);
if (window.doModal()) {
set_config_color(ConfigSection, "From", filter.getFrom());
set_config_color(ConfigSection, "To", filter.getTo());
set_config_int(ConfigSection, "Tolerance", filter.getTolerance());
if (ui) {
filter.setFrom(get_config_color(ConfigSection, "Color1", ColorBar::instance()->getFgColor()));
filter.setTo(get_config_color(ConfigSection, "Color2", ColorBar::instance()->getBgColor()));
filter.setTolerance(get_config_int(ConfigSection, "Tolerance", 0));
}
else {
filter.setFrom(ColorBar::instance()->getFgColor());
filter.setTo(ColorBar::instance()->getBgColor());
filter.setTolerance(params().tolerance());
}
if (params().from.isSet()) filter.setFrom(params().from());
if (params().to.isSet()) filter.setTo(params().to());
if (params().tolerance.isSet()) filter.setTolerance(params().tolerance());
if (params().target.isSet()) filterMgr.setTarget(params().target());
if (ui) {
ReplaceColorWindow window(filter, filterMgr);
if (window.doModal()) {
set_config_color(ConfigSection, "From", filter.getFrom());
set_config_color(ConfigSection, "To", filter.getTo());
set_config_int(ConfigSection, "Tolerance", filter.getTolerance());
}
}
else {
start_filter_worker(&filterMgr);
}
}

View File

@ -10,6 +10,7 @@
#include "app/commands/new_params.h"
#include "app/color.h"
#include "app/doc_exporter.h"
#include "app/sprite_sheet_type.h"
#include "base/convert_to.h"
@ -17,6 +18,7 @@
#include "doc/color_mode.h"
#ifdef ENABLE_SCRIPTING
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#endif
@ -83,6 +85,12 @@ void Param<doc::ColorMode>::fromString(const std::string& value)
setValue(doc::ColorMode::RGB);
}
template<>
void Param<app::Color>::fromString(const std::string& value)
{
setValue(app::Color::fromString(value));
}
#ifdef ENABLE_SCRIPTING
template<>
@ -133,6 +141,12 @@ void Param<doc::ColorMode>::fromLua(lua_State* L, int index)
setValue((doc::ColorMode)lua_tointeger(L, index));
}
template<>
void Param<app::Color>::fromLua(lua_State* L, int index)
{
setValue(script::convert_args_into_color(L, index));
}
void CommandWithNewParamsBase::loadParamsFromLuaTable(lua_State* L, int index)
{
onResetValues();

View File

@ -25,6 +25,7 @@
#include "doc/anidir.h"
#include "doc/blend_mode.h"
#include "doc/color_mode.h"
#include "filters/target.h"
#include <fstream>
#include <sstream>
@ -319,6 +320,17 @@ Engine::Engine()
setfield_integer(L, "NONE", doc::BrushPattern::PAINT_BRUSH);
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "FilterTarget");
setfield_integer(L, "RED", TARGET_RED_CHANNEL);
setfield_integer(L, "GREEN", TARGET_GREEN_CHANNEL);
setfield_integer(L, "BLUE", TARGET_BLUE_CHANNEL);
setfield_integer(L, "ALPHA", TARGET_ALPHA_CHANNEL);
setfield_integer(L, "GRAY", TARGET_GRAY_CHANNEL);
setfield_integer(L, "INDEX", TARGET_INDEX_CHANNEL);
lua_pop(L, 1);
// Register classes/prototypes
register_brush_class(L);
register_cel_class(L);