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
;
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
;
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();
40 private int getMouseKey(String name
) {
43 return Input
.Buttons
.LEFT
;
45 return Input
.Buttons
.RIGHT
;
47 return Input
.Buttons
.MIDDLE
;
49 return Input
.Buttons
.BACK
;
51 return Input
.Buttons
.FORWARD
;
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
);
68 x
= mGameRenderer
.getWidth() + x
;
71 y
= mGameRenderer
.getHeight() + y
;
73 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
78 private float transformScreenX(int screenX
) {
79 return mGameRenderer
.getWidth() / Gdx
.graphics
.getWidth() * screenX
;
82 private float transformScreenY(int screenY
) {
83 return mGameRenderer
.getHeight() / Gdx
.graphics
.getHeight() * screenY
;
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
)) {
97 public boolean keyDown(int keycode
) {
98 mGameInput
.keyDown(keycode
);
103 public boolean keyUp(int keycode
) {
104 mGameInput
.keyUp(keycode
);
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());
118 mGameInput
.keyDown(touchedKey
.getCode());
121 mGameInput
.touchDown(touchX
, touchY
, button
);
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());
136 mGameInput
.keyUp(mGameInput
.getKeyDownCode());
139 mGameInput
.touchUp(touchX
, touchY
, button
);
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());
153 mGameInput
.touchDragged(touchX
, touchY
);
159 public boolean scrolled(float amountX
, float amountY
) {
160 mGameInput
.scrolled(amountX
, amountY
);