DEADSOFTWARE

Fix codestyle
[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) {
51 x = GP.renderer.getWidth() + x;
52 }
53 if (y < 0) {
54 y = GP.renderer.getHeight() + y;
55 }
56 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
57 }
59 }
61 private float transformScreenX(int screenX) {
62 return GP.renderer.getWidth() / GameScreen.getWidth() * screenX;
63 }
65 private float transformScreenY(int screenY) {
66 return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
67 }
69 private TouchButton getTouchedKey(float touchX, float touchY) {
70 for (Entry entry : Assets.guiMap) {
71 TouchButton button = (TouchButton) entry.value;
72 if (button.getRect().contains(touchX, touchY)) {
73 return button;
74 }
75 }
76 return nullButton;
77 }
79 @Override
80 public boolean keyDown(int keycode) {
81 GP.input.keyDown(keycode);
82 return false;
83 }
85 @Override
86 public boolean keyUp(int keycode) {
87 GP.input.keyUp(keycode);
88 return false;
89 }
91 @Override
92 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
93 float touchX = transformScreenX(screenX);
94 float touchY = transformScreenY(screenY);
96 if (CaveGame.TOUCH) {
97 TouchButton touchedKey = getTouchedKey(touchX, touchY);
98 if (touchedKey.isMouse()) {
99 GP.input.touchDown(touchX, touchY, touchedKey.getCode());
100 } else {
101 GP.input.keyDown(touchedKey.getCode());
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 TouchButton touchedKey = getTouchedKey(touchX, touchY);
116 if (touchedKey.isMouse()) {
117 GP.input.touchUp(touchX, touchY, touchedKey.getCode());
118 } else {
119 GP.input.keyUp(GP.input.getKeyDownCode());
121 } else {
122 GP.input.touchUp(touchX, touchY, button);
124 return false;
127 @Override
128 public boolean touchDragged(int screenX, int screenY, int pointer) {
129 float touchX = transformScreenX(screenX);
130 float touchY = transformScreenY(screenY);
131 if (CaveGame.TOUCH && GP.input.isKeyDown()) {
132 if (getTouchedKey(touchX, touchY).getCode() == -1) {
133 GP.input.keyUp(GP.input.getKeyDownCode());
135 } else {
136 GP.input.touchDragged(touchX, touchY);
138 return false;
141 @Override
142 public boolean scrolled(int amount) {
143 GP.input.scrolled(amount);
144 return false;