Added Subject3D selection to View3D

- Changed how View3D is added to canvasBase to allow events to get through

#story[1190]
This commit is contained in:
Connor Taylor-Brown
2017-09-08 12:43:36 +12:00
parent 54baf4f884
commit b59eca1a8b
3 changed files with 30 additions and 16 deletions
@@ -33,9 +33,6 @@ public class Subject3D {
this.heading = new Rotate(0, Rotate.Y_AXIS);
this.mesh.getTransforms().addAll(position, heading, new Rotate(90, Rotate.X_AXIS), new Rotate(180, Rotate.Y_AXIS));
this.position.xProperty().addListener(((observable, oldValue, newValue) -> System.out.println("Boat x: " + newValue)));
this.position.yProperty().addListener(((observable, oldValue, newValue) -> System.out.println("Boat y: " + newValue)));
this.position.zProperty().addListener(((observable, oldValue, newValue) -> System.out.println("Boat z: " + newValue)));
}
public Shape3D getMesh() {
@@ -6,12 +6,17 @@ import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.SubScene;
import javafx.scene.input.PickResult;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Shape;
import javafx.scene.shape.Shape3D;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import java.util.HashMap;
import java.util.Map;
/**
* Control for rendering 3D objects visible through a PerspectiveCamera. Implements Adapter Pattern to
* interface with camera, and allows clients to add shapes to the scene. All scenes contain sea plane and
@@ -22,6 +27,10 @@ public class View3D extends Pane {
* Observable list of renderable items
*/
private ObservableList<Subject3D> items;
/**
* Map for selecting Subject3D from Shape3D
*/
private Map<Shape3D, Subject3D> selectionMap;
/**
* Rendering container for shapes
*/
@@ -56,6 +65,7 @@ public class View3D extends Pane {
*/
public View3D() {
world = new Group();
selectionMap = new HashMap<>();
SubScene scene = new SubScene(world, 300, 300);
scene.widthProperty().bind(this.widthProperty());
@@ -64,6 +74,13 @@ public class View3D extends Pane {
scene.setCamera(buildCamera());
scene.setOnMousePressed(e -> {
PickResult result = e.getPickResult();
if(result != null && result.getIntersectedNode() != null && result.getIntersectedNode() instanceof Shape3D) {
System.out.println(selectionMap.get(result.getIntersectedNode()));
}
});
this.getChildren().add(scene);
}
@@ -95,8 +112,14 @@ public class View3D extends Pane {
this.items.addListener((ListChangeListener<? super Subject3D>) c -> {
while(c.next()) {
if (c.wasRemoved() || c.wasAdded()) {
for (Subject3D shape : c.getRemoved()) world.getChildren().remove(shape.getMesh());
for (Subject3D shape : c.getAddedSubList()) world.getChildren().add(shape.getMesh());
for (Subject3D shape : c.getRemoved()) {
world.getChildren().remove(shape.getMesh());
selectionMap.remove(shape.getMesh());
}
for (Subject3D shape : c.getAddedSubList()) {
world.getChildren().add(shape.getMesh());
selectionMap.put(shape.getMesh(), shape);
}
}
}
});