DEADSOFTWARE

Move some methods
[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;
11 import static ru.deadsoftware.cavedroid.GameScreen.GP;
13 public class InputHandlerGame extends InputAdapter {
15 private static final int
16 UP = 0,
17 DOWN = 1,
18 LEFT = 2,
19 RIGHT = 3,
20 ALT = 4,
21 LMB = 5,
22 RMB = 6;
24 public InputHandlerGame() {
25 loadTouchButtonsFromJSON();
26 }
28 private void loadTouchButtonsFromJSON() {
29 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
30 for (JsonValue key = json.child(); key != null; key = key.next()) {
31 float x = key.getFloat("x");
32 float y = key.getFloat("y");
33 float w = key.getFloat("w");
34 float h = key.getFloat("h");
35 if (x < 0) x = GP.renderer.getWidth() + x;
36 if (y < 0) y = GP.renderer.getHeight() + y;
37 Assets.guiMap.put(key.name(), new Rectangle(x, y, w, h));
38 }
40 }
42 private float transformScreenX(int screenX) {
43 return GP.renderer.getWidth() / GameScreen.getWidth() * screenX;
44 }
46 private float transformScreenY(int screenY) {
47 return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
48 }
50 private int getTouchedKey(float touchX, float touchY) {
51 for (int i = 0; i < Assets.guiMap.size; i++) {
52 if (Assets.guiMap.getValueAt(i).contains(touchX, touchY)) {
53 return i;
54 }
55 }
56 return -1;
57 }
59 @Override
60 public boolean keyDown(int keycode) {
61 GP.input.keyDown(keycode);
62 return false;
63 }
65 @Override
66 public boolean keyUp(int keycode) {
67 GP.input.keyUp(keycode);
68 return false;
69 }
71 @Override
72 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
73 float touchX = transformScreenX(screenX);
74 float touchY = transformScreenY(screenY);
76 if (CaveGame.TOUCH) {
77 int touchedKey = getTouchedKey(touchX, touchY);
78 switch (touchedKey) {
79 case UP:
80 GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.W : Input.Keys.SPACE);
81 break;
82 case DOWN:
83 GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.S : Input.Keys.CONTROL_LEFT);
84 break;
85 case LEFT:
86 GP.input.keyDown(Input.Keys.A);
87 break;
88 case RIGHT:
89 GP.input.keyDown(Input.Keys.D);
90 break;
91 case ALT:
92 GP.input.keyDown(Input.Keys.ALT_LEFT);
93 break;
94 case LMB:
95 GP.input.touchDown(touchX, touchY, Input.Buttons.LEFT);
96 break;
97 case RMB:
98 GP.input.touchDown(touchX, touchY, Input.Buttons.RIGHT);
99 break;
100 default:
101 GP.input.touchDown(touchX, touchY, touchedKey);
103 } else {
104 GP.input.touchDown(touchX, touchY, button);
106 return false;
109 @Override
110 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
111 float touchX = transformScreenX(screenX);
112 float touchY = transformScreenY(screenY);
114 if (CaveGame.TOUCH) {
115 int touchedKey = getTouchedKey(touchX, touchY);
116 switch (touchedKey) {
117 case UP:
118 case DOWN:
119 case LEFT:
120 case RIGHT:
121 case ALT:
122 GP.input.keyUp(GP.input.getKeyDownCode());
123 break;
124 case LMB:
125 GP.input.touchUp(touchX, touchY, Input.Buttons.LEFT);
126 break;
127 case RMB:
128 GP.input.touchUp(touchX, touchY, Input.Buttons.RIGHT);
129 break;
130 default:
131 GP.input.touchUp(touchX, touchY, touchedKey);
133 } else {
134 GP.input.touchUp(touchX, touchY, button);
136 return false;
139 @Override
140 public boolean touchDragged(int screenX, int screenY, int pointer) {
141 float touchX = transformScreenX(screenX);
142 float touchY = transformScreenY(screenY);
143 if (CaveGame.TOUCH && GP.input.isKeyDown()) {
144 if (getTouchedKey(touchX, touchY) == -1) {
145 GP.input.keyUp(GP.input.getKeyDownCode());
147 } else {
148 GP.input.touchDragged(touchX, touchY);
150 return false;
153 @Override
154 public boolean scrolled(int amount) {
155 GP.input.scrolled(amount);
156 return false;