DEADSOFTWARE

Fix codestyle
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / InputHandlerGame.java
index 9ba00bcfca9ee5e393cb188651ed18b355bd23d7..6a7e4d388f9dce5c22a137331361039887d7422f 100644 (file)
@@ -7,28 +7,36 @@ import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.utils.JsonValue;
 import ru.deadsoftware.cavedroid.CaveGame;
 import ru.deadsoftware.cavedroid.GameScreen;
-import ru.deadsoftware.cavedroid.game.GameInput;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 
+import static com.badlogic.gdx.utils.ObjectMap.Entry;
 import static ru.deadsoftware.cavedroid.GameScreen.GP;
 
 public class InputHandlerGame extends InputAdapter {
 
-    private static final int
-            UP = 0,
-            DOWN = 1,
-            LEFT = 2,
-            RIGHT = 3,
-            ALT = 4,
-            LMB = 5,
-            RMB = 6;
-
-    private GameInput gameInput;
+    private static TouchButton nullButton = new TouchButton(null, -1, true);
 
     public InputHandlerGame() {
-        this.gameInput = new GameInput();
         loadTouchButtonsFromJSON();
     }
 
+    private int getMouseKey(String name) {
+        switch (name) {
+            case "Left":
+                return Input.Buttons.LEFT;
+            case "Right":
+                return Input.Buttons.RIGHT;
+            case "Middle":
+                return Input.Buttons.MIDDLE;
+            case "Back":
+                return Input.Buttons.BACK;
+            case "Forward":
+                return Input.Buttons.FORWARD;
+            default:
+                return -1;
+        }
+    }
+
     private void loadTouchButtonsFromJSON() {
         JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
         for (JsonValue key = json.child(); key != null; key = key.next()) {
@@ -36,9 +44,16 @@ public class InputHandlerGame extends InputAdapter {
             float y = key.getFloat("y");
             float w = key.getFloat("w");
             float h = key.getFloat("h");
-            if (x < 0) x = GP.renderer.getWidth() + x;
-            if (y < 0) y = GP.renderer.getHeight() + y;
-            Assets.guiMap.put(key.name(), new Rectangle(x, y, w, h));
+            boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
+            String name = key.getString("key");
+            int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
+            if (x < 0) {
+                x = GP.renderer.getWidth() + x;
+            }
+            if (y < 0) {
+                y = GP.renderer.getHeight() + y;
+            }
+            Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
         }
 
     }
@@ -51,24 +66,25 @@ public class InputHandlerGame extends InputAdapter {
         return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
     }
 
-    private int getTouchedKey(float touchX, float touchY) {
-        for (int i = 0; i < Assets.guiMap.size; i++) {
-            if (Assets.guiMap.getValueAt(i).contains(touchX, touchY)) {
-                return i;
+    private TouchButton getTouchedKey(float touchX, float touchY) {
+        for (Entry entry : Assets.guiMap) {
+            TouchButton button = (TouchButton) entry.value;
+            if (button.getRect().contains(touchX, touchY)) {
+                return button;
             }
         }
-        return -1;
+        return nullButton;
     }
 
     @Override
     public boolean keyDown(int keycode) {
-        gameInput.keyDown(keycode);
+        GP.input.keyDown(keycode);
         return false;
     }
 
     @Override
     public boolean keyUp(int keycode) {
-        gameInput.keyUp(keycode);
+        GP.input.keyUp(keycode);
         return false;
     }
 
@@ -78,34 +94,14 @@ public class InputHandlerGame extends InputAdapter {
         float touchY = transformScreenY(screenY);
 
         if (CaveGame.TOUCH) {
-            int touchedKey = getTouchedKey(touchX, touchY);
-            switch (touchedKey) {
-                case UP:
-                    gameInput.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.W : Input.Keys.SPACE);
-                    break;
-                case DOWN:
-                    gameInput.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.S : Input.Keys.CONTROL_LEFT);
-                    break;
-                case LEFT:
-                    gameInput.keyDown(Input.Keys.A);
-                    break;
-                case RIGHT:
-                    gameInput.keyDown(Input.Keys.D);
-                    break;
-                case ALT:
-                    gameInput.keyDown(Input.Keys.ALT_LEFT);
-                    break;
-                case LMB:
-                    gameInput.touchDown(touchX, touchY, Input.Buttons.LEFT);
-                    break;
-                case RMB:
-                    gameInput.touchDown(touchX, touchY, Input.Buttons.RIGHT);
-                    break;
-                default:
-                    gameInput.touchDown(touchX, touchY, touchedKey);
+            TouchButton touchedKey = getTouchedKey(touchX, touchY);
+            if (touchedKey.isMouse()) {
+                GP.input.touchDown(touchX, touchY, touchedKey.getCode());
+            } else {
+                GP.input.keyDown(touchedKey.getCode());
             }
         } else {
-            gameInput.touchDown(touchX, touchY, button);
+            GP.input.touchDown(touchX, touchY, button);
         }
         return false;
     }
@@ -116,26 +112,14 @@ public class InputHandlerGame extends InputAdapter {
         float touchY = transformScreenY(screenY);
 
         if (CaveGame.TOUCH) {
-            int touchedKey = getTouchedKey(touchX, touchY);
-            switch (touchedKey) {
-                case UP:
-                case DOWN:
-                case LEFT:
-                case RIGHT:
-                case ALT:
-                    gameInput.keyUp(GP.keyDownCode);
-                    break;
-                case LMB:
-                    gameInput.touchUp(touchX, touchY, Input.Buttons.LEFT);
-                    break;
-                case RMB:
-                    gameInput.touchUp(touchX, touchY, Input.Buttons.RIGHT);
-                    break;
-                default:
-                    gameInput.touchUp(touchX, touchY, touchedKey);
+            TouchButton touchedKey = getTouchedKey(touchX, touchY);
+            if (touchedKey.isMouse()) {
+                GP.input.touchUp(touchX, touchY, touchedKey.getCode());
+            } else {
+                GP.input.keyUp(GP.input.getKeyDownCode());
             }
         } else {
-            gameInput.touchUp(touchX, touchY, button);
+            GP.input.touchUp(touchX, touchY, button);
         }
         return false;
     }
@@ -144,19 +128,19 @@ public class InputHandlerGame extends InputAdapter {
     public boolean touchDragged(int screenX, int screenY, int pointer) {
         float touchX = transformScreenX(screenX);
         float touchY = transformScreenY(screenY);
-        if (CaveGame.TOUCH && GP.isKeyDown) {
-            if (getTouchedKey(touchX, touchY) == -1) {
-                gameInput.keyUp(GP.keyDownCode);
+        if (CaveGame.TOUCH && GP.input.isKeyDown()) {
+            if (getTouchedKey(touchX, touchY).getCode() == -1) {
+                GP.input.keyUp(GP.input.getKeyDownCode());
             }
         } else {
-            gameInput.touchDragged(touchX, touchY);
+            GP.input.touchDragged(touchX, touchY);
         }
         return false;
     }
 
     @Override
     public boolean scrolled(int amount) {
-        gameInput.scrolled(amount);
+        GP.input.scrolled(amount);
         return false;
     }
 }