@Override public void drag( Collection<Direction> resizeDirection, int diffX, int diffY, Point mousePosBeforeDrag, boolean isShiftKeyDown, boolean firstDrag, StickableMap stickables, boolean undoable) { Rectangle oldRect = getRectangle(); StickingPolygon stickingPolygonBeforeLocationChange = generateStickingBorder(); String oldAddAttr = getAdditionalAttributes(); if (resizeDirection.isEmpty()) { // Move GridElement setLocationDifference(diffX, diffY); } else { // Resize GridElement Rectangle rect = getRectangle(); if (isShiftKeyDown && diagonalResize(resizeDirection)) { // Proportional Resize boolean mouseToRight = diffX > 0 && diffX > diffY; boolean mouseDown = diffY > 0 && diffY > diffX; boolean mouseLeft = diffX < 0 && diffX < diffY; boolean mouseUp = diffY < 0 && diffY < diffX; if (mouseToRight || mouseLeft) { diffY = diffX; } if (mouseDown || mouseUp) { diffX = diffY; } } if (resizeDirection.contains(Direction.LEFT) && resizeDirection.contains(Direction.RIGHT)) { rect.setX(rect.getX() - diffX / 2); rect.setWidth(Math.max(rect.getWidth() + diffX, minSize())); } else if (resizeDirection.contains(Direction.LEFT)) { int newWidth = rect.getWidth() - diffX; if (newWidth >= minSize()) { rect.setX(rect.getX() + diffX); rect.setWidth(newWidth); } } else if (resizeDirection.contains(Direction.RIGHT)) { rect.setWidth(Math.max(rect.getWidth() + diffX, minSize())); } if (resizeDirection.contains(Direction.UP)) { int newHeight = rect.getHeight() - diffY; if (newHeight >= minSize()) { rect.setY(rect.getY() + diffY); rect.setHeight(newHeight); } } if (resizeDirection.contains(Direction.DOWN)) { rect.setHeight(Math.max(rect.getHeight() + diffY, minSize())); } setRectangle(rect); updateModelFromText(); } moveStickables(stickables, undoable, oldRect, stickingPolygonBeforeLocationChange, oldAddAttr); }
/** * realigns a rectangle to the grid enlarging any side if necessary (eg: x=9, width=10, x2=19 will * be realigned to x=0, width=20, x2=20) * * @param rectangle rectangle to realign * @return a realigned copy of the input rectangle * @param realignHeightAndWidth if true height and width are also realigned if necessary */ public static Rectangle realignToGrid(Rectangle rectangle, boolean realignHeightAndWidth) { int x = realignToGrid(false, rectangle.getX(), false); int y = realignToGrid(false, rectangle.getY(), false); if (realignHeightAndWidth) { int width = realignToGrid( false, rectangle.getX() - x + rectangle.getWidth(), true); // IMPORTANT: the difference from original x and realigned x must be added, // otherwise the upper example would return x=0, width=10, x2=10. x2 would be // too small int height = realignToGrid(false, rectangle.getY() - y + rectangle.getHeight(), true); return new Rectangle(x, y, width, height); } else { return new Rectangle(x, y, rectangle.getWidth(), rectangle.getHeight()); } }