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
@@ -11,17 +11,21 @@ import java.awt.geom.CubicCurve2D;
import org.diylc.core.ComponentState;
import org.diylc.core.Project;
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> {
private static final long serialVersionUID = 1L;
public static Color GUIDELINE_COLOR = Color.blue;
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
protected Point[] controlPoints = new Point[] {
new Point(-5 * Constants.GRID, -5 * Constants.GRID), new Point(0, -5 * Constants.GRID),
new Point(0, 5 * Constants.GRID), new Point(5 * Constants.GRID, 5 * Constants.GRID) };
new Point(-DEFAULT_SIZE.convertToPixels() / 2, -DEFAULT_SIZE.convertToPixels() / 2),
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();
@@ -32,11 +32,13 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
public static Color LABEL_COLOR = Color.black;
public static Color LABEL_COLOR_SELECTED = Color.red;
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 height;
protected Point[] points = new Point[] { new Point((int) (-Constants.GRID * 5), 0),
new Point((int) (Constants.GRID * 5), 0) };
protected Point[] points = new Point[] {
new Point((int) (-DEFAULT_SIZE.convertToPixels() / 2), 0),
new Point(DEFAULT_SIZE.convertToPixels() / 2, 0) };
protected Color bodyColor = getDefaultBodyColor();
protected Color borderColor = getDefaultBorderColor();
@@ -24,12 +24,14 @@ public class Project implements Serializable {
public static String DEFAULT_TITLE = "New Project";
public static Size DEFAULT_WIDTH = new Size(29d, 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 author;
private String description;
private Size width;
private Size height;
private Size gridSpacing;
private List<IDIYComponent<?>> components;
private Set<Set<IDIYComponent<?>>> groups;
@@ -40,9 +42,10 @@ public class Project implements Serializable {
author = System.getProperty("user.name");
width = DEFAULT_WIDTH;
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) {
super();
this.title = title;
@@ -50,6 +53,7 @@ public class Project implements Serializable {
this.description = description;
this.width = width;
this.height = height;
this.gridSpacing = gridSpacing;
this.components = components;
this.groups = groups;
}
@@ -98,6 +102,15 @@ public class Project implements Serializable {
public void setHeight(Size 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.
@@ -17,10 +17,12 @@ public class Size extends AbstractMeasure<SizeUnit> {
}
public int convertToPixels() {
double factor = getUnit().getFactor() / SizeUnit.in.getFactor();
int grids = (int) (factor * getValue() * Constants.GRIDS_PER_INCH);
double remainder = (factor * getValue() * Constants.GRIDS_PER_INCH) - grids;
return (int) Math.round(Constants.GRID * (grids + remainder));
// double factor = getUnit().getFactor() / SizeUnit.in.getFactor();
// int grids = (int) (factor * getValue() * Constants.GRIDS_PER_INCH);
// double remainder = (factor * getValue() * Constants.GRIDS_PER_INCH) - grids;
// 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
@@ -58,7 +58,7 @@ public class Presenter implements IPlugInPort {
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 METRIC_KEY = "metric";
@@ -235,7 +235,7 @@ public class Presenter implements IPlugInPort {
g2dWrapper.fillRect(0, 0, d.width, d.height);
if (drawOptions.contains(DrawOption.GRID)) {
double zoomStep = Constants.GRID * zoomLevel;
double zoomStep = currentProject.getGridSpacing().convertToPixels() * zoomLevel;
g2dWrapper.setColor(Constants.GRID_COLOR);
for (double i = zoomStep; i < d.width; i += zoomStep) {
@@ -331,7 +331,8 @@ public class Presenter implements IPlugInPort {
@Override
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);
}
@@ -620,24 +621,25 @@ public class Presenter implements IPlugInPort {
int dx = (scaledPoint.x - previousDragPoint.x);
int dy = (scaledPoint.y - previousDragPoint.y);
if (snapToGrid) {
dx = (int) (Math.round(dx / Constants.GRID) * Constants.GRID);
dy = (int) (Math.round(dy / Constants.GRID) * Constants.GRID);
dx = roundToGrid(dx);
dy = roundToGrid(dy);
}
// Only repaint if there's an actual change.
repaint = dx != 0 || dy != 0;
previousDragPoint.translate(dx, dy);
if (repaint) {
previousDragPoint.translate(dx, dy);
// Update all points.
for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap.entrySet()) {
IDIYComponent<?> c = entry.getKey();
for (Integer index : entry.getValue()) {
Point p = new Point(c.getControlPoint(index));
p.translate(dx, dy);
c.setControlPoint(p, index);
// Update all points.
for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap.entrySet()) {
IDIYComponent<?> c = entry.getKey();
for (Integer index : entry.getValue()) {
Point p = new Point(c.getControlPoint(index));
p.translate(dx, dy);
c.setControlPoint(p, index);
}
}
}
// dragStartPoint = point;
} else if (selectedComponents.isEmpty()) {
// If there's no selection, the only thing to do is update the
// selection rectangle and refresh.
@@ -654,6 +656,17 @@ public class Presenter implements IPlugInPort {
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
public void dragEnded(Point point) {
LOG.debug(String.format("dragEnded(%s)", point));
@@ -867,8 +880,8 @@ public class Presenter implements IPlugInPort {
}
double width = area.getBounds2D().getWidth();
double height = area.getBounds2D().getHeight();
width /= Constants.GRID * Constants.GRIDS_PER_INCH;
height /= Constants.GRID * Constants.GRIDS_PER_INCH;
width /= Constants.PIXELS_PER_INCH;
height /= Constants.PIXELS_PER_INCH;
if (isMetric) {
width *= 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 y = controlPoint.y + point.y;
if (snapToGrid) {
x = (int) (Math.round(x / Constants.GRID) * Constants.GRID);
y = (int) (Math.round(y / Constants.GRID) * Constants.GRID);
x = roundToGrid(x);
y = roundToGrid(y);
}
controlPoint.setLocation(x, y);
component.setControlPoint(controlPoint, j);
@@ -5,15 +5,12 @@ import java.awt.Color;
import java.awt.Font;
import java.awt.Stroke;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
public class Constants {
// public static final int PIXELS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution();
public static int GRIDS_PER_INCH = 10;
public static Size GRID_SIZE = new Size(1d / GRIDS_PER_INCH, SizeUnit.in);
public static final int GRID = 10;
public static final int PIXELS_PER_INCH = 100;//Toolkit.getDefaultToolkit().getScreenResolution();
public static int GRIDS_PER_INCH = 8;
// public static Size GRID_SIZE = new Size(1d / GRIDS_PER_INCH, SizeUnit.in);
// public static final int GRID = 20;
public static Color CANVAS_COLOR = Color.white;
public static Color GRID_COLOR = new Color(240, 240, 240);