DEADSOFTWARE

Rewrite menu
authorfred-boy <fredboy@protonmail.com>
Sat, 28 Sep 2019 20:19:36 +0000 (03:19 +0700)
committerfred-boy <fredboy@protonmail.com>
Sun, 29 Sep 2019 04:39:18 +0000 (11:39 +0700)
fix #1

15 files changed:
android/assets/json/menu_main_buttons.json [new file with mode: 0644]
android/assets/json/menu_new_game_buttons.json [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/GameScreen.java
core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java [deleted file]
core/src/ru/deadsoftware/cavedroid/menu/objects/Button.java
core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonDrawer.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonEventListener.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/misc/Assets.java
core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java [deleted file]
core/src/ru/deadsoftware/cavedroid/misc/Renderer.java

diff --git a/android/assets/json/menu_main_buttons.json b/android/assets/json/menu_main_buttons.json
new file mode 100644 (file)
index 0000000..c52f47c
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "new_game": {
+    "label": "New Game"
+  },
+  "load_game": {
+    "label": "Load Game",
+    "type": 0
+  },
+  "quit": {
+    "label": "Quit"
+  }
+}
\ No newline at end of file
diff --git a/android/assets/json/menu_new_game_buttons.json b/android/assets/json/menu_new_game_buttons.json
new file mode 100644 (file)
index 0000000..c61adc3
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "creative": {
+    "label": "Creative"
+  },
+  "survival": {
+    "label": "Survival",
+    "type": 0
+  },
+  "back": {
+    "label": "Back"
+  }
+}
\ No newline at end of file
index 09604fd4b79bb6a4c1661649513cd72f045ab15b..286ba49ed846779c741b282246d9f3bbb9e0e6ef 100644 (file)
@@ -5,10 +5,9 @@ import com.badlogic.gdx.Screen;
 import ru.deadsoftware.cavedroid.game.GameItems;
 import ru.deadsoftware.cavedroid.game.GameProc;
 import ru.deadsoftware.cavedroid.game.GameSaver;
-import ru.deadsoftware.cavedroid.menu.MenuRenderer;
+import ru.deadsoftware.cavedroid.menu.MenuProc;
 import ru.deadsoftware.cavedroid.misc.Assets;
 import ru.deadsoftware.cavedroid.misc.InputHandlerGame;
-import ru.deadsoftware.cavedroid.misc.InputHandlerMenu;
 import ru.deadsoftware.cavedroid.misc.Renderer;
 import ru.deadsoftware.cavedroid.misc.states.AppState;
 import ru.deadsoftware.cavedroid.misc.states.GameState;
