1// Aseprite
2// Copyright (C) 2018-2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_SCRIPTING_H_INCLUDED
9#define APP_SCRIPTING_H_INCLUDED
10#pragma once
11
12#ifndef ENABLE_SCRIPTING
13 #error ENABLE_SCRIPTING must be defined
14#endif
15
16#include "app/color.h"
17#include "app/commands/params.h"
18#include "app/extensions.h"
19#include "doc/brush.h"
20#include "doc/frame.h"
21#include "doc/object_ids.h"
22#include "doc/pixel_format.h"
23#include "gfx/fwd.h"
24
25#include <cstdio>
26#include <functional>
27#include <map>
28#include <string>
29
30struct lua_State;
31struct lua_Debug;
32
33namespace base {
34 class Version;
35}
36
37namespace gfx {
38 class ColorSpace;
39}
40
41namespace doc {
42 class Cel;
43 class Image;
44 class Layer;
45 class LayerGroup;
46 class Mask;
47 class Palette;
48 class Sprite;
49 class Tag;
50 class Tileset;
51 class Tilesets;
52 class WithUserData;
53}
54
55namespace app {
56
57 class Site;
58
59 namespace tools {
60 class Tool;
61 }
62
63 namespace script {
64
65 class EngineDelegate {
66 public:
67 virtual ~EngineDelegate() { }
68 virtual void onConsoleError(const char* text) = 0;
69 virtual void onConsolePrint(const char* text) = 0;
70 };
71
72 class DebuggerDelegate {
73 public:
74 virtual ~DebuggerDelegate() { }
75 virtual void hook(lua_State* L, lua_Debug* ar) = 0;
76 virtual void startFile(const std::string& file,
77 const std::string& content) = 0;
78 virtual void endFile(const std::string& file) = 0;
79 };
80
81 class Engine {
82 public:
83 Engine();
84 ~Engine();
85
86 void destroy();
87
88 EngineDelegate* delegate() { return m_delegate; }
89 void setDelegate(EngineDelegate* delegate) {
90 m_delegate = delegate;
91 }
92
93 void printLastResult();
94 bool evalCode(const std::string& code,
95 const std::string& filename = std::string());
96 bool evalFile(const std::string& filename,
97 const Params& params = Params());
98
99 void consolePrint(const char* text) {
100 onConsolePrint(text);
101 }
102
103 int returnCode() const {
104 return m_returnCode;
105 }
106
107 lua_State* luaState() { return L; }
108
109 void startDebugger(DebuggerDelegate* debuggerDelegate);
110 void stopDebugger();
111
112 private:
113 void onConsoleError(const char* text);
114 void onConsolePrint(const char* text);
115
116 lua_State* L;
117 EngineDelegate* m_delegate;
118 bool m_printLastResult;
119 int m_returnCode;
120 };
121
122 class ScopedEngineDelegate {
123 public:
124 ScopedEngineDelegate(Engine* engine, EngineDelegate* delegate)
125 : m_engine(engine),
126 m_oldDelegate(engine->delegate()) {
127 m_engine->setDelegate(delegate);
128 }
129 ~ScopedEngineDelegate() {
130 m_engine->setDelegate(m_oldDelegate);
131 }
132 private:
133 Engine* m_engine;
134 EngineDelegate* m_oldDelegate;
135 };
136
137 void push_app_events(lua_State* L);
138 int push_image_iterator_function(lua_State* L, const doc::Image* image, int extraArgIndex);
139 void push_brush(lua_State* L, const doc::BrushRef& brush);
140 void push_cel_image(lua_State* L, doc::Cel* cel);
141 void push_cel_images(lua_State* L, const doc::ObjectIds& cels);
142 void push_cels(lua_State* L, const doc::ObjectIds& cels);
143 void push_cels(lua_State* L, doc::Layer* layer);
144 void push_cels(lua_State* L, doc::Sprite* sprite);
145 void push_color_space(lua_State* L, const gfx::ColorSpace& cs);
146 void push_doc_range(lua_State* L, Site& site);
147 void push_group_layers(lua_State* L, doc::LayerGroup* group);
148 void push_image(lua_State* L, doc::Image* image);
149 void push_layers(lua_State* L, const doc::ObjectIds& layers);
150 void push_palette(lua_State* L, doc::Palette* palette);
151 void push_plugin(lua_State* L, Extension* ext);
152 void push_sprite_cel(lua_State* L, doc::Cel* cel);
153 void push_sprite_events(lua_State* L, doc::Sprite* sprite);
154 void push_sprite_frame(lua_State* L, doc::Sprite* sprite, doc::frame_t frame);
155 void push_sprite_frames(lua_State* L, doc::Sprite* sprite);
156 void push_sprite_frames(lua_State* L, doc::Sprite* sprite, const std::vector<doc::frame_t>& frames);
157 void push_sprite_layers(lua_State* L, doc::Sprite* sprite);
158 void push_sprite_palette(lua_State* L, doc::Sprite* sprite, doc::Palette* palette);
159 void push_sprite_palettes(lua_State* L, doc::Sprite* sprite);
160 void push_sprite_selection(lua_State* L, doc::Sprite* sprite);
161 void push_sprite_slices(lua_State* L, doc::Sprite* sprite);
162 void push_sprite_tags(lua_State* L, doc::Sprite* sprite);
163 void push_sprites(lua_State* L);
164 void push_tileset(lua_State* L, doc::Tileset* tileset);
165 void push_tileset_image(lua_State* L, doc::Tileset* tileset, doc::Image* image);
166 void push_tilesets(lua_State* L, doc::Tilesets* tilesets);
167 void push_tool(lua_State* L, app::tools::Tool* tool);
168 void push_userdata(lua_State* L, doc::WithUserData* userData);
169 void push_version(lua_State* L, const base::Version& ver);
170
171 gfx::Point convert_args_into_point(lua_State* L, int index);
172 gfx::Rect convert_args_into_rect(lua_State* L, int index);
173 gfx::Size convert_args_into_size(lua_State* L, int index);
174 app::Color convert_args_into_color(lua_State* L, int index);
175 doc::color_t convert_args_into_pixel_color(lua_State* L, int index,
176 const doc::PixelFormat pixelFormat);
177 doc::Palette* get_palette_from_arg(lua_State* L, int index);
178 doc::Image* may_get_image_from_arg(lua_State* L, int index);
179 doc::Image* get_image_from_arg(lua_State* L, int index);
180 doc::Cel* get_image_cel_from_arg(lua_State* L, int index);
181 doc::frame_t get_frame_number_from_arg(lua_State* L, int index);
182 const doc::Mask* get_mask_from_arg(lua_State* L, int index);
183 app::tools::Tool* get_tool_from_arg(lua_State* L, int index);
184 doc::BrushRef get_brush_from_arg(lua_State* L, int index);
185
186 // Used by App.open(), Sprite{ fromFile }, and Image{ fromFile }
187 enum class LoadSpriteFromFileParam { FullAniAsSprite,
188 OneFrameAsSprite,
189 OneFrameAsImage };
190 int load_sprite_from_file(lua_State* L, const char* filename,
191 const LoadSpriteFromFileParam param);
192
193#ifdef ENABLE_UI
194 // close all opened Dialogs before closing the UI
195 void close_all_dialogs();
196#endif
197
198} // namespace script
199} // namespace app
200
201#endif
202