From fa34453a1b52040e4100e47dbee37c74f023befd Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 4 Sep 2018 17:21:33 -0300 Subject: [PATCH] lua: add app.command.CommandName() --- src/app/CMakeLists.txt | 1 + src/app/script/app_command_object.cpp | 95 +++++++++++++++++++++++++++ src/app/script/engine.cpp | 2 + 3 files changed, 98 insertions(+) create mode 100644 src/app/script/app_command_object.cpp diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 9e12e5a30..a5f597c58 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -148,6 +148,7 @@ if(ENABLE_SCRIPTING) endif() set(scripting_files commands/cmd_run_script.cpp + script/app_command_object.cpp script/app_object.cpp script/cel_class.cpp script/cels_class.cpp diff --git a/src/app/script/app_command_object.cpp b/src/app/script/app_command_object.cpp new file mode 100644 index 000000000..3e6828e8e --- /dev/null +++ b/src/app/script/app_command_object.cpp @@ -0,0 +1,95 @@ +// Aseprite +// Copyright (C) 2018 David Capello +// +// This program is distributed under the terms of +// the End-User License Agreement for Aseprite. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/app.h" +#include "app/commands/command.h" +#include "app/commands/commands.h" +#include "app/commands/params.h" +#include "app/context.h" +#include "app/script/luacpp.h" + +namespace app { +namespace script { + +namespace { + +struct AppCommand { }; + +int Command_call(lua_State* L) +{ + app::Context* ctx = App::instance()->context(); + if (!ctx) + return 0; + + auto command = get_ptr(L, 1); + Params params; + + if (lua_istable(L, 2)) { + lua_pushnil(L); + while (lua_next(L, 2) != 0) { + const char* k = lua_tostring(L, -2); + if (k) { + const char* v = luaL_tolstring(L, -1, nullptr); + if (v) { + params.set(k, v); + } + lua_pop(L, 1); + } + lua_pop(L, 1); + } + } + + ctx->executeCommand(command, params); + return 1; +} + +int AppCommand_index(lua_State* L) +{ + const char* id = lua_tostring(L, 2); + if (!id) + return luaL_error(L, "id in app.command.id() must be a string"); + + Command* cmd = Commands::instance()->byId(id); + if (!cmd) + return luaL_error(L, "command '%s' not found", id); + + push_ptr(L, cmd); + return 1; +} + +const luaL_Reg Command_methods[] = { + { "__call", Command_call }, + { nullptr, nullptr } +}; + +const luaL_Reg AppCommand_methods[] = { + { "__index", AppCommand_index }, + { nullptr, nullptr } +}; + +} // anonymous namespace + +DEF_MTNAME(Command); +DEF_MTNAME(AppCommand); + +void register_app_command_object(lua_State* L) +{ + REG_CLASS(L, Command); + REG_CLASS(L, AppCommand); + + lua_getglobal(L, "app"); + lua_pushstring(L, "command"); + push_new(L); + lua_rawset(L, -3); + lua_pop(L, 1); +} + +} // namespace script +} // namespace app diff --git a/src/app/script/engine.cpp b/src/app/script/engine.cpp index 399b2904c..1369c3152 100644 --- a/src/app/script/engine.cpp +++ b/src/app/script/engine.cpp @@ -80,6 +80,7 @@ int unsupported(lua_State* L) void register_app_object(lua_State* L); void register_app_pixel_color_object(lua_State* L); +void register_app_command_object(lua_State* L); void register_cel_class(lua_State* L); void register_cels_class(lua_State* L); @@ -140,6 +141,7 @@ Engine::Engine() // Register global app object register_app_object(L); register_app_pixel_color_object(L); + register_app_command_object(L); // Register constants lua_newtable(L);