⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segmentplaneeditor.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2001, 2008 United States Government as represented by
the Administrator of the National Aeronautics and Space Administration.
All Rights Reserved.
*/
package gov.nasa.worldwind.render;

import gov.nasa.worldwind.*;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.AbstractLayer;
import gov.nasa.worldwind.pick.PickedObject;
import gov.nasa.worldwind.render.airspaces.editor.AirspaceEditorUtil;
import gov.nasa.worldwind.util.Logging;

import java.awt.*;

/**
 * @author dcollins
 * @version $Id: SegmentPlaneEditor.java 9634 2009-03-24 17:03:21Z dcollins $
 */
public class SegmentPlaneEditor extends AbstractLayer
{
    protected static final int SEGMENT_BEGIN_INDEX = 0;
    protected static final int SEGMENT_END_INDEX = 1;

    private boolean armed;
    private boolean snapToGrid;
    private SegmentPlane segmentPlane; // Can be null.
    private SegmentPlaneRenderer renderer; // Can be null.

    public SegmentPlaneEditor()
    {
        this.armed = false;
        this.snapToGrid = true;
        this.renderer = new SegmentPlaneRenderer();
    }

    public boolean isArmed()
    {
        return this.armed;
    }

    public void setArmed(boolean armed)
    {
        this.armed = armed;
    }

    public boolean isSnapToGrid()
    {
        return this.snapToGrid;
    }

    public void setSnapToGrid(boolean snapToGrid)
    {
        this.snapToGrid = snapToGrid;
    }

    public SegmentPlane getSegmentPlane()
    {
        return this.segmentPlane;
    }

    public void setSegmentPlane(SegmentPlane segmentPlane)
    {
        this.segmentPlane = segmentPlane;
    }

    public SegmentPlaneRenderer getSegmentPlaneRenderer()
    {
        return this.renderer;
    }

    public void setSegmentPlaneRenderer(SegmentPlaneRenderer renderer)
    {
        this.renderer = renderer;
    }

    protected void doRender(DrawContext dc)
    {
        if (!this.isArmed())
            return;

        if (this.getSegmentPlane() == null || this.getSegmentPlaneRenderer() == null)
            return;

        this.getSegmentPlaneRenderer().render(dc, this.getSegmentPlane());
    }

    protected void doPick(DrawContext dc, java.awt.Point pickPoint)
    {
        if (!this.isArmed())
            return;

        if (this.getSegmentPlane() == null || this.getSegmentPlaneRenderer() == null)
            return;

        this.getSegmentPlaneRenderer().pick(dc, this.getSegmentPlane(), pickPoint, this);
    }

    public void moveControlPoint(WorldWindow wwd, PickedObject pickedObject, Point mousePoint, Point previousMousePoint)
    {
        if (wwd == null)
        {
            String message = Logging.getMessage("nullValue.WorldWindow");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }
        if (pickedObject == null)
        {
            String message = Logging.getMessage("nullValue.PickedObject");
            Logging.logger().severe(message);
            throw new IllegalArgumentException(message);
        }

        // Include this test to ensure any derived implementation performs it.
        if (this.getSegmentPlane() == null)
        {
            return;
        }

        if (!(pickedObject.getObject() instanceof SegmentPlane.ControlPoint))
        {
            return;
        }

        SegmentPlane.ControlPoint controlPoint = (SegmentPlane.ControlPoint) pickedObject.getObject();
        if (this.getSegmentPlane() != controlPoint.getOwner())
        {
            return;
        }

        this.doMoveControlPoint(wwd, pickedObject, mousePoint, previousMousePoint);
    }

    protected void doMoveControlPoint(WorldWindow wwd, PickedObject pickedObject,
        Point mousePoint, Point previousMousePoint)
    {
        SegmentPlane.ControlPoint controlPoint = (SegmentPlane.ControlPoint) pickedObject.getObject();

        Object key = controlPoint.getKey();
        if (key == null)
        {
            return;
        }

        if (key.equals(SegmentPlane.SEGMENT_BEGIN) || key.equals(SegmentPlane.SEGMENT_END))
        {
            this.doMoveSegmentPoint(wwd, pickedObject, mousePoint, previousMousePoint);
        }
        else if (key.equals(SegmentPlane.CONTROL_POINT_LOWER_LEFT))
        {
            this.doMoveSegmentPlane(wwd, pickedObject, mousePoint, previousMousePoint);
        }
        else if (key.equals(SegmentPlane.CONTROL_POINT_LOWER_RIGHT) 
            || key.equals(SegmentPlane.CONTROL_POINT_UPPER_RIGHT))
        {
            this.doMoveLateralControlPoint(wwd, pickedObject, mousePoint, previousMousePoint);
        }
        else if (key.equals(SegmentPlane.CONTROL_POINT_TOP_EDGE))
        {
            this.doMoveVerticalControlPoint(wwd, pickedObject, mousePoint, previousMousePoint);
        }
        else if (key.equals(SegmentPlane.CONTROL_POINT_LEADING_EDGE))
        {
            this.doMoveHorizontalControlPoint(wwd, pickedObject, mousePoint, previousMousePoint);
        }
    }

