2007-03-25 21:04:27 +00:00
|
|
|
package ei.engine.input;
|
|
|
|
|
|
|
|
|
|
import org.lwjgl.input.Mouse;
|
2007-03-29 23:21:00 +00:00
|
|
|
import org.lwjgl.opengl.GL11;
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
import ei.engine.LWJGLGameWindow;
|
|
|
|
|
import ei.engine.math.Vector2f;
|
2007-03-29 23:21:00 +00:00
|
|
|
import ei.engine.math.Vector3f;
|
2007-03-25 21:04:27 +00:00
|
|
|
import ei.engine.scene.Sprite;
|
|
|
|
|
|
|
|
|
|
/**
|
2007-03-26 08:48:36 +00:00
|
|
|
* Handles the mouse input
|
2007-03-25 21:04:27 +00:00
|
|
|
* @author Ziver
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public abstract class MouseInput extends Input{
|
2007-03-29 23:21:00 +00:00
|
|
|
public static final int LEFT_MOUSE_BUTTON = 0;
|
|
|
|
|
public static final int RIGHT_MOUSE_BUTTON = 1;
|
|
|
|
|
public static final int MIDDLE_MOUSE_BUTTON = 2;
|
2007-03-25 21:04:27 +00:00
|
|
|
// The x pos of the mouse
|
2007-03-26 08:48:36 +00:00
|
|
|
private static int cursorX;
|
2007-03-25 21:04:27 +00:00
|
|
|
// The y pos of the mouse
|
2007-03-26 08:48:36 +00:00
|
|
|
private static int cursorY;
|
2007-03-25 21:04:27 +00:00
|
|
|
// The texute for the mouse
|
|
|
|
|
private Sprite cursor;
|
|
|
|
|
|
|
|
|
|
public MouseInput(String name) {
|
|
|
|
|
super(name);
|
|
|
|
|
// init mouse: this will hide the native cursor (see drawCursor())
|
2007-03-29 23:21:00 +00:00
|
|
|
Mouse.setGrabbed(false);
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
// set initial cursor pos to center screen
|
|
|
|
|
cursorX = (int) LWJGLGameWindow.getWidth() / 2;
|
|
|
|
|
cursorY = (int) LWJGLGameWindow.getHeight() / 2;
|
|
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
|
|
|
|
|
public MouseInput(String name,String texture){
|
|
|
|
|
super(name);
|
|
|
|
|
// init mouse: this will hide the native cursor (see drawCursor())
|
|
|
|
|
Mouse.setGrabbed(true);
|
|
|
|
|
cursor = new Sprite(name+" spatial",texture);
|
|
|
|
|
// set initial cursor pos to center screen
|
|
|
|
|
cursorX = (int) LWJGLGameWindow.getWidth() / 2;
|
|
|
|
|
cursorY = (int) LWJGLGameWindow.getHeight() / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the sprite of th mouse
|
|
|
|
|
*
|
|
|
|
|
* @return The sprite of the mouse
|
|
|
|
|
*/
|
|
|
|
|
public Sprite getSprite(){
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renders the mous texture if texture set
|
|
|
|
|
*/
|
|
|
|
|
public void render() {
|
|
|
|
|
if(cursor != null){
|
2007-03-29 23:21:00 +00:00
|
|
|
GL11.glPushMatrix();
|
|
|
|
|
Vector3f v = LWJGLGameWindow.getCamera().getLocation();
|
|
|
|
|
GL11.glTranslatef(v.getX(),v.getY(), v.getZ());
|
|
|
|
|
cursor.render();
|
|
|
|
|
GL11.glPopMatrix();
|
2007-03-25 21:04:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the pos of the mouse and send any
|
|
|
|
|
* change to mouseMove(), mouseDown() or mouseUp()
|
|
|
|
|
*/
|
|
|
|
|
public void update() {
|
|
|
|
|
int mouseDX = Mouse.getDX();
|
|
|
|
|
int mouseDY = Mouse.getDY();
|
|
|
|
|
int mouseDW = Mouse.getDWheel();
|
|
|
|
|
if (mouseDX != 0 || mouseDY != 0 || mouseDW != 0) {
|
|
|
|
|
cursorX += mouseDX;
|
|
|
|
|
cursorY += mouseDY;
|
|
|
|
|
if (cursorX < 0) {
|
|
|
|
|
cursorX = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (cursorX > LWJGLGameWindow.getWidth()) {
|
|
|
|
|
cursorX = LWJGLGameWindow.getWidth();
|
|
|
|
|
}
|
|
|
|
|
if (cursorY < 0) {
|
|
|
|
|
cursorY = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (cursorY > LWJGLGameWindow.getHeight()) {
|
|
|
|
|
cursorY = LWJGLGameWindow.getHeight();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
mouseUpdate(cursorX,cursorY,mouseDW);
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
while ( Mouse.next() ) {
|
2007-03-29 23:21:00 +00:00
|
|
|
if(Mouse.getEventButton() >= 0 && Mouse.getEventButtonState() == true) {
|
|
|
|
|
mouseDown(Mouse.getEventButton(),cursorX, cursorY);
|
2007-03-25 21:04:27 +00:00
|
|
|
}
|
2007-03-29 23:21:00 +00:00
|
|
|
if(Mouse.getEventButton() >= 0 && Mouse.getEventButtonState() == false) {
|
|
|
|
|
mouseUp(Mouse.getEventButton(),cursorX, cursorY);
|
2007-03-25 21:04:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(cursor != null){
|
2007-03-29 23:21:00 +00:00
|
|
|
cursor.setLocation(new Vector2f(cursorX,
|
|
|
|
|
LWJGLGameWindow.getHeight()-cursorY));
|
2007-03-25 21:04:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called by update() when mouse moves
|
|
|
|
|
*/
|
2007-03-29 23:21:00 +00:00
|
|
|
public abstract void mouseUpdate(int x, int y, int w);
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called by update() when mouse button is pressed
|
|
|
|
|
*/
|
2007-03-29 23:21:00 +00:00
|
|
|
public abstract void mouseDown(int event,int x, int y);
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called by update() when mouse button is released
|
|
|
|
|
*/
|
2007-03-29 23:21:00 +00:00
|
|
|
public abstract void mouseUp(int event,int x, int y);
|
2007-03-25 21:04:27 +00:00
|
|
|
|
|
|
|
|
}
|