Java tutorial
/******************************************************************************* * Copyright (c) HALive, 2015. * For Licence information see LICENSE.md ******************************************************************************/ package halive.shootinoutside.menu; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Button; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.utils.viewport.FitViewport; import halive.shootinoutside.ShootinOutside; import halive.shootinoutside.game.ShootinOutsideGame; import halive.shootinoutside.menu.dialogs.game.JoinGameDialog; import halive.shootinoutside.menu.dialogs.mapeditor.OpenMapEditorDialog; import halive.shootinoutside.misc.UIConstants; import halive.shootinoutside.util.ui.ClickHandler; import halive.shootinoutside.util.ui.MessageDialog; import static halive.shootinoutside.misc.UIConstants.Menues.*; public class MainMenu extends ScreenAdapter { private ShootinOutside game; private Stage stage; private Skin skin; private Button joinButton; private Button hostButton; private Button editorButton; private JoinGameDialog.LastValues lastValues = null; public MainMenu(ShootinOutside game) { this.game = game; } public Skin getSkin() { return skin; } private void init() { float h = Gdx.graphics.getHeight(); float w = Gdx.graphics.getWidth(); stage = new Stage(new FitViewport(w, h)); skin = new Skin(Gdx.files.internal(SKIN_PATH)); Table mainTable = new Table(skin); mainTable.setColor(Color.DARK_GRAY); Image logo = new Image(new Texture(Gdx.files.internal(LOGO_PATH))); Button joinGameButton = new TextButton("Join Game", skin); Button hostGameButton = new TextButton("Host Game", skin); Button mapEditorButton = new TextButton("Map Editor", skin); Button aboutGameButton = new TextButton("About", skin); Button exitButton = new TextButton("Quit Game", skin); joinGameButton.addListener(new ClickHandler() { @Override public void onClicked(InputEvent evt, float x, float y) { joinGameClicked(evt, x, y); } }); hostGameButton.addListener(new ClickHandler() { @Override public void onClicked(InputEvent evt, float x, float y) { hostGameClicked(evt, x, y); } }); mapEditorButton.addListener(new ClickHandler() { @Override public void onClicked(InputEvent evt, float x, float y) { editorClicked(evt, x, y); } }); aboutGameButton.addListener(new ClickHandler() { @Override public void onClicked(InputEvent evt, float x, float y) { showAboutDialog(); } }); exitButton.addListener(new ClickHandler() { @Override public void onClicked(InputEvent evt, float x, float y) { Gdx.app.exit(); } }); joinButton = joinGameButton; hostButton = hostGameButton; editorButton = mapEditorButton; mainTable.add(logo).padBottom(PADDING_BOTTOM).row(); Button[] buttons = { joinGameButton, hostGameButton, editorButton, aboutGameButton, exitButton }; for (int i = 0; i < buttons.length; i++) { mainTable.add(buttons[i]).size(BUTTON_WIDTH, BUTTON_HEIGHT).padBottom(PADDING_BOTTOM).row(); } mainTable.setFillParent(true); stage.addActor(mainTable); Gdx.input.setInputProcessor(stage); } private void joinGameClicked(InputEvent evt, float x, float y) { JoinGameDialog dialog = new JoinGameDialog(this, lastValues); dialog.show(stage); } private void hostGameClicked(InputEvent evt, float x, float y) { //TODO implement Internal server ShootinOutsideGame game = new ShootinOutsideGame(this); game.init(); this.game.setScreen(game); } private void editorClicked(InputEvent evt, float x, float y) { OpenMapEditorDialog dialog = new OpenMapEditorDialog("Launch Map Editor", this); dialog.show(stage); } private void showAboutDialog() { Dialog dialog = new Dialog("About", UIConstants.Dialogs.loadSkin()); Table content = dialog.getContentTable(); for (String s : UIConstants.Dialogs.About.ABOUT_DIALOG_TEXT) { content.add(new Label(s, UIConstants.Dialogs.loadSkin())) .padBottom(UIConstants.Dialogs.PADDING_BOTTOM / 3) .padTop(UIConstants.Dialogs.PADDING_BOTTOM / 3).row(); } dialog.button("Close"); dialog.show(stage); } @Override public void show() { init(); } @Override public void render(float delta) { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(delta); stage.draw(); } @Override public void resize(int width, int height) { stage.getViewport().update(width, height, false); } @Override public void dispose() { stage.dispose(); skin.dispose(); } public Stage getStage() { return stage; } public ShootinOutside getGame() { return game; } public void showMessageDialog(String title, String msg) { MessageDialog dialog = new MessageDialog(title, msg, UIConstants.Dialogs.loadSkin()); dialog.show(stage); } public void setLastValues(JoinGameDialog.LastValues lastValues) { this.lastValues = lastValues; } }