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
This commit is contained in:
2026-03-26 23:54:31 +01:00
parent fbe3714182
commit 3a6f98455a
4 changed files with 47 additions and 50 deletions

View File

@@ -9,7 +9,7 @@ import java.util.TreeMap;
public abstract class AbstractShape implements Shape {
private final Map<String, Attributes> attributes = new TreeMap<>();
private final Rectangle bounds;
protected final Rectangle bounds;
AbstractShape() {
this(null);
@@ -31,46 +31,45 @@ public abstract class AbstractShape implements Shape {
@Override
public void translate(int dx, int dy) {
getBounds().translate(dx, dy);
this.bounds.translate(dx, dy);
}
@Override
public void resize(ResizeHandle handle, int dx, int dy) {
Rectangle bounds = getBounds();
switch (handle) {
case E -> bounds.width += dx;
case E -> this.bounds.width += dx;
case W -> {
bounds.x += dx;
bounds.width -= dx;
this.bounds.x += dx;
this.bounds.width -= dx;
}
case S -> bounds.height += dy;
case S -> this.bounds.height += dy;
case N -> {
bounds.y += dy;
bounds.height -= dy;
this.bounds.y += dy;
this.bounds.height -= dy;
}
case SE -> {
bounds.width += dx;
bounds.height += dy;
this.bounds.width += dx;
this.bounds.height += dy;
}
case SW -> {
bounds.x += dx;
bounds.width -= dx;
bounds.height += dy;
this.bounds.x += dx;
this.bounds.width -= dx;
this.bounds.height += dy;
}
case NE -> {
bounds.width += dx;
bounds.y += dy;
bounds.height -= dy;
this.bounds.width += dx;
this.bounds.y += dy;
this.bounds.height -= dy;
}
case NW -> {
bounds.x += dx;
bounds.width -= dx;
bounds.y += dy;
bounds.height -= dy;
this.bounds.x += dx;
this.bounds.width -= dx;
this.bounds.y += dy;
this.bounds.height -= dy;
}
}
if (bounds.width < 1) bounds.width = 1;
if (bounds.height < 1) bounds.height = 1;
if (this.bounds.width < 1) this.bounds.width = 1;
if (this.bounds.height < 1) this.bounds.height = 1;
}
@Override