aseprite/src/app/script/app_object.cpp

120 lines
2.9 KiB
C++
Raw Normal View History

2016-04-07 02:37:13 +08:00
// Aseprite
// Copyright (C) 2015-2017 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
#include "app/document.h"
#include "app/commands/commands.h"
#include "app/commands/params.h"
#include "app/script/app_scripting.h"
#include "app/script/sprite_wrap.h"
2016-04-07 02:37:13 +08:00
#include "app/ui_context.h"
#include "script/engine.h"
#include <iostream>
namespace app {
namespace {
void App_open(script::ContextHandle handle)
{
script::Context ctx(handle);
const char* filename = ctx.requireString(1);
app::Document* oldDoc = UIContext::instance()->activeDocument();
2017-11-30 22:57:18 +08:00
Command* openCommand =
Commands::instance()->byId(CommandId::OpenFile());
Params params;
params.set("filename", filename);
UIContext::instance()->executeCommand(openCommand, params);
app::Document* newDoc = UIContext::instance()->activeDocument();
if (newDoc != oldDoc)
ctx.newObject("Sprite", unwrap_engine(ctx)->wrapSprite(newDoc), nullptr);
else
ctx.pushNull();
}
void App_exit(script::ContextHandle handle)
2016-05-07 03:49:43 +08:00
{
script::Context ctx(handle);
UIContext* appCtx = UIContext::instance();
if (appCtx && appCtx->isUIAvailable()) {
2017-11-30 22:57:18 +08:00
Command* exitCommand =
Commands::instance()->byId(CommandId::Exit());
appCtx->executeCommand(exitCommand);
}
ctx.pushUndefined();
2016-05-07 03:49:43 +08:00
}
void App_get_activeSprite(script::ContextHandle handle)
2016-04-07 02:37:13 +08:00
{
script::Context ctx(handle);
app::Document* doc = UIContext::instance()->activeDocument();
if (doc)
ctx.newObject("Sprite", unwrap_engine(ctx)->wrapSprite(doc), nullptr);
else
ctx.pushNull();
}
void App_get_activeImage(script::ContextHandle handle)
{
script::Context ctx(handle);
app::Document* doc = UIContext::instance()->activeDocument();
if (doc) {
SpriteWrap* sprWrap = unwrap_engine(ctx)->wrapSprite(doc);
ASSERT(sprWrap);
ImageWrap* imgWrap = sprWrap->activeImage();
if (imgWrap != nullptr) {
ctx.newObject("Image", imgWrap, nullptr);
return;
}
}
ctx.pushNull();
2016-04-07 02:37:13 +08:00
}
void App_get_pixelColor(script::ContextHandle handle)
2016-04-07 02:37:13 +08:00
{
script::Context ctx(handle);
ctx.newObject("PixelColor", nullptr, nullptr);
2016-04-07 02:37:13 +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[] = {
{ "open", App_open, 1 },
{ "exit", App_exit, 0 },
2016-04-07 02:37:13 +08:00
{ nullptr, nullptr, 0 }
};
const script::PropertyEntry App_props[] = {
{ "activeSprite", App_get_activeSprite, nullptr },
{ "activeImage", App_get_activeImage, nullptr },
2016-04-07 02:37:13 +08:00
{ "pixelColor", App_get_pixelColor, nullptr },
{ "version", App_get_version, nullptr },
{ 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