From 55594a4d33d8a1f265fe255afab18abeafba75ec Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 27 Aug 2019 19:56:33 -0300 Subject: [PATCH] [lua] Add support to index layers by name --- src/app/script/layers_class.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/app/script/layers_class.cpp b/src/app/script/layers_class.cpp index 19592f293..68d8694ee 100644 --- a/src/app/script/layers_class.cpp +++ b/src/app/script/layers_class.cpp @@ -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(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(layerId); + if (layer && + base::utf8_icmp(layer->name(), name) == 0) { + push_docobj(L, layerId); + return 1; + } + } + } + } + const int i = lua_tonumber(L, 2); if (i >= 1 && i <= int(obj->layers.size())) push_docobj(L, obj->layers[i-1]);