Add RunScript command with Edit > Scripts menu

This commit is contained in:
David Capello 2015-08-27 12:40:20 -03:00
parent ffdf8ad568
commit 890931f270
6 changed files with 110 additions and 2 deletions

View File

@ -535,6 +535,11 @@
<separator />
<item command="Despeckle" text="&amp;Despeckle (median filter)" />
</menu>
<menu text="Scripts">
<item command="RunScript" text="Transparency from White Background">
<param name="filename" value="white_to_alpha.js" />
</item>
</menu>
<separator />
<item command="KeyboardShortcuts" text="&amp;Keyboard Shortcuts..." />
<item command="Options" text="Pre&amp;ferences..." />

View File

@ -0,0 +1,15 @@
// Aseprite
// Copyright (C) 2015 by David Capello
var spr = activeSprite
for (y=0; y<spr.height; ++y) {
for (x=0; x<spr.width; ++x) {
var c = spr.getPixel(x, y)
var v = (rgbaR(c)+rgbaG(c)+rgbaB(c))/3
spr.putPixel(x, y, rgba(rgbaR(c),
rgbaG(c),
rgbaB(c),
255-v))
}
}

View File

@ -218,6 +218,7 @@ add_library(app-lib
commands/cmd_reselect_mask.cpp
commands/cmd_reverse_frames.cpp
commands/cmd_rotate.cpp
commands/cmd_run_script.cpp
commands/cmd_save_file.cpp
commands/cmd_save_mask.cpp
commands/cmd_save_palette.cpp

View File

@ -0,0 +1,80 @@
// Aseprite
// Copyright (C) 2001-2015 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/commands/command.h"
#include "app/commands/params.h"
#include "app/console.h"
#include "app/resource_finder.h"
#include "app/scripting/app_scripting.h"
#include "base/path.h"
#include "scripting/engine_delegate.h"
#include "ui/manager.h"
#include <cstdio>
namespace app {
class ConsoleEngineDelegate : public scripting::EngineDelegate {
public:
void onConsolePrint(const char* text) override {
m_console.printf("%s\n", text);
}
private:
Console m_console;
};
class RunScriptCommand : public Command {
public:
RunScriptCommand();
Command* clone() const override { return new RunScriptCommand(*this); }
protected:
void onLoadParams(const Params& params) override;
void onExecute(Context* context) override;
private:
std::string m_filename;
};
RunScriptCommand::RunScriptCommand()
: Command("RunScript",
"Run Script",
CmdRecordableFlag)
{
}
void RunScriptCommand::onLoadParams(const Params& params)
{
m_filename = params.get("filename");
if (base::get_file_path(m_filename).empty()) {
ResourceFinder rf;
rf.includeDataDir(base::join_path("scripts", m_filename).c_str());
if (rf.findFirst())
m_filename = rf.filename();
}
}
void RunScriptCommand::onExecute(Context* context)
{
ConsoleEngineDelegate delegate;
AppScripting engine(&delegate);
engine.evalFile(m_filename);
ui::Manager::getDefault()->invalidate();
}
Command* CommandFactory::createRunScriptCommand()
{
return new RunScriptCommand;
}
} // namespace app

View File

@ -94,6 +94,7 @@ FOR_EACH_COMMAND(ReplaceColor)
FOR_EACH_COMMAND(ReselectMask)
FOR_EACH_COMMAND(ReverseFrames)
FOR_EACH_COMMAND(Rotate)
FOR_EACH_COMMAND(RunScript)
FOR_EACH_COMMAND(SaveFile)
FOR_EACH_COMMAND(SaveFileAs)
FOR_EACH_COMMAND(SaveFileCopyAs)

View File

@ -221,7 +221,10 @@ void Engine::eval(const std::string& jsCode)
ContextHandle handle = m_ctx.handle();
duk_eval_string(handle, jsCode.c_str());
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
if (!duk_is_null_or_undefined(handle, -1))
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
duk_pop(handle);
}
catch (const std::exception& ex) {
@ -258,7 +261,10 @@ void Engine::evalFile(const std::string& file)
duk_push_string(handle, duk_to_string(handle, -1));
duk_eval_raw(handle, nullptr, 0, DUK_COMPILE_EVAL);
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
if (!duk_is_null_or_undefined(handle, -1))
m_delegate->onConsolePrint(duk_safe_to_string(handle, -1));
duk_pop(handle);
fail: