DEADSOFTWARE

Refactor
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / misc / InputHandlerGame.java
1 package ru.deadsoftware.cavecraft.misc;
3 import com.badlogic.gdx.Input;
4 import com.badlogic.gdx.InputProcessor;
5 import ru.deadsoftware.cavecraft.CaveGame;
6 import ru.deadsoftware.cavecraft.game.GameInput;
7 import ru.deadsoftware.cavecraft.game.GameProc;
9 import static ru.deadsoftware.cavecraft.GameScreen.getHeight;
10 import static ru.deadsoftware.cavecraft.GameScreen.getWidth;
12 public class InputHandlerGame implements InputProcessor {
14 private GameProc gp;
15 private GameInput gameInput;
17 private float tWidth, tHeight;
19 public InputHandlerGame(GameProc gp) {
20 this.gp = gp;
21 this.gameInput = new GameInput(gp);
22 tWidth = gp.renderer.getWidth();
23 tHeight = gp.renderer.getHeight();
24 }
26 @Override
27 public boolean keyDown(int keycode) {
28 gameInput.keyDown(keycode);
29 return false;
30 }
32 @Override
33 public boolean keyUp(int keycode) {
34 gameInput.keyUp(keycode);
35 return false;
36 }
38 @Override
39 public boolean keyTyped(char character) {
40 return false;
41 }
43 @Override
44 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
45 int tx = (int) (screenX * tWidth / getWidth());
46 int ty = (int) (screenY * tHeight / getHeight());
47 screenX *= gp.renderer.getWidth() / getWidth();
48 screenY *= gp.renderer.getHeight() / getHeight();
50 if (CaveGame.TOUCH) {
51 if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
52 if (gp.ctrlMode == 1) gameInput.keyDown(Input.Keys.W);
53 else gameInput.keyDown(Input.Keys.SPACE);
54 } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
55 gameInput.keyDown(Input.Keys.A);
56 } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
57 if (gp.ctrlMode == 1) gameInput.keyDown(Input.Keys.S);
58 else gameInput.keyDown(Input.Keys.CONTROL_LEFT);
59 } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
60 gameInput.keyDown(Input.Keys.D);
61 } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
62 gameInput.keyDown(Input.Keys.ALT_LEFT);
63 } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
64 gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
65 } else if (tx > tWidth - 26 && screenY > ty - 26) {
66 gameInput.touchDown(screenX, screenY, Input.Buttons.RIGHT);
67 } else {
68 gameInput.touchDown(screenX, screenY, -1);
69 }
70 } else {
71 gameInput.touchDown(screenX, screenY, button);
72 }
73 return false;
74 }
76 @Override
77 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
78 int tx = (int) (screenX * tWidth / getWidth());
79 int ty = (int) (screenY * tHeight / getHeight());
80 screenX *= gp.renderer.getWidth() / getWidth();
81 screenY *= gp.renderer.getHeight() / getHeight();
82 if (CaveGame.TOUCH) {
83 if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
84 if (gp.ctrlMode == 1) gameInput.keyUp(Input.Keys.W);
85 else gameInput.keyUp(Input.Keys.SPACE);
86 } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
87 gameInput.keyUp(Input.Keys.A);
88 } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
89 if (gp.ctrlMode == 1) gameInput.keyUp(Input.Keys.S);
90 else gameInput.keyUp(Input.Keys.CONTROL_LEFT);
91 } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
92 gameInput.keyUp(Input.Keys.D);
93 } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
94 gameInput.keyUp(Input.Keys.ALT_LEFT);
95 } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
96 gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
97 } else if (tx > tWidth - 26 && screenY > ty - 26) {
98 gameInput.touchUp(screenX, screenY, Input.Buttons.RIGHT);
99 } else {
100 gameInput.touchUp(screenX, screenY, -1);
102 } else {
103 gameInput.touchUp(screenX, screenY, button);
105 return false;
108 @Override
109 public boolean touchDragged(int screenX, int screenY, int pointer) {
110 screenX *= gp.renderer.getWidth() / getWidth();
111 screenY *= gp.renderer.getHeight() / getHeight();
112 if (gp.isKeyDown && (screenX > 78 || screenY < gp.renderer.getHeight() - 52)) {
113 gameInput.keyUp(gp.keyDownCode);
114 } else {
115 gameInput.touchDragged(screenX, screenY);
117 return false;
120 @Override
121 public boolean mouseMoved(int screenX, int screenY) {
122 return false;
125 @Override
126 public boolean scrolled(int amount) {
127 gameInput.scrolled(amount);
128 return false;