Drawing API cleanup

This commit is contained in:
Patrick Haßel 2023-02-27 11:20:47 +01:00
parent cae3f0b2cf
commit 7cc298638c
5 changed files with 42 additions and 40 deletions

View File

@ -8,7 +8,7 @@ import lombok.Getter;
@Getter
public class Environment {
private final Publisher<Drawing> onNewDrawing = new Publisher<>();
private final Publisher<Drawing> onDrawingChange = new Publisher<>();
private final Tools tools = new Tools();
@ -19,8 +19,8 @@ public class Environment {
}
public void newDrawing() {
drawing = new Drawing(1920, 1080);
onNewDrawing.publish(drawing);
drawing = new Drawing(1920, 1080, onDrawingChange::publish);
onDrawingChange.publish(drawing);
}
}

View File

@ -1,5 +1,6 @@
package de.ph87.kindermalen.drawing;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@ -10,6 +11,7 @@ import java.io.File;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Consumer;
@Slf4j
@ToString
@ -19,41 +21,37 @@ public class Drawing {
public final int height;
private final Consumer<Drawing> onChange;
public final ZonedDateTime created = ZonedDateTime.now();
@Getter
@ToString.Exclude
private final Layer current;
private final Layer layer;
public Drawing(final int width, final int height) {
public Drawing(final int width, final int height, final Consumer<Drawing> onChange) {
this.width = width;
this.height = height;
current = new Layer(width, height);
this.onChange = onChange;
layer = new Layer(width, height, l -> onChange.accept(this));
log.info("New Drawing: {}", this);
}
public Graphics2D getGraphics() {
return (Graphics2D) current.getCurrent().getGraphics();
}
public void publish() {
current.publish();
}
public void undo() {
current.undo();
layer.undo();
}
public void redo() {
current.redo();
layer.redo();
}
public void newRevision() {
current.newRevision();
layer.newRevision();
}
public void save(final File dir) throws IOException {
final int revision = current.getRevision();
final BufferedImage image = current.getCurrent();
final int revision = layer.getRevision();
final BufferedImage image = layer.getBuffer();
final File subdir = new File(dir, created.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
final File file = new File(subdir, "%05d".formatted(revision) + ".png");
if (subdir.mkdirs()) {
@ -69,7 +67,7 @@ public class Drawing {
}
public Image getImage() {
return current.getCurrent();
return layer.getBuffer();
}
}

View File

@ -37,7 +37,7 @@ public class DrawingPanel extends MyComponent {
public DrawingPanel(final Environment environment) {
this.environment = environment;
subscribe(environment.getTools().getOnSelect(), this::onToolSelect);
subscribe(environment.getOnNewDrawing(), drawing -> repaint());
subscribe(environment.getOnDrawingChange(), drawing -> repaint());
}
private void onToolSelect(final Tool tool) {

View File

@ -1,12 +1,13 @@
package de.ph87.kindermalen.drawing;
import de.ph87.kindermalen.util.Publisher;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import static de.ph87.kindermalen.util.ImageHelper.COPY;
@ -17,25 +18,25 @@ public class Layer {
private final int height;
@Getter
private BufferedImage current;
private final Consumer<Layer> onChange;
private List<BufferedImage> history = new ArrayList<>();
@Getter
private BufferedImage buffer;
@Getter
private int index = 0;
@Getter
private int revision = 0;
@Getter
private final Publisher<BufferedImage> onChange = new Publisher<>();
public Layer(final int width, final int height) {
public Layer(final int width, final int height, final Consumer<Layer> onChange) {
this.width = width;
this.height = height;
this.current = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
this.history.add(current);
this.onChange = onChange;
this.buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
this.history.add(buffer);
}
public void newRevision() {
@ -45,19 +46,19 @@ public class Layer {
history = history.subList(0, keep);
log.info("{} Revisions deleted", old - keep);
}
current = COPY(current);
buffer = COPY(buffer);
index = history.size();
revision++;
history.add(current);
history.add(buffer);
log.debug("Revision {} created", index);
}
public void undo() {
if (index > 0) {
index--;
current = history.get(index);
buffer = history.get(index);
log.info("UNDO: Revision {} loaded", index);
publish();
onChange.accept(this);
} else {
log.warn("No UNDO steps left.");
}
@ -66,9 +67,9 @@ public class Layer {
public void redo() {
if (canRedo()) {
index++;
current = history.get(index);
buffer = history.get(index);
log.info("REDO: Revision {} loaded", index);
publish();
onChange.accept(this);
} else {
log.warn("No REDO steps left.");
}
@ -78,8 +79,10 @@ public class Layer {
return index < history.size() - 1;
}
public void publish() {
onChange.publish(current);
public void draw(final BufferedImage image, final int x, final int y) {
final Graphics2D g = (Graphics2D) buffer.getGraphics();
g.drawImage(image, x, y, null);
onChange.accept(this);
}
}

View File

@ -96,8 +96,9 @@ public class StampTool extends Tool {
}
private void apply(final Drawing drawing, final Vector point) {
drawing.getGraphics().drawImage(prepared, point.intX() - prepared.getWidth() / 2, point.intY() - prepared.getHeight() / 2, null);
drawing.publish();
final int x = point.intX() - prepared.getWidth() / 2;
final int y = point.intY() - prepared.getHeight() / 2;
drawing.getLayer().draw(prepared, x, y);
}
@Override