Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2015-2016, theosirian * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.theosirian.ppioo.controllers; import com.badlogic.gdx.Input; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.badlogic.gdx.utils.Array; import com.theosirian.ppioo.Puno; import com.theosirian.ppioo.football.Player; import com.theosirian.ppioo.football.Player.PlayerComp; import com.theosirian.ppioo.util.Utils; import com.theosirian.ppioo.widget.CheckBox; import com.theosirian.ppioo.widget.ImageButton; import com.theosirian.ppioo.widget.PlayerSortDialog; import com.theosirian.ppioo.widget.TextButton; import java.text.NumberFormat; /** * Created by theosirian on 07/01/2016. */ @SuppressWarnings("Duplicates") public class PlayerSelectionController extends Controller { private NumberFormat percentFormatter; private Label titleLabel; private TextButton randomizePlayersButton; private TextButton randomizePositionsButton; private TextButton playerSortButton; private TextButton positionSortButton; private TextButton clearButton; private TextButton backButton; private TextButton helpButton; private TextButton proceedButton; private ScrollPane playersPane; private ScrollPane positionsPane; private Table playersTable; private Table positionsTable; private PlayerComp playerPlayerComp, positionPlayerComp; public PlayerSelectionController(Puno game) throws Exception { super(game); if (game.data.getOwnedPlayers().size < game.data.getTeamSize()) throw new Exception("Something went wrong. You can't be here without enough players to form a team."); } @Override protected void createComponents() { percentFormatter = NumberFormat.getPercentInstance(); percentFormatter.setMaximumFractionDigits(0); playerPlayerComp = new PlayerComp(Player.firstNameComparator, false); rootLayout = new Table(); rootLayout.setFillParent(true); rootLayout.setBackground(game.skin.newDrawable("white", Color.DARK_GRAY)); titleLabel = new Label("Please choose your players and positions:", game.skin, "big.white"); titleLabel.setAlignment(2); // 2 == center randomizePlayersButton = new TextButton("Randomize Players", game.skin); randomizePlayersButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Array<Player> copy = game.data.getOwnedPlayers(); copy.shuffle(); copy.truncate(game.data.getTeamSize()); game.data.getTeam().clear(); game.data.getTeam().addAll(copy); invalidate(); } }); randomizePositionsButton = new TextButton("Randomize Positions", game.skin); randomizePositionsButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); game.data.getTeam().shuffle(); invalidate(); } }); playerSortButton = new TextButton("Sort Players", game.skin); playerSortButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Dialog info = new PlayerSortDialog("Sort Players By:", game.skin, false) { @Override protected void result(Object object) { if (object == null) { return; } playerPlayerComp = (PlayerComp) object; PlayerSelectionController.this.invalidate(); } }; info.show(game.stage); invalidate(); } }); positionSortButton = new TextButton("Sort Positions", game.skin); positionSortButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Dialog info = new PlayerSortDialog("Sort Positions By:", game.skin, false) { @Override protected void result(Object object) { if (object == null) { return; } positionPlayerComp = (PlayerComp) object; PlayerSelectionController.this.invalidate(); } }; info.show(game.stage); invalidate(); } }); clearButton = new TextButton("Clear", game.skin); clearButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); game.data.getTeam().clear(); invalidate(); } }); backButton = new TextButton("Return", game.skin, "error"); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); game.changeController = MenuController.class; } }); helpButton = new TextButton("Help", game.skin); helpButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Dialog info = new Dialog("Help!", game.skin); info.text( "In this screen you are shown your players in the left and the current team setup in the right.\n" + "To advance you need to select 6 players and their positions, one goalkeeper and five kickers."); info.button("Close", false); info.key(Input.Keys.ENTER, false); info.key(Keys.ESCAPE, false); info.show(game.stage); } }); proceedButton = new TextButton("Proceed", game.skin, "success"); proceedButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (game.data.getTeam().size == game.data.getTeamSize()) { game.getSoundController().soundPlay(1); game.changeController = GameController.class; } else { Dialog info = new Dialog("Team not selected!", game.skin); info.text("You haven't selected the right amount of players.\nPlease select " + game.data.getTeamSize() + " players."); info.button("Close", false); info.key(Input.Keys.ENTER, false); info.key(Keys.ESCAPE, false); info.show(game.stage); } } }); playersTable = new Table(); positionsTable = new Table(); playersPane = new ScrollPane(playersTable, game.skin); positionsPane = new ScrollPane(positionsTable, game.skin); } @Override protected void buildUI() { rootLayout.clear(); rootLayout.add(titleLabel).pad(8).expandX().fill().row(); Table selectionButtonsTable = new Table(); selectionButtonsTable.background(game.skin.newDrawable("white", Color.GRAY)); selectionButtonsTable.add(randomizePlayersButton).pad(8, 8, 4, 4).expandX().fillX().uniformX(); selectionButtonsTable.add(playerSortButton).pad(8, 8, 4, 4).expandX().fillX().uniformX(); selectionButtonsTable.add(randomizePositionsButton).pad(8, 8, 4, 4).expandX().fillX().uniformX(); selectionButtonsTable.add(positionSortButton).pad(8, 4, 4, 8).expandX().fillX().uniformX(); selectionButtonsTable.row(); rootLayout.add(selectionButtonsTable).expandX().fill().row(); Table panelsTable = new Table(); panelsTable.background(game.skin.newDrawable("white", Color.GRAY)); buildPlayerPane(game.data.getOwnedPlayers()); panelsTable.add(playersPane).pad(4, 8, 8, 4).expand().fill().uniformX(); buildPlayerSetupList(game.data.getTeam()); panelsTable.add(positionsPane).pad(4, 4, 8, 8).expand().fill().uniformX(); panelsTable.row(); rootLayout.add(panelsTable).expand().fill().row(); Table buttonTable = new Table(); buttonTable.add(backButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); buttonTable.add(helpButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); buttonTable.add(clearButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); buttonTable.add(proceedButton).pad(8, 4, 8, 8).expandX().fillX().uniformX(); rootLayout.add(buttonTable).expandX().fill().row(); } protected void buildPlayerPane(Array<Player> players) { playersTable.clear(); Drawable odd = game.skin.newDrawable("white", Color.valueOf("C0C0C0FF")); Drawable even = game.skin.newDrawable("white", Color.valueOf("E0E0E0FF")); Drawable[] turn = new Drawable[] { odd, even }; players.sort(playerPlayerComp.getComparator()); if (playerPlayerComp.isReverse()) { players.reverse(); } int counter = 0; for (final Player player : players) { playersTable.add(buildPlayerListItem(player, turn[counter % 2])).expand().fill(); counter++; playersTable.row(); } playersPane.setWidget(playersTable); } private Actor buildPlayerListItem(Player player, Drawable background) { CheckBox checkBox = new CheckBox(null, game.skin, "slider.horizontal"); checkBox.putExtra("player", player); checkBox.setChecked(game.data.getTeam().contains(player, true)); checkBox.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); System.out.println("Clicked!"); CheckBox cb = (CheckBox) event.getListenerActor(); if (cb.isChecked()) { if (game.data.getTeam().size < game.data.getTeamSize()) { game.data.getTeam().add((Player) cb.getExtra("player")); } else { cb.setChecked(false); cb.invalidate(); } } else { game.data.getTeam().removeValue((Player) cb.getExtra("player"), false); } invalidate(); } }); Table innerTable = new Table(); innerTable.setBackground(background); Table outerTable = new Table(); outerTable.setBackground(background); outerTable.add(innerTable).expand().fill(); outerTable.add(checkBox).fill().padRight(16); innerTable.add(new Label(player.getFirstName() + " " + player.getLastName() + ", " + player.getAge(), game.skin, "medium")).pad(4, 0, 2, 0).colspan(2).expandX(); innerTable.row(); innerTable.add(new Label("Quality: " + percentFormatter.format(player.getQuality()), game.skin, "default")) .pad(2, 0, 4, 0).expandX(); innerTable.add( new Label("Confidence: " + percentFormatter.format(player.getConfidence()), game.skin, "default")) .pad(2, 0, 4, 0).expandX(); innerTable.row(); innerTable.add(new Label("Victories: " + player.getVictories(), game.skin, "small")).pad(2, 0, 2, 0) .expandX(); innerTable.add(new Label("Defeats: " + player.getDefeats(), game.skin, "small")).pad(2, 0, 2, 0).expandX(); innerTable.row(); return outerTable; } protected void buildPlayerSetupList(Array<Player> players) { positionsTable.clear(); Drawable odd = game.skin.newDrawable("white", Color.valueOf("C0C0C0FF")); Drawable even = game.skin.newDrawable("white", Color.valueOf("E0E0E0FF")); Drawable[] turn = new Drawable[] { odd, even }; if (positionPlayerComp != null) { players.sort(positionPlayerComp.getComparator()); if (positionPlayerComp.isReverse()) { players.reverse(); } positionPlayerComp = null; } int counter = 0; for (final Player player : players) { positionsTable.add(buildPlayerSetupListItem(player, turn[counter % 2])).expand().fill(); counter++; positionsTable.row(); } positionsPane.setWidget(positionsTable); } protected Actor buildPlayerSetupListItem(Player player, Drawable background) { int index = game.data.getTeam().indexOf(player, true); ImageButton upButton = new ImageButton(game.skin, "arrow.up"); upButton.putExtra("player", player); upButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Player player = (Player) ((ImageButton) event.getListenerActor()).getExtra("player"); int index = game.data.getTeam().indexOf(player, false); System.out.println("Swap " + (index - 1) + " with " + index); if (index - 1 > -1) { game.data.getTeam().swap(index - 1, index); invalidate(); } } }); upButton.setDisabled(!(index - 1 > -1)); ImageButton downButton = new ImageButton(game.skin, "arrow.down"); downButton.putExtra("player", player); downButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Player player = (Player) ((ImageButton) event.getListenerActor()).getExtra("player"); int index = game.data.getTeam().indexOf(player, false); System.out.println("Swap " + index + " with " + (index + 1)); if (index + 1 < game.data.getTeamSize()) { game.data.getTeam().swap(index, index + 1); invalidate(); } } }); downButton.setDisabled(!(index + 1 < game.data.getTeamSize())); Table table = new Table(); table.setBackground(background); Table infoTable = new Table(); infoTable.setBackground(background); Table buttonTable = new Table(); buttonTable.setBackground(background); buttonTable.add(upButton).expand().fill(); buttonTable.row(); buttonTable.add(downButton).expand().fill(); buttonTable.row(); infoTable.add(new Label(player.getFirstName() + " " + player.getLastName() + ", " + player.getAge(), game.skin, "medium")).pad(4, 0, 2, 0).expandX(); infoTable.add(new Label(Utils.getPositionString(index), game.skin, "big")); infoTable.row(); infoTable.add(new Label("Quality: " + percentFormatter.format(player.getQuality()), game.skin, "default")) .pad(2, 0, 4, 0).expandX(); infoTable.add( new Label("Confidence: " + percentFormatter.format(player.getConfidence()), game.skin, "default")) .pad(2, 0, 4, 0).expandX(); infoTable.row(); /* infoTable.add(new Label("Victories: " + player.getVictories(), game.skin, "small")).pad(2, 0, 2, 0).expandX(); infoTable.add(new Label("Defeats: " + player.getDefeats(), game.skin, "small")).pad(2, 0, 2, 0).expandX(); infoTable.row();*/ table.add(infoTable).expand().fill(); table.add(buttonTable).pad(16); table.row(); return table; } @Override public boolean keyDown(int keycode) { System.out.println("Pressed " + keycode); if (keycode == Input.Keys.D) { rootLayout.setDebug(!rootLayout.getDebug(), true); } return super.keyDown(keycode); } }