I have a game where movement of the player is performed via point-and-click. I also am using JMEDesktop where I make it the size of the display and place buttons at absolute positions.
My problem is whenever I click on a button, and if the terrain is behind it, I get both an action from the button (which is correct) and the player is moved to the point on the terrain where the clicking occurred. Obviously, I don't want any picking to occur if a Swing component was clicked…even if pickable Geometry exists behind the component.
Is there a good way to prevent these 2 events from occurring with 1 click?
For reference, I copied the usage of JMEDesktop as found in HelloJMEDesktop so I have:
public void setupGUI() {
guiNode = new Node("guiNode");
guiNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
final JMEDesktop desktop = new JMEDesktop("desktop", display.getWidth(), display.getHeight(), input);
guiNode.attachChild(desktop);
// center desktop on screen
desktop.getLocalTranslation().set(display.getWidth()/2, display.getHeight()/2, 0);
// perform all the swing stuff in the swing thread
// (Only access the Swing UI from the Swing event dispatch thread!
// See SwingUtilities.invokeLater()
// and http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html for details.)
SwingUtilities.invokeLater( new Runnable() {
public void run() {
// make it transparent blue
desktop.getJDesktop().setBackground( new Color( 0, 0, 1, 0.0f ) );
// create a swing button
final JButton button = new JButton( "Click Me" );
// and put it directly on the desktop
desktop.getJDesktop().add( button );
// desktop has no layout - we layout ourselves (could assign a layout to desktop here instead)
button.setLocation( 200, 200 );
button.setSize( button.getPreferredSize() );
// add some actions
// standard swing action:
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
// this gets executed in swing thread
// alter swing components ony in swing thread!
button.setLocation( FastMath.rand.nextInt( display.getWidth() ), FastMath.rand.nextInt( display.getHeight()) );
}
} );
// action that gets executed in the update thread:
button.addActionListener( new JMEAction( "my action", input ) {
public void performAction( InputActionEvent evt ) {
// this gets executed in jme thread
// do 3d system calls in jme thread only!
guiNode.updateRenderState(); // this call has no effect but should be done in jme thread :)
}
});
}
} );
// don't cull the gui away
guiNode.setCullMode( SceneElement.CULL_NEVER );
// gui needs no lighting
guiNode.setLightCombineMode( LightState.OFF );
// update the render states (especially the texture state of the deskop!)
guiNode.updateRenderState();
// update the world vectors (needed as we have altered local translation of the desktop and it's
// not called in the update loop)
guiNode.updateGeometricState( 0, true );
}
For the picking, I simply have a MouseInputListener and the picking occurs in the onButton method.