From 0ed259db50b9cab761cd5dca5cb229e69886854b Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sun, 29 Sep 2019 03:19:36 +0700 Subject: [PATCH] Rewrite menu fix #1 --- android/assets/json/menu_main_buttons.json | 12 ++ .../assets/json/menu_new_game_buttons.json | 12 ++ .../ru/deadsoftware/cavedroid/GameScreen.java | 23 ++- .../deadsoftware/cavedroid/menu/MenuProc.java | 81 +++++++++++ .../cavedroid/menu/MenuRenderer.java | 132 ------------------ .../cavedroid/menu/objects/Button.java | 41 ++++-- .../cavedroid/menu/objects/ButtonDrawer.java | 7 + .../menu/objects/ButtonEventListener.java | 13 ++ .../cavedroid/menu/submenus/Menu.java | 108 ++++++++++++++ .../cavedroid/menu/submenus/MenuInput.java | 46 ++++++ .../cavedroid/menu/submenus/MenuMain.java | 36 +++++ .../cavedroid/menu/submenus/MenuNewGame.java | 30 ++++ .../deadsoftware/cavedroid/misc/Assets.java | 2 +- .../cavedroid/misc/InputHandlerMenu.java | 77 ---------- .../deadsoftware/cavedroid/misc/Renderer.java | 43 +++++- 15 files changed, 425 insertions(+), 238 deletions(-) create mode 100644 android/assets/json/menu_main_buttons.json create mode 100644 android/assets/json/menu_new_game_buttons.json create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonDrawer.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/objects/ButtonEventListener.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java diff --git a/android/assets/json/menu_main_buttons.json b/android/assets/json/menu_main_buttons.json new file mode 100644 index 0000000..c52f47c --- /dev/null +++ b/android/assets/json/menu_main_buttons.json @@ -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 index 0000000..c61adc3 --- /dev/null +++ b/android/assets/json/menu_new_game_buttons.json @@ -0,0 +1,12 @@ +{ + "creative": { + "label": "Creative" + }, + "survival": { + "label": "Survival", + "type": 0 + }, + "back": { + "label": "Back" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/GameScreen.java index 09604fd..286ba49 100644 --- a/core/src/ru/deadsoftware/cavedroid/GameScreen.java +++ b/core/src/ru/deadsoftware/cavedroid/GameScreen.java @@ -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 index 0000000..0ab0bac --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java @@ -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 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 index fe1834d..0000000 --- a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java +++ /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