DEADSOFTWARE

New blocks structure
[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;
11 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
13 import javax.inject.Inject;
15 import static com.badlogic.gdx.utils.ObjectMap.Entry;
17 @GameScope
18 public class GameInputProcessor extends InputAdapter {
20 private static final TouchButton nullButton = new TouchButton(null, -1, true);
22 private final GameInput mGameInput;
23 private final GameRenderer mGameRenderer;
24 private final MainConfig mMainConfig;
25 private final AssetLoader mAssetLoader;
27 @Inject
28 public GameInputProcessor(GameInput gameInput,
29 GameRenderer gameRenderer,
30 MainConfig mainConfig,
31 AssetLoader assetLoader) {
32 mGameInput = gameInput;
33 mGameRenderer = gameRenderer;
34 mMainConfig = mainConfig;
35 mAssetLoader = assetLoader;
37 loadTouchButtonsFromJSON();
38 }
40 private int getMouseKey(String name) {
41 switch (name) {
42 case "Left":
43 return Input.Buttons.LEFT;
44 case "Right":
45 return Input.Buttons.RIGHT;
46 case "Middle":
47 return Input.Buttons.MIDDLE;
48 case "Back":
49 return Input.Buttons.BACK;
50 case "Forward":
51 return Input.Buttons.FORWARD;
52 default:
53 return -1;
54 }
55 }
57 private void loadTouchButtonsFromJSON() {
58 JsonValue json = Assets.jsonReader.parse(mAssetLoader.getAssetHandle("json/touch_buttons.json"));
59 for (JsonValue key = json.child(); key != null; key = key.next()) {
60 float x = key.getFloat("x");
61 float y = key.getFloat("y");
62 float w = key.getFloat("w");
63 float h = key.getFloat("h");
64 boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
65 String name = key.getString("key");
66 int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
67 if (x < 0) {
68 x = mGameRenderer.getWidth() + x;
69 }
70 if (y < 0) {
71 y = mGameRenderer.getHeight() + y;
72 }
73 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
74 }
76 }
78 private float transformScreenX(int screenX) {
79 return mGameRenderer.getWidth() / Gdx.graphics.getWidth() * screenX;
80 }
82 private float transformScreenY(int screenY) {
83 return mGameRenderer.getHeight() / Gdx.graphics.getHeight() * screenY;
84 }
86 private TouchButton getTouchedKey(float touchX, float touchY) {
87 for (Entry<String, TouchButton> entry : Assets.guiMap) {
88 TouchButton button = entry.value;
89 if (button.getRect().contains(touchX, touchY)) {
90 return button;
91 }
92 }
93 return nullButton;
94 }
96 @Override
97 public boolean keyDown(int keycode) {
98 mGameInput.keyDown(keycode);
99 return false;
102 @Override
103 public boolean keyUp(int keycode) {
104 mGameInput.keyUp(keycode);
105 return false;
108 @Override
109 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
110 float touchX = transformScreenX(screenX);
111 float touchY = transformScreenY(screenY);
113 if (mMainConfig.isTouch()) {
114 TouchButton touchedKey = getTouchedKey(touchX, touchY);
115 if (touchedKey.isMouse()) {
116 mGameInput.touchDown(touchX, touchY, touchedKey.getCode());
117 } else {
118 mGameInput.keyDown(touchedKey.getCode());
120 } else {
121 mGameInput.touchDown(touchX, touchY, button);
123 return false;
126 @Override
127 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
128 float touchX = transformScreenX(screenX);
129 float touchY = transformScreenY(screenY);
131 if (mMainConfig.isTouch()) {
132 TouchButton touchedKey = getTouchedKey(touchX, touchY);
133 if (touchedKey.isMouse()) {
134 mGameInput.touchUp(touchX, touchY, touchedKey.getCode());
135 } else {
136 mGameInput.keyUp(mGameInput.getKeyDownCode());
138 } else {
139 mGameInput.touchUp(touchX, touchY, button);
141 return false;
144 @Override
145 public boolean touchDragged(int screenX, int screenY, int pointer) {
146 float touchX = transformScreenX(screenX);
147 float touchY = transformScreenY(screenY);
148 if (mMainConfig.isTouch() && mGameInput.isKeyDown()) {
149 if (getTouchedKey(touchX, touchY).getCode() == -1) {
150 mGameInput.keyUp(mGameInput.getKeyDownCode());
152 } else {
153 mGameInput.touchDragged(touchX, touchY);
155 return false;
158 @Override
159 public boolean scrolled(float amountX, float amountY) {
160 mGameInput.scrolled(amountX, amountY);
161 return false;