make grid spacing flexible

This commit is contained in:
bancika
2011-01-15 10:11:09 +00:00
parent c8de2aa271
commit dd4080accb
14 changed files with 110 additions and 65 deletions
@@ -12,7 +12,7 @@
</entry> </entry>
<entry> <entry>
<string>metric</string> <string>metric</string>
<boolean>true</boolean> <boolean>false</boolean>
</entry> </entry>
<entry> <entry>
<string>default.org.diylc.components.semiconductors.DIL_IC:Value</string> <string>default.org.diylc.components.semiconductors.DIL_IC:Value</string>
@@ -11,17 +11,21 @@ import java.awt.geom.CubicCurve2D;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.utils.Constants; import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComponent<T> { public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComponent<T> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static Color GUIDELINE_COLOR = Color.blue; public static Color GUIDELINE_COLOR = Color.blue;
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
protected Point[] controlPoints = new Point[] { protected Point[] controlPoints = new Point[] {
new Point(-5 * Constants.GRID, -5 * Constants.GRID), new Point(0, -5 * Constants.GRID), new Point(-DEFAULT_SIZE.convertToPixels() / 2, -DEFAULT_SIZE.convertToPixels() / 2),
new Point(0, 5 * Constants.GRID), new Point(5 * Constants.GRID, 5 * Constants.GRID) }; new Point(0, -DEFAULT_SIZE.convertToPixels() / 2),
new Point(0, DEFAULT_SIZE.convertToPixels() / 2),
new Point(DEFAULT_SIZE.convertToPixels() / 2, DEFAULT_SIZE.convertToPixels() / 2) };
protected Color color = getDefaultColor(); protected Color color = getDefaultColor();
@@ -32,11 +32,13 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
public static Color LABEL_COLOR = Color.black; public static Color LABEL_COLOR = Color.black;
public static Color LABEL_COLOR_SELECTED = Color.red; public static Color LABEL_COLOR_SELECTED = Color.red;
public static Size LEAD_THICKNESS = new Size(0.3d, SizeUnit.mm); public static Size LEAD_THICKNESS = new Size(0.3d, SizeUnit.mm);
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
protected Size width; protected Size width;
protected Size height; protected Size height;
protected Point[] points = new Point[] { new Point((int) (-Constants.GRID * 5), 0), protected Point[] points = new Point[] {
new Point((int) (Constants.GRID * 5), 0) }; new Point((int) (-DEFAULT_SIZE.convertToPixels() / 2), 0),
new Point(DEFAULT_SIZE.convertToPixels() / 2, 0) };
protected Color bodyColor = getDefaultBodyColor(); protected Color bodyColor = getDefaultBodyColor();
protected Color borderColor = getDefaultBorderColor(); protected Color borderColor = getDefaultBorderColor();
@@ -24,12 +24,14 @@ public class Project implements Serializable {
public static String DEFAULT_TITLE = "New Project"; public static String DEFAULT_TITLE = "New Project";
public static Size DEFAULT_WIDTH = new Size(29d, SizeUnit.cm); public static Size DEFAULT_WIDTH = new Size(29d, SizeUnit.cm);
public static Size DEFAULT_HEIGHT = new Size(21d, SizeUnit.cm); public static Size DEFAULT_HEIGHT = new Size(21d, SizeUnit.cm);
public static Size DEFAULT_GRID_SPACING = new Size(0.2d, SizeUnit.in);
private String title; private String title;
private String author; private String author;
private String description; private String description;
private Size width; private Size width;
private Size height; private Size height;
private Size gridSpacing;
private List<IDIYComponent<?>> components; private List<IDIYComponent<?>> components;
private Set<Set<IDIYComponent<?>>> groups; private Set<Set<IDIYComponent<?>>> groups;
@@ -40,9 +42,10 @@ public class Project implements Serializable {
author = System.getProperty("user.name"); author = System.getProperty("user.name");
width = DEFAULT_WIDTH; width = DEFAULT_WIDTH;
height = DEFAULT_HEIGHT; height = DEFAULT_HEIGHT;
gridSpacing = DEFAULT_GRID_SPACING;
} }
public Project(String title, String author, String description, Size width, Size height, public Project(String title, String author, String description, Size width, Size height, Size gridSpacing,
List<IDIYComponent<?>> components, Set<Set<IDIYComponent<?>>> groups) { List<IDIYComponent<?>> components, Set<Set<IDIYComponent<?>>> groups) {
super(); super();
this.title = title; this.title = title;
@@ -50,6 +53,7 @@ public class Project implements Serializable {
this.description = description; this.description = description;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.gridSpacing = gridSpacing;
this.components = components; this.components = components;
this.groups = groups; this.groups = groups;
} }
@@ -99,6 +103,15 @@ public class Project implements Serializable {
this.height = height; this.height = height;
} }
@EditableProperty
public Size getGridSpacing() {
return gridSpacing;
}
public void setGridSpacing(Size gridSpacing) {
this.gridSpacing = gridSpacing;
}
/** /**
* List of components sorted by z-order ascending. * List of components sorted by z-order ascending.
* *
@@ -17,10 +17,12 @@ public class Size extends AbstractMeasure<SizeUnit> {
} }
public int convertToPixels() { public int convertToPixels() {
double factor = getUnit().getFactor() / SizeUnit.in.getFactor(); // double factor = getUnit().getFactor() / SizeUnit.in.getFactor();
int grids = (int) (factor * getValue() * Constants.GRIDS_PER_INCH); // int grids = (int) (factor * getValue() * Constants.GRIDS_PER_INCH);
double remainder = (factor * getValue() * Constants.GRIDS_PER_INCH) - grids; // double remainder = (factor * getValue() * Constants.GRIDS_PER_INCH) - grids;
return (int) Math.round(Constants.GRID * (grids + remainder)); // return (int) Math.round(Constants.PIXELS_PER_INCH / Constants.GRIDS_PER_INCH
// * (grids + remainder));
return (int) (getValue() * getUnit().getFactor() / SizeUnit.in.getFactor() * Constants.PIXELS_PER_INCH);
} }
@Override @Override
@@ -58,7 +58,7 @@ public class Presenter implements IPlugInPort {
private static final Logger LOG = Logger.getLogger(Presenter.class); private static final Logger LOG = Logger.getLogger(Presenter.class);
public static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0); public static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 1, 0);
public static final String DEFAULTS_KEY = "default."; public static final String DEFAULTS_KEY = "default.";
public static final String METRIC_KEY = "metric"; public static final String METRIC_KEY = "metric";
@@ -235,7 +235,7 @@ public class Presenter implements IPlugInPort {
g2dWrapper.fillRect(0, 0, d.width, d.height); g2dWrapper.fillRect(0, 0, d.width, d.height);
if (drawOptions.contains(DrawOption.GRID)) { if (drawOptions.contains(DrawOption.GRID)) {
double zoomStep = Constants.GRID * zoomLevel; double zoomStep = currentProject.getGridSpacing().convertToPixels() * zoomLevel;
g2dWrapper.setColor(Constants.GRID_COLOR); g2dWrapper.setColor(Constants.GRID_COLOR);
for (double i = zoomStep; i < d.width; i += zoomStep) { for (double i = zoomStep; i < d.width; i += zoomStep) {
@@ -331,7 +331,8 @@ public class Presenter implements IPlugInPort {
@Override @Override
public void injectGUIComponent(JComponent component, int position) throws BadPositionException { public void injectGUIComponent(JComponent component, int position) throws BadPositionException {
LOG.info(String.format("injectGUIComponent(%s, %s)", component.getClass().getName(), position)); LOG.info(String.format("injectGUIComponent(%s, %s)", component.getClass().getName(),
position));
view.addComponent(component, position); view.addComponent(component, position);
} }
@@ -620,12 +621,13 @@ public class Presenter implements IPlugInPort {
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) { if (snapToGrid) {
dx = (int) (Math.round(dx / Constants.GRID) * Constants.GRID); dx = roundToGrid(dx);
dy = (int) (Math.round(dy / Constants.GRID) * Constants.GRID); dy = roundToGrid(dy);
} }
// 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;
if (repaint) {
previousDragPoint.translate(dx, dy); previousDragPoint.translate(dx, dy);
// Update all points. // Update all points.
@@ -637,7 +639,7 @@ public class Presenter implements IPlugInPort {
c.setControlPoint(p, index); c.setControlPoint(p, index);
} }
} }
// dragStartPoint = point; }
} else if (selectedComponents.isEmpty()) { } else if (selectedComponents.isEmpty()) {
// 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
// selection rectangle and refresh. // selection rectangle and refresh.
@@ -654,6 +656,17 @@ public class Presenter implements IPlugInPort {
return true; return true;
} }
/**
* Rounds the number to the closest grid line.
*
* @param x
* @return
*/
private int roundToGrid(int x) {
int grid = currentProject.getGridSpacing().convertToPixels();
return (Math.round(x / grid) * grid);
}
@Override @Override
public void dragEnded(Point point) { public void dragEnded(Point point) {
LOG.debug(String.format("dragEnded(%s)", point)); LOG.debug(String.format("dragEnded(%s)", point));
@@ -867,8 +880,8 @@ public class Presenter implements IPlugInPort {
} }
double width = area.getBounds2D().getWidth(); double width = area.getBounds2D().getWidth();
double height = area.getBounds2D().getHeight(); double height = area.getBounds2D().getHeight();
width /= Constants.GRID * Constants.GRIDS_PER_INCH; width /= Constants.PIXELS_PER_INCH;
height /= Constants.GRID * Constants.GRIDS_PER_INCH; height /= Constants.PIXELS_PER_INCH;
if (isMetric) { if (isMetric) {
width *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor(); width *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor();
height *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor(); height *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor();
@@ -935,8 +948,8 @@ public class Presenter implements IPlugInPort {
int x = controlPoint.x + point.x; int x = controlPoint.x + point.x;
int y = controlPoint.y + point.y; int y = controlPoint.y + point.y;
if (snapToGrid) { if (snapToGrid) {
x = (int) (Math.round(x / Constants.GRID) * Constants.GRID); x = roundToGrid(x);
y = (int) (Math.round(y / Constants.GRID) * Constants.GRID); y = roundToGrid(y);
} }
controlPoint.setLocation(x, y); controlPoint.setLocation(x, y);
component.setControlPoint(controlPoint, j); component.setControlPoint(controlPoint, j);
@@ -5,15 +5,12 @@ import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.Stroke; import java.awt.Stroke;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
public class Constants { public class Constants {
// public static final int PIXELS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution(); public static final int PIXELS_PER_INCH = 100;//Toolkit.getDefaultToolkit().getScreenResolution();
public static int GRIDS_PER_INCH = 10; public static int GRIDS_PER_INCH = 8;
public static Size GRID_SIZE = new Size(1d / GRIDS_PER_INCH, SizeUnit.in); // public static Size GRID_SIZE = new Size(1d / GRIDS_PER_INCH, SizeUnit.in);
public static final int GRID = 10; // public static final int GRID = 20;
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);
@@ -2,6 +2,6 @@
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/diylc.jar" sourcepath="/DIYLC"/> <classpathentry combineaccessrules="false" kind="src" path="/DIYLC"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
@@ -11,7 +11,8 @@ import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.utils.Constants; import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
public abstract class AbstractBoard extends AbstractTransparentComponent<String> { public abstract class AbstractBoard extends AbstractTransparentComponent<String> {
@@ -19,12 +20,14 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
public static Color BOARD_COLOR = Color.decode("#BCED91"); public static Color BOARD_COLOR = Color.decode("#BCED91");
public static Color BORDER_COLOR = BOARD_COLOR.darker(); public static Color BORDER_COLOR = BOARD_COLOR.darker();
public static Size DEFAULT_WIDTH = new Size(1.5d, SizeUnit.in);
public static Size DEFAULT_HEIGHT = new Size(1d, SizeUnit.in);
protected String name; protected String name;
protected String value; protected String value;
protected Point[] controlPoints = new Point[] { protected Point[] controlPoints = new Point[] {
new Point((int) (-5 * Constants.GRID), (int) (-5 * Constants.GRID)), new Point(-DEFAULT_WIDTH.convertToPixels() / 2, -DEFAULT_HEIGHT.convertToPixels() / 2),
new Point((int) (5 * Constants.GRID), (int) (5 * Constants.GRID)) }; new Point(DEFAULT_WIDTH.convertToPixels() / 2, DEFAULT_HEIGHT.convertToPixels() / 2) };
protected Color boardColor = BOARD_COLOR; protected Color boardColor = BOARD_COLOR;
protected Color borderColor = BORDER_COLOR; protected Color borderColor = BORDER_COLOR;
@@ -30,7 +30,7 @@ public class HookupWire extends AbstractCurvedComponent<AWG> {
@Override @Override
protected void drawCurve(CubicCurve2D curve, Graphics2D g2d, ComponentState componentState) { protected void drawCurve(CubicCurve2D curve, Graphics2D g2d, ComponentState componentState) {
int thickness = (int) (Math.pow(Math.E, -1.12436 - 0.11594 * value.getValue()) int thickness = (int) (Math.pow(Math.E, -1.12436 - 0.11594 * value.getValue())
* Constants.GRID * Constants.GRIDS_PER_INCH * (1 + 2 * INSULATION_THICKNESS_PCT)); * Constants.PIXELS_PER_INCH * (1 + 2 * INSULATION_THICKNESS_PCT));
g2d.setColor(componentState == ComponentState.SELECTED g2d.setColor(componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : color.darker()); || componentState == ComponentState.DRAGGING ? SELECTION_COLOR : color.darker());
g2d.setStroke(new BasicStroke(thickness)); g2d.setStroke(new BasicStroke(thickness));
@@ -25,7 +25,7 @@ import org.diylc.utils.Constants;
public class BOM extends AbstractComponent<Void> { public class BOM extends AbstractComponent<Void> {
public static Size DEFAULT_SIZE = new Size(10d, SizeUnit.cm); public static Size DEFAULT_SIZE = new Size(10d, SizeUnit.cm);
public static Size SPACING = Constants.GRID_SIZE; public static Size SPACING = new Size(0.1d, SizeUnit.in);
public static Color COLOR = Color.black; public static Color COLOR = Color.black;
public static String DEFAULT_TEXT = "No components to show in the Bill of Materials"; public static String DEFAULT_TEXT = "No components to show in the Bill of Materials";
@@ -20,7 +20,6 @@ import org.diylc.utils.Constants;
public class ProjectInformation extends AbstractComponent<Void> { public class ProjectInformation extends AbstractComponent<Void> {
public static Size DEFAULT_SIZE = new Size(10d, SizeUnit.cm); public static Size DEFAULT_SIZE = new Size(10d, SizeUnit.cm);
public static Size SPACING = Constants.GRID_SIZE;
public static Color COLOR = Color.black; public static Color COLOR = Color.black;
public static String DEFAULT_TEXT = "No components to show in the Bill of Materials"; public static String DEFAULT_TEXT = "No components to show in the Bill of Materials";
@@ -41,13 +41,16 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
private String value = ""; private String value = "";
private Orientation orientation = Orientation.DEFAULT; private Orientation orientation = Orientation.DEFAULT;
private PinCount pinCount = PinCount._8; private PinCount pinCount = PinCount._8;
private Size pinSpacing = new Size(0.1d, SizeUnit.in);
private Size rowSpacing = new Size(0.3d, SizeUnit.in); private Size rowSpacing = new Size(0.3d, SizeUnit.in);
private Point[] controlPoints = new Point[] { new Point(0, 0), private Point[] controlPoints = new Point[] { new Point(0, 0) };
new Point(0, (int) Constants.GRID), new Point(0, (int) (2 * Constants.GRID)), // new Point(0, pinSpacing.convertToPixels()),
new Point(0, (int) (3 * Constants.GRID)), new Point((int) (3 * Constants.GRID), 0), // new Point(0, 2 * pinSpacing.convertToPixels()),
new Point((int) (3 * Constants.GRID), (int) Constants.GRID), // new Point(0, 3 * pinSpacing.convertToPixels()),
new Point((int) (3 * Constants.GRID), (int) (2 * Constants.GRID)), // new Point(3 * pinSpacing.convertToPixels(), 0),
new Point((int) (3 * Constants.GRID), (int) (3 * Constants.GRID)) }; // new Point(3 * pinSpacing.convertToPixels(), pinSpacing.convertToPixels()),
// new Point(3 * pinSpacing.convertToPixels(), 2 * pinSpacing.convertToPixels()),
// new Point(3 * pinSpacing.convertToPixels(), 3 * pinSpacing.convertToPixels()) };
private Shape body; private Shape body;
public DIL_IC() { public DIL_IC() {
@@ -87,6 +90,15 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
body = null; body = null;
} }
@EditableProperty(name = "Pin spacing")
public Size getPinSpacing() {
return pinSpacing;
}
public void setPinSpacing(Size pinSpacing) {
this.pinSpacing = pinSpacing;
}
@EditableProperty(name = "Row spacing") @EditableProperty(name = "Row spacing")
public Size getRowSpacing() { public Size getRowSpacing() {
return rowSpacing; return rowSpacing;
@@ -128,26 +140,26 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
switch (orientation) { switch (orientation) {
case DEFAULT: case DEFAULT:
dx1 = 0; dx1 = 0;
dy1 = (int) (i * Constants.GRID); dy1 = i * pinSpacing.convertToPixels();
dx2 = rowSpacing.convertToPixels(); dx2 = rowSpacing.convertToPixels();
dy2 = (int) (i * Constants.GRID); dy2 = i * pinSpacing.convertToPixels();
break; break;
case _90: case _90:
dx1 = (int) (-i * Constants.GRID); dx1 = -i * pinSpacing.convertToPixels();
dy1 = 0; dy1 = 0;
dx2 = (int) (-i * Constants.GRID); dx2 = -i * pinSpacing.convertToPixels();
dy2 = rowSpacing.convertToPixels(); dy2 = rowSpacing.convertToPixels();
break; break;
case _180: case _180:
dx1 = 0; dx1 = 0;
dy1 = (int) (-i * Constants.GRID); dy1 = -i * pinSpacing.convertToPixels();
dx2 = -rowSpacing.convertToPixels(); dx2 = -rowSpacing.convertToPixels();
dy2 = (int) (-i * Constants.GRID); dy2 = -i * pinSpacing.convertToPixels();
break; break;
case _270: case _270:
dx1 = (int) (i * Constants.GRID); dx1 = i * pinSpacing.convertToPixels();
dy1 = 0; dy1 = 0;
dx2 = (int) (i * Constants.GRID); dx2 = i * pinSpacing.convertToPixels();
dy2 = rowSpacing.convertToPixels(); dy2 = rowSpacing.convertToPixels();
break; break;
default: default:
@@ -169,26 +181,26 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
switch (orientation) { switch (orientation) {
case DEFAULT: case DEFAULT:
width = rowSpacing.convertToPixels() - pinSize; width = rowSpacing.convertToPixels() - pinSize;
height = (int) ((pinCount.getValue() / 2) * Constants.GRID); height = (pinCount.getValue() / 2) * pinSpacing.convertToPixels();
x += pinSize / 2; x += pinSize / 2;
y -= (int) (Constants.GRID / 2); y -= pinSpacing.convertToPixels() / 2;
break; break;
case _90: case _90:
width = (int) ((pinCount.getValue() / 2) * Constants.GRID); width = (pinCount.getValue() / 2) * pinSpacing.convertToPixels();
height = rowSpacing.convertToPixels() - pinSize; height = rowSpacing.convertToPixels() - pinSize;
x -= (int) (Constants.GRID / 2) + width - Constants.GRID; x -= (pinSpacing.convertToPixels() / 2) + width - pinSpacing.convertToPixels();
y += pinSize / 2; y += pinSize / 2;
break; break;
case _180: case _180:
width = rowSpacing.convertToPixels() - pinSize; width = rowSpacing.convertToPixels() - pinSize;
height = (int) ((pinCount.getValue() / 2) * Constants.GRID); height = (pinCount.getValue() / 2) * pinSpacing.convertToPixels();
x -= rowSpacing.convertToPixels() - pinSize / 2; x -= rowSpacing.convertToPixels() - pinSize / 2;
y -= (int) (Constants.GRID / 2) + height - Constants.GRID; y -= (pinSpacing.convertToPixels() / 2) + height - pinSpacing.convertToPixels();
break; break;
case _270: case _270:
width = (int) ((pinCount.getValue() / 2) * Constants.GRID); width = (pinCount.getValue() / 2) * pinSpacing.convertToPixels();
height = rowSpacing.convertToPixels() - pinSize; height = rowSpacing.convertToPixels() - pinSize;
x -= (int) (Constants.GRID / 2); x -= pinSpacing.convertToPixels() / 2;
y += pinSize / 2; y += pinSize / 2;
break; break;
default: default: