evil-inside/src/ei/engine/input/MouseInput.java

96 lines
2.5 KiB
Java

package ei.engine.input;
import org.lwjgl.input.Mouse;
import ei.engine.LWJGLGameWindow;
import ei.engine.math.Vector2f;
import ei.engine.scene.Sprite;
/**
* Handles the mous input
* @author Ziver
*
*/
public abstract class MouseInput extends Input{
// The x pos of the mouse
private int cursorX;
// The y pos of the mouse
private int cursorY;
// The texute for the mouse
private Sprite cursor;
public MouseInput(String name) {
super(name);
// init mouse: this will hide the native cursor (see drawCursor())
Mouse.setGrabbed(true);
// set initial cursor pos to center screen
cursorX = (int) LWJGLGameWindow.getWidth() / 2;
cursorY = (int) LWJGLGameWindow.getHeight() / 2;
}
/**
* Renders the mous texture if texture set
*/
public void render() {
if(cursor != null){
cursor.render();
}
}
/**
* 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();
}
mouseMove(cursorX,cursorY);
}
while ( Mouse.next() ) {
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState() == true) {
mouseDown(cursorX, cursorY);
}
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState() == false) {
mouseUp(cursorX, cursorY);
}
}
if(cursor != null){
cursor.setLocation(new Vector2f(cursorX, cursorY));
}
}
/**
* Called by update() when mouse moves
*/
public abstract void mouseMove(int x, int y);
/**
* Called by update() when mouse button is pressed
*/
public abstract void mouseDown(int x, int y);
/**
* Called by update() when mouse button is released
*/
public abstract void mouseUp(int x, int y);
}