1 package ru
.deadsoftware
.cavedroid
.menu
.submenus
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.files
.FileHandle
;
5 import com
.badlogic
.gdx
.graphics
.g2d
.SpriteBatch
;
6 import com
.badlogic
.gdx
.graphics
.g2d
.TextureRegion
;
7 import com
.badlogic
.gdx
.utils
.ArrayMap
;
8 import com
.badlogic
.gdx
.utils
.JsonValue
;
9 import ru
.deadsoftware
.cavedroid
.CaveGame
;
10 import ru
.deadsoftware
.cavedroid
.GameScreen
;
11 import ru
.deadsoftware
.cavedroid
.menu
.objects
.Button
;
12 import ru
.deadsoftware
.cavedroid
.menu
.objects
.ButtonDrawer
;
13 import ru
.deadsoftware
.cavedroid
.menu
.objects
.ButtonEventListener
;
14 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
16 import java
.util
.HashMap
;
18 public abstract class Menu
{
24 * {@link ArrayMap} of {@link Button Buttons} of this menu screen
26 private ArrayMap
<String
, Button
> buttons
;
29 * @param width Viewport width
30 * @param height Viewport height
32 Menu(float width
, float height
) {
39 * If you are loading buttons from json,
40 * override this method and create a HashMap with buttons' keys from json as keys
41 * and {@link ButtonEventListener ButtonEventListeners} as values.
43 * @return empty HashMap if not overridden
45 protected HashMap
<String
, ButtonEventListener
> getButtonEventListeners() {
46 return new HashMap
<>();
50 * You can call this from {@link #initButtons()} to load buttons from json
52 * @param jsonFile A {@link FileHandle} to json file
54 void loadButtonsFromJson(FileHandle jsonFile
) {
55 if (buttons
== null) buttons
= new ArrayMap
<>();
56 HashMap
<String
, ButtonEventListener
> eventListeners
= getButtonEventListeners();
57 JsonValue json
= Assets
.jsonReader
.parse(jsonFile
);
58 int y
= (int) height
/ 4;
59 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next(), y
+= Button
.HEIGHT
+ 10) {
60 buttons
.put(key
.name(),
61 new Button(Assets
.getStringFromJson(key
, "label", ""),
62 (int) width
/ 2 - Button
.WIDTH
/ 2,
63 Assets
.getIntFromJson(key
, "y", y
), Assets
.getIntFromJson(key
, "type", Button
.NORMAL
),
64 eventListeners
.containsKey(key
.name()) ? eventListeners
.get(key
.name()) : () -> {
70 * Draws the menu with background, logo and it's buttons
72 * @param spriter {@link SpriteBatch} that will draw it. Should be already started.
74 public void draw(SpriteBatch spriter
, ButtonDrawer buttonDrawer
, float width
, float height
) {
75 TextureRegion background
= Assets
.textureRegions
.get("background");
76 TextureRegion gamelogo
= Assets
.textureRegions
.get("gamelogo");
78 for (int x
= 0; x
<= width
/ 16; x
++) {
79 for (int y
= 0; y
<= height
/ 16; y
++) {
80 spriter
.draw(background
, x
* 16, y
* 16);
83 spriter
.draw(gamelogo
, width
/ 2 - (float) gamelogo
.getRegionWidth() / 2, 8);
85 float inputX
= Gdx
.input
.getX() * width
/ GameScreen
.getWidth();
86 float inputY
= Gdx
.input
.getY() * height
/ GameScreen
.getHeight();
87 for (Button button
: buttons
.values()) {
88 if (button
.getType() > 0) {
89 if (button
.getRect().contains(inputX
, inputY
) && (!CaveGame
.TOUCH
|| Gdx
.input
.isTouched())) {
95 button
.draw(buttonDrawer
);
99 public ArrayMap
<String
, Button
> getButtons() {
104 * This method is called from constructor and should initialize {@link #buttons} <br>
105 * You can run {@link #loadButtonsFromJson(FileHandle)} from it
107 protected abstract void initButtons();