MCPcopy Create free account
hub / github.com/LibreSprite/LibreSprite / pasteImage

Method pasteImage

src/app/ui/editor/editor.cpp:1486–1553  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1484}
1485
1486void Editor::pasteImage(const Image* image, const Mask* mask)
1487{
1488 ASSERT(image);
1489
1490 std::unique_ptr<Mask> temp_mask;
1491 if (!mask) {
1492 gfx::Rect visibleBounds = getVisibleSpriteBounds();
1493 gfx::Rect imageBounds = image->bounds();
1494
1495 temp_mask.reset(new Mask);
1496 temp_mask->replace(
1497 gfx::Rect(visibleBounds.x + visibleBounds.w/2 - imageBounds.w/2,
1498 visibleBounds.y + visibleBounds.h/2 - imageBounds.h/2,
1499 imageBounds.w, imageBounds.h));
1500
1501 mask = temp_mask.get();
1502 }
1503
1504 // Change to a selection tool: it's necessary for PixelsMovement
1505 // which will use the extra cel for transformation preview, and is
1506 // not compatible with the drawing cursor preview which overwrite
1507 // the extra cel.
1508 if (!getCurrentEditorInk()->isSelection()) {
1509 tools::Tool* defaultSelectionTool =
1510 App::instance()->toolBox()->getToolById(tools::WellKnownTools::RectangularMarquee);
1511
1512 ToolBar::instance()->selectTool(defaultSelectionTool);
1513 }
1514
1515 Sprite* sprite = this->sprite();
1516
1517 // Check bounds where the image will be pasted.
1518 int x = mask->bounds().x;
1519 int y = mask->bounds().y;
1520 {
1521 Rect visibleBounds = getVisibleSpriteBounds();
1522
1523 // If the pasted image original location center point isn't
1524 // visible, we center the image in the editor's visible bounds.
1525 if (!visibleBounds.contains(mask->bounds().center())) {
1526 x = visibleBounds.x + visibleBounds.w/2 - image->width()/2;
1527 y = visibleBounds.y + visibleBounds.h/2 - image->height()/2;
1528 }
1529 // In other case, if the center is visible, we put the pasted
1530 // image in its original location.
1531 else {
1532 x = MID(visibleBounds.x-image->width(), x, visibleBounds.x+visibleBounds.w-1);
1533 y = MID(visibleBounds.y-image->height(), y, visibleBounds.y+visibleBounds.h-1);
1534 }
1535
1536 // Also we always limit the image inside the sprite's bounds.
1537 x = MID(0, x, sprite->width() - image->width());
1538 y = MID(0, y, sprite->height() - image->height());
1539 }
1540
1541 // Clear brush preview, as the extra cel will be replaced with the
1542 // pasted image.
1543 m_brushPreview.hide();

Callers 2

onExecuteMethod · 0.80
pasteFunction · 0.80

Calls 15

spriteMethod · 0.95
instanceFunction · 0.85
replaceMethod · 0.80
getToolByIdMethod · 0.80
toolBoxMethod · 0.80
selectToolMethod · 0.80
centerMethod · 0.80
hideMethod · 0.80
setOriginMethod · 0.80
boundsMethod · 0.45
resetMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected