DEADSOFTWARE

Upgrade touch controls
[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.objects.TouchButton;
12 import static com.badlogic.gdx.utils.ObjectMap.Entry;
13 import static ru.deadsoftware.cavedroid.GameScreen.GP;
15 public class InputHandlerGame extends InputAdapter {
17 private static TouchButton nullButton = new TouchButton(null, -1, true);
19 public InputHandlerGame() {
20 loadTouchButtonsFromJSON();
21 }
23 private int getMouseKey(String name) {
24 switch (name) {
25 case "Left":
26 return Input.Buttons.LEFT;
27 case "Right":
28 return Input.Buttons.RIGHT;
29 case "Middle":
30 return Input.Buttons.MIDDLE;
31 case "Back":
32 return Input.Buttons.BACK;
33 case "Forward":
34 return Input.Buttons.FORWARD;
35 default:
36 return -1;
37 }
38 }
40 private void loadTouchButtonsFromJSON() {
41 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
42 for (JsonValue key = json.child(); key != null; key = key.next()) {
43 float x = key.getFloat("x");
44 float y = key.getFloat("y");
45 float w = key.getFloat("w");
46 float h = key.getFloat("h");
47 boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
48 String name = key.getString("key");
49 int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
50 if (x < 0) x = GP.renderer.getWidth() + x;
51 if (y < 0) y = GP.renderer.getHeight() + y;
52 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
53 }
55 }
57 private float transformScreenX(int screenX) {
58 return GP.renderer.getWidth() / GameScreen.getWidth() * screenX;
59 }
61 private float transformScreenY(int screenY) {
62 return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
63 }
65 private TouchButton getTouchedKey(float touchX, float touchY) {
66 for (Entry entry : Assets.guiMap) {
67 TouchButton button = (TouchButton) entry.value;
68 if (button.getRect().contains(touchX, touchY)) {
69 return button;
70 }
71 }
72 return nullButton;
73 }
75 @Override
76 public boolean keyDown(int keycode) {
77 GP.input.keyDown(keycode);
78 return false;
79 }
81 @Override
82 public boolean keyUp(int keycode) {
83 GP.input.keyUp(keycode);
84 return false;
85 }
87 @Override
88 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
89 float touchX = transformScreenX(screenX);
90 float touchY = transformScreenY(screenY);
92 if (CaveGame.TOUCH) {
93 TouchButton touchedKey = getTouchedKey(touchX, touchY);
94 if (touchedKey.isMouse()) {
95 GP.input.touchDown(touchX, touchY, touchedKey.getCode());
96 } else {
97 GP.input.keyDown(touchedKey.getCode());
98 }
99 } else {
100 GP.input.touchDown(touchX, touchY, button);
102 return false;
105 @Override
106 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
107 float touchX = transformScreenX(screenX);
108 float touchY = transformScreenY(screenY);
110 if (CaveGame.TOUCH) {
111 TouchButton touchedKey = getTouchedKey(touchX, touchY);
112 if (touchedKey.isMouse()) {
113 GP.input.touchUp(touchX, touchY, touchedKey.getCode());
114 } else {
115 GP.input.keyUp(GP.input.getKeyDownCode());
117 } else {
118 GP.input.touchUp(touchX, touchY, button);
120 return false;
123 @Override
124 public boolean touchDragged(int screenX, int screenY, int pointer) {
125 float touchX = transformScreenX(screenX);
126 float touchY = transformScreenY(screenY);
127 if (CaveGame.TOUCH && GP.input.isKeyDown()) {
128 if (getTouchedKey(touchX, touchY).getCode() == -1) {
129 GP.input.keyUp(GP.input.getKeyDownCode());
131 } else {
132 GP.input.touchDragged(touchX, touchY);
134 return false;
137 @Override
138 public boolean scrolled(int amount) {
139 GP.input.scrolled(amount);
140 return false;