/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package wge3.engine;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public final class MenuState extends GameState {
    
    private Stage stage;
    private Skin skin;
    
    private TextField mapNameField;
    private TextButton newGameButton;
    
    public MenuState(GameStateManager gsm) {
        super(gsm);
        init();
    }
    
    @Override
    public void init() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);
        skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
        
        int buttonWidth = 200;
        int buttonHeight = 50;
        int x = Gdx.graphics.getWidth() / 2 - (buttonWidth / 2);
        int y = Gdx.graphics.getHeight() - 200;
        
        Label menuLabel = new Label("WGE3 EXPERIMENTAL HQ", skin);
        menuLabel.setPosition(x, y);
        stage.addActor(menuLabel);
        
        String mapName;
        if (gsm.getNextMap() != null) mapName = gsm.getNextMap();
        else mapName = "";
        
        mapNameField = new TextField(mapName, skin);
        mapNameField.setPosition(x, y - buttonHeight*2);
        mapNameField.setSize(buttonWidth, buttonHeight);
        mapNameField.setMessageText("Enter map name");
        stage.addActor(mapNameField);
        
        newGameButton = new TextButton("NEW GAME", skin);
        newGameButton.setPosition(x, y - buttonHeight*4);
        newGameButton.setSize(buttonWidth, buttonHeight);
        newGameButton.addListener(new ClickListener() {
            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                startGame();
            }
        });
        stage.addActor(newGameButton);
        
        TextButton exitButton = new TextButton("EXIT", skin);
        exitButton.setPosition(x, y - buttonHeight*6);
        exitButton.setSize(buttonWidth, buttonHeight);
        exitButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeListener.ChangeEvent ce, Actor actor) {
                exitGame();
            }
        });
        stage.addActor(exitButton);
    }
    
    public void startGame() {
        if (!Gdx.files.internal("maps/" + mapNameField.getText() + ".tmx").exists()) {
            gsm.setState(0);
            return;
        }
        
        gsm.setNextMap(mapNameField.getText());
        gsm.setState(1);
    }
    
    public void exitGame() {
        Gdx.app.exit();
    }
    
    @Override
    public void update(float delta) {
        stage.act(delta);
        handleInput();
    }
    
    @Override
    public void draw(Batch batch) {
        stage.draw();
    }
    
    @Override
    public void handleInput() {
        if (Gdx.input.isKeyPressed(Keys.ENTER)) startGame();
        else if (Gdx.input.isKeyPressed(Keys.Q)) exitGame();
    }
    
    @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
    }
}

@@ -522,16 +522,69 @@
 .Label;%0A
+import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;%0A
 import c
@@ -623,16 +623,16 @@
 i.Skin;%0A
-
 import c
@@ -681,61 +681,8 @@
 on;%0A
-import com.badlogic.gdx.scenes.scene2d.ui.TextField;%0A
 impo
@@ -797,16 +797,78 @@
 istener;
+%0Aimport com.badlogic.gdx.utils.Array;%0Aimport java.util.Arrays;
 %0A%0Apublic
@@ -984,30 +984,29 @@
 ate 
-TextField mapNameField
+SelectBox mapSelector
 ;%0A  
@@ -1668,176 +1668,189 @@
     
-String mapName;%0A        if (gsm.getNextMap() != null) mapName = gsm.getNextMap();%0A        else mapName = %22%22;%0A        %0A        mapNameField = new TextField(mapName, skin
+mapSelector = new SelectBox(skin);%0A        mapSelector.setItems(new Array(getMapNames()));%0A        if (gsm.getNextMap() != null)%0A            mapSelector.setSelected(gsm.getNextMap()
 );%0A 
@@ -1855,33 +1855,32 @@
 %0A        map
-NameField
+Selector
 .setPosition
@@ -1901,18 +1901,16 @@
 Height*2
-);
 %0A       
@@ -1914,54 +1914,60 @@
     
-mapNameField.setSize(buttonWidth, button
+        + (buttonHeight - mapSelector.get
 Height
+())/2
 );%0A 
@@ -1980,49 +1980,37 @@
  map
-NameField.setMessageText(%22Enter map name%22
+Selector.setWidth(buttonWidth
 );%0A 
@@ -2034,25 +2034,24 @@
 ctor(map
-NameField
+Selector
 );%0A     
@@ -2973,198 +2973,55 @@
     
-if (!Gdx.files.internal(%22maps/%22 + mapNameField.getText() + %22.tmx%22).exists()) %7B%0A            gsm.setState(0);%0A            return;%0A        %7D%0A        %0A        gsm.setNextMap(mapNameField.getText
+gsm.setNextMap((String) mapSelector.getSelected
 ());
@@ -3558,24 +3558,24 @@
 .dispose();%0A
-
         skin
@@ -3592,10 +3592,227 @@
 ;%0A    %7D%0A
+    %0A    private String%5B%5D getMapNames() %7B%0A        return Arrays.stream(Gdx.files.internal(%22maps%22).list(%22.tmx%22))%0A                .map(path -%3E path.nameWithoutExtension())%0A                .toArray(String%5B%5D::new);%0A    %7D%0A
 %7D%0A

Replace map name TextField with SelectBox