1
0
mirror of https://github.com/aseprite/aseprite.git synced 2025-02-22 06:41:08 +00:00

lua: Add app.activeTool + Tool object

This commit is contained in:
David Capello 2019-03-23 09:18:59 -03:00
parent 54f95dda77
commit 396824383f
5 changed files with 91 additions and 3 deletions

@ -181,6 +181,7 @@ if(ENABLE_SCRIPTING)
script/sprites_class.cpp
script/tag_class.cpp
script/tags_class.cpp
script/tool_class.cpp
shell.cpp
${scripting_files_ui})
endif()

@ -205,9 +205,8 @@ int App_toolStroke(lua_State* L)
tools::Tool* tool = App::instance()->activeToolManager()->activeTool();
tools::Ink* ink = tool->getInk(0);
int type = lua_getfield(L, 1, "tool");
if (type == LUA_TSTRING) {
const char* toolId = lua_tostring(L, -1);
tool = App::instance()->toolBox()->getToolById(toolId);
if (auto toolArg = get_tool_from_arg(L, -1)) {
tool = toolArg;
ink = tool->getInk(0);
}
lua_pop(L, 1);
@ -490,6 +489,20 @@ int App_set_activeImage(lua_State* L)
return 0;
}
int App_get_activeTool(lua_State* L)
{
tools::Tool* tool = App::instance()->activeToolManager()->activeTool();
push_tool(L, tool);
return 1;
}
int App_set_activeTool(lua_State* L)
{
if (auto tool = get_tool_from_arg(L, 2))
App::instance()->activeToolManager()->setSelectedTool(tool);
return 0;
}
const luaL_Reg App_methods[] = {
{ "open", App_open },
{ "exit", App_exit },
@ -509,6 +522,7 @@ const Property App_properties[] = {
{ "activeCel", App_get_activeCel, App_set_activeCel },
{ "activeImage", App_get_activeImage, App_set_activeImage },
{ "activeTag", App_get_activeTag, nullptr },
{ "activeTool", App_get_activeTool, App_set_activeTool },
{ "sprites", App_get_sprites, nullptr },
{ "fgColor", App_get_fgColor, App_set_fgColor },
{ "bgColor", App_get_bgColor, App_set_bgColor },

@ -163,6 +163,7 @@ void register_sprite_class(lua_State* L);
void register_sprites_class(lua_State* L);
void register_tag_class(lua_State* L);
void register_tags_class(lua_State* L);
void register_tool_class(lua_State* L);
void set_app_params(lua_State* L, const Params& params);
@ -325,6 +326,7 @@ Engine::Engine()
register_sprites_class(L);
register_tag_class(L);
register_tags_class(L);
register_tool_class(L);
// Check that we have a clean start (without dirty in the stack)
ASSERT(lua_gettop(L) == top);

@ -40,6 +40,10 @@ namespace doc {
class WithUserData;
}
namespace tools {
class Tool;
}
namespace app {
class DocRange;
@ -124,6 +128,7 @@ namespace app {
void push_sprite_tags(lua_State* L, doc::Sprite* sprite);
void push_sprites(lua_State* L);
void push_userdata(lua_State* L, doc::WithUserData* userData);
void push_tool(lua_State* L, tools::Tool* tool);
gfx::Point convert_args_into_point(lua_State* L, int index);
gfx::Rect convert_args_into_rect(lua_State* L, int index);
@ -136,6 +141,7 @@ namespace app {
doc::Cel* get_image_cel_from_arg(lua_State* L, int index);
doc::frame_t get_frame_number_from_arg(lua_State* L, int index);
const doc::Mask* get_mask_from_arg(lua_State* L, int index);
tools::Tool* get_tool_from_arg(lua_State* L, int index);
} // namespace script
} // namespace app

@ -0,0 +1,65 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
//
// 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/script/engine.h"
#include "app/script/luacpp.h"
#include "app/tools/tool.h"
#include "app/tools/tool_box.h"
namespace app {
namespace script {
namespace {
int Tool_get_id(lua_State* L)
{
auto tool = get_ptr<tools::Tool>(L, 1);
lua_pushstring(L, tool->getId().c_str());
return 1;
}
const luaL_Reg Tool_methods[] = {
{ nullptr, nullptr }
};
const Property Tool_properties[] = {
{ "id", Tool_get_id, nullptr },
{ nullptr, nullptr, nullptr }
};
} // anonymous namespace
using Tool = tools::Tool;
DEF_MTNAME(Tool);
void register_tool_class(lua_State* L)
{
REG_CLASS(L, Tool);
REG_CLASS_PROPERTIES(L, Tool);
}
void push_tool(lua_State* L, tools::Tool* tool)
{
push_ptr<Tool>(L, tool);
}
tools::Tool* get_tool_from_arg(lua_State* L, int index)
{
if (auto tool = may_get_ptr<tools::Tool>(L, index))
return tool;
else if (const char* id = lua_tostring(L, index))
return App::instance()->toolBox()->getToolById(id);
else
return nullptr;
}
} // namespace script
} // namespace app