Files
new-shapes/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java
Thibaud 3a6f98455a fix: fix resize and dragging
- 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
2026-03-26 23:55:38 +01:00

88 lines
2.2 KiB
Java

package ovh.gasser.newshapes.shapes;
import ovh.gasser.newshapes.attributes.Attributes;
import java.awt.*;
import java.util.Map;
import java.util.TreeMap;
public abstract class AbstractShape implements Shape {
private final Map<String, Attributes> attributes = new TreeMap<>();
protected final Rectangle bounds;
AbstractShape() {
this(null);
}
AbstractShape(Rectangle bounds) {
this.bounds = bounds;
}
@Override
public Attributes getAttributes(String key) {
return attributes.get(key);
}
@Override
public void addAttributes(Attributes attrs) {
attributes.put(attrs.getID(), attrs);
}
@Override
public void translate(int dx, int dy) {
this.bounds.translate(dx, dy);
}
@Override
public void resize(ResizeHandle handle, int dx, int dy) {
switch (handle) {
case E -> this.bounds.width += dx;
case W -> {
this.bounds.x += dx;
this.bounds.width -= dx;
}
case S -> this.bounds.height += dy;
case N -> {
this.bounds.y += dy;
this.bounds.height -= dy;
}
case SE -> {
this.bounds.width += dx;
this.bounds.height += dy;
}
case SW -> {
this.bounds.x += dx;
this.bounds.width -= dx;
this.bounds.height += dy;
}
case NE -> {
this.bounds.width += dx;
this.bounds.y += dy;
this.bounds.height -= dy;
}
case NW -> {
this.bounds.x += dx;
this.bounds.width -= dx;
this.bounds.y += dy;
this.bounds.height -= dy;
}
}
if (this.bounds.width < 1) this.bounds.width = 1;
if (this.bounds.height < 1) this.bounds.height = 1;
}
@Override
public Rectangle getBounds() {
return new Rectangle(this.bounds);
}
@Override
public String toString() {
return String.format("x=%d, y=%d, width=%d, height=%d", bounds.x, bounds.y, bounds.width, bounds.height);
}
@Override
public abstract Shape clone();
}