This commit is contained in:
bancika
2011-01-03 19:23:10 +00:00
parent 602bd81bb4
commit 79a48a04e0
3 changed files with 43 additions and 27 deletions
@@ -131,6 +131,8 @@ public class ResistorInstance implements IComponentInstance {
g2d.setStroke(new BasicStroke(1)); g2d.setStroke(new BasicStroke(1));
} }
g2d.drawLine(leftTopCorner.x, leftTopCorner.y, otherCorner.x, otherCorner.y); g2d.drawLine(leftTopCorner.x, leftTopCorner.y, otherCorner.x, otherCorner.y);
g2d.rotate(Math.PI / 4, (leftTopCorner.x + otherCorner.x) / 2,
(leftTopCorner.y + otherCorner.y) / 2);
g2d.fillRect((leftTopCorner.x + otherCorner.x) / 2 - 10, g2d.fillRect((leftTopCorner.x + otherCorner.x) / 2 - 10,
(leftTopCorner.y + otherCorner.y) / 2 - 10, 20, 20); (leftTopCorner.y + otherCorner.y) / 2 - 10, 20, 20);
} }
@@ -42,6 +42,10 @@ import java.util.Map;
class G2DWrapper extends Graphics2D { class G2DWrapper extends Graphics2D {
private Graphics2D canvasGraphics; private Graphics2D canvasGraphics;
private Stroke originalStroke;
private Color originalColor;
private Composite originalComposite;
private AffineTransform originalTx;
private AffineTransform currentTx; private AffineTransform currentTx;
private Area currentArea; private Area currentArea;
@@ -54,35 +58,38 @@ class G2DWrapper extends Graphics2D {
super(); super();
this.canvasGraphics = canvasGraphics; this.canvasGraphics = canvasGraphics;
startedDrawingComponent(); startedDrawingComponent();
resetTx();
} }
/** /**
* Clears out current area. * Clears out the current area and caches canvas settings.
*/ */
public void startedDrawingComponent() { public void startedDrawingComponent() {
currentArea = new Area(); currentArea = new Area();
originalStroke = canvasGraphics.getStroke();
originalColor = canvasGraphics.getColor();
originalTx = canvasGraphics.getTransform();
originalComposite = canvasGraphics.getComposite();
currentTx = new AffineTransform();
} }
/** /**
* Reverts {@link Graphics2D} state and returns area drawn by component. * Reverts {@link Graphics2D} settings and returns area drawn by component
* in the meantime.
* *
* @return * @return
*/ */
public Area finishedDrawingComponent() { public Area finishedDrawingComponent() {
canvasGraphics.setStroke(originalStroke);
canvasGraphics.setColor(originalColor);
canvasGraphics.setTransform(originalTx);
canvasGraphics.setComposite(originalComposite);
return currentArea; return currentArea;
} }
/**
* Resets transformation to identity. Should be called only once before any
* drawing is done.
*/
public void resetTx() {
currentTx = new AffineTransform();
}
private void appendShape(Shape s) { private void appendShape(Shape s) {
currentArea.add(new Area(s)); Area area = new Area(s);
area.transform(currentTx);
currentArea.add(area);
} }
@Override @Override
@@ -98,6 +105,7 @@ class G2DWrapper extends Graphics2D {
@Override @Override
public void draw(Shape s) { public void draw(Shape s) {
canvasGraphics.draw(s); canvasGraphics.draw(s);
appendShape(s);
} }
@Override @Override
@@ -212,16 +220,19 @@ class G2DWrapper extends Graphics2D {
@Override @Override
public void rotate(double theta) { public void rotate(double theta) {
canvasGraphics.rotate(theta); canvasGraphics.rotate(theta);
currentTx.rotate(theta);
} }
@Override @Override
public void rotate(double theta, double x, double y) { public void rotate(double theta, double x, double y) {
canvasGraphics.rotate(theta, x, y); canvasGraphics.rotate(theta, x, y);
currentTx.rotate(theta, x, y);
} }
@Override @Override
public void scale(double sx, double sy) { public void scale(double sx, double sy) {
canvasGraphics.scale(sx, sy); canvasGraphics.scale(sx, sy);
currentTx.scale(sx, sy);
} }
@Override @Override
@@ -366,6 +377,7 @@ class G2DWrapper extends Graphics2D {
@Override @Override
public void drawOval(int x, int y, int width, int height) { public void drawOval(int x, int y, int width, int height) {
canvasGraphics.drawOval(x, y, width, height); canvasGraphics.drawOval(x, y, width, height);
appendShape(new Ellipse2D.Double(x, y, width, height));
} }
@Override @Override
@@ -381,6 +393,7 @@ class G2DWrapper extends Graphics2D {
@Override @Override
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
canvasGraphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight); canvasGraphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
appendShape(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
} }
@Override @Override
@@ -69,9 +69,9 @@ public class Presenter implements IPlugInPort {
private List<IPlugIn> plugIns; private List<IPlugIn> plugIns;
private ComponentSelection selectedComponents; private ComponentSelection selectedComponents;
// List of component names that have at least one of their control points // List of components that have at least one of their control points
// under the last recorded mouse position. // under the last recorded mouse position.
private Map<IComponentInstance, ControlPointWrapper> componentsUnderCursor; private Map<IComponentInstance, ControlPointWrapper> componentControlPointMap;
private Cloner cloner; private Cloner cloner;
@@ -146,7 +146,7 @@ public class Presenter implements IPlugInPort {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
} }
} }
if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) { if (componentControlPointMap != null && !componentControlPointMap.isEmpty()) {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
} }
} }
@@ -247,7 +247,7 @@ public class Presenter implements IPlugInPort {
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();
List<IComponentInstance> components = getCurrentProject().getComponents(); List<IComponentInstance> components = getCurrentProject().getComponents();
componentAreaMap.clear(); componentAreaMap.clear();
@@ -305,7 +305,7 @@ public class Presenter implements IPlugInPort {
g2d.scale(zoomLevel, zoomLevel); g2d.scale(zoomLevel, zoomLevel);
} }
// At the end draw selection rectangle. // At the end draw selection rectangle if needed.
if (drawOptions.contains(DrawOption.SELECTION) && (selectionRect != null)) { if (drawOptions.contains(DrawOption.SELECTION) && (selectionRect != null)) {
g2d.setColor(Color.white); g2d.setColor(Color.white);
g2d.draw(selectionRect); g2d.draw(selectionRect);
@@ -314,6 +314,8 @@ public class Presenter implements IPlugInPort {
g2d.draw(selectionRect); g2d.draw(selectionRect);
} }
// // Draw component area for test
// g2d.setStroke(new BasicStroke());
// g2d.setColor(Color.red); // g2d.setColor(Color.red);
// for (Area area : componentAreaMap.values()) { // for (Area area : componentAreaMap.values()) {
// g2d.draw(area); // g2d.draw(area);
@@ -416,8 +418,8 @@ public class Presenter implements IPlugInPort {
break; break;
} }
} }
if (!components.equals(componentsUnderCursor)) { if (!components.equals(componentControlPointMap)) {
componentsUnderCursor = components; componentControlPointMap = components;
messageDispatcher.dispatchMessage(EventType.AVAILABLE_CTRL_POINTS_CHANGED, components); messageDispatcher.dispatchMessage(EventType.AVAILABLE_CTRL_POINTS_CHANGED, components);
} }
} }
@@ -445,10 +447,9 @@ public class Presenter implements IPlugInPort {
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
previousDragPoint = scaledPoint; previousDragPoint = scaledPoint;
List<IComponentInstance> components = findComponentsAt(scaledPoint); List<IComponentInstance> components = findComponentsAt(scaledPoint);
if (!componentsUnderCursor.isEmpty()) { if (!componentControlPointMap.isEmpty() || components.isEmpty()) {
// If there are control points under the cursor, drag them. // If we're dragging control points or there are no components are
} else if (components.isEmpty()) { // under the cursor, reset selection.
// If no components are under the cursor, reset selection.
selectedComponents.clear(); selectedComponents.clear();
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents); messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -468,13 +469,13 @@ public class Presenter implements IPlugInPort {
@Override @Override
public boolean dragOver(Point point) { public boolean dragOver(Point point) {
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
if (!componentsUnderCursor.isEmpty()) { if (!componentControlPointMap.isEmpty()) {
// We're dragging control point(s). // We're dragging control point(s).
// int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel); // int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel);
// int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel); // int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel);
// //
IComponentInstance firstComponent = componentsUnderCursor.keySet().iterator().next(); IComponentInstance firstComponent = componentControlPointMap.keySet().iterator().next();
ControlPointWrapper controlPoint = componentsUnderCursor.get(firstComponent); ControlPointWrapper controlPoint = componentControlPointMap.get(firstComponent);
// Re-read the value just in case. // Re-read the value just in case.
try { try {
controlPoint.readFrom(firstComponent); controlPoint.readFrom(firstComponent);
@@ -489,7 +490,7 @@ public class Presenter implements IPlugInPort {
/ Constants.GRID) * Constants.GRID); / Constants.GRID) * Constants.GRID);
previousDragPoint.setLocation(x, y); previousDragPoint.setLocation(x, y);
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentsUnderCursor for (Entry<IComponentInstance, ControlPointWrapper> entry : componentControlPointMap
.entrySet()) { .entrySet()) {
try { try {
controlPoint = entry.getValue(); controlPoint = entry.getValue();