9ba00bcfca9ee5e393cb188651ed18b355bd23d7
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
.GameInput
;
12 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
14 public class InputHandlerGame
extends InputAdapter
{
16 private static final int
25 private GameInput gameInput
;
27 public InputHandlerGame() {
28 this.gameInput
= new GameInput();
29 loadTouchButtonsFromJSON();
32 private void loadTouchButtonsFromJSON() {
33 JsonValue json
= Assets
.jsonReader
.parse(Gdx
.files
.internal("json/touch_buttons.json"));
34 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next()) {
35 float x
= key
.getFloat("x");
36 float y
= key
.getFloat("y");
37 float w
= key
.getFloat("w");
38 float h
= key
.getFloat("h");
39 if (x
< 0) x
= GP
.renderer
.getWidth() + x
;
40 if (y
< 0) y
= GP
.renderer
.getHeight() + y
;
41 Assets
.guiMap
.put(key
.name(), new Rectangle(x
, y
, w
, h
));
46 private float transformScreenX(int screenX
) {
47 return GP
.renderer
.getWidth() / GameScreen
.getWidth() * screenX
;
50 private float transformScreenY(int screenY
) {
51 return GP
.renderer
.getHeight() / GameScreen
.getHeight() * screenY
;
54 private int getTouchedKey(float touchX
, float touchY
) {
55 for (int i
= 0; i
< Assets
.guiMap
.size
; i
++) {
56 if (Assets
.guiMap
.getValueAt(i
).contains(touchX
, touchY
)) {
64 public boolean keyDown(int keycode
) {
65 gameInput
.keyDown(keycode
);
70 public boolean keyUp(int keycode
) {
71 gameInput
.keyUp(keycode
);
76 public boolean touchDown(int screenX
, int screenY
, int pointer
, int button
) {
77 float touchX
= transformScreenX(screenX
);
78 float touchY
= transformScreenY(screenY
);
81 int touchedKey
= getTouchedKey(touchX
, touchY
);
84 gameInput
.keyDown(GP
.controlMode
== ControlMode
.CURSOR ? Input
.Keys
.W
: Input
.Keys
.SPACE
);
87 gameInput
.keyDown(GP
.controlMode
== ControlMode
.CURSOR ? Input
.Keys
.S
: Input
.Keys
.CONTROL_LEFT
);
90 gameInput
.keyDown(Input
.Keys
.A
);
93 gameInput
.keyDown(Input
.Keys
.D
);
96 gameInput
.keyDown(Input
.Keys
.ALT_LEFT
);
99 gameInput
.touchDown(touchX
, touchY
, Input
.Buttons
.LEFT
);
102 gameInput
.touchDown(touchX
, touchY
, Input
.Buttons
.RIGHT
);
105 gameInput
.touchDown(touchX
, touchY
, touchedKey
);
108 gameInput
.touchDown(touchX
, touchY
, button
);
114 public boolean touchUp(int screenX
, int screenY
, int pointer
, int button
) {
115 float touchX
= transformScreenX(screenX
);
116 float touchY
= transformScreenY(screenY
);
118 if (CaveGame
.TOUCH
) {
119 int touchedKey
= getTouchedKey(touchX
, touchY
);
120 switch (touchedKey
) {
126 gameInput
.keyUp(GP
.keyDownCode
);
129 gameInput
.touchUp(touchX
, touchY
, Input
.Buttons
.LEFT
);
132 gameInput
.touchUp(touchX
, touchY
, Input
.Buttons
.RIGHT
);
135 gameInput
.touchUp(touchX
, touchY
, touchedKey
);
138 gameInput
.touchUp(touchX
, touchY
, button
);
144 public boolean touchDragged(int screenX
, int screenY
, int pointer
) {
145 float touchX
= transformScreenX(screenX
);
146 float touchY
= transformScreenY(screenY
);
147 if (CaveGame
.TOUCH
&& GP
.isKeyDown
) {
148 if (getTouchedKey(touchX
, touchY
) == -1) {
149 gameInput
.keyUp(GP
.keyDownCode
);
152 gameInput
.touchDragged(touchX
, touchY
);
158 public boolean scrolled(int amount
) {
159 gameInput
.scrolled(amount
);