1 package ru
.deadsoftware
.cavecraft
.misc
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.InputProcessor
;
5 import ru
.deadsoftware
.cavecraft
.CaveGame
;
6 import ru
.deadsoftware
.cavecraft
.game
.GameInput
;
7 import ru
.deadsoftware
.cavecraft
.game
.GameProc
;
9 import static ru
.deadsoftware
.cavecraft
.GameScreen
.getHeight
;
10 import static ru
.deadsoftware
.cavecraft
.GameScreen
.getWidth
;
12 public class InputHandlerGame
implements InputProcessor
{
14 private GameProc gameProc
;
15 private GameInput gameInput
;
17 private float tWidth
, tHeight
;
19 public InputHandlerGame(GameProc gameProc
) {
20 this.gameProc
= gameProc
;
21 this.gameInput
= new GameInput(gameProc
);
22 tWidth
= gameProc
.renderer
.camera
.viewportWidth
;
23 tHeight
= gameProc
.renderer
.camera
.viewportHeight
;
27 public boolean keyDown(int keycode
) {
28 gameInput
.keyDown(keycode
);
33 public boolean keyUp(int keycode
) {
34 gameInput
.keyUp(keycode
);
39 public boolean keyTyped(char character
) {
44 public boolean touchDown(int screenX
, int screenY
, int pointer
, int button
) {
45 int tx
= (int) (screenX
* tWidth
/ getWidth());
46 int ty
= (int) (screenY
* tHeight
/ getHeight());
47 screenX
*= gameProc
.renderer
.camera
.viewportWidth
/ getWidth();
48 screenY
*= gameProc
.renderer
.camera
.viewportHeight
/ getHeight();
51 if (tx
> 26 && tx
< 52 && ty
> tHeight
- 52 && ty
< tHeight
- 26) {
52 if (gameProc
.ctrlMode
== 1) gameInput
.keyDown(Input
.Keys
.W
);
53 else gameInput
.keyDown(Input
.Keys
.SPACE
);
54 } else if (tx
> 0 && tx
< 26 && ty
> tHeight
- 26) {
55 gameInput
.keyDown(Input
.Keys
.A
);
56 } else if (tx
> 26 && tx
< 52 && ty
> tHeight
- 26) {
57 if (gameProc
.ctrlMode
== 1) gameInput
.keyDown(Input
.Keys
.S
);
58 else gameInput
.keyDown(Input
.Keys
.CONTROL_LEFT
);
59 } else if (tx
> 52 && tx
< 78 && ty
> tHeight
- 26) {
60 gameInput
.keyDown(Input
.Keys
.D
);
61 } else if (tx
> 78 && tx
< 104 && ty
> tHeight
- 26) {
62 gameInput
.keyDown(Input
.Keys
.ALT_LEFT
);
63 } else if (tx
> tWidth
- 52 && tx
< tWidth
- 26 && ty
> tHeight
- 26) {
64 gameInput
.touchDown(screenX
, screenY
, Input
.Buttons
.LEFT
);
65 } else if (tx
> tWidth
- 26 && screenY
> ty
- 26) {
66 gameInput
.touchDown(screenX
, screenY
, Input
.Buttons
.RIGHT
);
68 gameInput
.touchDown(screenX
, screenY
, -1);
71 gameInput
.touchDown(screenX
, screenY
, button
);
77 public boolean touchUp(int screenX
, int screenY
, int pointer
, int button
) {
78 int tx
= (int) (screenX
* tWidth
/ getWidth());
79 int ty
= (int) (screenY
* tHeight
/ getHeight());
80 screenX
*= gameProc
.renderer
.camera
.viewportWidth
/ getWidth();
81 screenY
*= gameProc
.renderer
.camera
.viewportHeight
/ getHeight();
83 if (tx
> 26 && tx
< 52 && ty
> tHeight
- 52 && ty
< tHeight
- 26) {
84 if (gameProc
.ctrlMode
== 1) gameInput
.keyUp(Input
.Keys
.W
);
85 else gameInput
.keyUp(Input
.Keys
.SPACE
);
86 } else if (tx
> 0 && tx
< 26 && ty
> tHeight
- 26) {
87 gameInput
.keyUp(Input
.Keys
.A
);
88 } else if (tx
> 26 && tx
< 52 && ty
> tHeight
- 26) {
89 if (gameProc
.ctrlMode
== 1) gameInput
.keyUp(Input
.Keys
.S
);
90 else gameInput
.keyUp(Input
.Keys
.CONTROL_LEFT
);
91 } else if (tx
> 52 && tx
< 78 && ty
> tHeight
- 26) {
92 gameInput
.keyUp(Input
.Keys
.D
);
93 } else if (tx
> 78 && tx
< 104 && ty
> tHeight
- 26) {
94 gameInput
.keyUp(Input
.Keys
.ALT_LEFT
);
95 } else if (tx
> tWidth
- 52 && tx
< tWidth
- 26 && ty
> tHeight
- 26) {
96 gameInput
.touchUp(screenX
, screenY
, Input
.Buttons
.LEFT
);
97 } else if (tx
> tWidth
- 26 && screenY
> ty
- 26) {
98 gameInput
.touchUp(screenX
, screenY
, Input
.Buttons
.RIGHT
);
100 gameInput
.touchUp(screenX
, screenY
, -1);
103 gameInput
.touchUp(screenX
, screenY
, button
);
109 public boolean touchDragged(int screenX
, int screenY
, int pointer
) {
110 screenX
*= gameProc
.renderer
.camera
.viewportWidth
/ getWidth();
111 screenY
*= gameProc
.renderer
.camera
.viewportHeight
/ getHeight();
112 if (gameProc
.isKeyDown
&& (screenX
> 78 || screenY
< gameProc
.renderer
.getHeight() - 52)) {
113 gameInput
.keyUp(gameProc
.keyDownCode
);
115 gameInput
.touchDragged(screenX
, screenY
);
121 public boolean mouseMoved(int screenX
, int screenY
) {
122 screenX
*= gameProc
.renderer
.camera
.viewportWidth
/ getWidth();
123 screenY
*= gameProc
.renderer
.camera
.viewportHeight
/ getHeight();
124 gameInput
.mouseMoved(screenX
, screenY
);
129 public boolean scrolled(int amount
) {
130 gameInput
.scrolled(amount
);