Add new/delete tiles from a tileset using Lua (fix #3663)

This commit is contained in:
Gaspar Capello 2023-01-10 18:11:46 -03:00 committed by David Capello
parent c299be50a5
commit 822357869a
3 changed files with 51 additions and 1 deletions

View File

@ -186,6 +186,7 @@ namespace app {
const doc::Mask* get_mask_from_arg(lua_State* L, int index);
app::tools::Tool* get_tool_from_arg(lua_State* L, int index);
doc::BrushRef get_brush_from_arg(lua_State* L, int index);
doc::Tileset* get_tile_index_from_arg(lua_State* L, int index, doc::tile_index& ts);
// Used by App.open(), Sprite{ fromFile }, and Image{ fromFile }
enum class LoadSpriteFromFileParam { FullAniAsSprite,

View File

@ -12,6 +12,7 @@
#include "app/app.h"
#include "app/cmd/add_layer.h"
#include "app/cmd/add_slice.h"
#include "app/cmd/add_tile.h"
#include "app/cmd/add_tileset.h"
#include "app/cmd/assign_color_profile.h"
#include "app/cmd/clear_cel.h"
@ -21,6 +22,7 @@
#include "app/cmd/remove_layer.h"
#include "app/cmd/remove_slice.h"
#include "app/cmd/remove_tag.h"
#include "app/cmd/remove_tile.h"
#include "app/cmd/remove_tileset.h"
#include "app/cmd/set_grid_bounds.h"
#include "app/cmd/set_mask.h"
@ -682,6 +684,44 @@ int Sprite_deleteTileset(lua_State* L)
}
}
int Sprite_newTile(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
auto ts = get_docobj<Tileset>(L, 2);
if (!ts) {
return luaL_error(L, "empty argument not allowed and must be a Tileset object");
}
tile_index ti = ts->size();
if (lua_isinteger(L, 3)) {
ti = tile_index(lua_tointeger(L, 3));
if (ti < 1)
return luaL_error(L, "index must be equal to or greater than 1");
}
ts->insert(ti, ts->makeEmptyTile());
Tx tx;
tx(new cmd::AddTile(ts, ti));
tx.commit();
push_tile(L, ts, ti);
return 1;
}
int Sprite_deleteTile(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
tile_index ti;
Tileset* ts = get_tile_index_from_arg(L, 2, ti);
if (!ts) {
return luaL_error(L, "inexistent Tileset inside of Tile object");
}
if (ti < 0 || ti >= ts->size())
return luaL_error(L, "index out of bounds");
Tx tx;
tx(new cmd::RemoveTile(ts, ti));
tx.commit();
push_tile(L, ts, ti);
return 1;
}
int Sprite_get_events(lua_State* L)
{
auto sprite = get_docobj<Sprite>(L, 1);
@ -943,9 +983,11 @@ const luaL_Reg Sprite_methods[] = {
// Slices
{ "newSlice", Sprite_newSlice },
{ "deleteSlice", Sprite_deleteSlice },
// Tilesets
// Tilesets & Tiles
{ "newTileset", Sprite_newTileset },
{ "deleteTileset", Sprite_deleteTileset },
{ "newTile", Sprite_newTile },
{ "deleteTile", Sprite_deleteTile },
{ nullptr, nullptr }
};

View File

@ -187,5 +187,12 @@ void push_tile(lua_State* L, const Tileset* ts, tile_index ti)
push_new<Tile>(L, ts, ti);
}
Tileset* get_tile_index_from_arg(lua_State* L, int index, tile_index& ti)
{
Tile* tile = get_obj<Tile>(L, index);
ti = tile->ti;
return doc::get<Tileset>(tile->id);
}
} // namespace script
} // namespace app