| 78 | } |
| 79 | |
| 80 | void DocumentApi::cropSprite(Sprite* sprite, const gfx::Rect& bounds) |
| 81 | { |
| 82 | setSpriteSize(sprite, bounds.w, bounds.h); |
| 83 | |
| 84 | app::Document* doc = static_cast<app::Document*>(sprite->document()); |
| 85 | std::vector<Layer*> layers; |
| 86 | sprite->getLayersList(layers); |
| 87 | for (Layer* layer : layers) { |
| 88 | if (!layer->isImage()) |
| 89 | continue; |
| 90 | |
| 91 | std::set<ObjectId> visited; |
| 92 | CelIterator it = ((LayerImage*)layer)->getCelBegin(); |
| 93 | CelIterator end = ((LayerImage*)layer)->getCelEnd(); |
| 94 | for (; it != end; ++it) { |
| 95 | auto cel = *it; |
| 96 | if (visited.find(cel->data()->id()) != visited.end()) |
| 97 | continue; |
| 98 | visited.insert(cel->data()->id()); |
| 99 | |
| 100 | if (layer->isBackground()) { |
| 101 | Image* image = cel->image(); |
| 102 | if (image && !cel->link()) { |
| 103 | ASSERT(cel->x() == 0); |
| 104 | ASSERT(cel->y() == 0); |
| 105 | |
| 106 | // Create the new image through a crop |
| 107 | ImageRef new_image( |
| 108 | crop_image(image, |
| 109 | bounds.x, bounds.y, |
| 110 | bounds.w, bounds.h, |
| 111 | doc->bgColor(layer))); |
| 112 | |
| 113 | // Replace the image in the stock that is pointed by the cel |
| 114 | replaceImage(sprite, cel->imageRef(), new_image); |
| 115 | } |
| 116 | } |
| 117 | else { |
| 118 | // Update the cel's position |
| 119 | setCelPosition(sprite, cel, |
| 120 | cel->x()-bounds.x, cel->y()-bounds.y); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!m_document->mask()->isEmpty()) |
| 126 | setMaskPosition(m_document->mask()->bounds().x-bounds.x, |
| 127 | m_document->mask()->bounds().y-bounds.y); |
| 128 | } |
| 129 | |
| 130 | void DocumentApi::trimSprite(Sprite* sprite) |
| 131 | { |
no test coverage detected