remove newPoints and reuse points for backward compatibility

This commit is contained in:
Branislav Stojkovic
2019-03-23 22:44:09 +01:00
parent 9fd5a8fe96
commit 812b7adb28
9 changed files with 60 additions and 65 deletions
Binary file not shown.
@@ -57,8 +57,7 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
public static Color COLOR = Color.black; public static Color COLOR = Color.black;
protected String value = ""; protected String value = "";
protected Point[] controlPoints; protected Point[] controlPoints = new Point[] { new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0) };
protected Point[] newControlPoints = new Point[] { new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0) };
protected Color color = COLOR; protected Color color = COLOR;
protected SymbolFlipping flip = SymbolFlipping.NONE; protected SymbolFlipping flip = SymbolFlipping.NONE;
protected Display display = Display.NAME; protected Display display = Display.NAME;
@@ -69,14 +68,14 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
public Abstract3LegSymbol() { public Abstract3LegSymbol() {
super(); super();
updateControlPoints(); updateControlPoints();
controlPoints[3] = getDefaultLabelPosition(controlPoints);
} }
public Point[] getNewControlPoints() { public Point[] getControlPoints() {
if (controlPoints != null) { if (controlPoints.length == 3) {
newControlPoints = new Point[] { controlPoints[0], controlPoints[1], controlPoints[2], getDefaultLabelPosition(controlPoints) }; controlPoints = new Point[] { controlPoints[0], controlPoints[1], controlPoints[2], getDefaultLabelPosition(controlPoints) };
controlPoints = null;
} }
return newControlPoints; return controlPoints;
} }
protected Point getDefaultLabelPosition(Point[] oldControlPoints) { protected Point getDefaultLabelPosition(Point[] oldControlPoints) {
@@ -111,7 +110,7 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
AffineTransform old = g2d.getTransform(); AffineTransform old = g2d.getTransform();
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
if (this.flip == SymbolFlipping.Y) { if (this.flip == SymbolFlipping.Y) {
g2d.translate(0, controlPoints[0].y); g2d.translate(0, controlPoints[0].y);
@@ -172,27 +171,27 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
@Override @Override
public Point getControlPoint(int index) { public Point getControlPoint(int index) {
return getNewControlPoints()[index]; return getControlPoints()[index];
} }
@Override @Override
public int getControlPointCount() { public int getControlPointCount() {
return getNewControlPoints().length; return getControlPoints().length;
} }
protected int getLabelX(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) { protected int getLabelX(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
int f = flip == SymbolFlipping.X ? -1 : 1; int f = flip == SymbolFlipping.X ? -1 : 1;
int d = flip == SymbolFlipping.X ? (int) PIN_SPACING.convertToPixels() / 2 : 0; int d = flip == SymbolFlipping.X ? (int) PIN_SPACING.convertToPixels() / 2 : 0;
return getNewControlPoints()[0].x + f * (int) (PIN_SPACING.convertToPixels() * 1.5 + d); return getControlPoints()[0].x + f * (int) (PIN_SPACING.convertToPixels() * 1.5 + d);
} }
protected int getLabelY(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) { protected int getLabelY(Rectangle2D shapeRect, Rectangle2D textRect, FontMetrics fontMetrics, boolean outlineMode) {
return getNewControlPoints()[0].y; return getControlPoints()[0].y;
} }
protected void updateControlPoints() { protected void updateControlPoints() {
int pinSpacing = (int) PIN_SPACING.convertToPixels(); int pinSpacing = (int) PIN_SPACING.convertToPixels();
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
int x = controlPoints[0].x; int x = controlPoints[0].x;
int y = controlPoints[0].y; int y = controlPoints[0].y;
@@ -254,7 +253,7 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
@Override @Override
public void setControlPoint(Point point, int index) { public void setControlPoint(Point point, int index) {
getNewControlPoints()[index].setLocation(point); getControlPoints()[index].setLocation(point);
// make sure we have a new drawing // make sure we have a new drawing
body = null; body = null;
@@ -330,7 +329,7 @@ public abstract class Abstract3LegSymbol extends AbstractComponent<String> {
if (getOrientation() == Orientation.DEFAULT) if (getOrientation() == Orientation.DEFAULT)
return; return;
Point first = getNewControlPoints()[0]; Point first = getControlPoints()[0];
double angle = Double.parseDouble(getOrientation().name().replace("_", "")); double angle = Double.parseDouble(getOrientation().name().replace("_", ""));
AffineTransform rotate = AffineTransform.getRotateInstance(Math.toRadians(angle), first.x, first.y); AffineTransform rotate = AffineTransform.getRotateInstance(Math.toRadians(angle), first.x, first.y);
@@ -71,9 +71,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
protected Size length; protected Size length;
protected Size width; protected Size width;
@Deprecated protected Point[] points = new Point[] {new Point((int) (-DEFAULT_SIZE.convertToPixels() / 2), 0),
protected Point[] points;
protected Point[] newPoints = new Point[] {new Point((int) (-DEFAULT_SIZE.convertToPixels() / 2), 0),
new Point((int) (DEFAULT_SIZE.convertToPixels() / 2), 0), new Point(0, 0)}; new Point((int) (DEFAULT_SIZE.convertToPixels() / 2), 0), new Point(0, 0)};
protected Color bodyColor = Color.white; protected Color bodyColor = Color.white;
protected Color borderColor = Color.black; protected Color borderColor = Color.black;
@@ -98,24 +96,22 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} catch (NullPointerException e) { } catch (NullPointerException e) {
// This will happen if components do not have any shape. // This will happen if components do not have any shape.
} }
if (newPoints != null) points[2] = calculateLabelPosition(points[0], points[1]);
newPoints[2] = calculateLabelPosition(newPoints[0], newPoints[1]);
} }
protected boolean IsCopperArea() { protected boolean IsCopperArea() {
return false; return false;
} }
protected Point[] getNewPoints() { protected Point[] getPoints() {
// convert old points to new // convert old points to new
if (points != null) { if (points.length == 2) {
newPoints = new Point[] { points[0], points[1], calculateLabelPosition(points[0], points[1]) }; points = new Point[] { points[0], points[1], calculateLabelPosition(points[0], points[1]) };
points = null;
// to make standing components backward compatible and not show a label until the user switches the display to something else // to make standing components backward compatible and not show a label until the user switches the display to something else
if (isStanding()) if (isStanding())
display = Display.NONE; display = Display.NONE;
} }
return newPoints; return points;
} }
protected Point calculateLabelPosition(Point point1, Point point2) { protected Point calculateLabelPosition(Point point1, Point point2) {
@@ -129,16 +125,16 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
IDrawingObserver drawingObserver) { IDrawingObserver drawingObserver) {
if (gamma != null) { if (gamma != null) {
// recalculate center position and theta, then adjust label point accordingly, while preserving alpha and p // recalculate center position and theta, then adjust label point accordingly, while preserving alpha and p
double x = (getNewPoints()[1].x + getNewPoints()[0].x) / 2.0; double x = (getPoints()[1].x + getPoints()[0].x) / 2.0;
double y = (getNewPoints()[1].y + getNewPoints()[0].y) / 2.0; double y = (getPoints()[1].y + getPoints()[0].y) / 2.0;
double theta = Math.atan2(getNewPoints()[1].y - getNewPoints()[0].y, getNewPoints()[1].x - getNewPoints()[0].x); double theta = Math.atan2(getPoints()[1].y - getPoints()[0].y, getPoints()[1].x - getPoints()[0].x);
double beta = gamma - (Math.PI / 2 - theta); double beta = gamma - (Math.PI / 2 - theta);
getNewPoints()[2].setLocation(x + Math.cos(beta) * r, y + Math.sin(beta) * r); getPoints()[2].setLocation(x + Math.cos(beta) * r, y + Math.sin(beta) * r);
gamma = null; gamma = null;
r = null; r = null;
} }
double distance = getNewPoints()[0].distance(getNewPoints()[1]); double distance = getPoints()[0].distance(getPoints()[1]);
Shape shape = getBodyShape(); Shape shape = getBodyShape();
// If there's no body, just draw the line connecting the ending points. // If there's no body, just draw the line connecting the ending points.
if (shape == null) { if (shape == null) {
@@ -150,15 +146,15 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
double width; double width;
double length; double length;
Rectangle shapeRect; Rectangle shapeRect;
double theta = Math.atan2(getNewPoints()[1].y - getNewPoints()[0].y, getNewPoints()[1].x - getNewPoints()[0].x); double theta = Math.atan2(getPoints()[1].y - getPoints()[0].y, getPoints()[1].x - getPoints()[0].x);
if (isStanding()) { if (isStanding()) {
// When ending points are too close draw the component in standing // When ending points are too close draw the component in standing
// mode. // mode.
width = length = getClosestOdd(this.width.convertToPixels()); width = length = getClosestOdd(this.width.convertToPixels());
Shape body = Shape body =
new Ellipse2D.Double((getFlipStanding() ? getNewPoints()[1] : getNewPoints()[0]).x - width / 2, new Ellipse2D.Double((getFlipStanding() ? getPoints()[1] : getPoints()[0]).x - width / 2,
(getFlipStanding() ? getNewPoints()[1] : getNewPoints()[0]).y - width / 2, width, width); (getFlipStanding() ? getPoints()[1] : getPoints()[0]).y - width / 2, width, width);
shapeRect = body.getBounds(); shapeRect = body.getBounds();
Composite oldComposite = g2d.getComposite(); Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) { if (alpha < MAX_ALPHA) {
@@ -206,14 +202,14 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
Area leadArea = new Area(); Area leadArea = new Area();
int endX = (int) (getNewPoints()[0].x + Math.cos(theta) * leadLength); int endX = (int) (getPoints()[0].x + Math.cos(theta) * leadLength);
int endY = (int) (getNewPoints()[0].y + Math.sin(theta) * leadLength); int endY = (int) (getPoints()[0].y + Math.sin(theta) * leadLength);
Line2D line = new Line2D.Double(getNewPoints()[0].x, getNewPoints()[0].y, endX, endY); Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY);
leadArea.add(new Area(leadStroke.createStrokedShape(line))); leadArea.add(new Area(leadStroke.createStrokedShape(line)));
endX = (int) (getNewPoints()[1].x + Math.cos(theta - Math.PI) * leadLength); endX = (int) (getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength);
endY = (int) (getNewPoints()[1].y + Math.sin(theta - Math.PI) * leadLength); endY = (int) (getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength);
line = new Line2D.Double(getNewPoints()[1].x, getNewPoints()[1].y, endX, endY); line = new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY);
leadArea.add(new Area(leadStroke.createStrokedShape(line))); leadArea.add(new Area(leadStroke.createStrokedShape(line)));
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
@@ -238,7 +234,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
width = getWidth().convertToPixels(); width = getWidth().convertToPixels();
length = getLength().convertToPixels(); length = getLength().convertToPixels();
} }
g2d.translate((getNewPoints()[0].x + getNewPoints()[1].x - length) / 2, (getNewPoints()[0].y + getNewPoints()[1].y - width) / 2); g2d.translate((getPoints()[0].x + getPoints()[1].x - length) / 2, (getPoints()[0].y + getPoints()[1].y - width) / 2);
g2d.rotate(theta, length / 2, width / 2); g2d.rotate(theta, length / 2, width / 2);
// Draw body. // Draw body.
Composite oldComposite = g2d.getComposite(); Composite oldComposite = g2d.getComposite();
@@ -331,7 +327,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
if (getMoveLabel()) { if (getMoveLabel()) {
g2d.setTransform(oldTransform); g2d.setTransform(oldTransform);
g2d.translate(getNewPoints()[2].x, getNewPoints()[2].y); g2d.translate(getPoints()[2].x, getPoints()[2].y);
if (getLabelOriantation() != LabelOriantation.Horizontal) { if (getLabelOriantation() != LabelOriantation.Horizontal) {
g2d.rotate(theta); g2d.rotate(theta);
} }
@@ -340,8 +336,8 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} else { } else {
if (isStanding() || getLabelOriantation() == LabelOriantation.Horizontal) { if (isStanding() || getLabelOriantation() == LabelOriantation.Horizontal) {
g2d.setTransform(oldTransform); g2d.setTransform(oldTransform);
double x = (getNewPoints()[0].x + getNewPoints()[1].x - length) / 2.0; double x = (getPoints()[0].x + getPoints()[1].x - length) / 2.0;
double y = (getNewPoints()[0].y + getNewPoints()[1].y - width) / 2.0; double y = (getPoints()[0].y + getPoints()[1].y - width) / 2.0;
g2d.drawString(label, (int) (x + (length - textRect.getWidth()) / 2 + offset), g2d.drawString(label, (int) (x + (length - textRect.getWidth()) / 2 + offset),
(int)(y + calculateLabelYOffset(shapeRect, textRect, fontMetrics))); (int)(y + calculateLabelYOffset(shapeRect, textRect, fontMetrics)));
} else { } else {
@@ -363,17 +359,17 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} }
protected boolean isStanding() { protected boolean isStanding() {
return supportsStandingMode() && this.length.convertToPixels() > getNewPoints()[0].distance(getNewPoints()[1]); return supportsStandingMode() && this.length.convertToPixels() > getPoints()[0].distance(getPoints()[1]);
} }
private void drawLeads(Graphics2D g2d, double theta, double leadLength) { private void drawLeads(Graphics2D g2d, double theta, double leadLength) {
double endX = getNewPoints()[0].x + Math.cos(theta) * leadLength; double endX = getPoints()[0].x + Math.cos(theta) * leadLength;
double endY = getNewPoints()[0].y + Math.sin(theta) * leadLength; double endY = getPoints()[0].y + Math.sin(theta) * leadLength;
g2d.draw(new Line2D.Double(getNewPoints()[0].x, getNewPoints()[0].y, endX, endY)); g2d.draw(new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY));
endX = getNewPoints()[1].x + Math.cos(theta - Math.PI) * leadLength; endX = getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength;
endY = getNewPoints()[1].y + Math.sin(theta - Math.PI) * leadLength; endY = getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength;
g2d.draw(new Line2D.Double(getNewPoints()[1].x, getNewPoints()[1].y, endX, endY)); g2d.draw(new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY));
} }
private void drawLead(Graphics2D g2d, ComponentState componentState, IDrawingObserver observer, boolean isCopperArea) { private void drawLead(Graphics2D g2d, ComponentState componentState, IDrawingObserver observer, boolean isCopperArea) {
@@ -382,7 +378,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
float thickness = getLeadThickness(); float thickness = getLeadThickness();
Line2D line = new Line2D.Double(getNewPoints()[0].x, getNewPoints()[0].y, getNewPoints()[1].x, getNewPoints()[1].y); Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, getPoints()[1].x, getPoints()[1].y);
if (shouldShadeLeads()) { if (shouldShadeLeads()) {
// for some reason the stroked line gets approx 1px thicker when converted to shape // for some reason the stroked line gets approx 1px thicker when converted to shape
@@ -519,12 +515,12 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
@Override @Override
public int getControlPointCount() { public int getControlPointCount() {
return getNewPoints().length; return getPoints().length;
} }
@Override @Override
public Point getControlPoint(int index) { public Point getControlPoint(int index) {
return (Point) getNewPoints()[index]; return (Point) getPoints()[index];
} }
@Override @Override
@@ -547,12 +543,12 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
// when moving one of the ending points, try to retain the angle and distance from the center point to label point // when moving one of the ending points, try to retain the angle and distance from the center point to label point
if (index < 2) { if (index < 2) {
if (gamma == null) { if (gamma == null) {
double x = (getNewPoints()[1].x + getNewPoints()[0].x) / 2.0; double x = (getPoints()[1].x + getPoints()[0].x) / 2.0;
double y = (getNewPoints()[1].y + getNewPoints()[0].y) / 2.0; double y = (getPoints()[1].y + getPoints()[0].y) / 2.0;
double theta = Math.atan2(getNewPoints()[1].y - getNewPoints()[0].y, getNewPoints()[1].x - getNewPoints()[0].x); double theta = Math.atan2(getPoints()[1].y - getPoints()[0].y, getPoints()[1].x - getPoints()[0].x);
double beta = Math.atan2(getNewPoints()[2].y - y, getNewPoints()[2].x - x); double beta = Math.atan2(getPoints()[2].y - y, getPoints()[2].x - x);
gamma = beta + (Math.PI / 2 - theta); gamma = beta + (Math.PI / 2 - theta);
r = getNewPoints()[2].distance(x, y); r = getPoints()[2].distance(x, y);
} else { // in case when we are copy pasting we don't want to recalculate 3rd point position as they will all move in unison } else { // in case when we are copy pasting we don't want to recalculate 3rd point position as they will all move in unison
// when we moved the first point, gamma and r were initialized, so now we are canceling // when we moved the first point, gamma and r were initialized, so now we are canceling
gamma = null; gamma = null;
@@ -560,7 +556,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
} }
} }
getNewPoints()[index].setLocation(point); getPoints()[index].setLocation(point);
} }
@EditableProperty(name = "Color") @EditableProperty(name = "Color")
@@ -49,7 +49,7 @@ public class PotentiometerSymbol extends Abstract3LegSymbol {
public Shape[] getBody() { public Shape[] getBody() {
Shape[] body = new Shape[3]; Shape[] body = new Shape[3];
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
int x = controlPoints[0].x; int x = controlPoints[0].x;
int y = controlPoints[0].y; int y = controlPoints[0].y;
int pinSpacing = (int) PIN_SPACING.convertToPixels(); int pinSpacing = (int) PIN_SPACING.convertToPixels();
@@ -47,7 +47,7 @@ public class BJTSymbol extends Abstract3LegSymbol {
public Shape[] getBody() { public Shape[] getBody() {
Shape[] body = new Shape[3]; Shape[] body = new Shape[3];
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
int x = controlPoints[0].x; int x = controlPoints[0].x;
int y = controlPoints[0].y; int y = controlPoints[0].y;
int pinSpacing = (int) PIN_SPACING.convertToPixels(); int pinSpacing = (int) PIN_SPACING.convertToPixels();
@@ -45,7 +45,7 @@ public class JFETSymbol extends Abstract3LegSymbol {
public Shape[] getBody() { public Shape[] getBody() {
Shape[] body = new Shape[3]; Shape[] body = new Shape[3];
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
int x = controlPoints[0].x; int x = controlPoints[0].x;
int y = controlPoints[0].y; int y = controlPoints[0].y;
int pinSpacing = (int) PIN_SPACING.convertToPixels(); int pinSpacing = (int) PIN_SPACING.convertToPixels();
@@ -45,7 +45,7 @@ public class MOSFETSymbol extends Abstract3LegSymbol {
public Shape[] getBody() { public Shape[] getBody() {
Shape[] body = new Shape[3]; Shape[] body = new Shape[3];
Point[] controlPoints = getNewControlPoints(); Point[] controlPoints = getControlPoints();
int x = controlPoints[0].x; int x = controlPoints[0].x;
int y = controlPoints[0].y; int y = controlPoints[0].y;
int pinSpacing = (int) PIN_SPACING.convertToPixels(); int pinSpacing = (int) PIN_SPACING.convertToPixels();
Binary file not shown.
Binary file not shown.