- AbstractShape: make bounds protected for subclass access - STriangle: refactor resize logic - SCircle: refactor resize logic (preserves equal dimensions) - SText: add reasonable default bounds for hit testing
83 lines
2.6 KiB
Java
83 lines
2.6 KiB
Java
package ovh.gasser.newshapes.shapes;
|
|
|
|
import ovh.gasser.newshapes.ShapeVisitor;
|
|
import ovh.gasser.newshapes.attributes.ColorAttributes;
|
|
import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
|
|
|
import java.awt.*;
|
|
|
|
public class STriangle extends AbstractShape {
|
|
private STriangle(int x, int y, int size){
|
|
super(new Rectangle(x, y, size, size));
|
|
}
|
|
|
|
@Override
|
|
public void accept(ShapeVisitor visitor) {
|
|
visitor.visitTriangle(this);
|
|
}
|
|
|
|
@Override
|
|
public void resize(ResizeHandle handle, int dx, int dy) {
|
|
int delta = Math.max(Math.abs(dx), Math.abs(dy));
|
|
|
|
boolean shrink = switch (handle) {
|
|
case E -> dx < 0;
|
|
case W -> dx > 0;
|
|
case N -> dy > 0;
|
|
case S -> dy < 0;
|
|
case SE -> (dx < 0 || dy < 0);
|
|
case NW -> (dx > 0 || dy > 0);
|
|
case NE -> (dx < 0 || dy > 0);
|
|
case SW -> (dx > 0 || dy < 0);
|
|
default -> false;
|
|
};
|
|
|
|
int sizeChange = shrink ? -delta : delta;
|
|
|
|
switch (handle) {
|
|
case SE, E, W -> {
|
|
bounds.width += sizeChange;
|
|
bounds.height += sizeChange;
|
|
}
|
|
case NW -> {
|
|
bounds.x -= sizeChange;
|
|
bounds.width += sizeChange;
|
|
bounds.y -= sizeChange;
|
|
bounds.height += sizeChange;
|
|
}
|
|
case NE -> {
|
|
bounds.y -= sizeChange;
|
|
bounds.width += sizeChange;
|
|
bounds.height += sizeChange;
|
|
}
|
|
case SW -> {
|
|
bounds.x -= sizeChange;
|
|
bounds.width += sizeChange;
|
|
bounds.height += sizeChange;
|
|
}
|
|
case N, S -> {
|
|
bounds.width += sizeChange;
|
|
bounds.height += sizeChange;
|
|
}
|
|
}
|
|
|
|
if (bounds.width < 1) bounds.width = 1;
|
|
if (bounds.height < 1) bounds.height = 1;
|
|
}
|
|
|
|
@Override
|
|
public Shape clone() {
|
|
var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
|
|
Color strokeColor = color != null ? color.strokedColor : Color.BLACK;
|
|
Color fillColor = color != null ? color.filledColor : Color.BLACK;
|
|
return STriangle.create(super.getBounds().x, super.getBounds().y, super.getBounds().height, fillColor, strokeColor);
|
|
}
|
|
|
|
public static STriangle create(int x, int y, int size, Color filledColor, Color strokedColor) {
|
|
var tri = new STriangle(x, y, size);
|
|
tri.addAttributes(new SelectionAttributes());
|
|
tri.addAttributes(new ColorAttributes(true, true, filledColor, strokedColor));
|
|
return tri;
|
|
}
|
|
}
|