Switching Cursor On and Off

Hello all,



I'm working on a project using the FirstPersonHandler and FengGUI…  The problem of course is when the mouse cursor should be displayed vs. when the mouse should be feeding into FirstPersonHandler.



I solved this problem halfway by using:

FirstPersonHandler.setButtonPressRequired(true);





Now I'm simply trying to have the cursor turn on and off according to if the mouse button is held down.  To turn the cursor on or off statically, I've found this to work:


MouseInput.get().setCursorVisible(true);



but was unsuccessful in trying to set this up dynamically in the update of one of my GameStates:

// Decide if the mouse cursor should be displayed.
      if(MouseInput.get().isButtonDown(0))
      {
         MouseInput.get().setCursorVisible(false);
      }
      else
                {
         MouseInput.get().setCursorVisible(false);
                 }



Any ideas?  I feel defeated and tame :'(

The code you posted always makes the mouse invisible…


// Decide if the mouse cursor should be displayed.
if(MouseInput.get().isButtonDown(0))
{
        MouseInput.get().setCursorVisible(true);
}
else
{
        MouseInput.get().setCursorVisible(false);
}



The change I made here should set it to be visible when the left-button is down.

Lol, I hate it when that happens.

oh boy, now that's embarrassing haha… so I put in the change and it still doesn't work :frowning:



back to the drawing board I go, thanks for the catch folks

debug or make a system.out.println() when you set the cursor visible / invisible, to see if your code works

Core-Dump said:

debug or make a system.out.println() when you set the cursor visible / invisible, to see if your code works


I've encountered several instances where the JVM literally skips a line of code, and it has to be entered twice before it takes effect. So this is definitely a good idea. It's also possible that Feng is messing with your cursor, but I'm not sure how that would happen...
Trussell said:

I've encountered several instances where the JVM literally skips a line of code, and it has to be entered twice before it takes effect.


What? I cannot believe that, It would have been a very serious error in the JVM. Maybe the compiler might accidentally skip a line, but that would be a very bad compiler as well and not very likely.

How did your bytecode look like, was it skipped as well for the line?

I don't know. It's happened twice, and both times I just resolved it by adding the line over again and it worked. It happened once in my first year as a programming student, and again in my second year.

if its a system.out that has been 'skipped' it was probably a flushing problem.





Enabling/disabling the cursor seems to work fine, the only problem is, that the mouse jumps to its initial position when making the cursor visible again, but thats another story.



Is the following code not working together with FengGUI ?



import com.jme.bounding.BoundingBox;
import com.jme.input.FirstPersonHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jme.system.DisplaySystem;
import com.jmex.editors.swing.settings.GameSettingsPanel;
import com.jmex.game.StandardGame;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.GameStateManager;

public class TestStandardGame {
   public static void main(String[] args) throws Exception {
       System.setProperty("jme.stats", "set");
      StandardGame game = new StandardGame("A Simple Test");
      if (GameSettingsPanel.prompt(game.getSettings())) {
         game.start();
         GameStateManager.getInstance().attachChild(new MyGameState(game));
      }
   }
}

class MyGameState extends BasicGameState {
   private FirstPersonHandler fph;
   StandardGame game;
   public MyGameState(StandardGame game) {
      super("my_gs");
      this.game = game;
      setActive(true);
      
      Box b = new Box("b", new Vector3f(), 1,1,1);
      b.setModelBound(new BoundingBox());
      b.updateModelBound();
      rootNode.attachChild(b);
      fph = new FirstPersonHandler(DisplaySystem.getDisplaySystem().getRenderer().getCamera());
      fph.setButtonPressRequired(true);
      KeyBindingManager.getKeyBindingManager().add("exit", KeyInput.KEY_ESCAPE);
      DisplaySystem.getDisplaySystem().getRenderer().getCamera().getLocation().z -= 2;
   }
   @Override
   public void update(float tpf) {
      super.update(tpf);
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit")) {
         game.finish();
      }
      fph.update(tpf);
      if (MouseInput.get().isButtonDown(0)) {
         MouseInput.get().setCursorVisible(true);
      } else {
         MouseInput.get().setCursorVisible(false);
      }
      System.out.println("Y abs:" +MouseInput.get().getXAbsolute());
      System.out.println("Y delta:" +MouseInput.get().getXDelta());
   }
}

Nah it was logic code. The first one was changing a boolean.