DEADSOFTWARE

da3e9e43d9be2deaf71a0454ae92f0a92d288519
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / InputHandler.java
1 package ru.deadsoftware.cavecraft;
3 import com.badlogic.gdx.Input;
4 import com.badlogic.gdx.InputProcessor;
5 import ru.deadsoftware.cavecraft.game.GameInputHandler;
6 import ru.deadsoftware.cavecraft.game.GameProc;
8 import static ru.deadsoftware.cavecraft.GameScreen.getHeight;
9 import static ru.deadsoftware.cavecraft.GameScreen.getWidth;
11 public class InputHandler implements InputProcessor {
13 private GameProc gameProc;
14 private GameInputHandler gameInput;
16 private float tWidth, tHeight;
18 public InputHandler(GameProc gameProc) {
19 this.gameProc = gameProc;
20 this.gameInput = new GameInputHandler(gameProc);
21 tWidth = gameProc.renderer.camera.viewportWidth;
22 tHeight = gameProc.renderer.camera.viewportHeight;
23 }
25 @Override
26 public boolean keyDown(int keycode) {
27 gameInput.keyDown(keycode);
28 return false;
29 }
31 @Override
32 public boolean keyUp(int keycode) {
33 gameInput.keyUp(keycode);
34 return false;
35 }
37 @Override
38 public boolean keyTyped(char character) {
39 return false;
40 }
42 @Override
43 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
44 int tx = (int)(screenX * tWidth/getWidth());
45 int ty = (int)(screenY * tHeight/getHeight());
46 screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
47 screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
49 if (CaveGame.TOUCH) {
50 if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
51 if (gameProc.ctrlMode==1) gameInput.keyDown(Input.Keys.W);
52 else gameInput.keyDown(Input.Keys.SPACE);
53 } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
54 gameInput.keyDown(Input.Keys.A);
55 } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
56 if (gameProc.ctrlMode==1) gameInput.keyDown(Input.Keys.S);
57 else gameInput.keyDown(Input.Keys.CONTROL_LEFT);
58 } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
59 gameInput.keyDown(Input.Keys.D);
60 } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
61 gameInput.keyDown(Input.Keys.ALT_LEFT);
62 } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
63 gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
64 } else if (tx > tWidth - 26 && screenY > ty - 26) {
65 gameInput.touchDown(screenX, screenY, Input.Buttons.RIGHT);
66 } else {
67 gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
68 }
69 } else {
70 gameInput.touchDown(screenX, screenY, button);
71 }
72 return false;
73 }
75 @Override
76 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
77 int tx = (int)(screenX * tWidth/getWidth());
78 int ty = (int)(screenY * tHeight/getHeight());
79 screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
80 screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
81 if (CaveGame.TOUCH) {
82 if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
83 if (gameProc.ctrlMode==1) gameInput.keyUp(Input.Keys.W);
84 else gameInput.keyUp(Input.Keys.SPACE);
85 } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
86 gameInput.keyUp(Input.Keys.A);
87 } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
88 if (gameProc.ctrlMode==1) gameInput.keyUp(Input.Keys.S);
89 else gameInput.keyUp(Input.Keys.CONTROL_LEFT);
90 } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
91 gameInput.keyUp(Input.Keys.D);
92 } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
93 gameInput.keyUp(Input.Keys.ALT_LEFT);
94 } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
95 gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
96 } else if (tx > tWidth - 26 && screenY > ty - 26) {
97 gameInput.touchUp(screenX, screenY, Input.Buttons.RIGHT);
98 } else {
99 gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
101 } else {
102 gameInput.touchUp(screenX, screenY, button);
104 return false;
107 @Override
108 public boolean touchDragged(int screenX, int screenY, int pointer) {
109 screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
110 screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
111 gameInput.touchDragged(screenX, screenY);
112 return false;
115 @Override
116 public boolean mouseMoved(int screenX, int screenY) {
117 screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
118 screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
119 gameInput.mouseMoved(screenX,screenY);
120 return false;
123 @Override
124 public boolean scrolled(int amount) {
125 gameInput.scrolled(amount);
126 return false;