mirror of
https://github.com/aseprite/aseprite.git
synced 2025-01-26 03:35:23 +00:00
Add RunScript command with Edit > Scripts menu
This commit is contained in:
parent
ffdf8ad568
commit
890931f270
@ -535,6 +535,11 @@
|
||||
<separator />
|
||||
<item command="Despeckle" text="&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="&Keyboard Shortcuts..." />
|
||||
<item command="Options" text="Pre&ferences..." />
|
||||
|
15
data/scripts/white_to_alpha.js
Normal file
15
data/scripts/white_to_alpha.js
Normal 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))
|
||||
}
|
||||
}
|
@ -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
|
||||
|
80
src/app/commands/cmd_run_script.cpp
Normal file
80
src/app/commands/cmd_run_script.cpp
Normal 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
|
@ -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)
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user