[lua] Add support to call AddColor from scripts

This commit is contained in:
David Capello 2019-10-01 09:37:04 -03:00
parent 944634542e
commit 5e25d5a021
3 changed files with 50 additions and 44 deletions

View File

@ -197,7 +197,6 @@ if(ENABLE_UI)
app_menus.cpp app_menus.cpp
closed_docs.cpp closed_docs.cpp
commands/cmd_about.cpp commands/cmd_about.cpp
commands/cmd_add_color.cpp
commands/cmd_advanced_mode.cpp commands/cmd_advanced_mode.cpp
commands/cmd_cancel.cpp commands/cmd_cancel.cpp
commands/cmd_cel_properties.cpp commands/cmd_cel_properties.cpp
@ -489,6 +488,7 @@ add_library(app-lib
color_picker.cpp color_picker.cpp
color_spaces.cpp color_spaces.cpp
color_utils.cpp color_utils.cpp
commands/cmd_add_color.cpp
commands/cmd_background_from_layer.cpp commands/cmd_background_from_layer.cpp
commands/cmd_canvas_size.cpp commands/cmd_canvas_size.cpp
commands/cmd_cel_opacity.cpp commands/cmd_cel_opacity.cpp

View File

@ -1,4 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2016-2018 David Capello // Copyright (C) 2016-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -11,59 +12,64 @@
#include "app/app.h" #include "app/app.h"
#include "app/cmd/set_palette.h" #include "app/cmd/set_palette.h"
#include "app/commands/command.h" #include "app/commands/command.h"
#include "app/commands/params.h" #include "app/commands/new_params.h"
#include "app/console.h" #include "app/console.h"
#include "app/context.h" #include "app/context.h"
#include "app/context_access.h" #include "app/context_access.h"
#include "app/i18n/strings.h" #include "app/i18n/strings.h"
#include "app/pref/preferences.h"
#include "app/tx.h" #include "app/tx.h"
#include "app/ui/color_bar.h"
#include "app/ui/context_bar.h"
#include "app/ui/editor/editor.h"
#include "doc/palette.h" #include "doc/palette.h"
#include "fmt/format.h" #include "fmt/format.h"
#ifdef ENABLE_SCRIPTING
#include "app/script/luacpp.h"
#endif
namespace app { namespace app {
class AddColorCommand : public Command { enum class AddColorSource { Fg, Bg, Color };
template<>
void Param<AddColorSource>::fromString(const std::string& value)
{
if (value == "fg" ||
value == "foreground")
setValue(AddColorSource::Fg);
else if (value == "bg" ||
value == "background")
setValue(AddColorSource::Bg);
else
setValue(AddColorSource::Color);
}
#ifdef ENABLE_SCRIPTING
template<>
void Param<AddColorSource>::fromLua(lua_State* L, int index)
{
fromString(lua_tostring(L, index));
}
#endif // ENABLE_SCRIPTING
struct AddColorParams : public NewParams {
Param<AddColorSource> source { this, AddColorSource::Color, "source" };
Param<app::Color> color { this, app::Color::fromMask(), "color" };
};
class AddColorCommand : public CommandWithNewParams<AddColorParams> {
public: public:
enum class Source { Fg, Bg, Color };
AddColorCommand(); AddColorCommand();
protected: protected:
bool onNeedsParams() const override { return true; }
void onLoadParams(const Params& params) override;
bool onEnabled(Context* ctx) override; bool onEnabled(Context* ctx) override;
void onExecute(Context* ctx) override; void onExecute(Context* ctx) override;
std::string onGetFriendlyName() const override; std::string onGetFriendlyName() const override;
Source m_source;
app::Color m_color;
}; };
AddColorCommand::AddColorCommand() AddColorCommand::AddColorCommand()
: Command(CommandId::AddColor(), CmdUIOnlyFlag) : CommandWithNewParams<AddColorParams>(CommandId::AddColor(), 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) bool AddColorCommand::onEnabled(Context* ctx)
{ {
return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable); return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable);
@ -73,15 +79,15 @@ void AddColorCommand::onExecute(Context* ctx)
{ {
app::Color appColor; app::Color appColor;
switch (m_source) { switch (params().source()) {
case Source::Fg: case AddColorSource::Fg:
appColor = ColorBar::instance()->getFgColor(); appColor = Preferences::instance().colorBar.fgColor();
break; break;
case Source::Bg: case AddColorSource::Bg:
appColor = ColorBar::instance()->getBgColor(); appColor = Preferences::instance().colorBar.bgColor();
break; break;
case Source::Color: case AddColorSource::Color:
appColor = m_color; appColor = params().color();
break; break;
} }
@ -137,10 +143,10 @@ void AddColorCommand::onExecute(Context* ctx)
std::string AddColorCommand::onGetFriendlyName() const std::string AddColorCommand::onGetFriendlyName() const
{ {
std::string source; std::string source;
switch (m_source) { switch (params().source()) {
case Source::Fg: source = Strings::commands_AddColor_Foreground(); break; case AddColorSource::Fg: source = Strings::commands_AddColor_Foreground(); break;
case Source::Bg: source = Strings::commands_AddColor_Background(); break; case AddColorSource::Bg: source = Strings::commands_AddColor_Background(); break;
case Source::Color: source = Strings::commands_AddColor_Specific(); break; case AddColorSource::Color: source = Strings::commands_AddColor_Specific(); break;
} }
return fmt::format(getBaseFriendlyName(), source); return fmt::format(getBaseFriendlyName(), source);
} }

View File

@ -5,6 +5,7 @@
// This program is distributed under the terms of // This program is distributed under the terms of
// the End-User License Agreement for Aseprite. // the End-User License Agreement for Aseprite.
FOR_EACH_COMMAND(AddColor)
FOR_EACH_COMMAND(AutocropSprite) FOR_EACH_COMMAND(AutocropSprite)
FOR_EACH_COMMAND(BackgroundFromLayer) FOR_EACH_COMMAND(BackgroundFromLayer)
FOR_EACH_COMMAND(BrightnessContrast) FOR_EACH_COMMAND(BrightnessContrast)
@ -42,7 +43,6 @@ FOR_EACH_COMMAND(Undo)
#ifdef ENABLE_UI #ifdef ENABLE_UI
FOR_EACH_COMMAND(About) FOR_EACH_COMMAND(About)
FOR_EACH_COMMAND(AddColor)
FOR_EACH_COMMAND(AdvancedMode) FOR_EACH_COMMAND(AdvancedMode)
FOR_EACH_COMMAND(Cancel) FOR_EACH_COMMAND(Cancel)
FOR_EACH_COMMAND(CelProperties) FOR_EACH_COMMAND(CelProperties)