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"
|
2018-08-20 22:25:08 +08:00
|
|
|
#include "app/script/engine.h"
|
|
|
|
#include "app/script/luacpp.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 <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)
|
2016-04-16 06:59:25 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
const char* filename = luaL_checkstring(L, 1);
|
2016-04-16 06:59:25 +08:00
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
app::Context* ctx = App::instance()->context();
|
|
|
|
Doc* oldDoc = ctx->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-08-20 22:25:08 +08:00
|
|
|
ctx->executeCommand(openCommand, params);
|
2016-04-16 06:59:25 +08:00
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
Doc* newDoc = ctx->activeDocument();
|
2016-04-16 06:59:25 +08:00
|
|
|
if (newDoc != oldDoc)
|
2018-08-20 22:25:08 +08:00
|
|
|
push_ptr(L, newDoc->sprite());
|
2016-04-16 06:59:25 +08:00
|
|
|
else
|
2018-08-20 22:25:08 +08:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
2016-04-16 06:59:25 +08:00
|
|
|
}
|
|
|
|
|
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 =
|
2017-12-02 02:10:21 +08:00
|
|
|
Commands::instance()->byId(CommandId::Exit());
|
2018-08-20 22:25:08 +08:00
|
|
|
ctx->executeCommand(exitCommand);
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
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-21 01:20:27 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
int top = lua_gettop(L);
|
|
|
|
int nresults = 0;
|
|
|
|
if (lua_isfunction(L, 1)) {
|
2018-08-21 01:20:27 +08:00
|
|
|
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-21 01:20:27 +08:00
|
|
|
}
|
2018-08-20 22:25:08 +08:00
|
|
|
return nresults;
|
2018-08-21 01:20:27 +08:00
|
|
|
}
|
|
|
|
|
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());
|
2016-04-07 06:05:06 +08:00
|
|
|
else
|
2018-08-20 22:25:08 +08:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
2016-04-07 06:05:06 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int App_get_activeImage(lua_State* L)
|
2016-04-07 06:05:06 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
app::Context* ctx = App::instance()->context();
|
|
|
|
Site site = ctx->activeSite();
|
2018-08-21 01:20:27 +08:00
|
|
|
if (site.image())
|
2018-08-20 22:25:08 +08:00
|
|
|
push_ptr(L, site.image());
|
2018-08-21 01:20:27 +08:00
|
|
|
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-21 02:15:38 +08:00
|
|
|
{
|
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-21 02:15:38 +08:00
|
|
|
}
|
|
|
|
|
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 },
|
2017-08-12 04:22:28 +08:00
|
|
|
{ "activeImage", App_get_activeImage, nullptr },
|
2016-04-07 02:37:13 +08:00
|
|
|
{ "version", App_get_version, nullptr },
|
2018-08-21 02:15:38 +08:00
|
|
|
{ "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
|