aseprite/src/app/script/app_object.cpp

168 lines
3.6 KiB
C++
Raw Normal View History

2016-04-07 02:37:13 +08:00
// Aseprite
// 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
#include "app/app.h"
#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"
2018-08-20 22:25:08 +08:00
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "app/site.h"
#include "app/site.h"
#include "app/tx.h"
2016-04-07 02:37:13 +08:00
#include <iostream>
namespace app {
2018-08-20 22:25:08 +08:00
namespace script {
2016-04-07 02:37:13 +08:00
namespace {
2018-08-20 22:25:08 +08:00
int App_open(lua_State* L)
{
2018-08-20 22:25:08 +08:00
const char* filename = luaL_checkstring(L, 1);
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
Doc* oldDoc = ctx->activeDocument();
2017-11-30 22:57:18 +08:00
Command* openCommand =
Commands::instance()->byId(CommandId::OpenFile());
Params params;
params.set("filename", filename);
2018-08-20 22:25:08 +08:00
ctx->executeCommand(openCommand, params);
2018-08-20 22:25:08 +08:00
Doc* newDoc = ctx->activeDocument();
if (newDoc != oldDoc)
2018-08-20 22:25:08 +08:00
push_ptr(L, newDoc->sprite());
else
2018-08-20 22:25:08 +08:00
lua_pushnil(L);
return 1;
}
2018-08-20 22:25:08 +08:00
int App_exit(lua_State* L)
2016-05-07 03:49:43 +08:00
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
if (ctx && ctx->isUIAvailable()) {
2017-11-30 22:57:18 +08:00
Command* exitCommand =
Commands::instance()->byId(CommandId::Exit());
2018-08-20 22:25:08 +08:00
ctx->executeCommand(exitCommand);
}
2018-08-20 22:25:08 +08:00
return 0;
2016-05-07 03:49:43 +08:00
}
2018-08-20 22:25:08 +08:00
int App_transaction(lua_State* L)
{
2018-08-20 22:25:08 +08:00
int top = lua_gettop(L);
int nresults = 0;
if (lua_isfunction(L, 1)) {
Tx tx; // Create a new transaction so it exists in the whole
// duration of the argument function call.
2018-08-20 22:25:08 +08:00
lua_pushvalue(L, -1);
if (lua_pcall(L, 0, LUA_MULTRET, 0) == LUA_OK)
tx.commit();
nresults = lua_gettop(L) - top;
}
2018-08-20 22:25:08 +08:00
return nresults;
}
2018-08-20 22:25:08 +08:00
int App_undo(lua_State* L)
2018-08-21 01:34:14 +08:00
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
if (ctx) {
2018-08-21 01:34:14 +08:00
Command* undo = Commands::instance()->byId(CommandId::Undo());
2018-08-20 22:25:08 +08:00
ctx->executeCommand(undo);
2018-08-21 01:34:14 +08:00
}
2018-08-20 22:25:08 +08:00
return 0;
2018-08-21 01:34:14 +08:00
}
2018-08-20 22:25:08 +08:00
int App_redo(lua_State* L)
2018-08-21 01:34:14 +08:00
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
if (ctx) {
2018-08-21 01:34:14 +08:00
Command* redo = Commands::instance()->byId(CommandId::Redo());
2018-08-20 22:25:08 +08:00
ctx->executeCommand(redo);
2018-08-21 01:34:14 +08:00
}
2018-08-20 22:25:08 +08:00
return 0;
2018-08-21 01:34:14 +08:00
}
2018-08-20 22:25:08 +08:00
int App_get_activeSprite(lua_State* L)
2016-04-07 02:37:13 +08:00
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
Doc* doc = ctx->activeDocument();
2016-04-07 02:37:13 +08:00
if (doc)
2018-08-20 22:25:08 +08:00
push_ptr(L, doc->sprite());
else
2018-08-20 22:25:08 +08:00
lua_pushnil(L);
return 1;
}
2018-08-20 22:25:08 +08:00
int App_get_activeImage(lua_State* L)
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite();
if (site.cel())
push_cel_image(L, site.cel());
else
2018-08-20 22:25:08 +08:00
lua_pushnil(L);
return 1;
2016-04-07 02:37:13 +08:00
}
2018-08-20 22:25:08 +08:00
int App_get_site(lua_State* L)
{
2018-08-20 22:25:08 +08:00
app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite();
push_obj(L, site);
return 1;
}
2018-08-20 22:25:08 +08:00
int App_get_version(lua_State* L)
2016-04-07 02:37:13 +08:00
{
2018-08-20 22:25:08 +08:00
lua_pushstring(L, VERSION);
return 1;
2016-04-07 02:37:13 +08:00
}
2018-08-20 22:25:08 +08:00
const luaL_Reg App_methods[] = {
{ "open", App_open },
{ "exit", App_exit },
{ "transaction", App_transaction },
{ "undo", App_undo },
{ "redo", App_redo },
{ nullptr, nullptr }
2016-04-07 02:37:13 +08:00
};
2018-08-20 22:25:08 +08:00
const Property App_properties[] = {
2016-04-07 02:37:13 +08:00
{ "activeSprite", App_get_activeSprite, nullptr },
{ "activeImage", App_get_activeImage, nullptr },
2016-04-07 02:37:13 +08:00
{ "version", App_get_version, nullptr },
{ "site", App_get_site, nullptr },
2018-08-20 22:25:08 +08:00
{ nullptr, nullptr, nullptr }
2016-04-07 02:37:13 +08:00
};
} // anonymous namespace
2018-08-20 22:25:08 +08:00
DEF_MTNAME(App);
void register_app_object(lua_State* L)
2016-04-07 02:37:13 +08:00
{
2018-08-20 22:25:08 +08:00
REG_CLASS(L, App);
REG_CLASS_PROPERTIES(L, App);
lua_newtable(L); // Create a table which will be the "app" object
lua_pushvalue(L, -1);
luaL_getmetatable(L, get_mtname<App>());
lua_setmetatable(L, -2);
lua_setglobal(L, "app");
lua_pop(L, 1); // Pop app table
2016-04-07 02:37:13 +08:00
}
2018-08-20 22:25:08 +08:00
} // namespace script
2016-04-07 02:37:13 +08:00
} // namespace app