measuretoolcontroller.java

来自「world wind java sdk 源码」· Java 代码 · 共 461 行 · 第 1/2 页

JAVA
461
字号
            measureTool.firePropertyChange(MeasureTool.EVENT_RUBBERBAND_STOP, null, null);
        }
        else if (this.isMoving()  && mouseEvent.getButton() == MouseEvent.BUTTON1)
        {
            this.setMoving(false);
            this.movingTarget = null;
            mouseEvent.consume();
        }
    }

    // Handle single click for removing control points
    public void mouseClicked(MouseEvent mouseEvent)
    {
        if (measureTool == null)
            return;

        if (this.isArmed() && mouseEvent.getButton() == MouseEvent.BUTTON1)
        {
            if (mouseEvent.isControlDown())
                measureTool.removeControlPoint();
            else if (!this.isUseRubberBand())
            {
                measureTool.addControlPoint();
                // Disarm after second control point of a line or regular shape
                autoDisarm();
            }
            mouseEvent.consume();
        }
    }


    // Handle mouse motion
    public void mouseDragged(MouseEvent mouseEvent)
    {
        if (measureTool == null || !this.active)
            return;

        if (this.isArmed() && (mouseEvent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0)
        {
            // Don't update the control point here because the wwd current cursor position will not
            // have been updated to reflect the current mouse position. Wait to update in the
            // position listener, but consume the event so the view doesn't respond to it.
            mouseEvent.consume();
        }
    }

    public void mouseMoved(MouseEvent mouseEvent)
    {

    }

    // Handle cursor position change for rubber band
    public void moved(PositionEvent event)
    {
        if (measureTool == null || (!this.active && !this.moving))
            return;

        this.doMoved(event);
    }

    // Handle dragging of control points
    public void selected(SelectEvent event)
    {
        if (measureTool == null || (this.isArmed() && this.isUseRubberBand()))
            return;

        if (dragger == null)
            dragger = new BasicDragger(measureTool.getWwd());

        if (event.getEventAction().equals(SelectEvent.ROLLOVER))
            highlight(event.getTopObject());

        this.doSelected(event);
    }

    // Wait for end of rendering to update metrics - length, area...
    public void stageChanged(RenderingEvent event)
    {
        if (measureTool == null)
            return;

        if (event.getStage().equals(RenderingEvent.AFTER_BUFFER_SWAP))
        {
            measureTool.firePropertyChange(MeasureTool.EVENT_METRIC_CHANGED, null, null);
        }
    }

    @SuppressWarnings({"UnusedDeclaration"})
    protected void doMoved(PositionEvent event)
    {
        if (this.active && rubberBandTarget != null && measureTool.getWwd().getCurrentPosition() != null)
        {
            if (!isFreeHand() || (!measureTool.getMeasureShape().equals(MeasureTool.SHAPE_PATH)
                    && !measureTool.getMeasureShape().equals(MeasureTool.SHAPE_POLYGON)))
            {
                // Rubber band - Move control point and update shape
                Position lastPosition = rubberBandTarget.getPosition();
                rubberBandTarget.setPosition(new Position(measureTool.getWwd().getCurrentPosition(), 0));
                measureTool.moveControlPoint(rubberBandTarget);
                measureTool.firePropertyChange(MeasureTool.EVENT_POSITION_REPLACE,
                        lastPosition, rubberBandTarget.getPosition());
                measureTool.getWwd().redraw();
            }
            else
            {
                // Free hand - Compute distance from current control point (rubber band target)
                Position lastPosition = rubberBandTarget.getPosition();
                Position newPosition = measureTool.getWwd().getCurrentPosition();
                double distance = LatLon.greatCircleDistance(lastPosition, newPosition).radians
                        * measureTool.getWwd().getModel().getGlobe().getRadius();
                if (distance >= freeHandMinSpacing)
                {
                    // Add new control point
                    measureTool.addControlPoint();
                    rubberBandTarget = (MeasureTool.ControlPoint)getMeasureTool().getControlPoints().get(
                            getMeasureTool().getControlPoints().size() - 1);
                    measureTool.getWwd().redraw();
                }
            }
        }
        else if (this.moving && movingTarget != null && measureTool.getWwd().getCurrentPosition() != null)
        {
            // Moving the whole shape
            Position lastPosition = movingTarget.getPosition();
            Position newPosition = measureTool.getWwd().getCurrentPosition();
            this.moveToPosition(lastPosition, newPosition);
            //measureTool.getWwd().redraw();
        }
    }

    /**
     * Move the shape to the specified new position
     * @param oldPosition Previous position of shape
     * @param newPosition New position for shape
     */
    protected void moveToPosition(Position oldPosition, Position newPosition)
    {
        Angle distanceAngle = LatLon.greatCircleDistance(oldPosition, newPosition);
        Angle azimuthAngle = LatLon.greatCircleAzimuth(oldPosition, newPosition);
        measureTool.moveMeasureShape(azimuthAngle, distanceAngle);
        measureTool.firePropertyChange(MeasureTool.EVENT_POSITION_REPLACE, oldPosition, newPosition);
    }

    protected void doSelected(SelectEvent event)
    {
        if (event.getTopObject() != null)
        {
            if (event.getTopObject() instanceof MeasureTool.ControlPoint)
            {
                MeasureTool.ControlPoint point = (MeasureTool.ControlPoint)event.getTopObject();
                // Check whether this control point belongs to our measure tool
                if (point.getParent() != measureTool)
                    return;

                // Have rollover events highlight the rolled-over object.
                if (event.getEventAction().equals(SelectEvent.ROLLOVER) && !this.dragger.isDragging())
                {
                    this.highlight(point);
                }
                // Have drag events drag the selected object.
                else if (movingTarget == null && (event.getEventAction().equals(SelectEvent.DRAG_END)
                        || event.getEventAction().equals(SelectEvent.DRAG)))
                {
                    this.dragSelected(event);
                }
            }
        }
    }

    protected void dragSelected(SelectEvent event)
    {
        MeasureTool.ControlPoint point = (MeasureTool.ControlPoint)event.getTopObject();
        
        LatLon lastPosition = point.getPosition();
        if (point.getValue("PositionIndex") != null)
            lastPosition = measureTool.getPositions().get((Integer)point.getValue("PositionIndex"));

        // Delegate dragging computations to a dragger.
        this.dragger.selected(event);
        
        measureTool.moveControlPoint(point);
        if (measureTool.isShowAnnotation())
            measureTool.updateAnnotation(point.getPosition());
        measureTool.firePropertyChange(MeasureTool.EVENT_POSITION_REPLACE,
                lastPosition, point.getPosition());
        measureTool.getWwd().redraw();
    }

    protected void highlight(Object o)
    {
        // Manage highlighting of control points
        if (this.lastPickedObject == o)
            return; // Same thing selected

        if (o != null && o instanceof MeasureTool.ControlPoint && ((MeasureTool.ControlPoint)o).parent != measureTool)
            return; // Does not belong to this measure tool

        // Turn off highlight if on.
        if (this.lastPickedObject != null)
        {
            this.lastPickedObject.getAttributes().setHighlighted(false);
            this.lastPickedObject.getAttributes().setBackgroundColor(null); // use default
            this.lastPickedObject = null;
            if (measureTool.isShowAnnotation())
                measureTool.updateAnnotation(null);
        }

        // Turn on highlight if object selected.
        if (o != null && o instanceof MeasureTool.ControlPoint)
        {
            this.lastPickedObject = (MeasureTool.ControlPoint) o;
            this.lastPickedObject.getAttributes().setHighlighted(true);
            // Highlite using text color
            this.lastPickedObject.getAttributes().setBackgroundColor(
                    this.lastPickedObject.getAttributes().getTextColor());
            if (measureTool.isShowAnnotation())
                measureTool.updateAnnotation(this.lastPickedObject.getPosition());
        }
    }

    protected void autoDisarm()
    {
        // Disarm after second control point of a line or regular shape
        if (measureTool.isRegularShape() || measureTool.getMeasureShape().equals(MeasureTool.SHAPE_LINE))
            if (measureTool.getControlPoints().size() > 1)
                this.setArmed(false);
    }

}

⌨️ 快捷键说明

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