use of com.ardor3d.intersection.IntersectionRecord in project energy3d by concord-consortium.
the class Heliodon method initMouse.
private void initMouse(final LogicalLayer logicalLayer) {
logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.F), new TriggerAction() {
@Override
public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
setSunRegionAlwaysVisible(!forceSunRegionOn);
}
}));
logicalLayer.registerTrigger(new InputTrigger(new MouseButtonPressedCondition(MouseButton.LEFT), new TriggerAction() {
@Override
public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
oldHourAngle = hourAngle;
changeTimeAndDateCommand = new ChangeTimeAndDateWithHeliodonCommand(calendar.getTime());
final int x = inputStates.getCurrent().getMouseState().getX();
final int y = inputStates.getCurrent().getMouseState().getY();
final Ray3 pickRay = SceneManager.getInstance().getCanvas().getCanvasRenderer().getCamera().getPickRay(new Vector2(x, y), false, null);
pickResults.clear();
PickingUtil.findPick(sun, pickRay, pickResults);
if (pickResults.getNumber() != 0) {
sunGrabbed = true;
} else {
sunGrabbed = false;
}
if (forceSunRegionOn) {
selectDifferentDeclinationWithMouse = true;
} else {
selectDifferentDeclinationWithMouse = false;
}
SceneManager.getInstance().setMouseControlEnabled(!sunGrabbed);
}
}));
logicalLayer.registerTrigger(new InputTrigger(new MouseButtonReleasedCondition(MouseButton.LEFT), new TriggerAction() {
@Override
public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
sunGrabbed = false;
if (!forceSunRegionOn) {
sunRegion.getSceneHints().setCullHint(CullHint.Always);
}
SceneManager.getInstance().setMouseControlEnabled(true);
if (!Util.isEqual(oldHourAngle, hourAngle) && changeTimeAndDateCommand != null) {
SceneManager.getInstance().getUndoManager().addEdit(changeTimeAndDateCommand);
}
}
}));
logicalLayer.registerTrigger(new InputTrigger(new MouseMovedCondition(), new TriggerAction() {
@Override
public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
if (!sunGrabbed) {
return;
}
final MouseState mouse = inputStates.getCurrent().getMouseState();
final Ray3 pickRay = SceneManager.getInstance().getCamera().getPickRay(new Vector2(mouse.getX(), mouse.getY()), false, null);
pickResults.clear();
PickingUtil.findPick(sunRegion, pickRay, pickResults);
final Vector3 intersectionPoint;
if (pickResults.getNumber() > 0) {
final IntersectionRecord intersectionRecord = pickResults.getPickData(0).getIntersectionRecord();
intersectionPoint = intersectionRecord.getIntersectionPoint(intersectionRecord.getClosestIntersection());
} else {
intersectionPoint = null;
}
double smallestDistance = Double.MAX_VALUE;
int hourVertex = -1;
int totalHourVertices = 0;
final Vector3 newSunLocation = new Vector3();
final Vector3 p = new Vector3();
final Vector3 p_abs = new Vector3();
final ReadOnlyTransform rootTansform = root.getTransform();
if (!selectDifferentDeclinationWithMouse) {
final FloatBuffer buf = sunPath.getMeshData().getVertexBuffer();
buf.rewind();
while (buf.hasRemaining()) {
p.set(buf.get(), buf.get(), buf.get());
rootTansform.applyForward(p, p_abs);
final double d;
d = pickRay.distanceSquared(p_abs, null);
if (d < smallestDistance) {
smallestDistance = d;
hourVertex = buf.position() / 3 - 1;
newSunLocation.set(p);
}
}
totalHourVertices = buf.limit() / 3;
}
if (smallestDistance > 5.0 * root.getTransform().getScale().getX() * root.getTransform().getScale().getX()) {
selectDifferentDeclinationWithMouse = true;
}
boolean declinationChanged = false;
if (selectDifferentDeclinationWithMouse) {
sunRegion.getSceneHints().setCullHint(CullHint.Inherit);
int rowCounter = 0;
int resultRow = -1;
final FloatBuffer buf = sunRegion.getMeshData().getVertexBuffer();
buf.rewind();
final double r = 5.0 / 2.0;
final Vector3 prev = new Vector3();
int quadVertexCounter = 0;
final double maxVertexInRow = HOUR_DIVISIONS * 4.0;
int rowVertexCounter = 0;
boolean foundInThisRow = false;
while (buf.hasRemaining()) {
p.set(buf.get(), buf.get(), buf.get());
rootTansform.applyForward(p, p_abs);
final double d;
if (intersectionPoint != null) {
d = intersectionPoint.distanceSquared(p_abs);
} else {
d = pickRay.distanceSquared(p_abs, null);
}
if (d < smallestDistance && p.getZ() >= -MathUtils.ZERO_TOLERANCE) {
smallestDistance = d;
newSunLocation.set(p);
resultRow = rowCounter + (quadVertexCounter >= 2 ? 1 : 0);
hourVertex = rowVertexCounter / 4 + (quadVertexCounter == 1 || quadVertexCounter == 2 ? 1 : 0);
foundInThisRow = true;
}
if (prev.lengthSquared() != 0 && (prev.distance(p) > r || rowVertexCounter >= maxVertexInRow)) {
rowCounter++;
if (foundInThisRow) {
totalHourVertices = rowVertexCounter / 4;
}
foundInThisRow = false;
rowVertexCounter = 0;
}
prev.set(p);
quadVertexCounter = (quadVertexCounter + 1) % 4;
rowVertexCounter++;
}
rowCounter++;
if (resultRow != -1) {
if (rowCounter < DECLINATION_DIVISIONS && latitude > 0) {
resultRow += DECLINATION_DIVISIONS - rowCounter;
}
final double newDeclinationAngle = -TILT_ANGLE + (2.0 * TILT_ANGLE * resultRow / DECLINATION_DIVISIONS);
declinationChanged = !Util.isEqual(newDeclinationAngle, declinationAngle);
if (declinationChanged) {
setDeclinationAngle(newDeclinationAngle, false, true);
dirtySunPath = true;
}
}
}
final double newHourAngle = (hourVertex - Math.floor(totalHourVertices / 2.0)) * Math.PI / 48.0;
final boolean hourAngleChanged = !Util.isEqual(newHourAngle, hourAngle);
if (hourAngleChanged) {
setHourAngle(newHourAngle, false, true, false);
}
if (declinationChanged || hourAngleChanged) {
setSunLocation(newSunLocation);
drawSunTriangle();
EnergyPanel.getInstance().updateRadiationHeatMap();
}
}
}));
}
Aggregations