Hello!
I’m trying to implement an RTS-like selection boxes, but I’ve got stuck when trying to implement mouse dragging, so that I can draw the rectangle on the screen.
First, I registered the following mappings:
[java]this.inputManager.addMapping(“Pick”, new MouseButtonTrigger(MouseInput.
BUTTON_LEFT));
this.inputManager.addMapping(“PickMouseMove”,
new MouseAxisTrigger(MouseInput.AXIS_X, true),
new MouseAxisTrigger(MouseInput.AXIS_X, false),
new MouseAxisTrigger(MouseInput.AXIS_Y, true),
new MouseAxisTrigger(MouseInput.AXIS_Y, false)
);[/java]
And here’s my input listener code:
[java]private static class ApplicationInput implements AnalogListener,
ActionListener {
private final SimpleApplication app;
private boolean dragging = false;
private Vector2f dragStart;
public ApplicationInput(final SimpleApplication application) {
this.app = application;
}
@Override
public void onAnalog(String name, float value, float tpf) {
switch (name) {
// .. omitted ..
case "PickMouseMove": {
if (this.dragging) {
// Here's where it gets weird.
System.out.printf("%s - %s\n", this.dragStart.toString(),
app.getInputManager().getCursorPosition().toString());
}
break;
}
}
}
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Pick")) {
if (!isPressed) {
this.dragging = false;
// .. omitted ..
} else {
if (!this.dragging) {
this.dragging = true;
this.dragStart = app.getInputManager().
getCursorPosition();
}
}
}
}
}[/java]
I noticed that getCursorPosition() always returns the same coordinates, even though the mouse is moving.
Do you have any idea why the above is happening? I suspect that I may be using the listeners wrong, please let me know if that’s the case.
Thank you,
Patryk.