Added input but is not implemented in the engine yet
This commit is contained in:
parent
d3a6c31dd1
commit
aeb3f9a877
4 changed files with 307 additions and 0 deletions
44
src/ei/engine/input/Input.java
Normal file
44
src/ei/engine/input/Input.java
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
package ei.engine.input;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class Input{
|
||||||
|
// The name of this input
|
||||||
|
private String name;
|
||||||
|
// is this input enabled
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
|
public Input(String name){
|
||||||
|
this.enabled = true;
|
||||||
|
InputHandler.getInstance().addInput(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The name of the entity
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns if this input is enabled
|
||||||
|
*
|
||||||
|
* @return if this input is enabled
|
||||||
|
*/
|
||||||
|
public boolean isEnabled(){
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set if this input is enabled
|
||||||
|
*
|
||||||
|
* @param b if this input is enabled
|
||||||
|
*/
|
||||||
|
public void setEnabled(boolean b){
|
||||||
|
enabled = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public abstract void update();
|
||||||
|
|
||||||
|
public abstract void render();
|
||||||
|
}
|
||||||
110
src/ei/engine/input/InputHandler.java
Normal file
110
src/ei/engine/input/InputHandler.java
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
package ei.engine.input;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class handels all the input in the engine
|
||||||
|
*
|
||||||
|
* @author Ziver
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class InputHandler {
|
||||||
|
// The instance of this handler
|
||||||
|
private static InputHandler instance;
|
||||||
|
// The array of inputs
|
||||||
|
private ArrayList<Input> input;
|
||||||
|
|
||||||
|
public InputHandler(){
|
||||||
|
input = new ArrayList<Input>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* updates all the inputs
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void update(){
|
||||||
|
for(int i=0; i<input.size() ;i++){
|
||||||
|
input.get(i).update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* renders all the inputs
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void render(){
|
||||||
|
for(int i=0; i<input.size() ;i++){
|
||||||
|
input.get(i).render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a input
|
||||||
|
*
|
||||||
|
* @param i the input
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean addInput(Input i){
|
||||||
|
if(!input.contains(i)){
|
||||||
|
input.add(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a input from the handler
|
||||||
|
*
|
||||||
|
* @param i The input to remove
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean removeInput(Input i){
|
||||||
|
if(input.contains(i)){
|
||||||
|
input.remove(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove input from handler by
|
||||||
|
* index in handler
|
||||||
|
*
|
||||||
|
* @param i The index of the input
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean removeInput(int i){
|
||||||
|
if(i >= 0 && i < input.size()){
|
||||||
|
input.remove(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a input by name
|
||||||
|
*
|
||||||
|
* @param name The name of the input to remove
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean removeInput(String name){
|
||||||
|
for(int i=0; i<input.size() ;i++){
|
||||||
|
if(input.get(i).getName().equals(name)){
|
||||||
|
return removeInput(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an insance of this class
|
||||||
|
*
|
||||||
|
* @return an insance of this class
|
||||||
|
*/
|
||||||
|
public static InputHandler getInstance(){
|
||||||
|
if(instance == null){
|
||||||
|
instance = new InputHandler();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/ei/engine/input/KeyboardInput.java
Normal file
57
src/ei/engine/input/KeyboardInput.java
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package ei.engine.input;
|
||||||
|
|
||||||
|
import org.lwjgl.LWJGLException;
|
||||||
|
import org.lwjgl.input.Keyboard;
|
||||||
|
|
||||||
|
import ei.engine.util.MultiPrintStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class handles key inputs
|
||||||
|
* @author Ziver
|
||||||
|
*/
|
||||||
|
public abstract class KeyboardInput extends Input {
|
||||||
|
|
||||||
|
public KeyboardInput(String name) {
|
||||||
|
super(name);
|
||||||
|
try {
|
||||||
|
//init keyboard
|
||||||
|
Keyboard.create();
|
||||||
|
} catch (LWJGLException e) {
|
||||||
|
MultiPrintStream.out.println("Keyboard Init Erorr: "+e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is not used for key input.
|
||||||
|
*/
|
||||||
|
public void render() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* looks if any key is presed
|
||||||
|
*/
|
||||||
|
public void update() {
|
||||||
|
// Handle key hits
|
||||||
|
while ( Keyboard.next() ) {
|
||||||
|
// pass key event to handler
|
||||||
|
if (Keyboard.getEventKeyState()) {
|
||||||
|
keyDown(Keyboard.getEventKey());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
keyUp(Keyboard.getEventKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by update() when any key is presed
|
||||||
|
*/
|
||||||
|
public abstract void keyDown(int keycode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by update() when any key is released
|
||||||
|
*/
|
||||||
|
public abstract void keyUp(int keycode);
|
||||||
|
|
||||||
|
}
|
||||||
96
src/ei/engine/input/MouseInput.java
Normal file
96
src/ei/engine/input/MouseInput.java
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue