Compare commits
1 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
| c863b74d73 |
6
pom.xml
6
pom.xml
@@ -68,6 +68,12 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.18.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import ovh.gasser.newshapes.ui.listeners.MenuEditListener;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.InputEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
@@ -19,6 +21,8 @@ public class App {
|
|||||||
private SCollection model;
|
private SCollection model;
|
||||||
private JCheckBoxMenuItem editFill;
|
private JCheckBoxMenuItem editFill;
|
||||||
private JCheckBoxMenuItem editBorder;
|
private JCheckBoxMenuItem editBorder;
|
||||||
|
private JMenuItem editGroup;
|
||||||
|
private JMenuItem editUngroup;
|
||||||
|
|
||||||
private App() throws HeadlessException {
|
private App() throws HeadlessException {
|
||||||
final JFrame frame = new JFrame("Reactive shapes");
|
final JFrame frame = new JFrame("Reactive shapes");
|
||||||
@@ -71,12 +75,38 @@ public class App {
|
|||||||
|
|
||||||
private JMenu buildFileMenu(ShapesView sview) {
|
private JMenu buildFileMenu(ShapesView sview) {
|
||||||
JMenu menuFile = new JMenu("File");
|
JMenu menuFile = new JMenu("File");
|
||||||
|
JMenuItem openItem = new JMenuItem("Open");
|
||||||
|
JMenuItem saveItem = new JMenuItem("Save");
|
||||||
JMenuItem addRectItem = new JMenuItem("Add SRectangle");
|
JMenuItem addRectItem = new JMenuItem("Add SRectangle");
|
||||||
JMenuItem addCircleItem = new JMenuItem("Add SCircle");
|
JMenuItem addCircleItem = new JMenuItem("Add SCircle");
|
||||||
JMenuItem addTextItem = new JMenuItem("Add Text");
|
JMenuItem addTextItem = new JMenuItem("Add Text");
|
||||||
JMenuItem htmlExportItem = new JMenuItem("Export to HTML");
|
JMenuItem htmlExportItem = new JMenuItem("Export to HTML");
|
||||||
JMenuItem svgExportItem = new JMenuItem("Export to SVG");
|
JMenuItem svgExportItem = new JMenuItem("Export to SVG");
|
||||||
JMenuItem exitItem = new JMenuItem("Exit");
|
JMenuItem exitItem = new JMenuItem("Exit");
|
||||||
|
|
||||||
|
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
|
||||||
|
openItem.addActionListener(evt -> {
|
||||||
|
JFileChooser chooser = new JFileChooser();
|
||||||
|
chooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JSON Files", "json"));
|
||||||
|
if (chooser.showOpenDialog(sview) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
sview.getController().loadDrawing(chooser.getSelectedFile());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
saveItem.addActionListener(evt -> {
|
||||||
|
JFileChooser chooser = new JFileChooser();
|
||||||
|
chooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JSON Files", "json"));
|
||||||
|
if (chooser.showSaveDialog(sview) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
java.io.File file = chooser.getSelectedFile();
|
||||||
|
if (!file.getName().endsWith(".json")) {
|
||||||
|
file = new java.io.File(file.getAbsolutePath() + ".json");
|
||||||
|
}
|
||||||
|
sview.getController().saveDrawing(file);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
addRectItem.addActionListener(new MenuAddListener("SRectangle", model, sview));
|
addRectItem.addActionListener(new MenuAddListener("SRectangle", model, sview));
|
||||||
addCircleItem.addActionListener(new MenuAddListener("SCircle", model, sview));
|
addCircleItem.addActionListener(new MenuAddListener("SCircle", model, sview));
|
||||||
addTextItem.addActionListener(evt -> sview.getController().enterTextMode());
|
addTextItem.addActionListener(evt -> sview.getController().enterTextMode());
|
||||||
@@ -95,6 +125,10 @@ public class App {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
exitItem.addActionListener(evt -> System.exit(0));
|
exitItem.addActionListener(evt -> System.exit(0));
|
||||||
|
|
||||||
|
menuFile.add(openItem);
|
||||||
|
menuFile.add(saveItem);
|
||||||
|
menuFile.addSeparator();
|
||||||
menuFile.add(addRectItem);
|
menuFile.add(addRectItem);
|
||||||
menuFile.add(addCircleItem);
|
menuFile.add(addCircleItem);
|
||||||
menuFile.add(addTextItem);
|
menuFile.add(addTextItem);
|
||||||
@@ -108,32 +142,62 @@ public class App {
|
|||||||
private JMenu buildEditMenu(ShapesView sview) {
|
private JMenu buildEditMenu(ShapesView sview) {
|
||||||
MenuEditListener editListener = new MenuEditListener(model, sview, sview.getController());
|
MenuEditListener editListener = new MenuEditListener(model, sview, sview.getController());
|
||||||
JMenu menuEdit = new JMenu("Edit");
|
JMenu menuEdit = new JMenu("Edit");
|
||||||
|
JMenuItem cutItem = new JMenuItem("Cut");
|
||||||
|
JMenuItem copyItem = new JMenuItem("Copy");
|
||||||
|
JMenuItem pasteItem = new JMenuItem("Paste");
|
||||||
JMenuItem editColor = new JMenuItem("Change color");
|
JMenuItem editColor = new JMenuItem("Change color");
|
||||||
JMenuItem editBorderColor = new JMenuItem("Change border color");
|
JMenuItem editBorderColor = new JMenuItem("Change border color");
|
||||||
JMenuItem deleteItem = new JMenuItem("Delete");
|
JMenuItem deleteItem = new JMenuItem("Delete");
|
||||||
|
editGroup = new JMenuItem("Group");
|
||||||
|
editUngroup = new JMenuItem("Ungroup");
|
||||||
editFill = new JCheckBoxMenuItem("Fill Shape");
|
editFill = new JCheckBoxMenuItem("Fill Shape");
|
||||||
editBorder = new JCheckBoxMenuItem("Draw border");
|
editBorder = new JCheckBoxMenuItem("Draw border");
|
||||||
|
|
||||||
|
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
editGroup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK));
|
||||||
|
editUngroup.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
||||||
|
|
||||||
|
cutItem.addActionListener(evt -> sview.getController().cutSelection());
|
||||||
|
copyItem.addActionListener(evt -> sview.getController().copySelection());
|
||||||
|
pasteItem.addActionListener(evt -> sview.getController().pasteClipboard());
|
||||||
editColor.addActionListener(editListener);
|
editColor.addActionListener(editListener);
|
||||||
editBorderColor.addActionListener(editListener);
|
editBorderColor.addActionListener(editListener);
|
||||||
deleteItem.addActionListener(editListener);
|
deleteItem.addActionListener(editListener);
|
||||||
|
editGroup.addActionListener(evt -> sview.getController().group());
|
||||||
|
editUngroup.addActionListener(evt -> sview.getController().ungroup());
|
||||||
editFill.addActionListener(editListener);
|
editFill.addActionListener(editListener);
|
||||||
editBorder.addActionListener(editListener);
|
editBorder.addActionListener(editListener);
|
||||||
|
editGroup.setEnabled(false);
|
||||||
|
editUngroup.setEnabled(false);
|
||||||
|
|
||||||
|
menuEdit.add(cutItem);
|
||||||
|
menuEdit.add(copyItem);
|
||||||
|
menuEdit.add(pasteItem);
|
||||||
|
menuEdit.addSeparator();
|
||||||
menuEdit.add(editColor);
|
menuEdit.add(editColor);
|
||||||
menuEdit.add(editBorderColor);
|
menuEdit.add(editBorderColor);
|
||||||
menuEdit.add(deleteItem);
|
menuEdit.add(deleteItem);
|
||||||
menuEdit.addSeparator();
|
menuEdit.addSeparator();
|
||||||
|
menuEdit.add(editGroup);
|
||||||
|
menuEdit.add(editUngroup);
|
||||||
|
menuEdit.addSeparator();
|
||||||
menuEdit.add(editBorder);
|
menuEdit.add(editBorder);
|
||||||
menuEdit.add(editFill);
|
menuEdit.add(editFill);
|
||||||
return menuEdit;
|
return menuEdit;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMenuState(Iterable<Shape> selectedShapes) {
|
private void updateMenuState(Iterable<Shape> selectedShapes) {
|
||||||
|
int selectionCount = 0;
|
||||||
|
boolean singleCollectionSelected = false;
|
||||||
boolean hasToggleableShapes = false;
|
boolean hasToggleableShapes = false;
|
||||||
boolean allFilled = true;
|
boolean allFilled = true;
|
||||||
boolean allStroked = true;
|
boolean allStroked = true;
|
||||||
|
|
||||||
for (Shape s : selectedShapes) {
|
for (Shape s : selectedShapes) {
|
||||||
|
selectionCount++;
|
||||||
|
singleCollectionSelected = selectionCount == 1 && s instanceof SCollection;
|
||||||
if (s instanceof SText) {
|
if (s instanceof SText) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -145,6 +209,8 @@ public class App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editGroup.setEnabled(selectionCount > 1);
|
||||||
|
editUngroup.setEnabled(selectionCount == 1 && singleCollectionSelected);
|
||||||
updateMenuItem(editFill, hasToggleableShapes, allFilled);
|
updateMenuItem(editFill, hasToggleableShapes, allFilled);
|
||||||
updateMenuItem(editBorder, hasToggleableShapes, allStroked);
|
updateMenuItem(editBorder, hasToggleableShapes, allStroked);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package ovh.gasser.newshapes.persistence;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.Version;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
import ovh.gasser.newshapes.shapes.SCollection;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class DrawingSerializer {
|
||||||
|
private static final String VERSION = "1.0";
|
||||||
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
|
public DrawingSerializer() {
|
||||||
|
this.mapper = new ObjectMapper();
|
||||||
|
SimpleModule module = new SimpleModule("ShapeModule", new Version(1, 0, 0, null, null, null));
|
||||||
|
module.addSerializer(Shape.class, new ShapeSerializer());
|
||||||
|
module.addDeserializer(Shape.class, new ShapeDeserializer());
|
||||||
|
this.mapper.registerModule(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(SCollection model, File file) throws IOException {
|
||||||
|
DrawingData data = new DrawingData();
|
||||||
|
data.version = VERSION;
|
||||||
|
data.shapes = new java.util.ArrayList<>();
|
||||||
|
for (Shape shape : model) {
|
||||||
|
data.shapes.add(shape);
|
||||||
|
}
|
||||||
|
mapper.writerWithDefaultPrettyPrinter().writeValue(file, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SCollection load(File file) throws IOException {
|
||||||
|
DrawingData data = mapper.readValue(file, DrawingData.class);
|
||||||
|
return SCollection.of(data.shapes.toArray(new Shape[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DrawingData {
|
||||||
|
public String version;
|
||||||
|
public java.util.List<Shape> shapes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
package ovh.gasser.newshapes.persistence;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import ovh.gasser.newshapes.shapes.SCircle;
|
||||||
|
import ovh.gasser.newshapes.shapes.SCollection;
|
||||||
|
import ovh.gasser.newshapes.shapes.SPolygon;
|
||||||
|
import ovh.gasser.newshapes.shapes.SRectangle;
|
||||||
|
import ovh.gasser.newshapes.shapes.SText;
|
||||||
|
import ovh.gasser.newshapes.shapes.STriangle;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ShapeDeserializer extends StdDeserializer<Shape> {
|
||||||
|
|
||||||
|
public ShapeDeserializer() {
|
||||||
|
super(Shape.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Shape deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
JsonNode node = p.getCodec().readTree(p);
|
||||||
|
String type = node.get("type").asText();
|
||||||
|
|
||||||
|
return switch (type) {
|
||||||
|
case "rectangle" -> deserializeRectangle(node);
|
||||||
|
case "circle" -> deserializeCircle(node);
|
||||||
|
case "triangle" -> deserializeTriangle(node);
|
||||||
|
case "text" -> deserializeText(node);
|
||||||
|
case "polygon" -> deserializePolygon(node);
|
||||||
|
case "collection" -> deserializeCollection(node, p, ctxt);
|
||||||
|
default -> throw new IllegalArgumentException("Unknown shape type: " + type);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private SRectangle deserializeRectangle(JsonNode node) {
|
||||||
|
int x = node.get("x").asInt();
|
||||||
|
int y = node.get("y").asInt();
|
||||||
|
int width = node.get("width").asInt();
|
||||||
|
int height = node.get("height").asInt();
|
||||||
|
|
||||||
|
Color strokeColor = extractStrokeColor(node);
|
||||||
|
return SRectangle.create(x, y, width, height, strokeColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SCircle deserializeCircle(JsonNode node) {
|
||||||
|
int x = node.get("x").asInt();
|
||||||
|
int y = node.get("y").asInt();
|
||||||
|
int radius = node.get("radius").asInt();
|
||||||
|
|
||||||
|
Color strokeColor = extractStrokeColor(node);
|
||||||
|
Color fillColor = extractFillColor(node);
|
||||||
|
|
||||||
|
SCircle circle = SCircle.create(x + radius, y + radius, radius);
|
||||||
|
circle.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
||||||
|
fillColor != null, strokeColor != null, strokeColor, fillColor));
|
||||||
|
return circle;
|
||||||
|
}
|
||||||
|
|
||||||
|
private STriangle deserializeTriangle(JsonNode node) {
|
||||||
|
int x = node.get("x").asInt();
|
||||||
|
int y = node.get("y").asInt();
|
||||||
|
int size = node.get("size").asInt();
|
||||||
|
|
||||||
|
Color strokeColor = extractStrokeColor(node);
|
||||||
|
Color fillColor = extractFillColor(node);
|
||||||
|
|
||||||
|
return STriangle.create(x, y, size, fillColor != null ? fillColor : Color.YELLOW,
|
||||||
|
strokeColor != null ? strokeColor : Color.BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SText deserializeText(JsonNode node) {
|
||||||
|
int x = node.get("x").asInt();
|
||||||
|
int y = node.get("y").asInt();
|
||||||
|
String text = node.get("text").asText();
|
||||||
|
|
||||||
|
Color strokeColor = extractStrokeColor(node);
|
||||||
|
|
||||||
|
SText sText = SText.create(x, y, text);
|
||||||
|
if (strokeColor != null) {
|
||||||
|
sText.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
||||||
|
true, true, strokeColor, strokeColor));
|
||||||
|
}
|
||||||
|
return sText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SPolygon deserializePolygon(JsonNode node) {
|
||||||
|
List<Point> points = new ArrayList<>();
|
||||||
|
JsonNode pointsNode = node.get("points");
|
||||||
|
for (JsonNode pointNode : pointsNode) {
|
||||||
|
int x = pointNode.get("x").asInt();
|
||||||
|
int y = pointNode.get("y").asInt();
|
||||||
|
points.add(new Point(x, y));
|
||||||
|
}
|
||||||
|
|
||||||
|
Color strokeColor = extractStrokeColor(node);
|
||||||
|
Color fillColor = extractFillColor(node);
|
||||||
|
|
||||||
|
SPolygon poly = SPolygon.create(points);
|
||||||
|
if (strokeColor != null || fillColor != null) {
|
||||||
|
poly.addAttributes(new ovh.gasser.newshapes.attributes.ColorAttributes(
|
||||||
|
fillColor != null, strokeColor != null,
|
||||||
|
strokeColor != null ? strokeColor : Color.BLACK,
|
||||||
|
fillColor != null ? fillColor : Color.BLACK));
|
||||||
|
}
|
||||||
|
return poly;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SCollection deserializeCollection(JsonNode node, JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
List<Shape> children = new ArrayList<>();
|
||||||
|
JsonNode shapesNode = node.get("shapes");
|
||||||
|
if (shapesNode != null && shapesNode.isArray()) {
|
||||||
|
for (JsonNode childNode : shapesNode) {
|
||||||
|
JsonParser childParser = childNode.traverse(p.getCodec());
|
||||||
|
children.add(deserialize(childParser, ctxt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SCollection.of(children.toArray(new Shape[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color extractStrokeColor(JsonNode node) {
|
||||||
|
if (node.has("color")) {
|
||||||
|
JsonNode colorNode = node.get("color");
|
||||||
|
if (colorNode.has("stroked") && colorNode.get("stroked").asBoolean()) {
|
||||||
|
return hexToColor(colorNode.get("strokedColor").asText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color extractFillColor(JsonNode node) {
|
||||||
|
if (node.has("color")) {
|
||||||
|
JsonNode colorNode = node.get("color");
|
||||||
|
if (colorNode.has("filled") && colorNode.get("filled").asBoolean()) {
|
||||||
|
return hexToColor(colorNode.get("filledColor").asText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color hexToColor(String hex) {
|
||||||
|
if (hex == null || hex.isEmpty()) return Color.BLACK;
|
||||||
|
try {
|
||||||
|
return new Color(
|
||||||
|
Integer.parseInt(hex.substring(1, 3), 16),
|
||||||
|
Integer.parseInt(hex.substring(3, 5), 16),
|
||||||
|
Integer.parseInt(hex.substring(5, 7), 16)
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return Color.BLACK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package ovh.gasser.newshapes.persistence;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import ovh.gasser.newshapes.attributes.ColorAttributes;
|
||||||
|
import ovh.gasser.newshapes.shapes.SCircle;
|
||||||
|
import ovh.gasser.newshapes.shapes.SCollection;
|
||||||
|
import ovh.gasser.newshapes.shapes.SPolygon;
|
||||||
|
import ovh.gasser.newshapes.shapes.SRectangle;
|
||||||
|
import ovh.gasser.newshapes.shapes.SText;
|
||||||
|
import ovh.gasser.newshapes.shapes.STriangle;
|
||||||
|
import ovh.gasser.newshapes.shapes.Shape;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ShapeSerializer extends JsonSerializer<Shape> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Shape shape, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
|
gen.writeStartObject();
|
||||||
|
|
||||||
|
if (shape instanceof SRectangle rect) {
|
||||||
|
gen.writeStringField("type", "rectangle");
|
||||||
|
writeRectangle(rect, gen);
|
||||||
|
} else if (shape instanceof SCircle circle) {
|
||||||
|
gen.writeStringField("type", "circle");
|
||||||
|
writeCircle(circle, gen);
|
||||||
|
} else if (shape instanceof STriangle tri) {
|
||||||
|
gen.writeStringField("type", "triangle");
|
||||||
|
writeTriangle(tri, gen);
|
||||||
|
} else if (shape instanceof SText text) {
|
||||||
|
gen.writeStringField("type", "text");
|
||||||
|
writeText(text, gen);
|
||||||
|
} else if (shape instanceof SPolygon poly) {
|
||||||
|
gen.writeStringField("type", "polygon");
|
||||||
|
writePolygon(poly, gen);
|
||||||
|
} else if (shape instanceof SCollection coll) {
|
||||||
|
gen.writeStringField("type", "collection");
|
||||||
|
writeCollection(coll, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeRectangle(SRectangle rect, JsonGenerator gen) throws IOException {
|
||||||
|
Rectangle bounds = rect.getBounds();
|
||||||
|
gen.writeNumberField("x", bounds.x);
|
||||||
|
gen.writeNumberField("y", bounds.y);
|
||||||
|
gen.writeNumberField("width", bounds.width);
|
||||||
|
gen.writeNumberField("height", bounds.height);
|
||||||
|
writeColorAttributes(rect, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeCircle(SCircle circle, JsonGenerator gen) throws IOException {
|
||||||
|
Rectangle bounds = circle.getBounds();
|
||||||
|
gen.writeNumberField("x", bounds.x);
|
||||||
|
gen.writeNumberField("y", bounds.y);
|
||||||
|
gen.writeNumberField("radius", circle.getRadius());
|
||||||
|
writeColorAttributes(circle, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeTriangle(STriangle tri, JsonGenerator gen) throws IOException {
|
||||||
|
Rectangle bounds = tri.getBounds();
|
||||||
|
gen.writeNumberField("x", bounds.x);
|
||||||
|
gen.writeNumberField("y", bounds.y);
|
||||||
|
gen.writeNumberField("size", bounds.width);
|
||||||
|
writeColorAttributes(tri, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeText(SText text, JsonGenerator gen) throws IOException {
|
||||||
|
Rectangle bounds = text.getBounds();
|
||||||
|
gen.writeNumberField("x", bounds.x);
|
||||||
|
gen.writeNumberField("y", bounds.y);
|
||||||
|
gen.writeStringField("text", text.getText());
|
||||||
|
gen.writeStringField("fontName", text.getFontName());
|
||||||
|
gen.writeNumberField("fontSize", text.getFontSize());
|
||||||
|
gen.writeNumberField("fontStyle", text.getFontStyle());
|
||||||
|
writeColorAttributes(text, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writePolygon(SPolygon poly, JsonGenerator gen) throws IOException {
|
||||||
|
gen.writeArrayFieldStart("points");
|
||||||
|
for (Point p : poly.getPoints()) {
|
||||||
|
gen.writeStartObject();
|
||||||
|
gen.writeNumberField("x", p.x);
|
||||||
|
gen.writeNumberField("y", p.y);
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
gen.writeEndArray();
|
||||||
|
writeColorAttributes(poly, gen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeCollection(SCollection coll, JsonGenerator gen) throws IOException {
|
||||||
|
gen.writeArrayFieldStart("shapes");
|
||||||
|
for (Shape child : coll) {
|
||||||
|
serialize(child, gen, null);
|
||||||
|
}
|
||||||
|
gen.writeEndArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeColorAttributes(Shape shape, JsonGenerator gen) throws IOException {
|
||||||
|
ColorAttributes attrs = (ColorAttributes) shape.getAttributes(ColorAttributes.ID);
|
||||||
|
if (attrs != null) {
|
||||||
|
gen.writeObjectFieldStart("color");
|
||||||
|
gen.writeBooleanField("filled", attrs.filled);
|
||||||
|
gen.writeBooleanField("stroked", attrs.stroked);
|
||||||
|
gen.writeStringField("filledColor", colorToHex(attrs.filledColor));
|
||||||
|
gen.writeStringField("strokedColor", colorToHex(attrs.strokedColor));
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colorToHex(Color c) {
|
||||||
|
if (c == null) return "#000000";
|
||||||
|
return String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -117,30 +117,6 @@ public class ShapeDraftman implements ShapeVisitor {
|
|||||||
drawHandlerIfSelected(text);
|
drawHandlerIfSelected(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitPolygon(SPolygon polygon) {
|
|
||||||
ColorAttributes colAttrs = (ColorAttributes) polygon.getAttributes(ColorAttributes.ID);
|
|
||||||
if (colAttrs == null) {
|
|
||||||
colAttrs = DEFAULT_COLOR_ATTRIBUTES;
|
|
||||||
}
|
|
||||||
|
|
||||||
java.util.List<Point> points = polygon.getPoints();
|
|
||||||
int[] xPoints = points.stream().mapToInt(p -> p.x).toArray();
|
|
||||||
int[] yPoints = points.stream().mapToInt(p -> p.y).toArray();
|
|
||||||
int nPoints = points.size();
|
|
||||||
|
|
||||||
if (colAttrs.filled) {
|
|
||||||
this.g2d.setColor(colAttrs.filledColor);
|
|
||||||
this.g2d.fillPolygon(xPoints, yPoints, nPoints);
|
|
||||||
}
|
|
||||||
if (colAttrs.stroked) {
|
|
||||||
this.g2d.setColor(colAttrs.strokedColor);
|
|
||||||
this.g2d.drawPolygon(xPoints, yPoints, nPoints);
|
|
||||||
}
|
|
||||||
|
|
||||||
drawHandlerIfSelected(polygon);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color resolveTextColor(ColorAttributes attrs) {
|
private Color resolveTextColor(ColorAttributes attrs) {
|
||||||
if (attrs == null) {
|
if (attrs == null) {
|
||||||
return Color.BLACK;
|
return Color.BLACK;
|
||||||
|
|||||||
@@ -16,12 +16,8 @@ public class ShapesView extends JPanel {
|
|||||||
private Rectangle currentSelectionBox;
|
private Rectangle currentSelectionBox;
|
||||||
|
|
||||||
public ShapesView(SCollection model) {
|
public ShapesView(SCollection model) {
|
||||||
this(model, () -> { });
|
|
||||||
}
|
|
||||||
|
|
||||||
public ShapesView(SCollection model, Runnable onModelChanged) {
|
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.controller = new Controller(this, model, onModelChanged);
|
this.controller = new Controller(this, model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,128 +0,0 @@
|
|||||||
package ovh.gasser.newshapes.ui;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
|
||||||
import ovh.gasser.newshapes.shapes.SCollection;
|
|
||||||
import ovh.gasser.newshapes.shapes.SRectangle;
|
|
||||||
import ovh.gasser.newshapes.shapes.Shape;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.InputEvent;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
class BoxSelectionTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void draggingSelectionBoxSelectsAllIntersectingShapes() throws Exception {
|
|
||||||
SRectangle first = SRectangle.create(20, 20, 30, 30);
|
|
||||||
SRectangle second = SRectangle.create(80, 80, 30, 30);
|
|
||||||
SRectangle outside = SRectangle.create(160, 160, 30, 30);
|
|
||||||
ShapesView view = createView(first, second, outside);
|
|
||||||
|
|
||||||
dragSelection(view, new Point(5, 5), new Point(100, 100), 0);
|
|
||||||
|
|
||||||
assertTrue(isSelected(first));
|
|
||||||
assertTrue(isSelected(second));
|
|
||||||
assertFalse(isSelected(outside));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shiftDraggingSelectionBoxAddsToExistingSelection() throws Exception {
|
|
||||||
SRectangle first = SRectangle.create(10, 10, 30, 30);
|
|
||||||
SRectangle second = SRectangle.create(80, 10, 30, 30);
|
|
||||||
SRectangle outside = SRectangle.create(160, 10, 30, 30);
|
|
||||||
ShapesView view = createView(first, second, outside);
|
|
||||||
|
|
||||||
click(view, new Point(20, 20), 0);
|
|
||||||
dragSelection(view, new Point(70, 5), new Point(120, 50), InputEvent.SHIFT_DOWN_MASK);
|
|
||||||
|
|
||||||
assertTrue(isSelected(first));
|
|
||||||
assertTrue(isSelected(second));
|
|
||||||
assertFalse(isSelected(outside));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shapesViewPaintsSelectionBoxDuringRendering() throws Exception {
|
|
||||||
ShapesView view = createView();
|
|
||||||
|
|
||||||
BufferedImage withoutSelectionBox = paintView(view, null);
|
|
||||||
BufferedImage withSelectionBox = paintView(view, new Rectangle(10, 10, 40, 40));
|
|
||||||
|
|
||||||
assertTrue(imagesDiffer(withoutSelectionBox, withSelectionBox));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ShapesView createView(Shape... shapes) throws InvocationTargetException, InterruptedException {
|
|
||||||
final ShapesView[] ref = new ShapesView[1];
|
|
||||||
SwingUtilities.invokeAndWait(() -> {
|
|
||||||
ref[0] = new ShapesView(SCollection.of(shapes));
|
|
||||||
ref[0].setOpaque(true);
|
|
||||||
ref[0].setBackground(Color.WHITE);
|
|
||||||
ref[0].setSize(240, 240);
|
|
||||||
});
|
|
||||||
return ref[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private void click(ShapesView view, Point point, int modifiers) throws InvocationTargetException, InterruptedException {
|
|
||||||
dispatch(view, MouseEvent.MOUSE_PRESSED, point, modifiers);
|
|
||||||
dispatch(view, MouseEvent.MOUSE_RELEASED, point, modifiers);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dragSelection(ShapesView view, Point start, Point end, int modifiers) throws InvocationTargetException, InterruptedException {
|
|
||||||
dispatch(view, MouseEvent.MOUSE_PRESSED, start, modifiers);
|
|
||||||
dispatch(view, MouseEvent.MOUSE_DRAGGED, end, modifiers);
|
|
||||||
dispatch(view, MouseEvent.MOUSE_RELEASED, end, modifiers);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dispatch(ShapesView view, int eventId, Point point, int modifiers)
|
|
||||||
throws InvocationTargetException, InterruptedException {
|
|
||||||
SwingUtilities.invokeAndWait(() -> view.dispatchEvent(new MouseEvent(
|
|
||||||
view,
|
|
||||||
eventId,
|
|
||||||
System.currentTimeMillis(),
|
|
||||||
modifiers,
|
|
||||||
point.x,
|
|
||||||
point.y,
|
|
||||||
1,
|
|
||||||
false,
|
|
||||||
MouseEvent.BUTTON1
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private BufferedImage paintView(ShapesView view, Rectangle box)
|
|
||||||
throws InvocationTargetException, InterruptedException {
|
|
||||||
final BufferedImage[] ref = new BufferedImage[1];
|
|
||||||
SwingUtilities.invokeAndWait(() -> {
|
|
||||||
view.setCurrentSelectionBox(box);
|
|
||||||
BufferedImage image = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);
|
|
||||||
Graphics2D graphics = image.createGraphics();
|
|
||||||
try {
|
|
||||||
view.paint(graphics);
|
|
||||||
} finally {
|
|
||||||
graphics.dispose();
|
|
||||||
}
|
|
||||||
ref[0] = image;
|
|
||||||
});
|
|
||||||
return ref[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean imagesDiffer(BufferedImage first, BufferedImage second) {
|
|
||||||
for (int y = 0; y < first.getHeight(); y++) {
|
|
||||||
for (int x = 0; x < first.getWidth(); x++) {
|
|
||||||
if (first.getRGB(x, y) != second.getRGB(x, y)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isSelected(Shape shape) {
|
|
||||||
return ((SelectionAttributes) shape.getAttributes(SelectionAttributes.ID)).selected;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user