Skip to content
Snippets Groups Projects
Commit 1b5f93f4 authored by dages's avatar dages
Browse files

axis builder : doc

parent 86444383
No related branches found
No related tags found
No related merge requests found
package fr.ill.ics.gui;
import fr.ill.ics.model.Axis;
import fr.ill.ics.model.Component;
import javafx.geometry.Point2D;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.transform.NonInvertibleTransformException;
import javafx.scene.transform.Rotate;
/**
* Class for creating and placing axes.
* @author dages
*/
public class AxisBuilder {
public static final Color buildingColor = Color.color(0, 1, 0.8);
private Axis axis;
private Group parentGroup;
private Point2D mousePos = null;
private Point2D mouseOldPos = null;
/**
* Default constructor.
*/
public AxisBuilder() {
this.axis = null;
this.parentGroup = null;
}
public void start(Axis axis, Group parentGroup, CameraControls controls, boolean modified) {
this.axis = axis;
this.axis.setDirection(new Point3D(0, 1, 0));
this.axis.setPosition(controls.getCenter());
try {
this.axis.setSceneToParent(parentGroup.getLocalToSceneTransform().createInverse());
} catch (NonInvertibleTransformException e) {
e.printStackTrace();
}
this.axis.getVisualGroup().setVisible(true);
this.parentGroup = parentGroup;
if (!modified) {
parentGroup.getChildren().add(this.axis.getVisualGroup());
}
this.axis.getVisualGroup().setOnMousePressed(me -> {
mousePos = new Point2D(me.getSceneX(), me.getSceneY());
mouseOldPos = new Point2D(me.getSceneX(), me.getSceneY());
});
this.axis.getVisualGroup().setOnMouseDragged(me -> {
mouseOldPos = mousePos;
mousePos = new Point2D(me.getSceneX(), me.getSceneY());
Point2D delta = mousePos.subtract(mouseOldPos);
Point3D screenAxisDir3D = controls.getTransform().deltaTransform(axis.getDirection());
Point2D screenAxisDir = new Point2D(screenAxisDir3D.getX(), screenAxisDir3D.getY());
Point3D viewDir = controls.viewDirection();
double angle = screenAxisDir.angle(delta);
while (angle < 0) angle += 360;
while (angle > 180) angle -= 180;
angle = (angle/90) - 1;
if (Math.abs(angle) < 1) {
angle = Math.exp(-1/(1 - angle*angle));
} else {
angle = 0;
}
angle *= delta.magnitude() * -Math.signum(delta.dotProduct(screenAxisDir.getY(), -screenAxisDir.getX()));
angle *= controls.modifier(me.isControlDown(), me.isShiftDown());
rotate(viewDir, angle);
});
}
public void setType(Axis.Type type) {
this.axis.setType(type);
}
public void translate(Point3D destination) {
if (this.axis == null) {
return;
}
this.axis.setPosition(destination);
}
public void rotate(Point3D axis, double angle) {
if (this.axis == null) {
return;
}
Rotate r = new Rotate(angle, axis);
this.axis.setDirection(r.transform(this.axis.getDirection()).normalize());
}
public void stop(boolean modified) {
if (!modified && this.axis != null && this.parentGroup != null) {
this.parentGroup.getChildren().remove(this.axis.getVisualGroup());
}
this.axis = null;
this.parentGroup = null;
}
public void stop(Component component) {
component.setAxis(this.axis.clone());
stop(true);
}
}
package fr.ill.ics.gui;
import fr.ill.ics.model.Axis;
import fr.ill.ics.model.Component;
import javafx.geometry.Point2D;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.transform.NonInvertibleTransformException;
import javafx.scene.transform.Rotate;
/**
* Class for creating and placing axes.
* @author dages
*/
public class AxisBuilder {
/** Edited axis. */
private Axis axis;
/** Parent of the visual group of the axis. */
private Group parentGroup;
/** Mouse position, used when dragging the mouse. */
private Point2D mousePos = null;
/** Mouse old position, used when dragging the mouse. */
private Point2D mouseOldPos = null;
/**
* Default constructor.
*/
public AxisBuilder() {
this.axis = null;
this.parentGroup = null;
}
/**
* Starts the axis editing.
* @param axis Axis to edit
* @param parentGroup Parent of the visual group of the axis
* @param controls Camera controls
* @param modified true if the axis is modified, false if it added
*/
public void start(Axis axis, Group parentGroup, CameraControls controls, boolean modified) {
this.axis = axis;
this.axis.setDirection(new Point3D(0, 1, 0));
this.axis.setPosition(controls.getCenter());
try {
this.axis.setSceneToParent(parentGroup.getLocalToSceneTransform().createInverse());
} catch (NonInvertibleTransformException e) {
e.printStackTrace();
}
this.axis.getVisualGroup().setVisible(true);
this.parentGroup = parentGroup;
if (!modified) {
parentGroup.getChildren().add(this.axis.getVisualGroup());
}
this.axis.getVisualGroup().setOnMousePressed(me -> {
mousePos = new Point2D(me.getSceneX(), me.getSceneY());
mouseOldPos = new Point2D(me.getSceneX(), me.getSceneY());
});
this.axis.getVisualGroup().setOnMouseDragged(me -> {
mouseOldPos = mousePos;
mousePos = new Point2D(me.getSceneX(), me.getSceneY());
Point2D delta = mousePos.subtract(mouseOldPos);
Point3D screenAxisDir3D = controls.getTransform().deltaTransform(axis.getDirection());
Point2D screenAxisDir = new Point2D(screenAxisDir3D.getX(), screenAxisDir3D.getY());
Point3D viewDir = controls.viewDirection();
double angle = screenAxisDir.angle(delta);
while (angle < 0) angle += 360;
while (angle > 180) angle -= 180;
angle = (angle/90) - 1;
if (Math.abs(angle) < 1) {
angle = Math.exp(-1/(1 - angle*angle));
} else {
angle = 0;
}
angle *= delta.magnitude() * -Math.signum(delta.dotProduct(screenAxisDir.getY(), -screenAxisDir.getX()));
angle *= controls.modifier(me.isControlDown(), me.isShiftDown());
rotate(viewDir, angle);
});
}
/**
* Sets the type of the edited axis.
* @param type New type
*/
public void setType(Axis.Type type) {
this.axis.setType(type);
}
/**
* Translates the axis.
* @param destination Position of the axis once the translation is over, in global coordinate system
*/
public void translate(Point3D destination) {
if (this.axis == null) {
return;
}
this.axis.setPosition(destination);
}
/**
* Rotates the axis.
* @param axis Rotation axis, in global coordinate system
* @param angle Rotation angle
*/
public void rotate(Point3D axis, double angle) {
if (this.axis == null) {
return;
}
Rotate r = new Rotate(angle, axis);
this.axis.setDirection(r.transform(this.axis.getDirection()).normalize());
}
/**
* Stops the axis editing.
* @param modified true if the axis was modified, false if it was added
*/
public void stop(boolean modified) {
if (!modified && this.axis != null && this.parentGroup != null) {
this.parentGroup.getChildren().remove(this.axis.getVisualGroup());
}
this.axis = null;
this.parentGroup = null;
}
/**
* Stops the axis editing and store the axis.
* @param component Owner of the modified axis
*/
public void stop(Component component) {
component.setAxis(this.axis.clone());
stop(true);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment