fixed d&d jumping issue

This commit is contained in:
bancika
2011-01-08 10:55:08 +00:00
parent 69f1e8afcd
commit a91e2c2617
6 changed files with 83 additions and 56 deletions
@@ -1,9 +1,7 @@
package org.diylc.presenter; package org.diylc.presenter;
import java.awt.AlphaComposite;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Composite;
import java.awt.Cursor; import java.awt.Cursor;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
@@ -297,8 +295,9 @@ public class Presenter implements IPlugInPort {
g2dWrapper.scale(zoomLevel, zoomLevel); g2dWrapper.scale(zoomLevel, zoomLevel);
} }
Composite mainComposite = g2d.getComposite(); // Composite mainComposite = g2d.getComposite();
Composite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); // Composite alphaComposite =
// AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
// g2dWrapper.resetTx(); // g2dWrapper.resetTx();
@@ -318,15 +317,15 @@ public class Presenter implements IPlugInPort {
} }
// If the component is being dragged, draw it in a separate // If the component is being dragged, draw it in a separate
// composite. // composite.
if (state == ComponentState.DRAGGING) { // if (state == ComponentState.DRAGGING) {
g2dWrapper.setComposite(alphaComposite); // g2dWrapper.setComposite(alphaComposite);
} // }
// Draw the component through the g2dWrapper. // Draw the component through the g2dWrapper.
component.draw(g2dWrapper, state, currentProject); component.draw(g2dWrapper, state, currentProject);
// Restore the composite if needed. // Restore the composite if needed.
if (state == ComponentState.DRAGGING) { // if (state == ComponentState.DRAGGING) {
g2dWrapper.setComposite(mainComposite); // g2dWrapper.setComposite(mainComposite);
} // }
componentAreaMap.put(component, g2dWrapper.finishedDrawingComponent()); componentAreaMap.put(component, g2dWrapper.finishedDrawingComponent());
// Draw control points // Draw control points
@@ -483,9 +482,11 @@ public class Presenter implements IPlugInPort {
break; break;
} }
} }
if (!components.equals(controlPointMap)) { if (!components.equals(controlPointMap)) {
controlPointMap = components; controlPointMap = components;
messageDispatcher.dispatchMessage(EventType.AVAILABLE_CTRL_POINTS_CHANGED, components); messageDispatcher.dispatchMessage(EventType.AVAILABLE_CTRL_POINTS_CHANGED,
new HashMap<IDIYComponent<?>, Set<Integer>>(components));
} }
} }
@@ -534,11 +535,25 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents); messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
} }
// If there aren't any control points, try to add all the selected
// components with all their control points. That will allow the
// user to
// drag the whole component.
for (IDIYComponent<?> c : selectedComponents) {
Set<Integer> pointIndices = new HashSet<Integer>();
if (component.getControlPointCount() > 0) {
for (int i = 0; i < component.getControlPointCount(); i++) {
pointIndices.add(i);
}
controlPointMap.put(c, pointIndices);
}
}
} }
} }
@Override @Override
public boolean dragOver(Point point) { public boolean dragOver(Point point) {
// System.err.println("DragOver: " + point);
if (point == null) { if (point == null) {
return false; return false;
} }
@@ -567,7 +582,7 @@ public class Presenter implements IPlugInPort {
dy = (int) (Math.round(dy / Constants.GRID) * Constants.GRID); dy = (int) (Math.round(dy / Constants.GRID) * Constants.GRID);
} }
// Only repaint if there's an actual change. // Only repaint if there's an actual change.
repaint = dx != 0 && dy != 0; repaint = dx != 0 || dy != 0;
previousDragPoint.translate(dx, dy); previousDragPoint.translate(dx, dy);
@@ -588,10 +603,12 @@ public class Presenter implements IPlugInPort {
repaint = true; repaint = true;
// messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED, // messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED,
// selectionRect); // selectionRect);
} else { // } else {
// If there are components selected translate their control points. // // If there are components selected translate their control
repaint = translateSelectedComponents(previousDragPoint, scaledPoint); // points.
// dragStartPoint = point; // repaint = translateSelectedComponents(previousDragPoint,
// scaledPoint);
// // dragStartPoint = point;
} }
if (repaint) { if (repaint) {
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -633,29 +650,32 @@ public class Presenter implements IPlugInPort {
dragInProgress = false; dragInProgress = false;
} }
private boolean translateSelectedComponents(Point fromPoint, Point toPoint) { // private boolean translateSelectedComponents(Point fromPoint, Point
if (toPoint == null) { // toPoint) {
LOG.debug("Drag ended outside the drawing area."); // if (toPoint == null) {
return false; // LOG.debug("Drag ended outside the drawing area.");
} // return false;
int dx = (int) (Math.round((toPoint.x - fromPoint.x) / zoomLevel / Constants.GRID) * Constants.GRID); // }
int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID); // int dx = (int) (Math.round((toPoint.x - fromPoint.x) / zoomLevel /
fromPoint.translate(dx, dy); // Constants.GRID) * Constants.GRID);
for (IDIYComponent<?> component : selectedComponents) { // int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel /
for (int i = 0; i < component.getControlPointCount(); i++) { // Constants.GRID) * Constants.GRID);
Point controlPoint = new Point(component.getControlPoint(i)); // fromPoint.translate(dx, dy);
translateControlPoint(controlPoint, dx, dy); // for (IDIYComponent<?> component : selectedComponents) {
component.setControlPoint(controlPoint, i); // for (int i = 0; i < component.getControlPointCount(); i++) {
} // Point controlPoint = new Point(component.getControlPoint(i));
} // translateControlPoint(controlPoint, dx, dy);
return dx != 0 && dy != 0; // component.setControlPoint(controlPoint, i);
} // }
// }
// return dx != 0 && dy != 0;
// }
private void translateControlPoint(Point controlPoint, int dx, int dy) { // private void translateControlPoint(Point controlPoint, int dx, int dy) {
int x = controlPoint.x + dx; // int x = controlPoint.x + dx;
int y = controlPoint.y + dy; // int y = controlPoint.y + dy;
controlPoint.setLocation(x, y); // controlPoint.setLocation(x, y);
} // }
@Override @Override
public void addComponents(List<IDIYComponent<?>> components, Point preferredPoint) { public void addComponents(List<IDIYComponent<?>> components, Point preferredPoint) {
@@ -8,7 +8,7 @@ import java.awt.Toolkit;
public class Constants { public class Constants {
public static final float GRID = (int) (Toolkit.getDefaultToolkit().getScreenResolution() / 10f); public static final int GRID = (int) (Toolkit.getDefaultToolkit().getScreenResolution() / 10f);
public static Color CANVAS_COLOR = Color.white; public static Color CANVAS_COLOR = Color.white;
public static Color GRID_COLOR = new Color(240, 240, 240); public static Color GRID_COLOR = new Color(240, 240, 240);
@@ -28,9 +28,11 @@ public abstract class AbstractBoard implements IDIYComponent<String> {
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) { public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
g2d.setStroke(new BasicStroke()); g2d.setStroke(new BasicStroke());
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(getBoardColor()); g2d.setColor(getBoardColor());
g2d.fillRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x g2d.fillRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x
- controlPoints[0].x, controlPoints[1].y - controlPoints[0].y); - controlPoints[0].x, controlPoints[1].y - controlPoints[0].y);
}
g2d.setColor(getBorderColor()); g2d.setColor(getBorderColor());
g2d.drawRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x g2d.drawRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x
- controlPoints[0].x, controlPoints[1].y - controlPoints[0].y); - controlPoints[0].x, controlPoints[1].y - controlPoints[0].y);
@@ -26,7 +26,7 @@ public class PerfBoard extends AbstractBoard {
private static Size PAD_SIZE = new Size(0.08d, SizeUnit.in); private static Size PAD_SIZE = new Size(0.08d, SizeUnit.in);
private static Size HOLE_SIZE = new Size(0.8d, SizeUnit.mm); private static Size HOLE_SIZE = new Size(0.8d, SizeUnit.mm);
// private Area copperArea; // private Area copperArea;
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) { public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
@@ -41,6 +41,8 @@ public class PerfBoard extends AbstractBoard {
// long t = System.nanoTime(); // long t = System.nanoTime();
super.draw(g2d, componentState, project); super.draw(g2d, componentState, project);
if (componentState != ComponentState.DRAGGING) {
Point p = new Point(controlPoints[0]); Point p = new Point(controlPoints[0]);
int radius = PAD_SIZE.convertToPixels() / 2; int radius = PAD_SIZE.convertToPixels() / 2;
int holeRadius = HOLE_SIZE.convertToPixels() / 2; int holeRadius = HOLE_SIZE.convertToPixels() / 2;
@@ -54,7 +56,10 @@ public class PerfBoard extends AbstractBoard {
g2d.setColor(COPPER_COLOR); g2d.setColor(COPPER_COLOR);
g2d.fillOval(p.x - radius, p.y - radius, radius * 2, radius * 2); g2d.fillOval(p.x - radius, p.y - radius, radius * 2, radius * 2);
g2d.setColor(Constants.CANVAS_COLOR); g2d.setColor(Constants.CANVAS_COLOR);
g2d.fillOval(p.x - holeRadius, p.y - holeRadius, holeRadius * 2, holeRadius * 2); g2d
.fillOval(p.x - holeRadius, p.y - holeRadius, holeRadius * 2,
holeRadius * 2);
}
} }
} }
// System.err.println("OldDraw: " + (System.nanoTime() - t)); // System.err.println("OldDraw: " + (System.nanoTime() - t));