From 9db66ce6b246acd19d6986c0643dac273da1dae4 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sat, 11 Feb 2012 18:35:46 -0300 Subject: [PATCH] Add bounds checking when the user paste the clipboard's image. In this way, the pasted image will be always visible by the user. --- src/widgets/editor/editor.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index 2cc3f83ed..67d32e22e 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -1181,8 +1181,33 @@ void Editor::pasteImage(const Image* image, int x, int y) Document* document = getDocument(); int opacity = 255; Sprite* sprite = getSprite(); - PixelsMovement* pixelsMovement = new PixelsMovement(document, sprite, image, x, y, opacity, - "Paste"); + + // Check bounds where the image will be pasted. + { + // First we limit the image inside the sprite's bounds. + x = MID(0, x, sprite->getWidth() - image->w); + y = MID(0, y, sprite->getHeight() - image->h); + + // Then we check if the image will be visible by the user. + Rect visibleBounds = getVisibleSpriteBounds(); + x = MID(visibleBounds.x-image->w, x, visibleBounds.x+visibleBounds.w-1); + y = MID(visibleBounds.y-image->h, y, visibleBounds.y+visibleBounds.h-1); + + // If the visible part of the pasted image will not fit in the + // visible bounds of the editor, we put the image in the center of + // the visible bounds. + Rect visiblePasted = visibleBounds.createIntersect(gfx::Rect(x, y, image->w, image->h)); + if (((visibleBounds.w >= image->w && visiblePasted.w < image->w/2) || + (visibleBounds.w < image->w && visiblePasted.w < visibleBounds.w/2)) || + ((visibleBounds.h >= image->h && visiblePasted.h < image->w/2) || + (visibleBounds.h < image->h && visiblePasted.h < visibleBounds.h/2))) { + x = visibleBounds.x + visibleBounds.w/2 - image->w/2; + y = visibleBounds.y + visibleBounds.h/2 - image->h/2; + } + } + + PixelsMovement* pixelsMovement = + new PixelsMovement(document, sprite, image, x, y, opacity, "Paste"); // Select the pasted image so the user can move it and transform it. pixelsMovement->maskImage(image, x, y);