Adding ability to attach labels to a shape. #16.

This commit is contained in:
Rob Greene 2018-06-24 15:29:31 -05:00
parent 89262427bb
commit f77a945c7e
7 changed files with 42 additions and 18 deletions

View File

@ -13,15 +13,23 @@ import java.util.function.Supplier;
* easier to understand than vectors.
*/
public class BitmapShape implements Shape {
public final String label;
public final List<List<Boolean>> grid = new ArrayList<>();
public final Point origin = new Point();
public BitmapShape() {
this(0,0);
this(0, 0, null);
}
public BitmapShape(String label) {
this(0, 0, label);
}
public BitmapShape(int height, int width) {
while (grid.size() < height) {
grid.add(newRow(width));
this(height, width, null);
}
public BitmapShape(int height, int width, String label) {
this.label = label;
while (this.grid.size() < height) {
this.grid.add(newRow(width));
}
}

View File

@ -24,19 +24,23 @@ public class ShapeGenerator {
if (comment > -1) line = line.substring(0, comment);
line = line.trim();
switch (line.toLowerCase()) {
String[] parts = line.split("\\s+");
String command = parts[0];
String label = parts.length > 1 ? parts[1] : null;
switch (command.toLowerCase()) {
case ".short":
VectorShape shortShape = new VectorShape();
VectorShape shortShape = new VectorShape(label);
st.shapes.add(shortShape);
shapeConsumer = shortShape::appendShortCommands;
break;
case ".long":
VectorShape longShape = new VectorShape();
VectorShape longShape = new VectorShape(label);
st.shapes.add(longShape);
shapeConsumer = longShape::appendLongCommands;
break;
case ".bitmap":
BitmapShape bitmapShape = new BitmapShape();
BitmapShape bitmapShape = new BitmapShape(label);
st.shapes.add(bitmapShape);
shapeConsumer = bitmapShape::appendBitmapRow;
break;

View File

@ -40,8 +40,16 @@ public class VectorShape implements Shape {
return shape;
}
public final String label;
public final List<VectorCommand> vectors = new ArrayList<>();
public VectorShape() {
this.label = null;
}
public VectorShape(String label) {
this.label = label;
}
public VectorShape moveUp() { return append(VectorCommand.MOVE_UP); }
public VectorShape moveRight() { return append(VectorCommand.MOVE_RIGHT); }
public VectorShape moveDown() { return append(VectorCommand.MOVE_DOWN); }

View File

@ -1,6 +1,6 @@
package io.github.applecommander.bastools.api.shapes;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
@ -13,14 +13,14 @@ public class ShapeGeneratorTest {
public void generateBoxShortformTest() throws IOException {
ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-shortform.st"));
assertShapeIsBox(st);
assertShapeBoxVectors(st);
assertShapeBoxVectors(st, "label-short");
}
@Test
public void generateBoxLongformTest() throws IOException {
ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-longform.st"));
assertShapeIsBox(st);
assertShapeBoxVectors(st);
assertShapeBoxVectors(st, "label-long");
}
@Test
@ -28,6 +28,7 @@ public class ShapeGeneratorTest {
ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-bitmap.st"));
assertShapeIsBox(st);
// Unable to test vectors for bitmaps
assertEquals("label-bitmap", st.shapes.get(0).toBitmap().label);
}
public void assertShapeIsBox(ShapeTable st) throws IOException {
@ -45,11 +46,11 @@ public class ShapeGeneratorTest {
assertShapeMatches(expected, st.shapes.get(0));
}
public void assertShapeBoxVectors(ShapeTable st) {
public void assertShapeBoxVectors(ShapeTable st, String label) {
assertNotNull(st);
assertEquals(1, st.shapes.size());
VectorShape expected = new VectorShape()
VectorShape expected = new VectorShape(label)
.moveDown().moveDown()
.plotLeft().plotLeft()
.moveUp().plotUp().plotUp().plotUp()
@ -57,9 +58,12 @@ public class ShapeGeneratorTest {
.moveDown().plotDown().plotDown().plotDown()
.moveLeft().plotLeft();
Shape s = st.shapes.get(0);
assertNotNull(s);
assertEquals(expected.vectors, s.toVector().vectors);
Shape shape = st.shapes.get(0);
assertNotNull(shape);
assertTrue(shape instanceof VectorShape);
VectorShape vshape = shape.toVector();
assertEquals(expected.vectors, vshape.vectors);
assertEquals(expected.label, vshape.label);
}
@Test

View File

@ -8,7 +8,7 @@
; * = origin. plot
; whitespace is ignored
.bitmap
.bitmap label-bitmap
.xxx.
x...x
x.+.x

View File

@ -6,7 +6,7 @@
; whitespace is ignored
; case insensitive
.long
.long label-long
movedown 2
plotleft 2
moveup

View File

@ -6,7 +6,7 @@
; whitespace is ignored
; case sensitive
.short
.short label-short
dd
LL
uUUU