mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
This commit is contained in:
+2
@@ -131,6 +131,8 @@ public class ResistorInstance implements IComponentInstance {
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
}
|
||||
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,
|
||||
(leftTopCorner.y + otherCorner.y) / 2 - 10, 20, 20);
|
||||
}
|
||||
|
||||
+25
-12
@@ -42,6 +42,10 @@ import java.util.Map;
|
||||
class G2DWrapper extends Graphics2D {
|
||||
|
||||
private Graphics2D canvasGraphics;
|
||||
private Stroke originalStroke;
|
||||
private Color originalColor;
|
||||
private Composite originalComposite;
|
||||
private AffineTransform originalTx;
|
||||
private AffineTransform currentTx;
|
||||
private Area currentArea;
|
||||
|
||||
@@ -54,35 +58,38 @@ class G2DWrapper extends Graphics2D {
|
||||
super();
|
||||
this.canvasGraphics = canvasGraphics;
|
||||
startedDrawingComponent();
|
||||
resetTx();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears out current area.
|
||||
* Clears out the current area and caches canvas settings.
|
||||
*/
|
||||
public void startedDrawingComponent() {
|
||||
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
|
||||
*/
|
||||
public Area finishedDrawingComponent() {
|
||||
canvasGraphics.setStroke(originalStroke);
|
||||
canvasGraphics.setColor(originalColor);
|
||||
canvasGraphics.setTransform(originalTx);
|
||||
canvasGraphics.setComposite(originalComposite);
|
||||
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) {
|
||||
currentArea.add(new Area(s));
|
||||
Area area = new Area(s);
|
||||
area.transform(currentTx);
|
||||
currentArea.add(area);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,6 +105,7 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void draw(Shape s) {
|
||||
canvasGraphics.draw(s);
|
||||
appendShape(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,16 +220,19 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void rotate(double theta) {
|
||||
canvasGraphics.rotate(theta);
|
||||
currentTx.rotate(theta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rotate(double theta, double x, double y) {
|
||||
canvasGraphics.rotate(theta, x, y);
|
||||
currentTx.rotate(theta, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scale(double sx, double sy) {
|
||||
canvasGraphics.scale(sx, sy);
|
||||
currentTx.scale(sx, sy);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -366,6 +377,7 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void drawOval(int x, int y, int width, int height) {
|
||||
canvasGraphics.drawOval(x, y, width, height);
|
||||
appendShape(new Ellipse2D.Double(x, y, width, height));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -381,6 +393,7 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
|
||||
canvasGraphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
|
||||
appendShape(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+16
-15
@@ -69,9 +69,9 @@ public class Presenter implements IPlugInPort {
|
||||
private List<IPlugIn> plugIns;
|
||||
|
||||
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.
|
||||
private Map<IComponentInstance, ControlPointWrapper> componentsUnderCursor;
|
||||
private Map<IComponentInstance, ControlPointWrapper> componentControlPointMap;
|
||||
|
||||
private Cloner cloner;
|
||||
|
||||
@@ -146,7 +146,7 @@ public class Presenter implements IPlugInPort {
|
||||
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||
}
|
||||
}
|
||||
if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) {
|
||||
if (componentControlPointMap != null && !componentControlPointMap.isEmpty()) {
|
||||
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||
}
|
||||
}
|
||||
@@ -247,7 +247,7 @@ public class Presenter implements IPlugInPort {
|
||||
Composite mainComposite = g2d.getComposite();
|
||||
Composite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
|
||||
|
||||
g2dWrapper.resetTx();
|
||||
// g2dWrapper.resetTx();
|
||||
|
||||
List<IComponentInstance> components = getCurrentProject().getComponents();
|
||||
componentAreaMap.clear();
|
||||
@@ -305,7 +305,7 @@ public class Presenter implements IPlugInPort {
|
||||
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)) {
|
||||
g2d.setColor(Color.white);
|
||||
g2d.draw(selectionRect);
|
||||
@@ -314,6 +314,8 @@ public class Presenter implements IPlugInPort {
|
||||
g2d.draw(selectionRect);
|
||||
}
|
||||
|
||||
// // Draw component area for test
|
||||
// g2d.setStroke(new BasicStroke());
|
||||
// g2d.setColor(Color.red);
|
||||
// for (Area area : componentAreaMap.values()) {
|
||||
// g2d.draw(area);
|
||||
@@ -416,8 +418,8 @@ public class Presenter implements IPlugInPort {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!components.equals(componentsUnderCursor)) {
|
||||
componentsUnderCursor = components;
|
||||
if (!components.equals(componentControlPointMap)) {
|
||||
componentControlPointMap = components;
|
||||
messageDispatcher.dispatchMessage(EventType.AVAILABLE_CTRL_POINTS_CHANGED, components);
|
||||
}
|
||||
}
|
||||
@@ -445,10 +447,9 @@ public class Presenter implements IPlugInPort {
|
||||
Point scaledPoint = scalePoint(point);
|
||||
previousDragPoint = scaledPoint;
|
||||
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
||||
if (!componentsUnderCursor.isEmpty()) {
|
||||
// If there are control points under the cursor, drag them.
|
||||
} else if (components.isEmpty()) {
|
||||
// If no components are under the cursor, reset selection.
|
||||
if (!componentControlPointMap.isEmpty() || components.isEmpty()) {
|
||||
// If we're dragging control points or there are no components are
|
||||
// under the cursor, reset selection.
|
||||
selectedComponents.clear();
|
||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
@@ -468,13 +469,13 @@ public class Presenter implements IPlugInPort {
|
||||
@Override
|
||||
public boolean dragOver(Point point) {
|
||||
Point scaledPoint = scalePoint(point);
|
||||
if (!componentsUnderCursor.isEmpty()) {
|
||||
if (!componentControlPointMap.isEmpty()) {
|
||||
// We're dragging control point(s).
|
||||
// int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel);
|
||||
// int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel);
|
||||
//
|
||||
IComponentInstance firstComponent = componentsUnderCursor.keySet().iterator().next();
|
||||
ControlPointWrapper controlPoint = componentsUnderCursor.get(firstComponent);
|
||||
IComponentInstance firstComponent = componentControlPointMap.keySet().iterator().next();
|
||||
ControlPointWrapper controlPoint = componentControlPointMap.get(firstComponent);
|
||||
// Re-read the value just in case.
|
||||
try {
|
||||
controlPoint.readFrom(firstComponent);
|
||||
@@ -489,7 +490,7 @@ public class Presenter implements IPlugInPort {
|
||||
/ Constants.GRID) * Constants.GRID);
|
||||
previousDragPoint.setLocation(x, y);
|
||||
|
||||
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentsUnderCursor
|
||||
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentControlPointMap
|
||||
.entrySet()) {
|
||||
try {
|
||||
controlPoint = entry.getValue();
|
||||
|
||||
Reference in New Issue
Block a user