【问题标题】:Disable globe movement on click in World Wind在 World Wind 中单击时禁用地球运动
【发布时间】:2015-10-03 14:11:45
【问题描述】:

我试图在 World Wind 中禁用鼠标单击时地球的移动。我希望能够做到:

void disableGlobeDrag(WorldWindowGLCanvas ww) {
    ww.addMouseMotionListener(new MyMouseMotionListener());
}

MyMouseMotionListener 使用所有鼠标事件。这不起作用,所以我必须这样做:

void disableGlobeDrag(WorldWindowGLCanvas ww) {
    for(MouseMotionListener l : ww.getMouseMotionListeners()) {
        if(l.getClass().toString().equals("class gov.nasa.worldwind.awt.AWTInputHandler")) {
            ww.removeMouseMotionListener(l);
        }
    }
}

预计消耗的鼠标事件仍应到达gov.nasa.worldwind.awt.AWTInputHandler 侦听器吗?

更新: WorldWindowGLCanvas 只是在 java.awt.Component 上调用 addMouseMotionListener(),所以显然我不明白消费事件是如何工作的。

更新 2: 尽管与 Swing 共享接口,但使用 AWTInputHandler 作为参数调用 WorldWindowGLCanvas.removeMouseMotionListener() 将阻止所有其他 MouseMotionListeners 接收事件。应该使用 AWTInputHandler 上的 add 和 remove 方法。

【问题讨论】:

  • 为什么添加另一个侦听器会阻止任何现有侦听器接收事件?
  • 如果我在最近添加的侦听器上消费该事件,那不应该阻止其他侦听器接收同一事件的通知吗?
  • 你如何“消费”这个事件?翻阅 java.awt.Component 和 java.awt.AWTEventMulticaster,我看不到任何消费事件的概念。看起来它总是将它们传递给所有注册的听众。
  • 使用MouseEvent.consume() - 这最终会起作用,但只有当侦听器被添加到 World Wind 的 AWTInputHandler 而不是使用 WorldWindowGLCanvas 上的方法时。

标签: java swing worldwind


【解决方案1】:

很遗憾,按照 @trashgod 的建议删除 MouseMotionListener 不起作用,因为发生了一些 World Wind 特定行为:删除 gov.nasa.worldwind.awt.AWTInputHandler 会导致其他 MouseMotionListeners 停止接收事件通知。

要禁用地球拖动并仍然在另一个MouseMotionListener 中接收事件,需要执行以下步骤:

参考 World Wind 的AWTInputHandler

AWTInputHandler wwHandler = null;
// get World Wind's AWTInputHandler class:
for (MouseMotionListener l : ww.getMouseMotionListeners()) {
    if(l instanceof AWTInputHandler) {
        wwHandler = (AWTInputHandler)l;
        break;
    }
}

创建一个使用事件的MouseMotionListener

public class MyMouseMotionListener implements MouseMotionListener {
    @Override
    public void mouseDragged(MouseEvent e) {
        // consume the event so the globe position does not change
        e.consume();
        if (e.getSource() instanceof WorldWindowGLCanvas) {
            // get the position of the mouse
            final WorldWindowGLCanvas canvas = ((WorldWindowGLCanvas) e.getSource());
            final Position p = canvas.getCurrentPosition();
            // do something with the position here
        }
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        e.consume();
    }
}

将鼠标运动监听器添加到AWTInputHandler

if(wwHandler != null) {
    wwHandler.addMouseMotionListener(new MyMouseMotionListener());
} else {
    // I don't think this should happen unless the AWTInputHandler
    // is explicitly removed by client code
    logger.error("Couldn't find AWTInputHandler");
}

也就是说,我不知道为什么WorldWindowGLCanvas 使用Component.addMouseMotionListener() 而不是AWTInputHandler.addMouseMotionListener()

【讨论】:

    【解决方案2】:

    为什么它不起作用:

    正如here 所讨论的,许多事件源都维护EventListenerList。规定的方案允许添加或删除任意数量的侦听器。这个相关的example 列出了注册到文本组件Document 的所有DocumentListener 实例。没有一个听众会抢占另一个听众。

    你可能会做什么:

    给定一个WorldWindowGLCanvas 的实例,您可以查看getMouseMotionListeners() 返回的数组并根据需要调用removeMouseMotionListener()

    【讨论】:

      【解决方案3】:

      在努力解决这个问题并偶然发现这个解决方案之后,我相信我已经想出了“正确”的解决方案。完全移除鼠标运动侦听器在技术上确实可行,但它会破坏其他可能有用的功能(KML 树节点选择,屏幕视图控件)。

      创建一个输入处理程序的子类以删除“移动到”功能

      public class MyOrbitViewInputHandler extends OrbitViewInputHandler
      {
        public MyOrbitViewInputHandler()
        {
          // Disable single click to pan functionality
          ViewInputAttributes.ActionAttributes actionAttrs =
            this.getAttributes().getActionMap(ViewInputAttributes.DEVICE_MOUSE).getActionAttributes(ViewInputAttributes.VIEW_MOVE_TO);
          actionAttrs.setMouseActionListener(null);
        }
      }
      

      在 worldwind.xml 配置文件中指定新的输入处理程序

      <Property name="gov.nasa.worldwind.avkey.ViewInputHandlerClassName"
                value="mil.dtra.nucs.coe.ui.map.MyOrbitViewInputHandler"/>
      

      使用此方法,画布上发生的所有其他鼠标交互仍将正常工作,但删除了“单击平移”功能。

      如果您想知道您可能会覆盖哪些其他行为,您可以查看 gov.nasa.worldwind.awt.BasicViewInputHandler

      【讨论】:

        猜你喜欢
        • 2012-07-09
        • 2017-11-29
        • 1970-01-01
        • 2017-04-12
        • 2013-01-12
        • 2011-08-14
        • 2013-12-01
        • 1970-01-01
        • 2011-09-24
        相关资源
        最近更新 更多