1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/app.h"
12#include "app/script/engine.h"
13#include "app/script/luacpp.h"
14#include "app/tools/tool.h"
15#include "app/tools/tool_box.h"
16
17namespace app {
18namespace script {
19
20namespace {
21
22int Tool_get_id(lua_State* L)
23{
24 auto tool = get_ptr<tools::Tool>(L, 1);
25 lua_pushstring(L, tool->getId().c_str());
26 return 1;
27}
28
29const luaL_Reg Tool_methods[] = {
30 { nullptr, nullptr }
31};
32
33const Property Tool_properties[] = {
34 { "id", Tool_get_id, nullptr },
35 { nullptr, nullptr, nullptr }
36};
37
38} // anonymous namespace
39
40using Tool = tools::Tool;
41DEF_MTNAME(Tool);
42
43void register_tool_class(lua_State* L)
44{
45 REG_CLASS(L, Tool);
46 REG_CLASS_PROPERTIES(L, Tool);
47}
48
49void push_tool(lua_State* L, tools::Tool* tool)
50{
51 push_ptr<Tool>(L, tool);
52}
53
54tools::Tool* get_tool_from_arg(lua_State* L, int index)
55{
56 if (auto tool = may_get_ptr<tools::Tool>(L, index))
57 return tool;
58 else if (const char* id = lua_tostring(L, index))
59 return App::instance()->toolBox()->getToolById(id);
60 else
61 return nullptr;
62}
63
64} // namespace script
65} // namespace app
66