mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-11 00:40:08 +00:00
[lua] Add new app.editor object (fix aseprite/Attachment-System#104)
This commit is contained in:
parent
622b02294a
commit
d7af7f4312
@ -173,6 +173,7 @@ if(ENABLE_SCRIPTING)
|
||||
script/color_class.cpp
|
||||
script/color_space_class.cpp
|
||||
script/dialog_class.cpp
|
||||
script/editor_class.cpp
|
||||
script/engine.cpp
|
||||
script/events_class.cpp
|
||||
script/frame_class.cpp
|
||||
|
@ -510,6 +510,18 @@ int App_get_uiScale(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int App_get_editor(lua_State* L)
|
||||
{
|
||||
#ifdef ENABLE_UI
|
||||
auto ctx = UIContext::instance();
|
||||
if (Editor* editor = ctx->activeEditor()) {
|
||||
push_editor(L, editor);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int App_get_sprite(lua_State* L)
|
||||
{
|
||||
app::Context* ctx = App::instance()->context();
|
||||
@ -805,6 +817,7 @@ const Property App_properties[] = {
|
||||
{ "events", App_get_events, nullptr },
|
||||
{ "theme", App_get_theme, nullptr },
|
||||
{ "uiScale", App_get_uiScale, nullptr },
|
||||
{ "editor", App_get_editor, nullptr },
|
||||
{ nullptr, nullptr, nullptr }
|
||||
};
|
||||
|
||||
|
114
src/app/script/editor_class.cpp
Normal file
114
src/app/script/editor_class.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
// Aseprite
|
||||
// Copyright (c) 2023 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/script/docobj.h"
|
||||
#include "app/script/luacpp.h"
|
||||
#include "app/ui/editor/editor.h"
|
||||
|
||||
#ifdef ENABLE_UI
|
||||
|
||||
namespace app {
|
||||
namespace script {
|
||||
|
||||
using namespace doc;
|
||||
|
||||
namespace {
|
||||
|
||||
class EditorObj : public EditorObserver {
|
||||
public:
|
||||
EditorObj(Editor* editor) : m_editor(editor) {
|
||||
if (m_editor)
|
||||
m_editor->add_observer(this);
|
||||
}
|
||||
|
||||
EditorObj(const EditorObj&) = delete;
|
||||
EditorObj& operator=(const EditorObj&) = delete;
|
||||
|
||||
~EditorObj() {
|
||||
ASSERT(!m_editor);
|
||||
}
|
||||
|
||||
void gc(lua_State* L) {
|
||||
removeEditor();
|
||||
}
|
||||
|
||||
Editor* editor() const { return m_editor; }
|
||||
|
||||
// EditorObserver
|
||||
void onDestroyEditor(Editor* editor) override {
|
||||
ASSERT(editor == m_editor);
|
||||
removeEditor();
|
||||
}
|
||||
|
||||
private:
|
||||
void removeEditor() {
|
||||
if (m_editor) {
|
||||
m_editor->remove_observer(this);
|
||||
m_editor = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Editor* m_editor = nullptr;
|
||||
};
|
||||
|
||||
int Editor_gc(lua_State* L)
|
||||
{
|
||||
auto obj = get_obj<EditorObj>(L, 1);
|
||||
obj->gc(L);
|
||||
obj->~EditorObj();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Editor_eq(lua_State* L)
|
||||
{
|
||||
auto a = get_obj<EditorObj>(L, 1);
|
||||
auto b = get_obj<EditorObj>(L, 2);
|
||||
lua_pushboolean(L, a->editor() == b->editor());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Editor_get_sprite(lua_State* L)
|
||||
{
|
||||
auto obj = get_obj<EditorObj>(L, 1);
|
||||
push_docobj(L, obj->editor()->sprite());
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg Editor_methods[] = {
|
||||
{ "__gc", Editor_gc },
|
||||
{ "__eq", Editor_eq },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
|
||||
const Property Editor_properties[] = {
|
||||
{ "sprite", Editor_get_sprite, nullptr },
|
||||
{ nullptr, nullptr, nullptr }
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
DEF_MTNAME(EditorObj);
|
||||
|
||||
void register_editor_class(lua_State* L)
|
||||
{
|
||||
using Editor = EditorObj;
|
||||
REG_CLASS(L, Editor);
|
||||
REG_CLASS_PROPERTIES(L, Editor);
|
||||
}
|
||||
|
||||
void push_editor(lua_State* L, Editor* editor)
|
||||
{
|
||||
push_new<EditorObj>(L, editor);
|
||||
}
|
||||
|
||||
} // namespace script
|
||||
} // namespace app
|
||||
|
||||
#endif // ENABLE_UI
|
@ -161,6 +161,7 @@ void register_color_class(lua_State* L);
|
||||
void register_color_space_class(lua_State* L);
|
||||
#ifdef ENABLE_UI
|
||||
void register_dialog_class(lua_State* L);
|
||||
void register_editor_class(lua_State* L);
|
||||
void register_graphics_context_class(lua_State* L);
|
||||
#endif
|
||||
void register_events_class(lua_State* L);
|
||||
@ -456,6 +457,7 @@ Engine::Engine()
|
||||
register_color_space_class(L);
|
||||
#ifdef ENABLE_UI
|
||||
register_dialog_class(L);
|
||||
register_editor_class(L);
|
||||
register_graphics_context_class(L);
|
||||
#endif
|
||||
register_events_class(L);
|
||||
@ -494,7 +496,7 @@ Engine::Engine()
|
||||
register_version_class(L);
|
||||
#if ENABLE_WEBSOCKET
|
||||
register_websocket_class(L);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Check that we have a clean start (without dirty in the stack)
|
||||
ASSERT(lua_gettop(L) == top);
|
||||
|
@ -55,6 +55,7 @@ namespace doc {
|
||||
|
||||
namespace app {
|
||||
|
||||
class Editor;
|
||||
class Site;
|
||||
|
||||
namespace tools {
|
||||
@ -148,6 +149,7 @@ namespace app {
|
||||
void push_cels(lua_State* L, doc::Sprite* sprite);
|
||||
void push_color_space(lua_State* L, const gfx::ColorSpace& cs);
|
||||
void push_doc_range(lua_State* L, Site& site);
|
||||
void push_editor(lua_State* L, Editor* editor);
|
||||
void push_group_layers(lua_State* L, doc::LayerGroup* group);
|
||||
void push_image(lua_State* L, doc::Image* image);
|
||||
void push_layers(lua_State* L, const doc::ObjectIds& layers);
|
||||
|
Loading…
x
Reference in New Issue
Block a user