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
;
11 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
13 public class InputHandlerGame
extends InputAdapter
{
15 private static final int
24 public InputHandlerGame() {
25 loadTouchButtonsFromJSON();
28 private void loadTouchButtonsFromJSON() {
29 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/touch_buttons.json"));
30 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next()) {
31 float x
= key
.getFloat("x");
32 float y
= key
.getFloat("y");
33 float w
= key
.getFloat("w");
34 float h
= key
.getFloat("h");
35 if (x
< 0) x
= GP
.renderer
.getWidth() + x
;
36 if (y
< 0) y
= GP
.renderer
.getHeight() + y
;
37 Assets
.guiMap
.put(key
.name(), new Rectangle(x
, y
, w
, h
));
42 private float transformScreenX(int screenX
) {
43 return GP
.renderer
.getWidth() / GameScreen
.getWidth() * screenX
;
46 private float transformScreenY(int screenY
) {
47 return GP
.renderer
.getHeight() / GameScreen
.getHeight() * screenY
;
50 private int getTouchedKey(float touchX
, float touchY
) {
51 for (int i
= 0; i
< Assets
.guiMap
.size
; i
++) {
52 if (Assets
.guiMap
.getValueAt(i
).contains(touchX
, touchY
)) {
60 public boolean keyDown(int keycode
) {
61 GP
.input
.keyDown(keycode
);
66 public boolean keyUp(int keycode
) {
67 GP
.input
.keyUp(keycode
);
72 public boolean touchDown(int screenX
, int screenY
, int pointer
, int button
) {
73 float touchX
= transformScreenX(screenX
);
74 float touchY
= transformScreenY(screenY
);
77 int touchedKey
= getTouchedKey(touchX
, touchY
);
80 GP
.input
.keyDown(GP
.controlMode
== ControlMode
.CURSOR ? Input
.Keys
.W
: Input
.Keys
.SPACE
);
83 GP
.input
.keyDown(GP
.controlMode
== ControlMode
.CURSOR ? Input
.Keys
.S
: Input
.Keys
.CONTROL_LEFT
);
86 GP
.input
.keyDown(Input
.Keys
.A
);
89 GP
.input
.keyDown(Input
.Keys
.D
);
92 GP
.input
.keyDown(Input
.Keys
.ALT_LEFT
);
95 GP
.input
.touchDown(touchX
, touchY
, Input
.Buttons
.LEFT
);
98 GP
.input
.touchDown(touchX
, touchY
, Input
.Buttons
.RIGHT
);
101 GP
.input
.touchDown(touchX
, touchY
, touchedKey
);
104 GP
.input
.touchDown(touchX
, touchY
, button
);
110 public boolean touchUp(int screenX
, int screenY
, int pointer
, int button
) {
111 float touchX
= transformScreenX(screenX
);
112 float touchY
= transformScreenY(screenY
);
114 if (CaveGame
.TOUCH
) {
115 int touchedKey
= getTouchedKey(touchX
, touchY
);
116 switch (touchedKey
) {
122 GP
.input
.keyUp(GP
.input
.getKeyDownCode());
125 GP
.input
.touchUp(touchX
, touchY
, Input
.Buttons
.LEFT
);
128 GP
.input
.touchUp(touchX
, touchY
, Input
.Buttons
.RIGHT
);
131 GP
.input
.touchUp(touchX
, touchY
, touchedKey
);
134 GP
.input
.touchUp(touchX
, touchY
, button
);
140 public boolean touchDragged(int screenX
, int screenY
, int pointer
) {
141 float touchX
= transformScreenX(screenX
);
142 float touchY
= transformScreenY(screenY
);
143 if (CaveGame
.TOUCH
&& GP
.input
.isKeyDown()) {
144 if (getTouchedKey(touchX
, touchY
) == -1) {
145 GP
.input
.keyUp(GP
.input
.getKeyDownCode());
148 GP
.input
.touchDragged(touchX
, touchY
);
154 public boolean scrolled(int amount
) {
155 GP
.input
.scrolled(amount
);