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();
23 private int getMouseKey(String name
) {
26 return Input
.Buttons
.LEFT
;
28 return Input
.Buttons
.RIGHT
;
30 return Input
.Buttons
.MIDDLE
;
32 return Input
.Buttons
.BACK
;
34 return Input
.Buttons
.FORWARD
;
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) x
= GP
.renderer
.getWidth() + x
;
51 if (y
< 0) y
= GP
.renderer
.getHeight() + y
;
52 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
57 private float transformScreenX(int screenX
) {
58 return GP
.renderer
.getWidth() / GameScreen
.getWidth() * screenX
;
61 private float transformScreenY(int screenY
) {
62 return GP
.renderer
.getHeight() / GameScreen
.getHeight() * screenY
;
65 private TouchButton
getTouchedKey(float touchX
, float touchY
) {
66 for (Entry entry
: Assets
.guiMap
) {
67 TouchButton button
= (TouchButton
) entry
.value
;
68 if (button
.getRect().contains(touchX
, touchY
)) {
76 public boolean keyDown(int keycode
) {
77 GP
.input
.keyDown(keycode
);
82 public boolean keyUp(int keycode
) {
83 GP
.input
.keyUp(keycode
);
88 public boolean touchDown(int screenX
, int screenY
, int pointer
, int button
) {
89 float touchX
= transformScreenX(screenX
);
90 float touchY
= transformScreenY(screenY
);
93 TouchButton touchedKey
= getTouchedKey(touchX
, touchY
);
94 if (touchedKey
.isMouse()) {
95 GP
.input
.touchDown(touchX
, touchY
, touchedKey
.getCode());
97 GP
.input
.keyDown(touchedKey
.getCode());
100 GP
.input
.touchDown(touchX
, touchY
, button
);
106 public boolean touchUp(int screenX
, int screenY
, int pointer
, int button
) {
107 float touchX
= transformScreenX(screenX
);
108 float touchY
= transformScreenY(screenY
);
110 if (CaveGame
.TOUCH
) {
111 TouchButton touchedKey
= getTouchedKey(touchX
, touchY
);
112 if (touchedKey
.isMouse()) {
113 GP
.input
.touchUp(touchX
, touchY
, touchedKey
.getCode());
115 GP
.input
.keyUp(GP
.input
.getKeyDownCode());
118 GP
.input
.touchUp(touchX
, touchY
, button
);
124 public boolean touchDragged(int screenX
, int screenY
, int pointer
) {
125 float touchX
= transformScreenX(screenX
);
126 float touchY
= transformScreenY(screenY
);
127 if (CaveGame
.TOUCH
&& GP
.input
.isKeyDown()) {
128 if (getTouchedKey(touchX
, touchY
).getCode() == -1) {
129 GP
.input
.keyUp(GP
.input
.getKeyDownCode());
132 GP
.input
.touchDragged(touchX
, touchY
);
138 public boolean scrolled(int amount
) {
139 GP
.input
.scrolled(amount
);