Mouse Focus Problem

Hey all…me again :wink:



So I'm starting to play around with the UI…and I've decided to give fengGUI a try and I've come across a problem.



I created a separate gamestate for the UI, but unfortunately, can't seem to get my mouse to not interact with the game when it is focused on the window or buttons I have created. Here's the code of the UI gamestate:





import org.fenggui.Button;
import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.RadioButton;
import org.fenggui.ToggableGroup;
import org.fenggui.composites.Window;
import org.fenggui.layout.BorderLayout;
import org.fenggui.layout.BorderLayoutData;
import org.fenggui.layout.GridLayout;
import org.fenggui.layout.RowLayout;
import org.fenggui.render.lwjgl.LWJGLBinding;
import org.fenggui.util.Point;
import org.fenggui.util.Spacing;

import utils.FengJMEInputHandler;

import com.jme.image.Texture;
import com.jme.input.Mouse;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.Timer;
import com.jmex.game.state.GameState;
import com.jme.renderer.ColorRGBA;
 
/**
 * FengJME - A test class for integrating FengGUI and jME.
 *
 * @author Josh (updated by neebie)
 *
 */
public class InGameUI extends GameState
{
   protected FengJMEInputHandler input = null;
   protected Display disp = null;
   protected Timer timer;
   protected static boolean focus;
   protected Window window;
    //protected Mouse mouse;

   public InGameUI() {
      
      initGUI();
      
   }

   /**
    * Create our GUI.  FengGUI init code goes in here
    *
    */
   protected void initGUI()
   {
      // Grab a display using an LWJGL binding
      //      (obviously, since jME uses LWJGL)
      final int width = DisplaySystem.getDisplaySystem().getWidth();
        final int height = DisplaySystem.getDisplaySystem().getHeight();
       
        disp = new Display(new LWJGLBinding());
        input = new FengJMEInputHandler(disp);
       
        window = FengGUI.createWindow(disp, true, false, false, true);
        window.setTitle("Test Menu");
        window.setPosition(new Point(50,200));
        window.getContentContainer().setLayoutManager(new RowLayout(false));
        window.getContentContainer().getAppearance().setPadding(new Spacing(10, 10));
       
        // center the window on the screen
        window.setX(width/2 - window.getWidth()/2);
        window.setY(0);
            
        final ToggableGroup<String> toggableGroup = new ToggableGroup<String>();
        RadioButton<String> radioButtonCoffee = new RadioButton<String>("coffee", toggableGroup, "coffee");
        RadioButton<String> radioButtonTea = new RadioButton<String>("tea", toggableGroup, "tea");
        radioButtonTea.setSelected(true);
       
        Button button1 = new Button("Guns!");
        window.getContentContainer().addWidget(button1);
        Button button2 = new Button("Laser");
        window.getContentContainer().addWidget(button2);
        Button button3 = new Button("Missile");
        window.getContentContainer().addWidget(button3);
       
       
     // container for Radio Buttons
        Container container = new Container(new RowLayout(true));
        container.addWidget(radioButtonCoffee);
        container.addWidget(radioButtonTea);
        window.getContentContainer().addWidget(container);
       
        disp.layout();
        //MouseInput.get().setCursorVisible(true);
   }

   @Override
   public void cleanup() {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void render(float tpf) {
      // TODO Auto-generated method stub
      Texture defTex = TextureState.getDefaultTexture().createSimpleClone();
        defTex.setScale(new Vector3f(1, 1, 1));
        TextureState defaultTextureState = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        defaultTextureState.setTexture(defTex);
        defaultTextureState.apply();
       
        // render the GUI
        disp.display();
      
   }

   @Override
   public void update(float tpf) {
      // TODO Auto-generated method stub
      input.update(tpf);
      
      if (window.hasFocus()) {
           InGameUI.setFocus(true);
        }else {
           InGameUI.setFocus(false);
        }
      
      System.out.println("focus is: " + InGameUI.isFocus());
   }

   public static boolean isFocus() {
      return focus;
   }

   public static void setFocus(boolean focus) {
      InGameUI.focus = focus;
   }
 
}



As you can see, I created a static boolean to track whether or not my window is in focus. In MouseClickToMove class (...the class that controls mouse click to move of my "ship"..) I check for that boolean's value like this:



      if (isPressed == 0 && InGameUI.isFocus() == false) {
         final Plane p1 = new Plane();
         // Plane p2 = new Plane();

         // Vector3f loc2 = new Vector3f();
         final Vector3f t1a = new Vector3f(0, 0, 0); // point a on Triangle
         // 1;
         final Vector3f t1b = new Vector3f(1, 0, 0); // point b on Triangle
         // 1;
         final Vector3f t1c = new Vector3f(0, 0, 1); // point c on Triangle
         // 1;

         final Triangle t1 = new Triangle(t1a, t1b, t1c);
         p1.setPlanePoints(t1);



...that's only part of the MouseClickToMove class...but for some reason, the "focus" variable is always false. I've manually set it to true, so if I was reading the flag correctly, the ship wouldn't move...so I'm wondering if the hasFocus() function in fengGUI is the function I really want to check here.

...any advice?

I am not sure if the is/hasFocus functions would help you, since the GUI is always focused regardless of where your mouse is.

What you can try doing is placing a panel behind the whole GUI that covers the screen, and then moving the ship when the mouse clicks that panel (use a MouseClickListener or whatever FengGUI has), that would only work if FengGUI correctly processes the clicks to the top-most component…