measuretoolcontroller.java

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

JAVA
461
字号
/*
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.util.measure;

import gov.nasa.worldwind.event.*;
import gov.nasa.worldwind.examples.BasicDragger;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.util.Logging;

import java.awt.event.*;

/**
 * Generic controller for the <code>MeasureTool</code>.
 *
 * @author Patrick Murris
 * @version $Id: MeasureToolController.java 10107 2009-04-11 00:43:11Z dcollins $
 * @see MeasureTool
 */
public class MeasureToolController extends MouseAdapter
        implements MouseListener, MouseMotionListener, SelectListener, PositionListener, RenderingListener
{
    protected MeasureTool measureTool;

    protected boolean armed = false;
    protected boolean active = false;
    protected boolean moving = false;
    protected boolean useRubberBand = true;
    protected boolean freeHand = false;
    protected double freeHandMinSpacing = 100;
    
    protected MeasureTool.ControlPoint rubberBandTarget;
    protected MeasureTool.ControlPoint movingTarget;
    protected MeasureTool.ControlPoint lastPickedObject;
    protected BasicDragger dragger;

    /**
     * Set the <code>MeasureTool</code> that this controller will be operating on.
     *
     * @param measureTool the <code>MeasureTool</code> that this controller will be operating on.
     */
    public void setMeasureTool(MeasureTool measureTool)
    {
        if (measureTool == null)
        {
            String msg = Logging.getMessage("nullValue.MeasureToolIsNull");
            Logging.logger().severe(msg);
            throw new IllegalArgumentException(msg);
        }

        this.measureTool = measureTool;
    }

    /**
     * Get the <code>MeasureTool</code> that this controller is operating on.
     *
     * @return the <code>MeasureTool</code> that this controller is operating on.
     */

    public MeasureTool getMeasureTool()
    {
        return this.measureTool;
    }

    /**
     * Returns true if this controller is using rubber band during shape creation. When using rubber band, new control
     * points are added by first pressing the left mouse button, then dragging the mouse toward the proper position,
     * then releasing the mouse button. Otherwise new control point are added for each new click of the mouse.
     *
     * @return true if this controller is using rubber band during shape creation.
     */
    public boolean isUseRubberBand()
    {
        return this.useRubberBand;
    }

    /**
     * Set whether this controller should use rubber band during shape creation. When using rubber band, new control
     * points are added by first pressing the left mouse button, then dragging the mouse toward the proper position,
     * then releasing the mouse button. Otherwise new control point are added for each new click of the mouse.
     *
     * @param state true if this controller should use rubber band during shape creation.
     */
    public void setUseRubberBand(boolean state)
    {
        this.useRubberBand = state;
    }

    /**
     * Get whether this controller allows free hand drawing of path and polygons while using rubber band mode.
     *
     * @return true if free hand drawing of path and polygons in rubber band mode.
     */
    public boolean isFreeHand()
    {
        return this.freeHand;
    }

    /**
     * Set whether this controller allows free hand drawing of path and polygons while using rubber band mode.
     *
     * @param state true to allow free hand drawing of path and polygons in rubber band mode.
     */
    public void setFreeHand(boolean state)
    {
        this.freeHand = state;
    }

    /**
     * Get the minimum distance in meters between two control points for free hand drawing.
     *
     * @return the minimum distance in meters between two control points for free hand drawing.
     */
    public double getFreeHandMinSpacing()
    {
        return this.freeHandMinSpacing;
    }

    /**
     * Set the minimum distance in meters between two control points for free hand drawing.
     *
     * @param distance the minimum distance in meters between two control points for free hand drawing.
     */
    public void setFreeHandMinSpacing(double distance)
    {
        this.freeHandMinSpacing = distance;
    }

    /**
     * Identifies whether the measure tool controller is armed.
     *
     * @return true if armed, false if not armed.
     */
    public boolean isArmed()
    {
        return this.armed;
    }

    /**
     * Arms and disarms the measure tool controller. When armed, the controller monitors user input and builds the
     * shape in response to user actions. When disarmed, the controller ignores all user input.
     *
     * @param armed true to arm the controller, false to disarm it.
     */
    public void setArmed(boolean armed)
    {
        if (this.armed != armed)
        {
            this.armed = armed;
            this.measureTool.firePropertyChange(MeasureTool.EVENT_ARMED, !armed, armed);
        }
    }

    /**
     * Returns true if the controller is in the middle of a rubber band operation.
     *
     * @return true if the controller is in the middle of a rubber band operation.
     */
    public boolean isActive()
    {
        return this.active;
    }

    protected void setActive(boolean state)
    {
        this.active = state;
    }

    /**
     * Returns true if the controller is moving the measure shape as a whole.
     *
     * @return true if the controller is moving the measure shape as a whole.
     */
    public boolean isMoving()
    {
        return this.moving;
    }

    protected void setMoving(boolean state)
    {
        this.moving = state;
    }


    // Handle mouse actions
    public void mousePressed(MouseEvent mouseEvent)
    {
        if (this.isArmed() && this.isUseRubberBand() && mouseEvent.getButton() == MouseEvent.BUTTON1)
        {
            if ((mouseEvent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0)
            {
                if (!mouseEvent.isControlDown())
                {
                    this.setActive(true);
                    measureTool.addControlPoint();
                    if (measureTool.getControlPoints().size() == 1)
                    {
                        measureTool.addControlPoint(); // Simulate a second click
                    }
                    // Set the rubber band target to the last control point or the east one for regular shapes.
                    rubberBandTarget = (MeasureTool.ControlPoint)measureTool.getControlPoints().get(
                            measureTool.isRegularShape() ? 2 : measureTool.getControlPoints().size() - 1);
                    measureTool.firePropertyChange(MeasureTool.EVENT_RUBBERBAND_START, null, null);
                }
            }
            mouseEvent.consume();
        }
        else if(!this.isArmed() && mouseEvent.getButton() == MouseEvent.BUTTON1 && mouseEvent.isAltDown())
        {
            this.setMoving(true);
            this.movingTarget = this.lastPickedObject;
            mouseEvent.consume();
        }
    }

    public void mouseReleased(MouseEvent mouseEvent)
    {
        if (this.isArmed() && this.isUseRubberBand() && mouseEvent.getButton() == MouseEvent.BUTTON1)
        {
            if (this.isUseRubberBand() && measureTool.getPositions().size() == 1)
                measureTool.removeControlPoint();
            this.setActive(false);
            rubberBandTarget = null;
            // Disarm after second control point of a line or regular shape
            autoDisarm();
            mouseEvent.consume();

⌨️ 快捷键说明

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