DEADSOFTWARE

9ba00bcfca9ee5e393cb188651ed18b355bd23d7
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / InputHandlerGame.java
1 package ru.deadsoftware.cavedroid.misc;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.InputAdapter;
6 import com.badlogic.gdx.math.Rectangle;
7 import com.badlogic.gdx.utils.JsonValue;
8 import ru.deadsoftware.cavedroid.CaveGame;
9 import ru.deadsoftware.cavedroid.GameScreen;
10 import ru.deadsoftware.cavedroid.game.GameInput;
12 import static ru.deadsoftware.cavedroid.GameScreen.GP;
14 public class InputHandlerGame extends InputAdapter {
16 private static final int
17 UP = 0,
18 DOWN = 1,
19 LEFT = 2,
20 RIGHT = 3,
21 ALT = 4,
22 LMB = 5,
23 RMB = 6;
25 private GameInput gameInput;
27 public InputHandlerGame() {
28 this.gameInput = new GameInput();
29 loadTouchButtonsFromJSON();
30 }
32 private void loadTouchButtonsFromJSON() {
33 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
34 for (JsonValue key = json.child(); key != null; key = key.next()) {
35 float x = key.getFloat("x");
36 float y = key.getFloat("y");
37 float w = key.getFloat("w");
38 float h = key.getFloat("h");
39 if (x < 0) x = GP.renderer.getWidth() + x;
40 if (y < 0) y = GP.renderer.getHeight() + y;
41 Assets.guiMap.put(key.name(), new Rectangle(x, y, w, h));
42 }
44 }
46 private float transformScreenX(int screenX) {
47 return GP.renderer.getWidth() / GameScreen.getWidth() * screenX;
48 }
50 private float transformScreenY(int screenY) {
51 return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
52 }
54 private int getTouchedKey(float touchX, float touchY) {
55 for (int i = 0; i < Assets.guiMap.size; i++) {
56 if (Assets.guiMap.getValueAt(i).contains(touchX, touchY)) {
57 return i;
58 }
59 }
60 return -1;
61 }
63 @Override
64 public boolean keyDown(int keycode) {
65 gameInput.keyDown(keycode);
66 return false;
67 }
69 @Override
70 public boolean keyUp(int keycode) {
71 gameInput.keyUp(keycode);
72 return false;
73 }
75 @Override
76 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
77 float touchX = transformScreenX(screenX);
78 float touchY = transformScreenY(screenY);
80 if (CaveGame.TOUCH) {
81 int touchedKey = getTouchedKey(touchX, touchY);
82 switch (touchedKey) {
83 case UP:
84 gameInput.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.W : Input.Keys.SPACE);
85 break;
86 case DOWN:
87 gameInput.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.S : Input.Keys.CONTROL_LEFT);
88 break;
89 case LEFT:
90 gameInput.keyDown(Input.Keys.A);
91 break;
92 case RIGHT:
93 gameInput.keyDown(Input.Keys.D);
94 break;
95 case ALT:
96 gameInput.keyDown(Input.Keys.ALT_LEFT);
97 break;
98 case LMB:
99 gameInput.touchDown(touchX, touchY, Input.Buttons.LEFT);
100 break;
101 case RMB:
102 gameInput.touchDown(touchX, touchY, Input.Buttons.RIGHT);
103 break;
104 default:
105 gameInput.touchDown(touchX, touchY, touchedKey);
107 } else {
108 gameInput.touchDown(touchX, touchY, button);
110 return false;
113 @Override
114 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
115 float touchX = transformScreenX(screenX);
116 float touchY = transformScreenY(screenY);
118 if (CaveGame.TOUCH) {
119 int touchedKey = getTouchedKey(touchX, touchY);
120 switch (touchedKey) {
121 case UP:
122 case DOWN:
123 case LEFT:
124 case RIGHT:
125 case ALT:
126 gameInput.keyUp(GP.keyDownCode);
127 break;
128 case LMB:
129 gameInput.touchUp(touchX, touchY, Input.Buttons.LEFT);
130 break;
131 case RMB:
132 gameInput.touchUp(touchX, touchY, Input.Buttons.RIGHT);
133 break;
134 default:
135 gameInput.touchUp(touchX, touchY, touchedKey);
137 } else {
138 gameInput.touchUp(touchX, touchY, button);
140 return false;
143 @Override
144 public boolean touchDragged(int screenX, int screenY, int pointer) {
145 float touchX = transformScreenX(screenX);
146 float touchY = transformScreenY(screenY);
147 if (CaveGame.TOUCH && GP.isKeyDown) {
148 if (getTouchedKey(touchX, touchY) == -1) {
149 gameInput.keyUp(GP.keyDownCode);
151 } else {
152 gameInput.touchDragged(touchX, touchY);
154 return false;
157 @Override
158 public boolean scrolled(int amount) {
159 gameInput.scrolled(amount);
160 return false;