Search in sources :

Example 1 with IntersectionRecord

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();
            }
        }
    }));
}
Also used : IntersectionRecord(com.ardor3d.intersection.IntersectionRecord) TriggerAction(com.ardor3d.input.logical.TriggerAction) MouseButtonReleasedCondition(com.ardor3d.input.logical.MouseButtonReleasedCondition) KeyPressedCondition(com.ardor3d.input.logical.KeyPressedCondition) MouseMovedCondition(com.ardor3d.input.logical.MouseMovedCondition) InputTrigger(com.ardor3d.input.logical.InputTrigger) Canvas(com.ardor3d.framework.Canvas) ReadOnlyTransform(com.ardor3d.math.type.ReadOnlyTransform) MouseButtonPressedCondition(com.ardor3d.input.logical.MouseButtonPressedCondition) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) FloatBuffer(java.nio.FloatBuffer) ChangeTimeAndDateWithHeliodonCommand(org.concord.energy3d.undo.ChangeTimeAndDateWithHeliodonCommand) CullHint(com.ardor3d.scenegraph.hint.CullHint) Ray3(com.ardor3d.math.Ray3) Vector2(com.ardor3d.math.Vector2) MouseState(com.ardor3d.input.MouseState) TwoInputStates(com.ardor3d.input.logical.TwoInputStates)

Aggregations

Canvas (com.ardor3d.framework.Canvas)1 MouseState (com.ardor3d.input.MouseState)1 InputTrigger (com.ardor3d.input.logical.InputTrigger)1 KeyPressedCondition (com.ardor3d.input.logical.KeyPressedCondition)1 MouseButtonPressedCondition (com.ardor3d.input.logical.MouseButtonPressedCondition)1 MouseButtonReleasedCondition (com.ardor3d.input.logical.MouseButtonReleasedCondition)1 MouseMovedCondition (com.ardor3d.input.logical.MouseMovedCondition)1 TriggerAction (com.ardor3d.input.logical.TriggerAction)1 TwoInputStates (com.ardor3d.input.logical.TwoInputStates)1 IntersectionRecord (com.ardor3d.intersection.IntersectionRecord)1 Ray3 (com.ardor3d.math.Ray3)1 Vector2 (com.ardor3d.math.Vector2)1 Vector3 (com.ardor3d.math.Vector3)1 ReadOnlyTransform (com.ardor3d.math.type.ReadOnlyTransform)1 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)1 CullHint (com.ardor3d.scenegraph.hint.CullHint)1 FloatBuffer (java.nio.FloatBuffer)1 ChangeTimeAndDateWithHeliodonCommand (org.concord.energy3d.undo.ChangeTimeAndDateWithHeliodonCommand)1