fixed drag&drop when spacing changes

This commit is contained in:
bancika
2011-01-21 23:06:48 +00:00
parent 9fdf7c3e46
commit 99d85a2d1f
2 changed files with 57 additions and 38 deletions
@@ -94,6 +94,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} }
g2d.setColor(bodyColor); g2d.setColor(bodyColor);
g2d.fill(shape); g2d.fill(shape);
decorateComponentBody(g2d);
g2d.setComposite(oldComposite); g2d.setComposite(oldComposite);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
g2d.setColor(componentState == ComponentState.SELECTED g2d.setColor(componentState == ComponentState.SELECTED
@@ -126,7 +127,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
g2d.setFont(Constants.LABEL_FONT); g2d.setFont(Constants.LABEL_FONT);
g2d g2d
.setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED .setColor(componentState == ComponentState.SELECTED ? LABEL_COLOR_SELECTED
: LABEL_COLOR); : getLabelColor());
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
String label = display == Display.NAME ? getName() : getValue().toString(); String label = display == Display.NAME ? getName() : getValue().toString();
// Adjust label angle if needed to make sure that it's readable. // Adjust label angle if needed to make sure that it's readable.
@@ -139,6 +140,14 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent()); (int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
} }
protected void decorateComponentBody(Graphics2D g2d) {
// Do nothing.
}
protected Color getLabelColor() {
return LABEL_COLOR;
}
protected boolean shouldShadeLeads() { protected boolean shouldShadeLeads() {
return true; return true;
} }
@@ -652,49 +652,59 @@ public class Presenter implements IPlugInPort {
// We're dragging control point(s). // We're dragging control point(s).
int dx = (scaledPoint.x - previousDragPoint.x); int dx = (scaledPoint.x - previousDragPoint.x);
int dy = (scaledPoint.y - previousDragPoint.y); int dy = (scaledPoint.y - previousDragPoint.y);
if (snapToGrid) { // After we make the transfer and snap to grid, calculate actual dx
dx = CalcUtils.roundToGrid(dx, currentProject.getGridSpacing()); // and dy. We'll use them to translate the previous drag point.
dy = CalcUtils.roundToGrid(dy, currentProject.getGridSpacing()); int actualDx = 0;
int actualDy = 0;
// Do a simulation of the move to see if any of them will
// collide.
for (Map.Entry<IDIYComponent<?>, Set<Integer>> 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) { // Update all points.
// Do a simulation of the move to see if any of them will for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap.entrySet()) {
// collide. IDIYComponent<?> c = entry.getKey();
for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap.entrySet()) { drawingManager.invalidateComponent(c);
IDIYComponent<?> c = entry.getKey(); for (Integer index : entry.getValue()) {
Point[] controlPoints = new Point[c.getControlPointCount()]; Point p = new Point(c.getControlPoint(index));
for (int i = 0; i < controlPoints.length; i++) { p.translate(dx, dy);
controlPoints[i] = new Point(c.getControlPoint(i)); if (snapToGrid) {
if (entry.getValue().contains(i)) { CalcUtils.snapPointToGrid(p, currentProject.getGridSpacing());
controlPoints[i].translate(dx, dy);
}
} }
for (int i = 0; i < controlPoints.length - 1; i++) { // When the first point is moved, calculate how much it
for (int j = i + 1; j < controlPoints.length; j++) { // actually moved after snapping.
if (controlPoints[i].equals(controlPoints[j])) { if (actualDx == 0 && actualDy == 0) {
LOG actualDx = p.x - c.getControlPoint(index).x;
.error("Control points collision detected, cannot make this move."); actualDy = p.y - c.getControlPoint(index).y;
return true;
}
}
}
}
previousDragPoint.translate(dx, dy);
// Update all points.
for (Map.Entry<IDIYComponent<?>, Set<Integer>> 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);
} }
c.setControlPoint(p, index);
} }
} }
if (actualDx != 0 || actualDy != 0) {
previousDragPoint.translate(actualDx, actualDy);
repaint = true;
}
} else if (selectedComponents.isEmpty() } else if (selectedComponents.isEmpty()
&& instantiationManager.getComponentTypeSlot() == null) { && instantiationManager.getComponentTypeSlot() == null) {
// If there's no selection, the only thing to do is update the // If there's no selection, the only thing to do is update the