Hey,
I’m trying to figure it out 2 things:
1 - If the mouse is currently inside the jme window
2 - If the jme window is focused
I failed to think a way I could achieve the first one. On the other hand, in the second I could override the Applicattion focus methods, call the super and put a flag on mine, but anyone else knows another way?
Have you looked at the native Java WindowFocusListener? I am not sure if it supports the lwjgl display jMonkey uses as default but you can use a JFrame and set the applications context to the contentpane of JFrame and then use the window listener to check whether the window has been de/activated.
The first one i am not sure myself I guess you need to catch mouse move events and manually set flags if the mouse position goes out of the window bounds (mouse.x < 0 && mouse.x > window.width) or something like that. Anyways this will only work if your application has focus, I think. Or else the currently focused window or the system will handle mouse events and I dont know how or if it is possible to pass them back to you application.
I suggest looking at WindowListener, WindowFocusListener and MouseListener, set some flags and play around with it till you have what you want. Maybe also the application method setPauseOnLostFocus(boolean) can help, depending on what you functionality you intend to get.
This doesn’t seem the easist way to do that, may there’s one more simple?
I thought about that a little more. This is what came out of it.
For the first one you need the system mouse position compared to the display size and position to check that. You can use this:
[java]
public static boolean mouseInWindow() {
int mx = MouseInfo.getPointerInfo().getLocation().x;
int my = MouseInfo.getPointerInfo().getLocation().y;
int minX = Display.getX();
int minY = Display.getY();
int maxX = minX + Display.getWidth();
int maxY = minY + Display.getHeight();
return !(mx < minX || mx > maxX || my < minY || my > maxY);
}
[/java]
The only thing, this is also true if another window is overlapping the display. There is a method Display.isVisible() but I think it rather has something to do with whether it is displayable or not and it depends on kind of some weird state of the renderer or system stuff I don’t know much about. So maybe you or someone can extend this to also check if view on the display is free. If so please share. But I think this could be quite useful for many cases anyways.
The second one is even more easy. You can easily change the executed code within update function using the setPauseOnLostFocus(false). You can than distinguish between code execution depending on whether your display is active or not:
[java]
public void simpleInitApp() {
setPauseOnLostFocus(false);
}
public void simpleUpdate(final float tpf) {
if (Display.isActive()) {
//some code to execute when focused
} else {
//some code to execute when not focused;
}
}
[/java]
Thats the easiest way I can think of by now.
Hey, thanks!
That Display.isActive is just what I needed, and different from the InputManager.getCursorPosition, MouseInfo does update even if the cursor is outside the jme window, so this is what I’ll be using. The only thing is that Display has no public API to grab the current X and Y like you said. It does has a private method to getWindowX and Y, but they are private. Now I need a way to turn that around. I really don’t know how you found Display class ^^
Hmm that was something I encountered at the very first when I started to play around with jMonkey. Maybe you imported the wrong one. There are indeed several in different libraries. It is Display from the lwjgl library (import org.lwjgl.opengl.Display;) and here is the javadoc http://www.lwjgl.org/javadoc/.
jMonkey uses it as its default display if you don’t grab the context and put it somewhere yourself.
The input.getcursorposition I thought about, too. But it is the position of the mouse inside you application relative to the world coordinates. And somehow extracts the 2d mouse position from the current 3d world coordinates and camera perspective or viewport - I am not sure about that. So checking it ouside the window would not make sense as you cannot see what you are pointing at.
Display.getX() and getY() work, the problem is that, at least in Linux, the returned coordinates include the window titlebar while getHeight() does not, then when you calculate maxY the result is a bit before the true bottom border of the window.
I’m trying to implement an RTE style camera, the camera moves when the pointer is near any of the four borders of the screen. In full screen this simply works, but in windowed mode I need to check the mouse position each game update and if it is outside the window stop the movement, in the x axis getX and getY are reliable, but for the y axis it starts a bit up of the game canvas (width of the titlebar).
Now this can be easy solved if I find a multiplatform way of check the width of the titlebar and add it to the value returned by getY.