diff --git a/src/app/document_api.cpp b/src/app/document_api.cpp index 478b8c75c..3fb2801fb 100644 --- a/src/app/document_api.cpp +++ b/src/app/document_api.cpp @@ -86,11 +86,43 @@ void DocumentApi::setSpriteTransparentColor(Sprite* sprite, color_t maskColor) void DocumentApi::cropSprite(Sprite* sprite, const gfx::Rect& bounds) { setSpriteSize(sprite, bounds.w, bounds.h); - displaceLayers(sprite->folder(), -bounds.x, -bounds.y); - Layer *background_layer = sprite->backgroundLayer(); - if (background_layer) - cropLayer(background_layer, 0, 0, sprite->width(), sprite->height()); + app::Document* doc = static_cast(sprite->document()); + std::vector layers; + sprite->getLayersList(layers); + for (Layer* layer : layers) { + if (!layer->isImage()) + continue; + + CelIterator it = ((LayerImage*)layer)->getCelBegin(); + CelIterator end = ((LayerImage*)layer)->getCelEnd(); + for (; it != end; ++it) { + Cel* cel = *it; + + if (layer->isBackground()) { + Image* image = cel->image(); + if (image && !cel->link()) { + ASSERT(cel->x() == 0); + ASSERT(cel->y() == 0); + + // Create the new image through a crop + ImageRef new_image( + crop_image(image, + bounds.x, bounds.y, + bounds.w, bounds.h, + doc->bgColor(layer))); + + // Replace the image in the stock that is pointed by the cel + replaceImage(sprite, cel->imageRef(), new_image); + } + } + else { + // Update the cel's position + setCelPosition(sprite, cel, + cel->x()-bounds.x, cel->y()-bounds.y); + } + } + } if (!m_document->mask()->isEmpty()) setMaskPosition(m_document->mask()->bounds().x-bounds.x, @@ -298,23 +330,6 @@ void DocumentApi::setCelOpacity(Sprite* sprite, Cel* cel, int newOpacity) m_transaction.execute(new cmd::SetCelOpacity(cel, newOpacity)); } -void DocumentApi::cropCel(Sprite* sprite, Cel* cel, int x, int y, int w, int h) -{ - Image* cel_image = cel->image(); - ASSERT(cel_image); - - // create the new image through a crop - ImageRef new_image(crop_image(cel_image, - x-cel->x(), y-cel->y(), w, h, - static_cast(sprite->document())->bgColor(cel->layer()))); - - // replace the image in the stock that is pointed by the cel - replaceImage(sprite, cel->imageRef(), new_image); - - // update the cel's position - setCelPosition(sprite, cel, x, y); -} - void DocumentApi::clearCel(LayerImage* layer, frame_t frame) { if (Cel* cel = layer->cel(frame)) @@ -406,44 +421,6 @@ void DocumentApi::restackLayerBefore(Layer* layer, Layer* beforeThis) restackLayerAfter(layer, layer->sprite()->indexToLayer(afterThisIdx)); } -void DocumentApi::cropLayer(Layer* layer, int x, int y, int w, int h) -{ - if (!layer->isImage()) - return; - - Sprite* sprite = layer->sprite(); - CelIterator it = ((LayerImage*)layer)->getCelBegin(); - CelIterator end = ((LayerImage*)layer)->getCelEnd(); - for (; it != end; ++it) - cropCel(sprite, *it, x, y, w, h); -} - -// Moves every frame in @a layer with the offset (@a dx, @a dy). -void DocumentApi::displaceLayers(Layer* layer, int dx, int dy) -{ - switch (layer->type()) { - - case ObjectType::LayerImage: { - CelIterator it = ((LayerImage*)layer)->getCelBegin(); - CelIterator end = ((LayerImage*)layer)->getCelEnd(); - for (; it != end; ++it) { - Cel* cel = *it; - setCelPosition(layer->sprite(), cel, cel->x()+dx, cel->y()+dy); - } - break; - } - - case ObjectType::LayerFolder: { - LayerIterator it = ((LayerFolder*)layer)->getLayerBegin(); - LayerIterator end = ((LayerFolder*)layer)->getLayerEnd(); - for (; it != end; ++it) - displaceLayers(*it, dx, dy); - break; - } - - } -} - void DocumentApi::backgroundFromLayer(Layer* layer) { m_transaction.execute(new cmd::BackgroundFromLayer(layer)); diff --git a/src/app/document_api.h b/src/app/document_api.h index ac5e8c279..aac57483b 100644 --- a/src/app/document_api.h +++ b/src/app/document_api.h @@ -78,7 +78,6 @@ namespace app { void clearCel(Cel* cel); void setCelPosition(Sprite* sprite, Cel* cel, int x, int y); void setCelOpacity(Sprite* sprite, Cel* cel, int newOpacity); - void cropCel(Sprite* sprite, Cel* cel, int x, int y, int w, int h); void moveCel( LayerImage* srcLayer, frame_t srcFrame, LayerImage* dstLayer, frame_t dstFrame); @@ -95,8 +94,6 @@ namespace app { void removeLayer(Layer* layer); void restackLayerAfter(Layer* layer, Layer* afterThis); void restackLayerBefore(Layer* layer, Layer* beforeThis); - void cropLayer(Layer* layer, int x, int y, int w, int h); - void displaceLayers(Layer* layer, int dx, int dy); void backgroundFromLayer(Layer* layer); void layerFromBackground(Layer* layer); void flattenLayers(Sprite* sprite);