Skip to content

Commit

Permalink
erase incomplete drawing if mode is changed during drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 13, 2024
1 parent 6e9c309 commit b0935b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.event.MouseWheelEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
Expand All @@ -15,7 +17,7 @@
import com.github.creme332.model.ShapeWrapper;
import com.github.creme332.view.Canvas;

public class CanvasController {
public class CanvasController implements PropertyChangeListener {
private Canvas canvas;

/**
Expand All @@ -35,6 +37,8 @@ public CanvasController(AppState app, Canvas canvas) {
this.canvas = canvas;
this.model = app.getCanvasModel();

app.addPropertyChangeListener(this);

canvas.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Expand Down Expand Up @@ -175,4 +179,16 @@ private void resetCanvasView() {
model.setCellSize(CanvasModel.DEFAULT_CELL_SIZE);
canvas.repaint();
}

@Override
public void propertyChange(PropertyChangeEvent e) {
final String propertyName = e.getPropertyName();
// if mode has changed while a shape is being drawn
if ("mode".equals(propertyName) && currentWrapper != null) {
// erase incomplete shape
model.getShapes().remove(currentWrapper);
currentWrapper = null;
canvas.repaint();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/github/creme332/model/AppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public CanvasModel getCanvasModel() {

public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener("sidebarVisibility", listener);
support.addPropertyChangeListener("mode", listener);
}

public boolean getSideBarVisibility() {
Expand Down

0 comments on commit b0935b5

Please sign in to comment.