From 99d85a2d1f76fc10df56f1ac8792fe6e19b829e2 Mon Sep 17 00:00:00 2001 From: bancika Date: Fri, 21 Jan 2011 23:06:48 +0000 Subject: [PATCH] fixed drag&drop when spacing changes --- .../components/AbstractLeadedComponent.java | 11 ++- .../src/org/diylc/presenter/Presenter.java | 84 +++++++++++-------- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/components/AbstractLeadedComponent.java b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/components/AbstractLeadedComponent.java index e3ab5048..0b7d3e0c 100644 --- a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/components/AbstractLeadedComponent.java +++ b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/components/AbstractLeadedComponent.java @@ -94,6 +94,7 @@ public abstract class AbstractLeadedComponent extends AbstractTransparentComp } g2d.setColor(bodyColor); g2d.fill(shape); + decorateComponentBody(g2d); g2d.setComposite(oldComposite); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1)); g2d.setColor(componentState == ComponentState.SELECTED @@ -126,7 +127,7 @@ public abstract class AbstractLeadedComponent extends AbstractTransparentComp g2d.setFont(Constants.LABEL_FONT); g2d .setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED - : LABEL_COLOR); + : getLabelColor()); FontMetrics fontMetrics = g2d.getFontMetrics(); String label = display == Display.NAME ? getName() : getValue().toString(); // Adjust label angle if needed to make sure that it's readable. @@ -139,6 +140,14 @@ public abstract class AbstractLeadedComponent extends AbstractTransparentComp (int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent()); } + protected void decorateComponentBody(Graphics2D g2d) { + // Do nothing. + } + + protected Color getLabelColor() { + return LABEL_COLOR; + } + protected boolean shouldShadeLeads() { return true; } diff --git a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java index 2a0c6c8a..970833d2 100644 --- a/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java +++ b/ diy-layout-creator --username bancika@gmail.com/DIYLC/src/org/diylc/presenter/Presenter.java @@ -652,49 +652,59 @@ public class Presenter implements IPlugInPort { // We're dragging control point(s). int dx = (scaledPoint.x - previousDragPoint.x); int dy = (scaledPoint.y - previousDragPoint.y); - if (snapToGrid) { - dx = CalcUtils.roundToGrid(dx, currentProject.getGridSpacing()); - dy = CalcUtils.roundToGrid(dy, currentProject.getGridSpacing()); + // After we make the transfer and snap to grid, calculate actual dx + // and dy. We'll use them to translate the previous drag point. + int actualDx = 0; + int actualDy = 0; + + // Do a simulation of the move to see if any of them will + // collide. + for (Map.Entry, Set> entry : controlPointMap.entrySet()) { + IDIYComponent c = entry.getKey(); + Point[] controlPoints = new Point[c.getControlPointCount()]; + for (int i = 0; i < controlPoints.length; i++) { + controlPoints[i] = new Point(c.getControlPoint(i)); + if (entry.getValue().contains(i)) { + controlPoints[i].translate(dx, dy); + if (snapToGrid) { + CalcUtils.snapPointToGrid(controlPoints[i], currentProject + .getGridSpacing()); + } + } + } + for (int i = 0; i < controlPoints.length - 1; i++) { + for (int j = i + 1; j < controlPoints.length; j++) { + if (controlPoints[i].equals(controlPoints[j])) { + LOG.error("Control points collision detected, cannot make this move."); + return true; + } + } + } } - // Only repaint if there's an actual change. - repaint = dx != 0 || dy != 0; - if (repaint) { - // Do a simulation of the move to see if any of them will - // collide. - for (Map.Entry, Set> entry : controlPointMap.entrySet()) { - IDIYComponent c = entry.getKey(); - Point[] controlPoints = new Point[c.getControlPointCount()]; - for (int i = 0; i < controlPoints.length; i++) { - controlPoints[i] = new Point(c.getControlPoint(i)); - if (entry.getValue().contains(i)) { - controlPoints[i].translate(dx, dy); - } + // Update all points. + for (Map.Entry, Set> entry : controlPointMap.entrySet()) { + IDIYComponent c = entry.getKey(); + drawingManager.invalidateComponent(c); + for (Integer index : entry.getValue()) { + Point p = new Point(c.getControlPoint(index)); + p.translate(dx, dy); + if (snapToGrid) { + CalcUtils.snapPointToGrid(p, currentProject.getGridSpacing()); } - for (int i = 0; i < controlPoints.length - 1; i++) { - for (int j = i + 1; j < controlPoints.length; j++) { - if (controlPoints[i].equals(controlPoints[j])) { - LOG - .error("Control points collision detected, cannot make this move."); - return true; - } - } - } - } - - previousDragPoint.translate(dx, dy); - - // Update all points. - for (Map.Entry, Set> entry : controlPointMap.entrySet()) { - IDIYComponent c = entry.getKey(); - drawingManager.invalidateComponent(c); - for (Integer index : entry.getValue()) { - Point p = new Point(c.getControlPoint(index)); - p.translate(dx, dy); - c.setControlPoint(p, index); + // When the first point is moved, calculate how much it + // actually moved after snapping. + if (actualDx == 0 && actualDy == 0) { + actualDx = p.x - c.getControlPoint(index).x; + actualDy = p.y - c.getControlPoint(index).y; } + c.setControlPoint(p, index); } } + if (actualDx != 0 || actualDy != 0) { + previousDragPoint.translate(actualDx, actualDy); + repaint = true; + } } else if (selectedComponents.isEmpty() && instantiationManager.getComponentTypeSlot() == null) { // If there's no selection, the only thing to do is update the