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
;
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
;
26 public GameInputProcessor(GameInput gameInput
,
27 GameRenderer gameRenderer
,
28 MainConfig mainConfig
) {
29 mGameInput
= gameInput
;
30 mGameRenderer
= gameRenderer
;
31 mMainConfig
= mainConfig
;
33 loadTouchButtonsFromJSON();
36 private int getMouseKey(String name
) {
39 return Input
.Buttons
.LEFT
;
41 return Input
.Buttons
.RIGHT
;
43 return Input
.Buttons
.MIDDLE
;
45 return Input
.Buttons
.BACK
;
47 return Input
.Buttons
.FORWARD
;
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
);
64 x
= mGameRenderer
.getWidth() + x
;
67 y
= mGameRenderer
.getHeight() + y
;
69 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
74 private float transformScreenX(int screenX
) {
75 return mGameRenderer
.getWidth() / Gdx
.graphics
.getWidth() * screenX
;
78 private float transformScreenY(int screenY
) {
79 return mGameRenderer
.getHeight() / Gdx
.graphics
.getHeight() * screenY
;
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
)) {
93 public boolean keyDown(int keycode
) {
94 mGameInput
.keyDown(keycode
);
99 public boolean keyUp(int keycode
) {
100 mGameInput
.keyUp(keycode
);
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());
114 mGameInput
.keyDown(touchedKey
.getCode());
117 mGameInput
.touchDown(touchX
, touchY
, button
);
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());
132 mGameInput
.keyUp(mGameInput
.getKeyDownCode());
135 mGameInput
.touchUp(touchX
, touchY
, button
);
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());
149 mGameInput
.touchDragged(touchX
, touchY
);
155 public boolean scrolled(int amount
) {
156 mGameInput
.scrolled(amount
);