lua: add app.sprites

This commit is contained in:
David Capello 2018-09-12 12:55:56 -03:00
parent b2f83e0a90
commit 02388e2d6a
5 changed files with 96 additions and 0 deletions

View File

@ -174,6 +174,7 @@ if(ENABLE_SCRIPTING)
script/slice_class.cpp
script/slices_class.cpp
script/sprite_class.cpp
script/sprites_class.cpp
script/tag_class.cpp
script/tags_class.cpp
shell.cpp

View File

@ -221,6 +221,12 @@ int App_get_activeImage(lua_State* L)
return 1;
}
int App_get_sprites(lua_State* L)
{
push_sprites(L);
return 1;
}
int App_get_fgColor(lua_State* L)
{
push_obj<app::Color>(L, Preferences::instance().colorBar.fgColor());
@ -365,6 +371,7 @@ const Property App_properties[] = {
{ "activeFrame", App_get_activeFrame, App_set_activeFrame },
{ "activeCel", App_get_activeCel, App_set_activeCel },
{ "activeImage", App_get_activeImage, App_set_activeImage },
{ "sprites", App_get_sprites, nullptr },
{ "fgColor", App_get_fgColor, App_set_fgColor },
{ "bgColor", App_get_bgColor, App_set_bgColor },
{ "version", App_get_version, nullptr },

View File

@ -105,6 +105,7 @@ void register_size_class(lua_State* L);
void register_slice_class(lua_State* L);
void register_slices_class(lua_State* L);
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);
@ -210,6 +211,7 @@ Engine::Engine()
register_slice_class(L);
register_slices_class(L);
register_sprite_class(L);
register_sprites_class(L);
register_tag_class(L);
register_tags_class(L);

View File

@ -94,6 +94,7 @@ namespace app {
void push_sprite_selection(lua_State* L, doc::Sprite* sprite);
void push_sprite_slices(lua_State* L, doc::Sprite* sprite);
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);
gfx::Point convert_args_into_point(lua_State* L, int index);

View File

@ -0,0 +1,85 @@
// 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/context.h"
#include "app/doc.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include <algorithm>
#include <iterator>
#include <vector>
namespace app {
namespace script {
using namespace doc;
namespace {
struct SpritesObj {
std::vector<Doc*> docs;
SpritesObj(const Docs& docs) {
std::copy(docs.begin(), docs.end(),
std::back_inserter(this->docs));
}
SpritesObj(const SpritesObj&) = delete;
SpritesObj& operator=(const SpritesObj&) = delete;
};
int Sprites_gc(lua_State* L)
{
get_obj<SpritesObj>(L, 1)->~SpritesObj();
return 0;
}
int Sprites_len(lua_State* L)
{
auto obj = get_obj<SpritesObj>(L, 1);
lua_pushinteger(L, obj->docs.size());
return 1;
}
int Sprites_index(lua_State* L)
{
auto obj = get_obj<SpritesObj>(L, 1);
const int i = lua_tonumber(L, 2);
if (i < 1 || i > int(obj->docs.size()))
return luaL_error(L, "index out of bounds %d", i);
push_ptr<Sprite>(L, obj->docs[i-1]->sprite());
return 1;
}
const luaL_Reg Sprites_methods[] = {
{ "__gc", Sprites_gc },
{ "__len", Sprites_len },
{ "__index", Sprites_index },
{ nullptr, nullptr }
};
} // anonymous namespace
DEF_MTNAME(SpritesObj);
void register_sprites_class(lua_State* L)
{
using Sprites = SpritesObj;
REG_CLASS(L, Sprites);
}
void push_sprites(lua_State* L)
{
app::Context* ctx = App::instance()->context();
push_new<SpritesObj>(L, ctx->documents());
}
} // namespace script
} // namespace app