问题描述
我试图在 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()
作为参数将阻止所有其他 MouseMotionListener
来自接收事件.应改用 AWTInputHandler
上的添加和删除方法.
不幸的是,按照 @trashgod 的建议删除 MouseMotionListener
不起作用,因为发生了一些 World Wind 特定行为:删除 gov.nasa.worldwind.awt.AWTInputHandler
导致其他 MouseMotionListener
停止接收事件通知.
要禁用地球拖动并仍然在另一个 MouseMotionListener
中接收事件,需要执行以下步骤:
获取对 World Wind 的 AWTInputHandler
的引用:
AWTInputHandler wwHandler = null;//获取 World Wind 的 AWTInputHandler 类:for (MouseMotionListener l : ww.getMouseMotionListeners()) {if(l instanceof AWTInputHandler) {wwHandler = (AWTInputHandler)l;休息;}}
创建一个使用事件的MouseMotionListener
:
public class MyMouseMotionListener 实现 MouseMotionListener {@覆盖public void mouseDragged(MouseEvent e) {//消耗事件,所以地球位置不会改变e.consum();if (e.getSource() instanceof WorldWindowGLCanvas) {//获取鼠标位置final WorldWindowGLCanvas canvas = ((WorldWindowGLCanvas) e.getSource());最终位置 p = canvas.getCurrentPosition();//对这里的位置做一些事情}}@覆盖public void mouseMoved(MouseEvent e) {e.consum();}}
在AWTInputHandler
中添加鼠标动作监听器:
if(wwHandler != null) {wwHandler.addMouseMotionListener(new MyMouseMotionListener());} 别的 {//我不认为这应该发生,除非 AWTInputHandler//被客户端代码显式删除logger.error("找不到AWTInputHandler");}
也就是说,我不知道为什么 WorldWindowGLCanvas
使用 Component.addMouseMotionListener()
而不是 AWTInputHandler.addMouseMotionListener()
.>
I am trying to disable movement of the globe on mouse click in World Wind. I expected to be able to do:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
ww.addMouseMotionListener(new MyMouseMotionListener());
}
where MyMouseMotionListener
consumes all of the mouse events. This is not working so instead I have to do:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
for(MouseMotionListener l : ww.getMouseMotionListeners()) {
if(l.getClass().toString().equals("class gov.nasa.worldwind.awt.AWTInputHandler")) {
ww.removeMouseMotionListener(l);
}
}
}
Is it expected that consumed mouse events should still reach the gov.nasa.worldwind.awt.AWTInputHandler
listener?
Update: WorldWindowGLCanvas
is just calling addMouseMotionListener()
on java.awt.Component
so apparently I don't understand how consuming events works.
Update 2: despite sharing interfaces with Swing, calling WorldWindowGLCanvas.removeMouseMotionListener()
with AWTInputHandler
as the argument will prevent all other MouseMotionListener
s from receiving events. The add and remove methods on AWTInputHandler
should be used instead.
Unfortunately removing the MouseMotionListener
as @trashgod suggested does not work since there is some World Wind specific behavior happening: removing gov.nasa.worldwind.awt.AWTInputHandler
causes other MouseMotionListener
s to stop receiving event notifications.
To disable globe dragging and still receive events in another MouseMotionListener
the following steps were necessary:
Get a reference to World Wind's AWTInputHandler
:
AWTInputHandler wwHandler = null;
// get World Wind's AWTInputHandler class:
for (MouseMotionListener l : ww.getMouseMotionListeners()) {
if(l instanceof AWTInputHandler) {
wwHandler = (AWTInputHandler)l;
break;
}
}
Create a MouseMotionListener
which consumes events:
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();
}
}
Add the mouse motion listener to the 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");
}
That said, I have no idea why WorldWindowGLCanvas
is using Component.addMouseMotionListener()
rather than AWTInputHandler.addMouseMotionListener()
.
这篇关于在 World Wind 中点击时禁用地球运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!