Fix Layer.stackIndex setter when we use a stackIndex greater than the current one

This commit is contained in:
David Capello 2019-11-29 11:08:18 -03:00
parent 116420f978
commit 48c4e49d67
2 changed files with 23 additions and 6 deletions

View File

@ -10,6 +10,6 @@
// Increment this value if the scripting API is modified between two
// released Aseprite versions.
#define API_VERSION 7
#define API_VERSION 8
#endif

View File

@ -19,6 +19,7 @@
#include "app/script/luacpp.h"
#include "app/script/userdata.h"
#include "app/tx.h"
#include "base/clamp.h"
#include "doc/layer.h"
#include "doc/sprite.h"
@ -247,16 +248,32 @@ int Layer_set_blendMode(lua_State* L)
int Layer_set_stackIndex(lua_State* L)
{
auto layer = get_docobj<Layer>(L, 1);
int stackIndex = lua_tointeger(L, 2);
const auto& layers = layer->parent()->layers();
int newStackIndex = lua_tointeger(L, 2);
int stackIndex = 1;
auto parent = layer->parent();
// First we need to know this layer stackIndex because we'll use
auto it = std::find(layers.begin(), layers.end(), layer);
ASSERT(it != layers.end());
if (it != layers.end())
stackIndex = it - layers.begin() + 1;
Layer* beforeThis = nullptr;
if (stackIndex < 1) {
beforeThis = parent->firstLayer();
if (newStackIndex > stackIndex) {
++newStackIndex;
}
else if (stackIndex <= int(parent->layers().size())) {
beforeThis = parent->layers()[stackIndex-1];
if (newStackIndex-1 < int(parent->layers().size())) {
beforeThis = parent->layers()[base::clamp(newStackIndex-1, 0, (int)parent->layers().size())];
}
else {
beforeThis = nullptr;
}
// Do nothing
if (beforeThis == layer)
return 0;
Doc* doc = static_cast<Doc*>(layer->sprite()->document());
Tx tx;