DEADSOFTWARE

Update version
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInputProcessor.java
1 package ru.deadsoftware.cavedroid.game;
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.MainConfig;
9 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
10 import ru.deadsoftware.cavedroid.misc.Assets;
12 import javax.inject.Inject;
14 import static com.badlogic.gdx.utils.ObjectMap.Entry;
16 @GameScope
17 public class GameInputProcessor extends InputAdapter {
19 private static final TouchButton nullButton = new TouchButton(null, -1, true);
21 private final GameInput mGameInput;
22 private final GameRenderer mGameRenderer;
23 private final MainConfig mMainConfig;
25 @Inject
26 public GameInputProcessor(GameInput gameInput,
27 GameRenderer gameRenderer,
28 MainConfig mainConfig) {
29 mGameInput = gameInput;
30 mGameRenderer = gameRenderer;
31 mMainConfig = mainConfig;
33 loadTouchButtonsFromJSON();
34 }
36 private int getMouseKey(String name) {
37 switch (name) {
38 case "Left":
39 return Input.Buttons.LEFT;
40 case "Right":
41 return Input.Buttons.RIGHT;
42 case "Middle":
43 return Input.Buttons.MIDDLE;
44 case "Back":
45 return Input.Buttons.BACK;
46 case "Forward":
47 return Input.Buttons.FORWARD;
48 default:
49 return -1;
50 }
51 }
53 private void loadTouchButtonsFromJSON() {
54 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
55 for (JsonValue key = json.child(); key != null; key = key.next()) {
56 float x = key.getFloat("x");
57 float y = key.getFloat("y");
58 float w = key.getFloat("w");
59 float h = key.getFloat("h");
60 boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
61 String name = key.getString("key");
62 int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
63 if (x < 0) {
64 x = mGameRenderer.getWidth() + x;
65 }
66 if (y < 0) {
67 y = mGameRenderer.getHeight() + y;
68 }
69 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
70 }
72 }
74 private float transformScreenX(int screenX) {
75 return mGameRenderer.getWidth() / Gdx.graphics.getWidth() * screenX;
76 }
78 private float transformScreenY(int screenY) {
79 return mGameRenderer.getHeight() / Gdx.graphics.getHeight() * screenY;
80 }
82 private TouchButton getTouchedKey(float touchX, float touchY) {
83 for (Entry<String, TouchButton> entry : Assets.guiMap) {
84 TouchButton button = entry.value;
85 if (button.getRect().contains(touchX, touchY)) {
86 return button;
87 }
88 }
89 return nullButton;
90 }
92 @Override
93 public boolean keyDown(int keycode) {
94 mGameInput.keyDown(keycode);
95 return false;
96 }
98 @Override
99 public boolean keyUp(int keycode) {
100 mGameInput.keyUp(keycode);
101 return false;
104 @Override
105 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
106 float touchX = transformScreenX(screenX);
107 float touchY = transformScreenY(screenY);
109 if (mMainConfig.isTouch()) {
110 TouchButton touchedKey = getTouchedKey(touchX, touchY);
111 if (touchedKey.isMouse()) {
112 mGameInput.touchDown(touchX, touchY, touchedKey.getCode());
113 } else {
114 mGameInput.keyDown(touchedKey.getCode());
116 } else {
117 mGameInput.touchDown(touchX, touchY, button);
119 return false;
122 @Override
123 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
124 float touchX = transformScreenX(screenX);
125 float touchY = transformScreenY(screenY);
127 if (mMainConfig.isTouch()) {
128 TouchButton touchedKey = getTouchedKey(touchX, touchY);
129 if (touchedKey.isMouse()) {
130 mGameInput.touchUp(touchX, touchY, touchedKey.getCode());
131 } else {
132 mGameInput.keyUp(mGameInput.getKeyDownCode());
134 } else {
135 mGameInput.touchUp(touchX, touchY, button);
137 return false;
140 @Override
141 public boolean touchDragged(int screenX, int screenY, int pointer) {
142 float touchX = transformScreenX(screenX);
143 float touchY = transformScreenY(screenY);
144 if (mMainConfig.isTouch() && mGameInput.isKeyDown()) {
145 if (getTouchedKey(touchX, touchY).getCode() == -1) {
146 mGameInput.keyUp(mGameInput.getKeyDownCode());
148 } else {
149 mGameInput.touchDragged(touchX, touchY);
151 return false;
154 @Override
155 public boolean scrolled(float amountX, float amountY) {
156 mGameInput.scrolled(amountX, amountY);
157 return false;