2016-04-07 02:37:13 +08:00
|
|
|
// Aseprite
|
2018-05-07 11:11:50 +08:00
|
|
|
// Copyright (C) 2015-2018 David Capello
|
2016-04-07 02:37:13 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2016-04-07 02:37:13 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2018-05-07 11:11:50 +08:00
|
|
|
#include "app/app.h"
|
2016-04-16 06:59:25 +08:00
|
|
|
#include "app/commands/commands.h"
|
|
|
|
#include "app/commands/params.h"
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/context.h"
|
|
|
|
#include "app/doc.h"
|
2016-04-07 06:05:06 +08:00
|
|
|
#include "app/script/app_scripting.h"
|
2018-08-21 01:20:27 +08:00
|
|
|
#include "app/site.h"
|
2018-08-21 02:15:38 +08:00
|
|
|
#include "app/site.h"
|
2018-08-21 01:20:27 +08:00
|
|
|
#include "app/tx.h"
|
2016-04-07 02:37:13 +08:00
|
|
|
#include "script/engine.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_open(script::ContextHandle handle)
|
2016-04-16 06:59:25 +08:00
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
2017-08-12 04:22:28 +08:00
|
|
|
const char* filename = ctx.requireString(1);
|
2016-04-16 06:59:25 +08:00
|
|
|
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context* appCtx = App::instance()->context();
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* oldDoc = appCtx->activeDocument();
|
2016-04-16 06:59:25 +08:00
|
|
|
|
2017-11-30 22:57:18 +08:00
|
|
|
Command* openCommand =
|
2017-12-02 02:10:21 +08:00
|
|
|
Commands::instance()->byId(CommandId::OpenFile());
|
2016-04-16 06:59:25 +08:00
|
|
|
Params params;
|
2017-08-12 04:22:28 +08:00
|
|
|
params.set("filename", filename);
|
2018-05-07 11:11:50 +08:00
|
|
|
appCtx->executeCommand(openCommand, params);
|
2016-04-16 06:59:25 +08:00
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* newDoc = appCtx->activeDocument();
|
2016-04-16 06:59:25 +08:00
|
|
|
if (newDoc != oldDoc)
|
2018-08-21 01:20:27 +08:00
|
|
|
push_sprite(ctx, newDoc->sprite());
|
2016-04-16 06:59:25 +08:00
|
|
|
else
|
|
|
|
ctx.pushNull();
|
|
|
|
}
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_exit(script::ContextHandle handle)
|
2016-05-07 03:49:43 +08:00
|
|
|
{
|
2017-08-12 04:22:28 +08:00
|
|
|
script::Context ctx(handle);
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context* appCtx = App::instance()->context();
|
2017-08-12 04:22:28 +08:00
|
|
|
if (appCtx && appCtx->isUIAvailable()) {
|
2017-11-30 22:57:18 +08:00
|
|
|
Command* exitCommand =
|
2017-12-02 02:10:21 +08:00
|
|
|
Commands::instance()->byId(CommandId::Exit());
|
2017-08-12 04:22:28 +08:00
|
|
|
appCtx->executeCommand(exitCommand);
|
|
|
|
}
|
|
|
|
ctx.pushUndefined();
|
2016-05-07 03:49:43 +08:00
|
|
|
}
|
|
|
|
|
2018-08-21 01:20:27 +08:00
|
|
|
void App_transaction(script::ContextHandle handle)
|
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
|
|
|
if (ctx.isCallable(1)) {
|
|
|
|
Tx tx; // Create a new transaction so it exists in the whole
|
|
|
|
// duration of the argument function call.
|
2018-08-21 01:42:28 +08:00
|
|
|
ctx.copy(1);
|
|
|
|
ctx.call(0);
|
2018-08-21 01:20:27 +08:00
|
|
|
tx.commit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ctx.pushUndefined();
|
|
|
|
}
|
|
|
|
|
2018-08-21 01:34:14 +08:00
|
|
|
void App_undo(script::ContextHandle handle)
|
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
|
|
|
app::Context* appCtx = App::instance()->context();
|
|
|
|
if (appCtx) {
|
|
|
|
Command* undo = Commands::instance()->byId(CommandId::Undo());
|
|
|
|
appCtx->executeCommand(undo);
|
|
|
|
}
|
|
|
|
ctx.pushUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
void App_redo(script::ContextHandle handle)
|
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
|
|
|
app::Context* appCtx = App::instance()->context();
|
|
|
|
if (appCtx) {
|
|
|
|
Command* redo = Commands::instance()->byId(CommandId::Redo());
|
|
|
|
appCtx->executeCommand(redo);
|
|
|
|
}
|
|
|
|
ctx.pushUndefined();
|
|
|
|
}
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_get_activeSprite(script::ContextHandle handle)
|
2016-04-07 02:37:13 +08:00
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context* appCtx = App::instance()->context();
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* doc = appCtx->activeDocument();
|
2016-04-07 02:37:13 +08:00
|
|
|
if (doc)
|
2018-08-21 01:20:27 +08:00
|
|
|
push_sprite(ctx, doc->sprite());
|
2016-04-07 06:05:06 +08:00
|
|
|
else
|
|
|
|
ctx.pushNull();
|
|
|
|
}
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_get_activeImage(script::ContextHandle handle)
|
2016-04-07 06:05:06 +08:00
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
2018-05-07 11:11:50 +08:00
|
|
|
app::Context* appCtx = App::instance()->context();
|
2018-08-21 01:20:27 +08:00
|
|
|
Site site = appCtx->activeSite();
|
|
|
|
if (site.image())
|
2018-08-21 02:15:38 +08:00
|
|
|
push_image(ctx, site.image());
|
2018-08-21 01:20:27 +08:00
|
|
|
else
|
|
|
|
ctx.pushNull();
|
2016-04-07 02:37:13 +08:00
|
|
|
}
|
|
|
|
|
2018-08-21 02:15:38 +08:00
|
|
|
void App_get_site(script::ContextHandle handle)
|
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
|
|
|
app::Context* appCtx = App::instance()->context();
|
|
|
|
Site site = appCtx->activeSite();
|
|
|
|
push_site(ctx, site);
|
|
|
|
}
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_get_pixelColor(script::ContextHandle handle)
|
2016-04-07 02:37:13 +08:00
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
2017-08-12 04:22:28 +08:00
|
|
|
ctx.newObject("PixelColor", nullptr, nullptr);
|
2016-04-07 02:37:13 +08:00
|
|
|
}
|
|
|
|
|
2017-08-12 04:22:28 +08:00
|
|
|
void App_get_version(script::ContextHandle handle)
|
2016-04-07 02:37:13 +08:00
|
|
|
{
|
|
|
|
script::Context ctx(handle);
|
|
|
|
ctx.pushString(VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
const script::FunctionEntry App_methods[] = {
|
2016-04-16 06:59:25 +08:00
|
|
|
{ "open", App_open, 1 },
|
2017-08-12 04:22:28 +08:00
|
|
|
{ "exit", App_exit, 0 },
|
2018-08-21 01:20:27 +08:00
|
|
|
{ "transaction", App_transaction, 1 },
|
2018-08-21 01:34:14 +08:00
|
|
|
{ "undo", App_undo, 0 },
|
|
|
|
{ "redo", App_redo, 0 },
|
2016-04-07 02:37:13 +08:00
|
|
|
{ nullptr, nullptr, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const script::PropertyEntry App_props[] = {
|
|
|
|
{ "activeSprite", App_get_activeSprite, nullptr },
|
2017-08-12 04:22:28 +08:00
|
|
|
{ "activeImage", App_get_activeImage, nullptr },
|
2016-04-07 02:37:13 +08:00
|
|
|
{ "pixelColor", App_get_pixelColor, nullptr },
|
|
|
|
{ "version", App_get_version, nullptr },
|
2018-08-21 02:15:38 +08:00
|
|
|
{ "site", App_get_site, nullptr },
|
2016-04-07 02:37:13 +08:00
|
|
|
{ nullptr, nullptr, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void register_app_object(script::Context& ctx)
|
|
|
|
{
|
|
|
|
ctx.pushGlobalObject();
|
|
|
|
ctx.registerObject(-1, "app", App_methods, App_props);
|
|
|
|
ctx.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|