java-app-qs/src/main/java/de/synolo/app/qs/editor/EditorCanvas.java

76 lines
2.7 KiB
Java

package de.synolo.app.qs.editor;
import de.synolo.lib.fw.utils.MultipleSelectionModelWrapper;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
public class EditorCanvas extends Pane {
private Canvas canvas;
private EditorControl control;
private ObservableList<EditorLayer> layers = FXCollections.observableArrayList();
private ReadOnlyObjectWrapper<EditorState> stateProperty = new ReadOnlyObjectWrapper<EditorState>(EditorState.INSERTING);
private MultipleSelectionModelWrapper<EditorLayer> layerSelectionModel = new MultipleSelectionModelWrapper<>(this.layers);
public EditorCanvas() {
this.canvas = new Canvas();
this.canvas.widthProperty().bind(widthProperty());
this.canvas.heightProperty().bind(heightProperty());
this.canvas.widthProperty().addListener((v, ov, n) -> paint());
this.canvas.heightProperty().addListener((v, ov, n) -> paint());
this.control = new EditorControl(this);
getChildren().add(this.canvas);
this.layers.addListener((Change<? extends EditorLayer> c) -> paint());
this.stateProperty.addListener((v, ov, nv) -> System.out.println(this.stateProperty.get()));
}
public EditorControl getControl() { return this.control; }
public ObservableList<EditorLayer> getLayers() { return this.layers; }
public MultipleSelectionModel<EditorLayer> getLayerSelectionModel() { return this.layerSelectionModel; }
public ReadOnlyObjectProperty<EditorState> stateProperty() { return this.stateProperty.getReadOnlyProperty(); }
public EditorState getState() { return this.stateProperty.get(); }
void setState(EditorState state) { this.stateProperty.set(state); }
public void paint() {
GraphicsContext gc = this.canvas.getGraphicsContext2D();
double width = this.canvas.getWidth(),
height = this.canvas.getHeight();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, width, height);
gc.save();
gc.scale(this.control.getZoom(), this.control.getZoom());
gc.translate(-this.control.getOffsetX(), -this.control.getOffsetY());
for(int i = this.layers.size() - 1; i >= 0; i--) {
this.layers.get(i).paint(gc);
}
gc.restore();
System.out.println("paint - " + this.stateProperty.get());
}
}