@@ -23,18 +22,16 @@ public class GameScreen implements Screen {
     public static boolean SHOW_MAP = false;
 
     private Renderer renderer;
-    private MenuRenderer menuRenderer;
+    private MenuProc menuProc;
 
     private InputHandlerGame inputHandlerGame;
-    private final InputHandlerMenu inputHandlerMenu;
 
     public GameScreen() {
         Assets.load();
         GameItems.load();
-        menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480);
-        inputHandlerMenu = new InputHandlerMenu(menuRenderer);
-        renderer = menuRenderer;
-        Gdx.input.setInputProcessor(inputHandlerMenu);
+        menuProc = new MenuProc(CaveGame.TOUCH ? 320 : 480);
+        renderer = menuProc;
+        Gdx.input.setInputProcessor(menuProc);
     }
 
     public static float getWidth() {
@@ -84,8 +81,8 @@ public class GameScreen implements Screen {
                 CaveGame.APP_STATE = AppState.MENU;
                 CaveGame.MENU_STATE = MenuState.MAIN;
                 GP.dispose();
-                renderer = menuRenderer;
-                Gdx.input.setInputProcessor(inputHandlerMenu);
+                renderer = menuProc;
+                Gdx.input.setInputProcessor(menuProc);
                 break;
         }
         renderer.render();
@@ -95,9 +92,9 @@ public class GameScreen implements Screen {
     public void resize(int width, int height) {
         switch (CaveGame.APP_STATE) {
             case MENU:
-                menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480);
-                Gdx.input.setInputProcessor(inputHandlerMenu);
-                renderer = menuRenderer;
+                menuProc = new MenuProc(CaveGame.TOUCH ? 320 : 480);
+                Gdx.input.setInputProcessor(menuProc);
+                renderer = menuProc;
                 break;
             case GAME:
                 GP.resetRenderer();
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java
new file mode 100644 (file)
index 0000000..0ab0bac
--- /dev/null
@@ -0,0 +1,81 @@
+package ru.deadsoftware.cavedroid.menu;
+
+import com.badlogic.gdx.utils.ObjectMap;
+import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.menu.objects.Button;
+import ru.deadsoftware.cavedroid.menu.submenus.Menu;
+import ru.deadsoftware.cavedroid.menu.submenus.MenuMain;
+import ru.deadsoftware.cavedroid.menu.submenus.MenuNewGame;
+import ru.deadsoftware.cavedroid.misc.Renderer;
+import ru.deadsoftware.cavedroid.misc.states.AppState;
+import ru.deadsoftware.cavedroid.misc.states.GameState;
+import ru.deadsoftware.cavedroid.misc.states.MenuState;
+
+import static ru.deadsoftware.cavedroid.misc.Assets.*;
+
+public class MenuProc extends Renderer {
+
+    private MenuMain mainMenu;
+    private MenuNewGame newGameMenu;
+
+    private Menu currentMenu;
+
+    public MenuProc(int width) {
+        super(width, width * GameScreen.getHeight() / GameScreen.getWidth());
+        mainMenu = new MenuMain(getWidth(), getHeight());
+        newGameMenu = new MenuNewGame(getWidth(), getHeight());
+        currentMenu = mainMenu;
+    }
+
+    private void drawButton(Button button) {
+        spriter.draw(textureRegions.get("button_" + button.getType()), button.getX(), button.getY());
+        setFontColor(255, 255, 255);
+        drawString(button.getLabel(),
+                (button.getX() + button.getWidth() / 2) - (float) getStringWidth(button.getLabel()) / 2,
+                (button.getY() + button.getHeight() / 2) - (float) getStringHeight(button.getLabel()) / 2);
+    }
+
+    @Override
+    public boolean touchUp(int screenX, int screenY, int pointer, int mb) {
+        screenX *= getWidth() / GameScreen.getWidth();
+        screenY *= getHeight() / GameScreen.getHeight();
+        for (ObjectMap.Entry<String, Button> entry : currentMenu.getButtons()) {
+            Button button = entry.value;
+            if (button.getRect().contains(screenX, screenY)) {
+                if (button.getType() > 0) {
+                    button.clicked();
+                }
+                break;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public void render() {
+        spriter.begin();
+        switch (CaveGame.MENU_STATE) {
+            case MAIN:
+                currentMenu = mainMenu;
+                break;
+            case NEW_GAME:
+                currentMenu = newGameMenu;
+                break;
+            case LOADING:
+                drawString("Generating World...");
+                CaveGame.APP_STATE = AppState.GAME;
+                CaveGame.GAME_STATE = GameState.PLAY;
+                break;
+            case SAVING:
+                drawString("Saving Game...");
+                CaveGame.APP_STATE = AppState.MENU;
+                CaveGame.MENU_STATE = MenuState.MAIN;
+                break;
+        }
+        currentMenu.draw(spriter, this::drawButton, getWidth(), getHeight());
+        drawString("CaveDroid " + CaveGame.VERSION, 0,
+                getHeight() - getStringHeight("CaveDroid " + CaveGame.VERSION) * 1.5f);
+        spriter.end();
+    }
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java
deleted file mode 100644 (file)
index fe1834d..0000000
+++ /dev/null
@@ -1,132 +0,0 @@
-package ru.deadsoftware.cavedroid.menu;
-
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.graphics.g2d.TextureRegion;
-import com.badlogic.gdx.utils.Array;
-import org.jetbrains.annotations.NotNull;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
-import ru.deadsoftware.cavedroid.game.GameProc;
-import ru.deadsoftware.cavedroid.game.GameSaver;
-import ru.deadsoftware.cavedroid.menu.objects.Button;
-import ru.deadsoftware.cavedroid.misc.Renderer;
-import ru.deadsoftware.cavedroid.misc.states.AppState;
-import ru.deadsoftware.cavedroid.misc.states.GameState;
-import ru.deadsoftware.cavedroid.misc.states.MenuState;
-
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-import static ru.deadsoftware.cavedroid.misc.Assets.*;
-
-public class MenuRenderer extends Renderer {
-
-    public final Array<Button> menuMainBtns;
-    public final Array<Button> menuNGBtns;
-
-    public MenuRenderer(int width) {
-        super(width, width * GameScreen.getHeight() / GameScreen.getWidth());
-        //main menu
-        menuMainBtns = new Array<>();
-        menuMainBtns.add(new Button("New game", getWidth() / 2 - 100, getHeight() / 4));
-        menuMainBtns.add(new Button("Load game", getWidth() / 2 - 100, getHeight() / 4 + 30, GameSaver.exists() ? 1 : 0));
-        menuMainBtns.add(new Button("Quit", getWidth() / 2 - 100, getHeight() / 4 + 60));
-        //new game menu
-        menuNGBtns = new Array<>();
-        menuNGBtns.add(new Button("Survival", getWidth() / 2 - 100, getHeight() / 4, 0));
-        menuNGBtns.add(new Button("Creative", getWidth() / 2 - 100, getHeight() / 4 + 30));
-        menuNGBtns.add(new Button("Back", getWidth() / 2 - 100, getHeight() / 4 + 60));
-
-    }
-
-    private void newGame(int gameMode) {
-        GP = new GameProc(gameMode);
-        GP.player.respawn();
-        GameSaver.save(GP);
-        CaveGame.APP_STATE = AppState.LOAD;
-    }
-
-    public void buttonClicked(@NotNull Button button) {
-        switch (button.getLabel().toLowerCase()) {
-            case "new game":
-                CaveGame.MENU_STATE = MenuState.NEW_GAME;
-                break;
-            case "load game":
-                CaveGame.APP_STATE = AppState.LOAD;
-                GP = GameSaver.load();
-                break;
-            case "quit":
-                Gdx.app.exit();
-                break;
-            case "survival":
-                newGame(0);
-                break;
-            case "creative":
-                newGame(1);
-                break;
-            case "back":
-                CaveGame.MENU_STATE = MenuState.MAIN;
-                break;
-        }
-    }
-
-    private void drawButton(Button button) {
-        spriter.draw(textureRegions.get("button_" + button.getType()), button.getX(), button.getY());
-        setFontColor(255, 255, 255);
-        drawString(button.getLabel(),
-                (button.getX() + button.getWidth() / 2) - (float) getStringWidth(button.getLabel()) / 2,
-                (button.getY() + button.getHeight() / 2) - (float) getStringHeight(button.getLabel()) / 2);
-    }
-
-    private void drawButtons(Array<Button> buttons) {
-        for (Button button : buttons) {
-            if (button.getType() > 0) {
-                if (button.getRect().contains(Gdx.input.getX() * getWidth() / GameScreen.getWidth(),
-                        Gdx.input.getY() * getHeight() / GameScreen.getHeight()) &&
-                        (!CaveGame.TOUCH || Gdx.input.isTouched()))
-                    button.setType(2);
-                else button.setType(1);
-            }
-            drawButton(button);
-        }
-    }
-
-    private void drawLabel(String str) {
-        drawString(str);
-    }
-
-    @Override
-    public void render() {
-        TextureRegion background = textureRegions.get("background");
-        TextureRegion gamelogo = textureRegions.get("gamelogo");
-
-        spriter.begin();
-        for (int x = 0; x <= getWidth() / 16; x++) {
-            for (int y = 0; y <= getHeight() / 16; y++) {
-                spriter.draw(background, x * 16, y * 16);
-            }
-        }
-        spriter.draw(gamelogo, getWidth() / 2 - (float) gamelogo.getRegionWidth() / 2, 8);
-
-        switch (CaveGame.MENU_STATE) {
-            case MAIN:
-                drawButtons(menuMainBtns);
-                break;
-            case NEW_GAME:
-                drawButtons(menuNGBtns);
-                break;
-            case LOADING:
-                drawLabel("Generating World...");
-                CaveGame.APP_STATE = AppState.GAME;
-                CaveGame.GAME_STATE = GameState.PLAY;
-                break;
-            case SAVING:
-                drawLabel("Saving Game...");
-                CaveGame.APP_STATE = AppState.MENU;
-                CaveGame.MENU_STATE = MenuState.MAIN;
-                break;
-        }
-
-        drawString("CaveDroid " + CaveGame.VERSION, 0,
-                getHeight() - getStringHeight("CaveDroid " + CaveGame.VERSION) * 1.5f);
-        spriter.end();
-    }
-}
index 123b9b55f8ac646f41a15ea31dca430e8b2021fb..88dc5b3030747caf2c399514b27a4132921e39e9 100644 (file)
@@ -4,26 +4,31 @@ import com.badlogic.gdx.math.Rectangle;
 
 public class Button {
 
+    public static final int WIDTH = 200;
+    public static final int HEIGHT = 20;
+
+    public static final int
+            DISABLED = 0,
+            NORMAL = 1,
+            SELECTED = 2;
+
+    private ButtonEventListener listener;
+
     private final Rectangle rect;
     private final String label;
     private int type;
 
-    public Button(String label, float x, float y, float width, float height, int type) {
+    /**
+     * @param label Label to be shown on button
+     * @param type  Type of button where 0 - disabled,  1 - normal, 2 - selected.
+     *              You should use these constants
+     *              {@link #DISABLED} {@link #NORMAL} {@link #SELECTED}
+     */
+    public Button(String label, int x, int y, int type, ButtonEventListener listener) {
         this.label = label;
-        rect = new Rectangle(x, y, width, height);
+        rect = new Rectangle(x, y, WIDTH, HEIGHT);
         this.type = type;
-    }
-
-    public Button(String label, float x, float y, float width, float height) {
-        this(label, x, y, width, height, 1);
-    }
-
-    public Button(String label, float x, float y, int type) {
-        this(label, x, y, 200, 20, type);
-    }
-
-    public Button(String label, float x, float y) {
-        this(label, x, y, 200, 20, 1);
+        this.listener = listener;
     }
 
     public Rectangle getRect() {
@@ -58,4 +63,12 @@ public class Button {
         this.type = type;
     }
 
+    public void draw(ButtonDrawer drawer) {
+        drawer.draw(this);
+    }
+
+    public void clicked() {
+        listener.buttonClicked();
+    }
+
 }
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonDrawer.java b/core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonDrawer.java
new file mode 100644 (file)
index 0000000..070f973
--- /dev/null
@@ -0,0 +1,7 @@
+package ru.deadsoftware.cavedroid.menu.objects;
+
+public interface ButtonDrawer {
+
+    void draw(Button button);
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonEventListener.java b/core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonEventListener.java
new file mode 100644 (file)
index 0000000..3797295
--- /dev/null
@@ -0,0 +1,13 @@
+package ru.deadsoftware.cavedroid.menu.objects;
+
+/**
+ * A {@link Button} event listener. Should be sent as lambda to Button's constructor.
+ */
+public interface ButtonEventListener {
+
+    /**
+     * Will be called by {@link Button} when clicked
+     */
+    void buttonClicked();
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java
new file mode 100644 (file)
index 0000000..f9d648a
--- /dev/null
@@ -0,0 +1,108 @@
+package ru.deadsoftware.cavedroid.menu.submenus;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.files.FileHandle;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
+import com.badlogic.gdx.utils.ArrayMap;
+import com.badlogic.gdx.utils.JsonValue;
+import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.menu.objects.Button;
+import ru.deadsoftware.cavedroid.menu.objects.ButtonDrawer;
+import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener;
+import ru.deadsoftware.cavedroid.misc.Assets;
+
+import java.util.HashMap;
+
+public abstract class Menu {
+
+    private float width;
+    private float height;
+
+    /**
+     * {@link ArrayMap} of {@link Button Buttons} of this menu screen
+     */
+    private ArrayMap<String, Button> buttons;
+
+    /**
+     * @param width  Viewport width
+     * @param height Viewport height
+     */
+    Menu(float width, float height) {
+        this.width = width;
+        this.height = height;
+        initButtons();
+    }
+
+    /**
+     * If you are loading buttons from json,
+     * override this method and create a HashMap with buttons' keys from json as keys
+     * and {@link ButtonEventListener ButtonEventListeners} as values.
+     *
+     * @return empty HashMap if not overridden
+     */
+    protected HashMap<String, ButtonEventListener> getButtonEventListeners() {
+        return new HashMap<>();
+    }
+
+    /**
+     * You can call this from {@link #initButtons()} to load buttons from json
+     *
+     * @param jsonFile A {@link FileHandle} to json file
+     */
+    void loadButtonsFromJson(FileHandle jsonFile) {
+        if (buttons == null) buttons = new ArrayMap<>();
+        HashMap<String, ButtonEventListener> eventListeners = getButtonEventListeners();
+        JsonValue json = Assets.jsonReader.parse(jsonFile);
+        int y = (int) height / 4;
+        for (JsonValue key = json.child(); key != null; key = key.next(), y += Button.HEIGHT + 10) {
+            buttons.put(key.name(),
+                    new Button(Assets.getStringFromJson(key, "label", ""),
+                            (int) width / 2 - Button.WIDTH / 2,
+                            Assets.getIntFromJson(key, "y", y), Assets.getIntFromJson(key, "type", Button.NORMAL),
+                            eventListeners.containsKey(key.name()) ? eventListeners.get(key.name()) : () -> {
+                            }));
+        }
+    }
+
+    /**
+     * Draws the menu with background, logo and it's buttons
+     *
+     * @param spriter {@link SpriteBatch} that will draw it. Should be already started.
+     */
+    public void draw(SpriteBatch spriter, ButtonDrawer buttonDrawer, float width, float height) {
+        TextureRegion background = Assets.textureRegions.get("background");
+        TextureRegion gamelogo = Assets.textureRegions.get("gamelogo");
+
+        for (int x = 0; x <= width / 16; x++) {
+            for (int y = 0; y <= height / 16; y++) {
+                spriter.draw(background, x * 16, y * 16);
+            }
+        }
+        spriter.draw(gamelogo, width / 2 - (float) gamelogo.getRegionWidth() / 2, 8);
+
+        float inputX = Gdx.input.getX() * width / GameScreen.getWidth();
+        float inputY = Gdx.input.getY() * height / GameScreen.getHeight();
+        for (Button button : buttons.values()) {
+            if (button.getType() > 0) {
+                if (button.getRect().contains(inputX, inputY) && (!CaveGame.TOUCH || Gdx.input.isTouched())) {
+                    button.setType(2);
+                } else {
+                    button.setType(1);
+                }
+            }
+            button.draw(buttonDrawer);
+        }
+    }
+
+    public ArrayMap<String, Button> getButtons() {
+        return buttons;
+    }
+
+    /**
+     * This method is called from constructor and should initialize {@link #buttons} <br>
+     * You can run {@link #loadButtonsFromJson(FileHandle)} from it
+     */
+    protected abstract void initButtons();
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java
new file mode 100644 (file)
index 0000000..2ebe427
--- /dev/null
@@ -0,0 +1,46 @@
+package ru.deadsoftware.cavedroid.menu.submenus;
+
+import com.badlogic.gdx.Gdx;
+import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.game.GameProc;
+import ru.deadsoftware.cavedroid.game.GameSaver;
+import ru.deadsoftware.cavedroid.misc.states.AppState;
+import ru.deadsoftware.cavedroid.misc.states.MenuState;
+
+import static ru.deadsoftware.cavedroid.GameScreen.GP;
+
+class MenuInput {
+
+    private static void startNewGame(int gameMode) {
+        GP = new GameProc(gameMode);
+        GP.player.respawn();
+        GameSaver.save(GP);
+        CaveGame.APP_STATE = AppState.LOAD;
+    }
+
+    static void newGameClicked() {
+        CaveGame.MENU_STATE = MenuState.NEW_GAME;
+    }
+
+    static void loadGameClicked() {
+        CaveGame.APP_STATE = AppState.LOAD;
+        GP = GameSaver.load();
+    }
+
+    static void quitClicked() {
+        Gdx.app.exit();
+    }
+
+    static void survivalClicked() {
+        startNewGame(0);
+    }
+
+    static void creativeClicked() {
+        startNewGame(1);
+    }
+
+    static void backClicked() {
+        CaveGame.MENU_STATE = MenuState.MAIN;
+    }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java
new file mode 100644 (file)
index 0000000..da84b26
--- /dev/null
@@ -0,0 +1,36 @@
+package ru.deadsoftware.cavedroid.menu.submenus;
+
+import com.badlogic.gdx.Gdx;
+import ru.deadsoftware.cavedroid.game.GameSaver;
+import ru.deadsoftware.cavedroid.menu.objects.Button;
+import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener;
+
+import java.util.HashMap;
+
+public class MenuMain extends Menu {
+
+    /**
+     * @param width  Viewport width
+     * @param height Viewport height
+     */
+    public MenuMain(float width, float height) {
+        super(width, height);
+    }
+
+    @Override
+    protected HashMap<String, ButtonEventListener> getButtonEventListeners() {
+        HashMap<String, ButtonEventListener> map = new HashMap<>();
+        map.put("new_game", MenuInput::newGameClicked);
+        map.put("load_game", MenuInput::loadGameClicked);
+        map.put("quit", MenuInput::quitClicked);
+        return map;
+    }
+
+    @Override
+    protected void initButtons() {
+        loadButtonsFromJson(Gdx.files.internal("json/menu_main_buttons.json"));
+        if (GameSaver.exists()) {
+            getButtons().get("load_game").setType(Button.NORMAL);
+        }
+    }
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java
new file mode 100644 (file)
index 0000000..c0ab739
--- /dev/null
@@ -0,0 +1,30 @@
+package ru.deadsoftware.cavedroid.menu.submenus;
+
+import com.badlogic.gdx.Gdx;
+import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener;
+
+import java.util.HashMap;
+
+public class MenuNewGame extends Menu {
+    /**
+     * @param width  Viewport width
+     * @param height Viewport height
+     */
+    public MenuNewGame(float width, float height) {
+        super(width, height);
+    }
+
+    @Override
+    protected HashMap<String, ButtonEventListener> getButtonEventListeners() {
+        HashMap<String, ButtonEventListener> map = new HashMap<>();
+        map.put("survival", MenuInput::survivalClicked);
+        map.put("creative", MenuInput::creativeClicked);
+        map.put("back", MenuInput::backClicked);
+        return map;
+    }
+
+    @Override
+    protected void initButtons() {
+        loadButtonsFromJson(Gdx.files.internal("json/menu_new_game_buttons.json"));
+    }
+}
index 944dec0cac70dd82b77d5c9a6d2f782b9e64f81b..93a5f86ea4a9509bfcd40f09f240db06a05ccb64 100644 (file)
@@ -18,7 +18,7 @@ public class Assets {
     public static final JsonReader jsonReader = new JsonReader();
 
     private static final GlyphLayout glyphLayout = new GlyphLayout();
-    static final BitmapFont minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);;
+    static final BitmapFont minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
 
     public static final Sprite[][] playerSprite = new Sprite[2][4];
     public static final Sprite[][] pigSprite = new Sprite[2][2];
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java
deleted file mode 100644 (file)
index b97b5fe..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-package ru.deadsoftware.cavedroid.misc;
-
-import com.badlogic.gdx.InputProcessor;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
-import ru.deadsoftware.cavedroid.menu.MenuRenderer;
-import ru.deadsoftware.cavedroid.menu.objects.Button;
-
-public class InputHandlerMenu implements InputProcessor {
-
-    private final MenuRenderer menuRenderer;
-
-    public InputHandlerMenu(MenuRenderer menuRenderer) {
-        this.menuRenderer = menuRenderer;
-    }
-
-    @Override
-    public boolean keyDown(int keycode) {
-        return false;
-    }
-
-    @Override
-    public boolean keyUp(int keycode) {
-        return false;
-    }
-
-    @Override
-    public boolean keyTyped(char character) {
-        return false;
-    }
-
-    @Override
-    public boolean touchDown(int screenX, int screenY, int pointer, int mb) {
-        return false;
-    }
-
-    @Override
-    public boolean touchUp(int screenX, int screenY, int pointer, int mb) {
-        screenX *= menuRenderer.getWidth() / GameScreen.getWidth();
-        screenY *= menuRenderer.getHeight() / GameScreen.getHeight();
-        switch (CaveGame.MENU_STATE) {
-            case MAIN:
-                for (Button button : menuRenderer.menuMainBtns) {
-                    if (button.getRect().contains(screenX, screenY) && button.getType() > 0) {
-                        menuRenderer.buttonClicked(button);
-                        break;
-                    }
-                }
-                break;
-            case NEW_GAME:
-                for (Button button : menuRenderer.menuNGBtns) {
-                    if (button.getRect().contains(screenX, screenY) && button.getType() > 0) {
-                        menuRenderer.buttonClicked(button);
-                        break;
-                    }
-                }
-                break;
-        }
-        return false;
-    }
-
-    @Override
-    public boolean touchDragged(int screenX, int screenY, int pointer) {
-        return false;
-    }
-
-    @Override
-    public boolean mouseMoved(int screenX, int screenY) {
-        return false;
-    }
-
-    @Override
-    public boolean scrolled(int amount) {
-        return false;
-    }
-
-}
index 4919bc86654461fd27e9e63b9b79a9e592e8dee3..179b76acabb8bd97a7280b16410ab094b0a98089 100644 (file)
@@ -1,11 +1,12 @@
 package ru.deadsoftware.cavedroid.misc;
 
+import com.badlogic.gdx.InputProcessor;
 import com.badlogic.gdx.graphics.OrthographicCamera;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
 import ru.deadsoftware.cavedroid.GameScreen;
 
-public abstract class Renderer {
+public abstract class Renderer implements InputProcessor {
 
     private final OrthographicCamera camera;
 
@@ -65,4 +66,44 @@ public abstract class Renderer {
 
     public abstract void render();
 
+    @Override
+    public boolean keyDown(int keycode) {
+        return false;
+    }
+
+    @Override
+    public boolean keyUp(int keycode) {
+        return false;
+    }
+
+    @Override
+    public boolean keyTyped(char character) {
+        return false;
+    }
+
+    @Override
+    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
+        return false;
+    }
+
+    @Override
+    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
+        return false;
+    }
+
+    @Override
+    public boolean touchDragged(int screenX, int screenY, int pointer) {
+        return false;
+    }
+
+    @Override
+    public boolean mouseMoved(int screenX, int screenY) {
+        return false;
+    }
+
+    @Override
+    public boolean scrolled(int amount) {
+        return false;
+    }
+
 }