Skip to content

Commit af349ce

Browse files
ckaiserdacap
authored andcommitted
[lua] Fix new layers not going to the top when selected (fix aseprite#5364)
1 parent 3bc4ac0 commit af349ce

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

src/app/commands/cmd_new_layer.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,8 @@ void NewLayerCommand::onExecute(Context* context)
267267
bool afterBackground = false;
268268

269269
switch (m_type) {
270-
case Type::Layer:
271-
272-
if (m_place == Place::BeforeActiveLayer) {
273-
layer = api.newLayer(parent, name);
274-
api.restackLayerBefore(layer, parent, activeLayer);
275-
}
276-
else
277-
layer = api.newLayerAfter(parent, name, activeLayer);
278-
break;
279-
case Type::Group: layer = api.newGroupAfter(parent, name, activeLayer); break;
270+
case Type::Layer: layer = api.newLayer(parent, name); break;
271+
case Type::Group: layer = api.newGroup(parent, name); break;
280272
case Type::ReferenceLayer:
281273
layer = api.newLayer(parent, name);
282274
if (layer)
@@ -311,6 +303,15 @@ void NewLayerCommand::onExecute(Context* context)
311303

312304
ASSERT(layer->parent());
313305

306+
// Reorder the resulting layer.
307+
switch (m_place) {
308+
case Place::AfterActiveLayer: api.restackLayerAfter(layer, parent, activeLayer); break;
309+
case Place::BeforeActiveLayer: api.restackLayerBefore(layer, parent, activeLayer); break;
310+
case Place::Top:
311+
api.restackLayerAfter(layer, sprite->root(), sprite->root()->lastLayer());
312+
break;
313+
}
314+
314315
// Put new layer as an overlay of the background or in the first
315316
// layer in case the sprite is transparent.
316317
if (afterBackground) {

tests/scripts/app_command.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,76 @@ do -- NewLayer/RemoveLayer
140140
assert(#s.layers == 1)
141141
end
142142

143+
-- NewLayer Ordering
144+
do
145+
local function testNewLayerOrdering(testGroup, testTilemap)
146+
local s = Sprite(32, 32)
147+
assert(#s.layers == 1)
148+
local base = app.layer
149+
150+
app.command.NewLayer{ name = "First", group = testGroup, tilemap = testTilemap }
151+
local first = app.layer
152+
app.command.NewLayer{ name = "Second", group = testGroup, tilemap = testTilemap }
153+
local second = app.layer
154+
app.command.NewLayer{ name = "Third", group = testGroup, tilemap = testTilemap }
155+
local third = app.layer
156+
157+
s:deleteLayer(base)
158+
159+
expect_eq(first.name, "First")
160+
expect_eq(second.name, "Second")
161+
expect_eq(third.name, "Third")
162+
163+
expect_eq(first.stackIndex, 1)
164+
expect_eq(second.stackIndex, 2)
165+
expect_eq(third.stackIndex, 3)
166+
167+
app.layer = second
168+
app.command.NewLayer{ before = true, name = "Before Second", group = testGroup, tilemap = testTilemap }
169+
local beforeSecond = app.layer
170+
171+
app.layer = second
172+
app.command.NewLayer{ before = false, name = "After Second", group = testGroup, tilemap = testTilemap }
173+
local afterSecond = app.layer
174+
175+
expect_eq(first.name, "First")
176+
expect_eq(second.name, "Second")
177+
expect_eq(third.name, "Third")
178+
expect_eq(beforeSecond.name, "Before Second")
179+
expect_eq(afterSecond.name, "After Second")
180+
181+
expect_eq(first.stackIndex, 1)
182+
expect_eq(beforeSecond.stackIndex, 2)
183+
expect_eq(second.stackIndex, 3)
184+
expect_eq(afterSecond.stackIndex, 4)
185+
expect_eq(third.stackIndex, 5)
186+
187+
app.layer = second
188+
app.command.NewLayer{ top = true, name = "Top", group = testGroup, tilemap = testTilemap }
189+
local top = app.layer
190+
191+
expect_eq(top.stackIndex, 6)
192+
193+
app.layer = first
194+
app.command.NewLayer{ before = true, name = "Bottom", group = testGroup, tilemap = testTilemap }
195+
local bottom = app.layer
196+
197+
expect_eq(bottom.stackIndex, 1)
198+
expect_eq(first.stackIndex, 2)
199+
expect_eq(beforeSecond.stackIndex, 3)
200+
expect_eq(second.stackIndex, 4)
201+
expect_eq(afterSecond.stackIndex, 5)
202+
expect_eq(third.stackIndex, 6)
203+
expect_eq(top.stackIndex, 7)
204+
205+
s:close()
206+
end
207+
208+
testNewLayerOrdering(false, false) -- Regular layers
209+
testNewLayerOrdering(true, false) -- Groups
210+
testNewLayerOrdering(false, true) -- Tilemaps
211+
end
212+
143213
do -- Background/Transparent layers
144214
local s = Sprite(32, 32)
145215
assert(s.layers[1].isTransparent)

0 commit comments

Comments
 (0)