Allow the origin of co-ordinates on boards to be specified (#428)

* Added support to specific co-ordinate origin.

Added the Coordinate origin property to the AbstractBoard class,
this allows the user to specify the corner (TopLeft, TopRight,
BottomLeft, BottomRight) cordinates should increment from.

Updated rendering to reflect this change.

* Use the coordinate origin setter method

To safely handle the case when a project produced with an earlier
verison of the application is loaded and the coordinate origin attribute
is set to NULL.
This commit is contained in:
Tim Savage
2019-02-10 01:53:11 +11:00
committed by bancika
parent 661e5e2403
commit 51a12d61e1
@@ -61,6 +61,7 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
protected Color coordinateColor = COORDINATE_COLOR; protected Color coordinateColor = COORDINATE_COLOR;
protected Boolean drawCoordinates = null; protected Boolean drawCoordinates = null;
protected CoordinateType xType = CoordinateType.Numbers; protected CoordinateType xType = CoordinateType.Numbers;
protected CoordinateOrigin coordinateOrigin = CoordinateOrigin.Top_Left;
protected CoordinateDisplay coordinateDisplay = CoordinateDisplay.One_Side; protected CoordinateDisplay coordinateDisplay = CoordinateDisplay.One_Side;
protected CoordinateType yType = CoordinateType.Letters; protected CoordinateType yType = CoordinateType.Letters;
@@ -91,34 +92,68 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
g2d.setColor(coordinateColor); g2d.setColor(coordinateColor);
g2d.setFont(project.getFont().deriveFont(COORDINATE_FONT_SIZE)); g2d.setFont(project.getFont().deriveFont(COORDINATE_FONT_SIZE));
// The half space is used to do rounding when calculating the range.
int halfSpace = spacing / 2;
CoordinateOrigin origin = getCoordinateOrigin();
if (getCoordinateDisplay() != CoordinateDisplay.None) { if (getCoordinateDisplay() != CoordinateDisplay.None) {
Point p = new Point(firstPoint); int range;
int t = 1; int yOffset;
while (p.y < secondPoint.y - spacing) { CoordinateType yType = getyType();
p.y += spacing; Point drawPoint = new Point(firstPoint);
int offset = (getyType() == CoordinateType.Numbers && t >= 10) || (getyType() == CoordinateType.Letters && t >= 27) ? 0 : 2;
drawCenteredText(g2d, getyType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x + offset, p.y, HorizontalAlignment.LEFT, if (origin == CoordinateOrigin.Top_Left || origin == CoordinateOrigin.Top_Right) {
VerticalAlignment.CENTER); range = (int) ((secondPoint.y - firstPoint.y + halfSpace) / spacing);
if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) { yOffset = spacing;
drawCenteredText(g2d, getyType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), secondPoint.x - offset, p.y, HorizontalAlignment.RIGHT, } else {
VerticalAlignment.CENTER); range = (int) ((secondPoint.y - firstPoint.y + halfSpace) / spacing);
} yOffset = -spacing;
t++; drawPoint.y = secondPoint.y;
}
for (int c = 1; c < range; c++) {
int xOffset = (yType == CoordinateType.Numbers && c >= 10) || (yType == CoordinateType.Letters && c >= 27) ? 0 : 2;
String label = yType == CoordinateType.Letters ? getCoordinateLabel(c) : Integer.toString(c);
drawPoint.y += yOffset;
drawCenteredText(g2d, label,
firstPoint.x + xOffset, drawPoint.y,
HorizontalAlignment.LEFT, VerticalAlignment.CENTER);
if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) {
drawCenteredText(g2d, label,
secondPoint.x - xOffset, drawPoint.y,
HorizontalAlignment.RIGHT, VerticalAlignment.CENTER);
}
} }
} }
if (getCoordinateDisplay() != CoordinateDisplay.None) { if (getCoordinateDisplay() != CoordinateDisplay.None) {
Point p = new Point(firstPoint); int range;
int t = 1; int xOffset;
while (p.x < secondPoint.x - spacing) { CoordinateType xType = getxType();
p.x += spacing; Point drawPoint = new Point(firstPoint);
drawCenteredText(g2d, getxType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x, p.y - 2, HorizontalAlignment.CENTER,
VerticalAlignment.TOP); if (origin == CoordinateOrigin.Top_Left || origin == CoordinateOrigin.Bottom_Left) {
range = (int) ((secondPoint.x - firstPoint.x + halfSpace) / spacing);
xOffset = spacing;
} else {
range = (int) ((secondPoint.x - firstPoint.x + halfSpace) / spacing);
xOffset = -spacing;
drawPoint.x = secondPoint.x;
}
for (int c = 1; c < range; c++) {
String label = xType == CoordinateType.Letters ? getCoordinateLabel(c) : Integer.toString(c);
drawPoint.x += xOffset;
drawCenteredText(g2d, label,
drawPoint.x, firstPoint.y - 2,
HorizontalAlignment.CENTER, VerticalAlignment.TOP);
if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) { if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) {
drawCenteredText(g2d, getxType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x, (int) (secondPoint.y - COORDINATE_FONT_SIZE), HorizontalAlignment.CENTER, drawCenteredText(g2d, label,
VerticalAlignment.BOTTOM); drawPoint.x, (int) (secondPoint.y - COORDINATE_FONT_SIZE),
HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
} }
t++;
} }
} }
} }
@@ -188,6 +223,17 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
this.coordinateDisplay = coordinateDisplay; this.coordinateDisplay = coordinateDisplay;
} }
@EditableProperty(name = "Coordinate Origin")
public CoordinateOrigin getCoordinateOrigin() {
if (coordinateOrigin == null)
coordinateOrigin = CoordinateOrigin.Top_Left;
return coordinateOrigin;
}
public void setCoordinateOrigin(CoordinateOrigin coordinateOrigin) {
this.coordinateOrigin = coordinateOrigin;
}
@EditableProperty(name = "Y") @EditableProperty(name = "Y")
public CoordinateType getyType() { public CoordinateType getyType() {
if (yType == null) if (yType == null)
@@ -251,4 +297,13 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
return super.toString().replace('_', ' '); return super.toString().replace('_', ' ');
}; };
} }
public static enum CoordinateOrigin {
Top_Left, Top_Right, Bottom_Right, Bottom_Left;
@Override
public String toString() {
return super.toString().replace('_', ' ');
};
}
} }