    //**************************************************************//
    //********************  Segment Plane Movement  ****************//
    //**************************************************************//

    protected void doMoveSegmentPlane(WorldWindow wwd, PickedObject pickedObject,
        Point mousePoint, Point previousMousePoint)
    {
        View view = wwd.getView();
        Globe globe = wwd.getModel().getGlobe();
        LatLon[] locations = this.getSegmentPlane().getPlaneLocations();

        Position pickedPos = pickedObject.getPosition();

        Position refPos = new Position(locations[0], pickedPos.getElevation());
        Vec4 refPoint = globe.computePointFromPosition(refPos);
        Vec4 screenRefPoint = view.project(refPoint);

        // Compute screen-coord delta since last event.
        int dx = mousePoint.x - previousMousePoint.x;
        int dy = mousePoint.y - previousMousePoint.y;

        // Find intersection of screen coord ref-point with globe.
        double x = screenRefPoint.x + dx;
        double y = screenRefPoint.y + dy;
        if (wwd instanceof Component)
        {
            y = ((Component) wwd).getSize().height - screenRefPoint.y + dy - 1;
        }

        Line ray = view.computeRayFromScreenPoint(x, y);
        Intersection[] intersections = globe.intersect(ray, refPos.getElevation());
        if (intersections == null || intersections.length == 0)
            return;

        Position newPos = globe.computePositionFromPoint(intersections[0].getIntersectionPoint());

        Angle heading = LatLon.greatCircleAzimuth(refPos, newPos);
        Angle distance = LatLon.greatCircleDistance(refPos, newPos);

        locations[0] = LatLon.greatCircleEndPosition(locations[0], heading, distance);
        locations[1] = LatLon.greatCircleEndPosition(locations[1], heading, distance);

        this.moveSegmentLocationWithPlane(locations, SEGMENT_BEGIN_INDEX);
        this.moveSegmentLocationWithPlane(locations, SEGMENT_END_INDEX);
        this.getSegmentPlane().setPlaneLocations(locations[0], locations[1]);
    }

    //**************************************************************//
    //********************  Segment Point Actions  *****************//
    //**************************************************************//

    @SuppressWarnings({"UnusedDeclaration"})
    protected void doMoveSegmentPoint(WorldWindow wwd, PickedObject pickedObject,
        Point mousePoint, Point previousMousePoint)
    {
        Position oldPosition = pickedObject.getPosition();
        Position newPosition = this.computeNewPositionFromPlaneGeometry(wwd);

        // If the mouse point is not on the plane geometry, we compute an intersection with the infinite plane
        // defined by the SegmentPlane's corners.
        if (newPosition == null)
        {
            newPosition  = this.computeNewPositionFromPlaneIntersection(wwd, mousePoint);
            if (newPosition != null)
            {
                newPosition = this.resizeSegmentPlaneToFitPosition(wwd, newPosition);
            }
        }

        if (newPosition == null)
        {
            return;
        }

        newPosition = this.computePositionOnOrAboveSurface(wwd, newPosition);

        Position[] positions = this.getSegmentPlane().getSegmentPositions();

        Object endpointId = pickedObject.getValue(AVKey.PICKED_OBJECT_ID);
        if (endpointId.equals(SegmentPlane.SEGMENT_BEGIN))
        {
            positions[0] = new Position(oldPosition, newPosition.getElevation());
        }
        else if (endpointId.equals(SegmentPlane.SEGMENT_END))
        {
            positions[1] = newPosition;
        }

        this.getSegmentPlane().setSegmentPositions(positions[0], positions[1]);
    }

    protected Position computeNewPositionFromPlaneGeometry(WorldWindow wwd)
    {
        if (this.isSnapToGrid())
        {
            PickedObject gridObject = this.getPickedSegmentPlaneObject(wwd, SegmentPlane.PLANE_GRID);
            if (gridObject != null)
            {
                return gridObject.getPosition();
            }
        }

        PickedObject planeObject = this.getPickedSegmentPlaneObject(wwd, SegmentPlane.PLANE_BACKGROUND);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -