[lua] Add support to index layers by name

This commit is contained in:
David Capello 2019-08-27 19:56:33 -03:00
parent e968758117
commit 55594a4d33

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018 Igara Studio S.A.
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This program is distributed under the terms of
@ -12,6 +12,7 @@
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "base/string.h"
#include "doc/layer.h"
#include "doc/object_ids.h"
#include "doc/sprite.h"
@ -54,6 +55,21 @@ int Layers_len(lua_State* L)
int Layers_index(lua_State* L)
{
auto obj = get_obj<LayersObj>(L, 1);
// Index by layer name
if (lua_isstring(L, 2)) {
if (const char* name = lua_tostring(L, 2)) {
for (ObjectId layerId : obj->layers) {
Layer* layer = doc::get<Layer>(layerId);
if (layer &&
base::utf8_icmp(layer->name(), name) == 0) {
push_docobj<Layer>(L, layerId);
return 1;
}
}
}
}
const int i = lua_tonumber(L, 2);
if (i >= 1 && i <= int(obj->layers.size()))
push_docobj<Layer>(L, obj->layers[i-1]);